blob: edd364cd9bce21d78503ed289587818f48713013 [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
22#include "Conversions.h"
23#include "Effect.h"
24#include "common/all-versions/default/EffectMap.h"
Kevin Rocard62588b62017-12-20 11:07:12 -080025
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070026#include <memory.h>
27
Mikhail Naganovb0abafb2017-01-31 17:24:48 -080028#define ATRACE_TAG ATRACE_TAG_AUDIO
29
Mikhail Naganovf363ed42020-12-10 18:47:51 -080030#include <HidlUtils.h>
Yifan Hongf9d30342016-11-30 13:45:34 -080031#include <android/log.h>
Mikhail Naganova331de12017-01-04 16:33:55 -080032#include <media/EffectsFactoryApi.h>
Mikhail Naganovb0abafb2017-01-31 17:24:48 -080033#include <utils/Trace.h>
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070034
Kevin Rocard30a7fcc2018-03-01 15:08:07 -080035#include "VersionUtils.h"
36
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070037namespace android {
38namespace hardware {
39namespace audio {
40namespace effect {
Kevin Rocard96d2cd92018-11-14 16:22:07 -080041namespace CPP_VERSION {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070042namespace implementation {
43
Mikhail Naganovf363ed42020-12-10 18:47:51 -080044#if MAJOR_VERSION <= 6
Kevin Rocard96d2cd92018-11-14 16:22:07 -080045using ::android::hardware::audio::common::CPP_VERSION::implementation::AudioChannelBitfield;
Mikhail Naganovf363ed42020-12-10 18:47:51 -080046#endif
47using ::android::hardware::audio::common::CPP_VERSION::implementation::HidlUtils;
Mikhail Naganova331de12017-01-04 16:33:55 -080048
49namespace {
50
51class ProcessThread : public Thread {
Kevin Rocard22505e62017-12-14 18:50:12 -080052 public:
Mikhail Naganova331de12017-01-04 16:33:55 -080053 // ProcessThread's lifespan never exceeds Effect's lifespan.
Kevin Rocard22505e62017-12-14 18:50:12 -080054 ProcessThread(std::atomic<bool>* stop, effect_handle_t effect,
55 std::atomic<audio_buffer_t*>* inBuffer, std::atomic<audio_buffer_t*>* outBuffer,
56 Effect::StatusMQ* statusMQ, EventFlag* efGroup)
57 : Thread(false /*canCallJava*/),
58 mStop(stop),
59 mEffect(effect),
60 mHasProcessReverse((*mEffect)->process_reverse != NULL),
61 mInBuffer(inBuffer),
62 mOutBuffer(outBuffer),
63 mStatusMQ(statusMQ),
64 mEfGroup(efGroup) {}
Mikhail Naganova331de12017-01-04 16:33:55 -080065 virtual ~ProcessThread() {}
66
Kevin Rocard22505e62017-12-14 18:50:12 -080067 private:
Mikhail Naganova331de12017-01-04 16:33:55 -080068 std::atomic<bool>* mStop;
69 effect_handle_t mEffect;
70 bool mHasProcessReverse;
71 std::atomic<audio_buffer_t*>* mInBuffer;
72 std::atomic<audio_buffer_t*>* mOutBuffer;
73 Effect::StatusMQ* mStatusMQ;
74 EventFlag* mEfGroup;
75
76 bool threadLoop() override;
77};
78
79bool ProcessThread::threadLoop() {
80 // This implementation doesn't return control back to the Thread until it decides to stop,
81 // as the Thread uses mutexes, and this can lead to priority inversion.
Kevin Rocard22505e62017-12-14 18:50:12 -080082 while (!std::atomic_load_explicit(mStop, std::memory_order_acquire)) {
Mikhail Naganova331de12017-01-04 16:33:55 -080083 uint32_t efState = 0;
Mikhail Naganove8674562017-02-10 08:37:19 -080084 mEfGroup->wait(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_ALL), &efState);
Kevin Rocard22505e62017-12-14 18:50:12 -080085 if (!(efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_ALL)) ||
86 (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT))) {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -080087 continue; // Nothing to do or time to quit.
Mikhail Naganova331de12017-01-04 16:33:55 -080088 }
89 Result retval = Result::OK;
Kevin Rocard22505e62017-12-14 18:50:12 -080090 if (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_REVERSE) &&
91 !mHasProcessReverse) {
Mikhail Naganova331de12017-01-04 16:33:55 -080092 retval = Result::NOT_SUPPORTED;
93 }
94
95 if (retval == Result::OK) {
96 // affects both buffer pointers and their contents.
97 std::atomic_thread_fence(std::memory_order_acquire);
98 int32_t processResult;
99 audio_buffer_t* inBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800100 std::atomic_load_explicit(mInBuffer, std::memory_order_relaxed);
Mikhail Naganova331de12017-01-04 16:33:55 -0800101 audio_buffer_t* outBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800102 std::atomic_load_explicit(mOutBuffer, std::memory_order_relaxed);
Mikhail Naganova331de12017-01-04 16:33:55 -0800103 if (inBuffer != nullptr && outBuffer != nullptr) {
104 if (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS)) {
105 processResult = (*mEffect)->process(mEffect, inBuffer, outBuffer);
106 } else {
107 processResult = (*mEffect)->process_reverse(mEffect, inBuffer, outBuffer);
108 }
109 std::atomic_thread_fence(std::memory_order_release);
110 } else {
111 ALOGE("processing buffers were not set before calling 'process'");
112 processResult = -ENODEV;
113 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800114 switch (processResult) {
115 case 0:
116 retval = Result::OK;
117 break;
118 case -ENODATA:
119 retval = Result::INVALID_STATE;
120 break;
121 case -EINVAL:
122 retval = Result::INVALID_ARGUMENTS;
123 break;
124 default:
125 retval = Result::NOT_INITIALIZED;
Mikhail Naganova331de12017-01-04 16:33:55 -0800126 }
127 }
128 if (!mStatusMQ->write(&retval)) {
129 ALOGW("status message queue write failed");
130 }
131 mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::DONE_PROCESSING));
132 }
133
134 return false;
135}
136
137} // namespace
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700138
139// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800140const char* Effect::sContextResultOfCommand = "returned status";
141const char* Effect::sContextCallToCommand = "error";
142const char* Effect::sContextCallFunction = sContextCallToCommand;
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800143const char* Effect::sContextConversion = "conversion";
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700144
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800145Effect::Effect(bool isInput, effect_handle_t handle)
146 : mIsInput(isInput), mHandle(handle), mEfGroup(nullptr), mStopProcessThread(false) {
147 (void)mIsInput; // prevent 'unused field' warnings in pre-V7 versions.
148}
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700149
150Effect::~Effect() {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800151 ATRACE_CALL();
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800152 (void)close();
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800153 if (mProcessThread.get()) {
154 ATRACE_NAME("mProcessThread->join");
155 status_t status = mProcessThread->join();
156 ALOGE_IF(status, "processing thread exit error: %s", strerror(-status));
157 }
158 if (mEfGroup) {
159 status_t status = EventFlag::deleteEventFlag(&mEfGroup);
160 ALOGE_IF(status, "processing MQ event flag deletion error: %s", strerror(-status));
161 }
162 mInBuffer.clear();
163 mOutBuffer.clear();
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800164#if MAJOR_VERSION <= 5
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800165 int status = EffectRelease(mHandle);
166 ALOGW_IF(status, "Error releasing effect %p: %s", mHandle, strerror(-status));
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800167#endif
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800168 EffectMap::getInstance().remove(mHandle);
169 mHandle = 0;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700170}
171
172// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800173template <typename T>
174size_t Effect::alignedSizeIn(size_t s) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700175 return (s + sizeof(T) - 1) / sizeof(T);
176}
177
178// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800179template <typename T>
180std::unique_ptr<uint8_t[]> Effect::hidlVecToHal(const hidl_vec<T>& vec, uint32_t* halDataSize) {
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800181 // Due to bugs in HAL, they may attempt to write into the provided
182 // input buffer. The original binder buffer is r/o, thus it is needed
183 // to create a r/w version.
184 *halDataSize = vec.size() * sizeof(T);
185 std::unique_ptr<uint8_t[]> halData(new uint8_t[*halDataSize]);
186 memcpy(&halData[0], &vec[0], *halDataSize);
187 return halData;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700188}
189
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800190#if MAJOR_VERSION <= 6
191
Kevin Rocard22505e62017-12-14 18:50:12 -0800192void Effect::effectAuxChannelsConfigFromHal(const channel_config_t& halConfig,
193 EffectAuxChannelsConfig* config) {
Kevin Rocard30a7fcc2018-03-01 15:08:07 -0800194 config->mainChannels = AudioChannelBitfield(halConfig.main_channels);
195 config->auxChannels = AudioChannelBitfield(halConfig.aux_channels);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700196}
197
198// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800199void Effect::effectAuxChannelsConfigToHal(const EffectAuxChannelsConfig& config,
200 channel_config_t* halConfig) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700201 halConfig->main_channels = static_cast<audio_channel_mask_t>(config.mainChannels);
202 halConfig->aux_channels = static_cast<audio_channel_mask_t>(config.auxChannels);
203}
204
Kevin Rocard22505e62017-12-14 18:50:12 -0800205void Effect::effectBufferConfigFromHal(const buffer_config_t& halConfig,
206 EffectBufferConfig* config) {
Mikhail Naganov9f289042017-02-23 08:39:36 -0800207 config->buffer.id = 0;
208 config->buffer.frameCount = 0;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700209 config->samplingRateHz = halConfig.samplingRate;
Kevin Rocard30a7fcc2018-03-01 15:08:07 -0800210 config->channels = AudioChannelBitfield(halConfig.channels);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700211 config->format = AudioFormat(halConfig.format);
212 config->accessMode = EffectBufferAccess(halConfig.accessMode);
Kevin Rocard30a7fcc2018-03-01 15:08:07 -0800213 config->mask = static_cast<decltype(config->mask)>(halConfig.mask);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700214}
215
216// static
217void Effect::effectBufferConfigToHal(const EffectBufferConfig& config, buffer_config_t* halConfig) {
Mikhail Naganova331de12017-01-04 16:33:55 -0800218 // Note: setting the buffers directly is considered obsolete. They need to be set
219 // using 'setProcessBuffers'.
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700220 halConfig->buffer.frameCount = 0;
221 halConfig->buffer.raw = NULL;
222 halConfig->samplingRate = config.samplingRateHz;
223 halConfig->channels = static_cast<uint32_t>(config.channels);
Mikhail Naganovb0dd0762017-03-22 10:36:14 -0700224 // Note: The framework code does not use BP.
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700225 halConfig->bufferProvider.cookie = NULL;
226 halConfig->bufferProvider.getBuffer = NULL;
227 halConfig->bufferProvider.releaseBuffer = NULL;
228 halConfig->format = static_cast<uint8_t>(config.format);
229 halConfig->accessMode = static_cast<uint8_t>(config.accessMode);
230 halConfig->mask = static_cast<uint8_t>(config.mask);
231}
232
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800233#else // MAJOR_VERSION <= 6
234
235void Effect::effectAuxChannelsConfigFromHal(const channel_config_t& halConfig,
236 EffectAuxChannelsConfig* config) {
237 (void)HidlUtils::audioChannelMaskFromHal(halConfig.main_channels, mIsInput,
238 &config->mainChannels);
239 (void)HidlUtils::audioChannelMaskFromHal(halConfig.aux_channels, mIsInput,
240 &config->auxChannels);
241}
242
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700243// static
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800244void Effect::effectAuxChannelsConfigToHal(const EffectAuxChannelsConfig& config,
245 channel_config_t* halConfig) {
246 (void)HidlUtils::audioChannelMaskToHal(config.mainChannels, &halConfig->main_channels);
247 (void)HidlUtils::audioChannelMaskToHal(config.auxChannels, &halConfig->aux_channels);
248}
249
250void Effect::effectBufferConfigFromHal(const buffer_config_t& halConfig,
251 EffectBufferConfig* config) {
252 config->buffer.id = 0;
253 config->buffer.frameCount = 0;
254 audio_config_base_t halConfigBase = {halConfig.samplingRate,
255 static_cast<audio_channel_mask_t>(halConfig.channels),
256 static_cast<audio_format_t>(halConfig.format)};
257 (void)HidlUtils::audioConfigBaseFromHal(halConfigBase, mIsInput, &config->base);
258 config->accessMode = EffectBufferAccess(halConfig.accessMode);
259 config->mask = static_cast<decltype(config->mask)>(halConfig.mask);
260}
261
262// static
263void Effect::effectBufferConfigToHal(const EffectBufferConfig& config, buffer_config_t* halConfig) {
264 // Note: setting the buffers directly is considered obsolete. They need to be set
265 // using 'setProcessBuffers'.
266 halConfig->buffer.frameCount = 0;
267 halConfig->buffer.raw = nullptr;
268 audio_config_base_t halConfigBase;
269 (void)HidlUtils::audioConfigBaseToHal(config.base, &halConfigBase);
270 halConfig->samplingRate = halConfigBase.sample_rate;
271 halConfig->channels = halConfigBase.channel_mask;
272 halConfig->format = halConfigBase.format;
273 // Note: The framework code does not use BP.
274 halConfig->bufferProvider.cookie = nullptr;
275 halConfig->bufferProvider.getBuffer = nullptr;
276 halConfig->bufferProvider.releaseBuffer = nullptr;
277 halConfig->accessMode = static_cast<uint8_t>(config.accessMode);
278 halConfig->mask = static_cast<uint8_t>(config.mask);
279}
280
281#endif // MAJOR_VERSION <= 6
282
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700283void Effect::effectConfigFromHal(const effect_config_t& halConfig, EffectConfig* config) {
284 effectBufferConfigFromHal(halConfig.inputCfg, &config->inputCfg);
285 effectBufferConfigFromHal(halConfig.outputCfg, &config->outputCfg);
286}
287
288// static
289void Effect::effectConfigToHal(const EffectConfig& config, effect_config_t* halConfig) {
290 effectBufferConfigToHal(config.inputCfg, &halConfig->inputCfg);
291 effectBufferConfigToHal(config.outputCfg, &halConfig->outputCfg);
292}
293
294// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800295void Effect::effectOffloadParamToHal(const EffectOffloadParameter& offload,
296 effect_offload_param_t* halOffload) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700297 halOffload->isOffload = offload.isOffload;
298 halOffload->ioHandle = offload.ioHandle;
299}
300
301// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800302std::vector<uint8_t> Effect::parameterToHal(uint32_t paramSize, const void* paramData,
303 uint32_t valueSize, const void** valueData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700304 size_t valueOffsetFromData = alignedSizeIn<uint32_t>(paramSize) * sizeof(uint32_t);
305 size_t halParamBufferSize = sizeof(effect_param_t) + valueOffsetFromData + valueSize;
306 std::vector<uint8_t> halParamBuffer(halParamBufferSize, 0);
Kevin Rocard22505e62017-12-14 18:50:12 -0800307 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700308 halParam->psize = paramSize;
309 halParam->vsize = valueSize;
310 memcpy(halParam->data, paramData, paramSize);
311 if (valueData) {
312 if (*valueData) {
313 // Value data is provided.
314 memcpy(halParam->data + valueOffsetFromData, *valueData, valueSize);
315 } else {
316 // The caller needs the pointer to the value data location.
317 *valueData = halParam->data + valueOffsetFromData;
318 }
319 }
320 return halParamBuffer;
321}
322
323Result Effect::analyzeCommandStatus(const char* commandName, const char* context, status_t status) {
324 return analyzeStatus("command", commandName, context, status);
325}
326
Kevin Rocard22505e62017-12-14 18:50:12 -0800327Result Effect::analyzeStatus(const char* funcName, const char* subFuncName,
328 const char* contextDescription, status_t status) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700329 if (status != OK) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800330 ALOGW("Effect %p %s %s %s: %s", mHandle, funcName, subFuncName, contextDescription,
331 strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700332 }
333 switch (status) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800334 case OK:
335 return Result::OK;
336 case -EINVAL:
337 return Result::INVALID_ARGUMENTS;
338 case -ENODATA:
339 return Result::INVALID_STATE;
340 case -ENODEV:
341 return Result::NOT_INITIALIZED;
342 case -ENOMEM:
343 return Result::RESULT_TOO_BIG;
344 case -ENOSYS:
345 return Result::NOT_SUPPORTED;
346 default:
347 return Result::INVALID_STATE;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700348 }
349}
350
351void Effect::getConfigImpl(int commandCode, const char* commandName, GetConfigCallback cb) {
352 uint32_t halResultSize = sizeof(effect_config_t);
Mikhail Naganov9f289042017-02-23 08:39:36 -0800353 effect_config_t halConfig{};
Kevin Rocard22505e62017-12-14 18:50:12 -0800354 status_t status =
355 (*mHandle)->command(mHandle, commandCode, 0, NULL, &halResultSize, &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700356 EffectConfig config;
357 if (status == OK) {
358 effectConfigFromHal(halConfig, &config);
359 }
360 cb(analyzeCommandStatus(commandName, sContextCallToCommand, status), config);
361}
362
Kevin Rocard22505e62017-12-14 18:50:12 -0800363Result Effect::getCurrentConfigImpl(uint32_t featureId, uint32_t configSize,
364 GetCurrentConfigSuccessCallback onSuccess) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700365 uint32_t halCmd = featureId;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800366 std::vector<uint32_t> halResult(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700367 uint32_t halResultSize = 0;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800368 return sendCommandReturningStatusAndData(
369 EFFECT_CMD_GET_FEATURE_CONFIG, "GET_FEATURE_CONFIG", sizeof(uint32_t), &halCmd,
370 &halResultSize, &halResult[0], sizeof(uint32_t), [&] { onSuccess(&halResult[1]); });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700371}
372
Kevin Rocard22505e62017-12-14 18:50:12 -0800373Result Effect::getParameterImpl(uint32_t paramSize, const void* paramData,
374 uint32_t requestValueSize, uint32_t replyValueSize,
375 GetParameterSuccessCallback onSuccess) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700376 // As it is unknown what method HAL uses for copying the provided parameter data,
377 // it is safer to make sure that input and output buffers do not overlap.
378 std::vector<uint8_t> halCmdBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800379 parameterToHal(paramSize, paramData, requestValueSize, nullptr);
380 const void* valueData = nullptr;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700381 std::vector<uint8_t> halParamBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800382 parameterToHal(paramSize, paramData, replyValueSize, &valueData);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700383 uint32_t halParamBufferSize = halParamBuffer.size();
384
385 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800386 EFFECT_CMD_GET_PARAM, "GET_PARAM", halCmdBuffer.size(), &halCmdBuffer[0],
387 &halParamBufferSize, &halParamBuffer[0], sizeof(effect_param_t), [&] {
388 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]);
389 onSuccess(halParam->vsize, valueData);
390 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700391}
392
Kevin Rocard22505e62017-12-14 18:50:12 -0800393Result Effect::getSupportedConfigsImpl(uint32_t featureId, uint32_t maxConfigs, uint32_t configSize,
394 GetSupportedConfigsSuccessCallback onSuccess) {
395 uint32_t halCmd[2] = {featureId, maxConfigs};
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700396 uint32_t halResultSize = 2 * sizeof(uint32_t) + maxConfigs * sizeof(configSize);
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800397 std::vector<uint8_t> halResult(static_cast<size_t>(halResultSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700398 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800399 EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS, "GET_FEATURE_SUPPORTED_CONFIGS", sizeof(halCmd),
400 halCmd, &halResultSize, &halResult[0], 2 * sizeof(uint32_t), [&] {
401 uint32_t* halResult32 = reinterpret_cast<uint32_t*>(&halResult[0]);
402 uint32_t supportedConfigs = *(++halResult32); // skip status field
403 if (supportedConfigs > maxConfigs) supportedConfigs = maxConfigs;
404 onSuccess(supportedConfigs, ++halResult32);
405 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700406}
407
Mikhail Naganova331de12017-01-04 16:33:55 -0800408Return<void> Effect::prepareForProcessing(prepareForProcessing_cb _hidl_cb) {
409 status_t status;
410 // Create message queue.
411 if (mStatusMQ) {
412 ALOGE("the client attempts to call prepareForProcessing_cb twice");
413 _hidl_cb(Result::INVALID_STATE, StatusMQ::Descriptor());
414 return Void();
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700415 }
Mikhail Naganova331de12017-01-04 16:33:55 -0800416 std::unique_ptr<StatusMQ> tempStatusMQ(new StatusMQ(1, true /*EventFlag*/));
417 if (!tempStatusMQ->isValid()) {
418 ALOGE_IF(!tempStatusMQ->isValid(), "status MQ is invalid");
419 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
420 return Void();
421 }
422 status = EventFlag::createEventFlag(tempStatusMQ->getEventFlagWord(), &mEfGroup);
423 if (status != OK || !mEfGroup) {
424 ALOGE("failed creating event flag for status MQ: %s", strerror(-status));
425 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
426 return Void();
427 }
428
429 // Create and launch the thread.
Kevin Rocard22505e62017-12-14 18:50:12 -0800430 mProcessThread = new ProcessThread(&mStopProcessThread, mHandle, &mHalInBufferPtr,
431 &mHalOutBufferPtr, tempStatusMQ.get(), mEfGroup);
Mikhail Naganova331de12017-01-04 16:33:55 -0800432 status = mProcessThread->run("effect", PRIORITY_URGENT_AUDIO);
433 if (status != OK) {
434 ALOGW("failed to start effect processing thread: %s", strerror(-status));
435 _hidl_cb(Result::INVALID_ARGUMENTS, MQDescriptorSync<Result>());
436 return Void();
437 }
438
439 mStatusMQ = std::move(tempStatusMQ);
440 _hidl_cb(Result::OK, *mStatusMQ->getDesc());
441 return Void();
442}
443
Kevin Rocard22505e62017-12-14 18:50:12 -0800444Return<Result> Effect::setProcessBuffers(const AudioBuffer& inBuffer,
445 const AudioBuffer& outBuffer) {
Mikhail Naganova331de12017-01-04 16:33:55 -0800446 AudioBufferManager& manager = AudioBufferManager::getInstance();
447 sp<AudioBufferWrapper> tempInBuffer, tempOutBuffer;
448 if (!manager.wrap(inBuffer, &tempInBuffer)) {
449 ALOGE("Could not map memory of the input buffer");
450 return Result::INVALID_ARGUMENTS;
451 }
452 if (!manager.wrap(outBuffer, &tempOutBuffer)) {
453 ALOGE("Could not map memory of the output buffer");
454 return Result::INVALID_ARGUMENTS;
455 }
456 mInBuffer = tempInBuffer;
457 mOutBuffer = tempOutBuffer;
458 // The processing thread only reads these pointers after waking up by an event flag,
459 // so it's OK to update the pair non-atomically.
460 mHalInBufferPtr.store(mInBuffer->getHalBuffer(), std::memory_order_release);
461 mHalOutBufferPtr.store(mOutBuffer->getHalBuffer(), std::memory_order_release);
462 return Result::OK;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700463}
464
465Result Effect::sendCommand(int commandCode, const char* commandName) {
466 return sendCommand(commandCode, commandName, 0, NULL);
467}
468
Kevin Rocard22505e62017-12-14 18:50:12 -0800469Result Effect::sendCommand(int commandCode, const char* commandName, uint32_t size, void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700470 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, 0, NULL);
471 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
472}
473
Kevin Rocard22505e62017-12-14 18:50:12 -0800474Result Effect::sendCommandReturningData(int commandCode, const char* commandName,
475 uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700476 return sendCommandReturningData(commandCode, commandName, 0, NULL, replySize, replyData);
477}
478
Kevin Rocard22505e62017-12-14 18:50:12 -0800479Result Effect::sendCommandReturningData(int commandCode, const char* commandName, uint32_t size,
480 void* data, uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700481 uint32_t expectedReplySize = *replySize;
482 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
483 if (status == OK && *replySize != expectedReplySize) {
484 status = -ENODATA;
485 }
486 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
487}
488
489Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName) {
490 return sendCommandReturningStatus(commandCode, commandName, 0, NULL);
491}
492
Kevin Rocard22505e62017-12-14 18:50:12 -0800493Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName, uint32_t size,
494 void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700495 uint32_t replyCmdStatus;
496 uint32_t replySize = sizeof(uint32_t);
Kevin Rocard22505e62017-12-14 18:50:12 -0800497 return sendCommandReturningStatusAndData(commandCode, commandName, size, data, &replySize,
498 &replyCmdStatus, replySize, [] {});
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700499}
500
Kevin Rocard22505e62017-12-14 18:50:12 -0800501Result Effect::sendCommandReturningStatusAndData(int commandCode, const char* commandName,
502 uint32_t size, void* data, uint32_t* replySize,
503 void* replyData, uint32_t minReplySize,
504 CommandSuccessCallback onSuccess) {
505 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700506 Result retval;
507 if (status == OK && minReplySize >= sizeof(uint32_t) && *replySize >= minReplySize) {
508 uint32_t commandStatus = *reinterpret_cast<uint32_t*>(replyData);
509 retval = analyzeCommandStatus(commandName, sContextResultOfCommand, commandStatus);
510 if (commandStatus == OK) {
511 onSuccess();
512 }
513 } else {
514 retval = analyzeCommandStatus(commandName, sContextCallToCommand, status);
515 }
516 return retval;
517}
518
Kevin Rocard22505e62017-12-14 18:50:12 -0800519Result Effect::setConfigImpl(int commandCode, const char* commandName, const EffectConfig& config,
520 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
521 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700522 effect_config_t halConfig;
523 effectConfigToHal(config, &halConfig);
524 if (inputBufferProvider != 0) {
525 LOG_FATAL("Using input buffer provider is not supported");
526 }
527 if (outputBufferProvider != 0) {
528 LOG_FATAL("Using output buffer provider is not supported");
529 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800530 return sendCommandReturningStatus(commandCode, commandName, sizeof(effect_config_t),
531 &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700532}
533
Kevin Rocard22505e62017-12-14 18:50:12 -0800534Result Effect::setParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize,
535 const void* valueData) {
536 std::vector<uint8_t> halParamBuffer =
537 parameterToHal(paramSize, paramData, valueSize, &valueData);
538 return sendCommandReturningStatus(EFFECT_CMD_SET_PARAM, "SET_PARAM", halParamBuffer.size(),
539 &halParamBuffer[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700540}
541
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800542// Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow.
Kevin Rocard22505e62017-12-14 18:50:12 -0800543Return<Result> Effect::init() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700544 return sendCommandReturningStatus(EFFECT_CMD_INIT, "INIT");
545}
546
Kevin Rocard22505e62017-12-14 18:50:12 -0800547Return<Result> Effect::setConfig(const EffectConfig& config,
548 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
549 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
550 return setConfigImpl(EFFECT_CMD_SET_CONFIG, "SET_CONFIG", config, inputBufferProvider,
551 outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700552}
553
Kevin Rocard22505e62017-12-14 18:50:12 -0800554Return<Result> Effect::reset() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700555 return sendCommand(EFFECT_CMD_RESET, "RESET");
556}
557
Kevin Rocard22505e62017-12-14 18:50:12 -0800558Return<Result> Effect::enable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700559 return sendCommandReturningStatus(EFFECT_CMD_ENABLE, "ENABLE");
560}
561
Kevin Rocard22505e62017-12-14 18:50:12 -0800562Return<Result> Effect::disable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700563 return sendCommandReturningStatus(EFFECT_CMD_DISABLE, "DISABLE");
564}
565
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800566Return<Result> Effect::setAudioSource(
567#if MAJOR_VERSION <= 6
568 AudioSource source
569#else
570 const AudioSource& source
571#endif
572) {
573 audio_source_t halSource;
574 if (status_t status = HidlUtils::audioSourceToHal(source, &halSource); status == NO_ERROR) {
575 uint32_t halSourceParam = static_cast<uint32_t>(halSource);
576 return sendCommand(EFFECT_CMD_SET_AUDIO_SOURCE, "SET_AUDIO_SOURCE", sizeof(uint32_t),
577 &halSourceParam);
578 } else {
579 return analyzeStatus(__func__, "audioSourceToHal", sContextConversion, status);
580 }
581}
582
583#if MAJOR_VERSION <= 6
584
Kevin Rocard30a7fcc2018-03-01 15:08:07 -0800585Return<Result> Effect::setDevice(AudioDeviceBitfield device) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700586 uint32_t halDevice = static_cast<uint32_t>(device);
587 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDevice);
588}
589
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800590Return<Result> Effect::setInputDevice(AudioDeviceBitfield device) {
591 uint32_t halDevice = static_cast<uint32_t>(device);
592 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
593 &halDevice);
594}
595
596#else // MAJOR_VERSION <= 6
597
598Return<Result> Effect::setDevice(const DeviceAddress& device) {
599 audio_devices_t halDevice;
600 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
601 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
602 status == NO_ERROR) {
603 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
604 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDeviceParam);
605 } else {
606 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
607 }
608}
609
610Return<Result> Effect::setInputDevice(const DeviceAddress& device) {
611 audio_devices_t halDevice;
612 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
613 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
614 status == NO_ERROR) {
615 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
616 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
617 &halDeviceParam);
618 } else {
619 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
620 }
621}
622
623#endif // MAJOR_VERSION <= 6
624
Kevin Rocard22505e62017-12-14 18:50:12 -0800625Return<void> Effect::setAndGetVolume(const hidl_vec<uint32_t>& volumes,
626 setAndGetVolume_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700627 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800628 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700629 uint32_t halResultSize = halDataSize;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800630 std::vector<uint32_t> halResult(volumes.size(), 0);
Kevin Rocard22505e62017-12-14 18:50:12 -0800631 Result retval = sendCommandReturningData(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800632 &halData[0], &halResultSize, &halResult[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700633 hidl_vec<uint32_t> result;
634 if (retval == Result::OK) {
635 result.setToExternal(&halResult[0], halResultSize);
636 }
637 _hidl_cb(retval, result);
638 return Void();
639}
640
Kevin Rocard22505e62017-12-14 18:50:12 -0800641Return<Result> Effect::volumeChangeNotification(const hidl_vec<uint32_t>& volumes) {
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800642 uint32_t halDataSize;
643 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Kevin Rocard22505e62017-12-14 18:50:12 -0800644 return sendCommand(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize, &halData[0]);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800645}
646
Kevin Rocard22505e62017-12-14 18:50:12 -0800647Return<Result> Effect::setAudioMode(AudioMode mode) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700648 uint32_t halMode = static_cast<uint32_t>(mode);
Kevin Rocard22505e62017-12-14 18:50:12 -0800649 return sendCommand(EFFECT_CMD_SET_AUDIO_MODE, "SET_AUDIO_MODE", sizeof(uint32_t), &halMode);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700650}
651
652Return<Result> Effect::setConfigReverse(
Kevin Rocard22505e62017-12-14 18:50:12 -0800653 const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
654 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
655 return setConfigImpl(EFFECT_CMD_SET_CONFIG_REVERSE, "SET_CONFIG_REVERSE", config,
656 inputBufferProvider, outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700657}
658
Kevin Rocard22505e62017-12-14 18:50:12 -0800659Return<void> Effect::getConfig(getConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700660 getConfigImpl(EFFECT_CMD_GET_CONFIG, "GET_CONFIG", _hidl_cb);
661 return Void();
662}
663
Kevin Rocard22505e62017-12-14 18:50:12 -0800664Return<void> Effect::getConfigReverse(getConfigReverse_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700665 getConfigImpl(EFFECT_CMD_GET_CONFIG_REVERSE, "GET_CONFIG_REVERSE", _hidl_cb);
666 return Void();
667}
668
Kevin Rocard22505e62017-12-14 18:50:12 -0800669Return<void> Effect::getSupportedAuxChannelsConfigs(uint32_t maxConfigs,
670 getSupportedAuxChannelsConfigs_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700671 hidl_vec<EffectAuxChannelsConfig> result;
672 Result retval = getSupportedConfigsImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800673 EFFECT_FEATURE_AUX_CHANNELS, maxConfigs, sizeof(channel_config_t),
674 [&](uint32_t supportedConfigs, void* configsData) {
675 result.resize(supportedConfigs);
676 channel_config_t* config = reinterpret_cast<channel_config_t*>(configsData);
677 for (size_t i = 0; i < result.size(); ++i) {
678 effectAuxChannelsConfigFromHal(*config++, &result[i]);
679 }
680 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700681 _hidl_cb(retval, result);
682 return Void();
683}
684
Kevin Rocard22505e62017-12-14 18:50:12 -0800685Return<void> Effect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700686 EffectAuxChannelsConfig result;
687 Result retval = getCurrentConfigImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800688 EFFECT_FEATURE_AUX_CHANNELS, sizeof(channel_config_t), [&](void* configData) {
689 effectAuxChannelsConfigFromHal(*reinterpret_cast<channel_config_t*>(configData),
690 &result);
691 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700692 _hidl_cb(retval, result);
693 return Void();
694}
695
Kevin Rocard22505e62017-12-14 18:50:12 -0800696Return<Result> Effect::setAuxChannelsConfig(const EffectAuxChannelsConfig& config) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800697 std::vector<uint32_t> halCmd(
698 alignedSizeIn<uint32_t>(sizeof(uint32_t) + sizeof(channel_config_t)), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700699 halCmd[0] = EFFECT_FEATURE_AUX_CHANNELS;
700 effectAuxChannelsConfigToHal(config, reinterpret_cast<channel_config_t*>(&halCmd[1]));
701 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800702 "SET_FEATURE_CONFIG AUX_CHANNELS", halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700703}
704
Kevin Rocard22505e62017-12-14 18:50:12 -0800705Return<Result> Effect::offload(const EffectOffloadParameter& param) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700706 effect_offload_param_t halParam;
707 effectOffloadParamToHal(param, &halParam);
Kevin Rocard22505e62017-12-14 18:50:12 -0800708 return sendCommandReturningStatus(EFFECT_CMD_OFFLOAD, "OFFLOAD", sizeof(effect_offload_param_t),
709 &halParam);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700710}
711
Kevin Rocard22505e62017-12-14 18:50:12 -0800712Return<void> Effect::getDescriptor(getDescriptor_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700713 effect_descriptor_t halDescriptor;
714 memset(&halDescriptor, 0, sizeof(effect_descriptor_t));
715 status_t status = (*mHandle)->get_descriptor(mHandle, &halDescriptor);
716 EffectDescriptor descriptor;
717 if (status == OK) {
718 effectDescriptorFromHal(halDescriptor, &descriptor);
719 }
720 _hidl_cb(analyzeStatus("get_descriptor", "", sContextCallFunction, status), descriptor);
721 return Void();
722}
723
Kevin Rocard22505e62017-12-14 18:50:12 -0800724Return<void> Effect::command(uint32_t commandId, const hidl_vec<uint8_t>& data,
725 uint32_t resultMaxSize, command_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700726 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800727 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(data, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700728 uint32_t halResultSize = resultMaxSize;
729 std::unique_ptr<uint8_t[]> halResult(new uint8_t[halResultSize]);
730 memset(&halResult[0], 0, halResultSize);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800731
732 void* dataPtr = halDataSize > 0 ? &halData[0] : NULL;
733 void* resultPtr = halResultSize > 0 ? &halResult[0] : NULL;
Kevin Rocard22505e62017-12-14 18:50:12 -0800734 status_t status =
735 (*mHandle)->command(mHandle, commandId, halDataSize, dataPtr, &halResultSize, resultPtr);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700736 hidl_vec<uint8_t> result;
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800737 if (status == OK && resultPtr != NULL) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700738 result.setToExternal(&halResult[0], halResultSize);
739 }
740 _hidl_cb(status, result);
741 return Void();
742}
743
Kevin Rocard22505e62017-12-14 18:50:12 -0800744Return<Result> Effect::setParameter(const hidl_vec<uint8_t>& parameter,
745 const hidl_vec<uint8_t>& value) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700746 return setParameterImpl(parameter.size(), &parameter[0], value.size(), &value[0]);
747}
748
Kevin Rocard22505e62017-12-14 18:50:12 -0800749Return<void> Effect::getParameter(const hidl_vec<uint8_t>& parameter, uint32_t valueMaxSize,
750 getParameter_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700751 hidl_vec<uint8_t> value;
752 Result retval = getParameterImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800753 parameter.size(), &parameter[0], valueMaxSize,
754 [&](uint32_t valueSize, const void* valueData) {
755 value.setToExternal(reinterpret_cast<uint8_t*>(const_cast<void*>(valueData)),
756 valueSize);
757 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700758 _hidl_cb(retval, value);
759 return Void();
760}
761
Kevin Rocard22505e62017-12-14 18:50:12 -0800762Return<void> Effect::getSupportedConfigsForFeature(uint32_t featureId, uint32_t maxConfigs,
763 uint32_t configSize,
764 getSupportedConfigsForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700765 uint32_t configCount = 0;
766 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800767 Result retval = getSupportedConfigsImpl(featureId, maxConfigs, configSize,
768 [&](uint32_t supportedConfigs, void* configsData) {
769 configCount = supportedConfigs;
770 result.resize(configCount * configSize);
771 memcpy(&result[0], configsData, result.size());
772 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700773 _hidl_cb(retval, configCount, result);
774 return Void();
775}
776
Kevin Rocard22505e62017-12-14 18:50:12 -0800777Return<void> Effect::getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize,
778 getCurrentConfigForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700779 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800780 Result retval = getCurrentConfigImpl(featureId, configSize, [&](void* configData) {
781 result.resize(configSize);
782 memcpy(&result[0], configData, result.size());
783 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700784 _hidl_cb(retval, result);
785 return Void();
786}
787
Kevin Rocard22505e62017-12-14 18:50:12 -0800788Return<Result> Effect::setCurrentConfigForFeature(uint32_t featureId,
789 const hidl_vec<uint8_t>& configData) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800790 std::vector<uint32_t> halCmd(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configData.size()), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700791 halCmd[0] = featureId;
792 memcpy(&halCmd[1], &configData[0], configData.size());
Kevin Rocard22505e62017-12-14 18:50:12 -0800793 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG, "SET_FEATURE_CONFIG",
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800794 halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700795}
796
Mikhail Naganova331de12017-01-04 16:33:55 -0800797Return<Result> Effect::close() {
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800798 if (mStopProcessThread.load(std::memory_order_relaxed)) { // only this thread modifies
799 return Result::INVALID_STATE;
Mikhail Naganova331de12017-01-04 16:33:55 -0800800 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800801 mStopProcessThread.store(true, std::memory_order_release);
Mikhail Naganova331de12017-01-04 16:33:55 -0800802 if (mEfGroup) {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800803 mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT));
Mikhail Naganova331de12017-01-04 16:33:55 -0800804 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800805#if MAJOR_VERSION <= 5
Mikhail Naganova331de12017-01-04 16:33:55 -0800806 return Result::OK;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800807#elif MAJOR_VERSION >= 6
808 // No need to join the processing thread, it is part of the API contract that the client
809 // must finish processing before closing the effect.
Mikhail Naganov532240f2019-12-04 16:18:50 -0800810 Result retval =
811 analyzeStatus("EffectRelease", "", sContextCallFunction, EffectRelease(mHandle));
812 EffectMap::getInstance().remove(mHandle);
813 return retval;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800814#endif
Mikhail Naganova331de12017-01-04 16:33:55 -0800815}
816
Mikhail Naganovfa021442019-02-22 14:28:26 -0800817Return<void> Effect::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& /* options */) {
818 if (fd.getNativeHandle() != nullptr && fd->numFds == 1) {
819 uint32_t cmdData = fd->data[0];
820 (void)sendCommand(EFFECT_CMD_DUMP, "DUMP", sizeof(cmdData), &cmdData);
821 }
822 return Void();
823}
824
Kevin Rocard22505e62017-12-14 18:50:12 -0800825} // namespace implementation
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800826} // namespace CPP_VERSION
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700827} // namespace effect
828} // namespace audio
829} // namespace hardware
830} // namespace android