blob: 58f1779ffdde149d316030c61a258ae2eebcb177 [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) {
Mikhail Naganovff611982021-01-27 02:16:53 +0000252 config->buffer.unspecified();
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800253 audio_config_base_t halConfigBase = {halConfig.samplingRate,
254 static_cast<audio_channel_mask_t>(halConfig.channels),
255 static_cast<audio_format_t>(halConfig.format)};
Mikhail Naganovff611982021-01-27 02:16:53 +0000256 (void)HidlUtils::audioConfigBaseOptionalFromHal(
257 halConfigBase, mIsInput, halConfig.mask & EFFECT_CONFIG_FORMAT,
258 halConfig.mask & EFFECT_CONFIG_SMP_RATE, halConfig.mask & EFFECT_CONFIG_CHANNELS,
259 &config->base);
260 if (halConfig.mask & EFFECT_CONFIG_ACC_MODE) {
261 config->accessMode.value(EffectBufferAccess(halConfig.accessMode));
262 }
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800263}
264
265// static
266void Effect::effectBufferConfigToHal(const EffectBufferConfig& config, buffer_config_t* halConfig) {
267 // Note: setting the buffers directly is considered obsolete. They need to be set
268 // using 'setProcessBuffers'.
269 halConfig->buffer.frameCount = 0;
270 halConfig->buffer.raw = nullptr;
Mikhail Naganovff611982021-01-27 02:16:53 +0000271 audio_config_base_t halConfigBase = AUDIO_CONFIG_BASE_INITIALIZER;
272 bool formatSpecified = false, sRateSpecified = false, channelMaskSpecified = false;
273 (void)HidlUtils::audioConfigBaseOptionalToHal(config.base, &halConfigBase, &formatSpecified,
274 &sRateSpecified, &channelMaskSpecified);
275 halConfig->mask = 0;
276 if (sRateSpecified) {
277 halConfig->mask |= EFFECT_CONFIG_SMP_RATE;
278 halConfig->samplingRate = halConfigBase.sample_rate;
279 }
280 if (channelMaskSpecified) {
281 halConfig->mask |= EFFECT_CONFIG_CHANNELS;
282 halConfig->channels = halConfigBase.channel_mask;
283 }
284 if (formatSpecified) {
285 halConfig->mask |= EFFECT_CONFIG_FORMAT;
286 halConfig->format = halConfigBase.format;
287 }
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800288 // Note: The framework code does not use BP.
289 halConfig->bufferProvider.cookie = nullptr;
290 halConfig->bufferProvider.getBuffer = nullptr;
291 halConfig->bufferProvider.releaseBuffer = nullptr;
Mikhail Naganovff611982021-01-27 02:16:53 +0000292 if (config.accessMode.getDiscriminator() ==
293 EffectBufferConfig::OptionalAccessMode::hidl_discriminator::value) {
294 halConfig->mask |= EFFECT_CONFIG_ACC_MODE;
295 halConfig->accessMode = static_cast<uint8_t>(config.accessMode.value());
296 }
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800297}
298
299#endif // MAJOR_VERSION <= 6
300
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700301void Effect::effectConfigFromHal(const effect_config_t& halConfig, EffectConfig* config) {
302 effectBufferConfigFromHal(halConfig.inputCfg, &config->inputCfg);
303 effectBufferConfigFromHal(halConfig.outputCfg, &config->outputCfg);
304}
305
306// static
307void Effect::effectConfigToHal(const EffectConfig& config, effect_config_t* halConfig) {
308 effectBufferConfigToHal(config.inputCfg, &halConfig->inputCfg);
309 effectBufferConfigToHal(config.outputCfg, &halConfig->outputCfg);
310}
311
312// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800313void Effect::effectOffloadParamToHal(const EffectOffloadParameter& offload,
314 effect_offload_param_t* halOffload) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700315 halOffload->isOffload = offload.isOffload;
316 halOffload->ioHandle = offload.ioHandle;
317}
318
319// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800320std::vector<uint8_t> Effect::parameterToHal(uint32_t paramSize, const void* paramData,
321 uint32_t valueSize, const void** valueData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700322 size_t valueOffsetFromData = alignedSizeIn<uint32_t>(paramSize) * sizeof(uint32_t);
323 size_t halParamBufferSize = sizeof(effect_param_t) + valueOffsetFromData + valueSize;
324 std::vector<uint8_t> halParamBuffer(halParamBufferSize, 0);
Kevin Rocard22505e62017-12-14 18:50:12 -0800325 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700326 halParam->psize = paramSize;
327 halParam->vsize = valueSize;
328 memcpy(halParam->data, paramData, paramSize);
329 if (valueData) {
330 if (*valueData) {
331 // Value data is provided.
332 memcpy(halParam->data + valueOffsetFromData, *valueData, valueSize);
333 } else {
334 // The caller needs the pointer to the value data location.
335 *valueData = halParam->data + valueOffsetFromData;
336 }
337 }
338 return halParamBuffer;
339}
340
341Result Effect::analyzeCommandStatus(const char* commandName, const char* context, status_t status) {
342 return analyzeStatus("command", commandName, context, status);
343}
344
Kevin Rocard22505e62017-12-14 18:50:12 -0800345Result Effect::analyzeStatus(const char* funcName, const char* subFuncName,
346 const char* contextDescription, status_t status) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700347 if (status != OK) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800348 ALOGW("Effect %p %s %s %s: %s", mHandle, funcName, subFuncName, contextDescription,
349 strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700350 }
351 switch (status) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800352 case OK:
353 return Result::OK;
354 case -EINVAL:
355 return Result::INVALID_ARGUMENTS;
356 case -ENODATA:
357 return Result::INVALID_STATE;
358 case -ENODEV:
359 return Result::NOT_INITIALIZED;
360 case -ENOMEM:
361 return Result::RESULT_TOO_BIG;
362 case -ENOSYS:
363 return Result::NOT_SUPPORTED;
364 default:
365 return Result::INVALID_STATE;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700366 }
367}
368
369void Effect::getConfigImpl(int commandCode, const char* commandName, GetConfigCallback cb) {
370 uint32_t halResultSize = sizeof(effect_config_t);
Mikhail Naganov9f289042017-02-23 08:39:36 -0800371 effect_config_t halConfig{};
Kevin Rocard22505e62017-12-14 18:50:12 -0800372 status_t status =
373 (*mHandle)->command(mHandle, commandCode, 0, NULL, &halResultSize, &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700374 EffectConfig config;
375 if (status == OK) {
376 effectConfigFromHal(halConfig, &config);
377 }
378 cb(analyzeCommandStatus(commandName, sContextCallToCommand, status), config);
379}
380
Kevin Rocard22505e62017-12-14 18:50:12 -0800381Result Effect::getCurrentConfigImpl(uint32_t featureId, uint32_t configSize,
382 GetCurrentConfigSuccessCallback onSuccess) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700383 uint32_t halCmd = featureId;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800384 std::vector<uint32_t> halResult(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700385 uint32_t halResultSize = 0;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800386 return sendCommandReturningStatusAndData(
387 EFFECT_CMD_GET_FEATURE_CONFIG, "GET_FEATURE_CONFIG", sizeof(uint32_t), &halCmd,
388 &halResultSize, &halResult[0], sizeof(uint32_t), [&] { onSuccess(&halResult[1]); });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700389}
390
Kevin Rocard22505e62017-12-14 18:50:12 -0800391Result Effect::getParameterImpl(uint32_t paramSize, const void* paramData,
392 uint32_t requestValueSize, uint32_t replyValueSize,
393 GetParameterSuccessCallback onSuccess) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700394 // As it is unknown what method HAL uses for copying the provided parameter data,
395 // it is safer to make sure that input and output buffers do not overlap.
396 std::vector<uint8_t> halCmdBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800397 parameterToHal(paramSize, paramData, requestValueSize, nullptr);
398 const void* valueData = nullptr;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700399 std::vector<uint8_t> halParamBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800400 parameterToHal(paramSize, paramData, replyValueSize, &valueData);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700401 uint32_t halParamBufferSize = halParamBuffer.size();
402
403 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800404 EFFECT_CMD_GET_PARAM, "GET_PARAM", halCmdBuffer.size(), &halCmdBuffer[0],
405 &halParamBufferSize, &halParamBuffer[0], sizeof(effect_param_t), [&] {
406 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]);
407 onSuccess(halParam->vsize, valueData);
408 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700409}
410
Kevin Rocard22505e62017-12-14 18:50:12 -0800411Result Effect::getSupportedConfigsImpl(uint32_t featureId, uint32_t maxConfigs, uint32_t configSize,
412 GetSupportedConfigsSuccessCallback onSuccess) {
413 uint32_t halCmd[2] = {featureId, maxConfigs};
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700414 uint32_t halResultSize = 2 * sizeof(uint32_t) + maxConfigs * sizeof(configSize);
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800415 std::vector<uint8_t> halResult(static_cast<size_t>(halResultSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700416 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800417 EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS, "GET_FEATURE_SUPPORTED_CONFIGS", sizeof(halCmd),
418 halCmd, &halResultSize, &halResult[0], 2 * sizeof(uint32_t), [&] {
419 uint32_t* halResult32 = reinterpret_cast<uint32_t*>(&halResult[0]);
420 uint32_t supportedConfigs = *(++halResult32); // skip status field
421 if (supportedConfigs > maxConfigs) supportedConfigs = maxConfigs;
422 onSuccess(supportedConfigs, ++halResult32);
423 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700424}
425
Mikhail Naganova331de12017-01-04 16:33:55 -0800426Return<void> Effect::prepareForProcessing(prepareForProcessing_cb _hidl_cb) {
427 status_t status;
428 // Create message queue.
429 if (mStatusMQ) {
430 ALOGE("the client attempts to call prepareForProcessing_cb twice");
431 _hidl_cb(Result::INVALID_STATE, StatusMQ::Descriptor());
432 return Void();
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700433 }
Mikhail Naganova331de12017-01-04 16:33:55 -0800434 std::unique_ptr<StatusMQ> tempStatusMQ(new StatusMQ(1, true /*EventFlag*/));
435 if (!tempStatusMQ->isValid()) {
436 ALOGE_IF(!tempStatusMQ->isValid(), "status MQ is invalid");
437 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
438 return Void();
439 }
440 status = EventFlag::createEventFlag(tempStatusMQ->getEventFlagWord(), &mEfGroup);
441 if (status != OK || !mEfGroup) {
442 ALOGE("failed creating event flag for status MQ: %s", strerror(-status));
443 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
444 return Void();
445 }
446
447 // Create and launch the thread.
Kevin Rocard22505e62017-12-14 18:50:12 -0800448 mProcessThread = new ProcessThread(&mStopProcessThread, mHandle, &mHalInBufferPtr,
449 &mHalOutBufferPtr, tempStatusMQ.get(), mEfGroup);
Mikhail Naganova331de12017-01-04 16:33:55 -0800450 status = mProcessThread->run("effect", PRIORITY_URGENT_AUDIO);
451 if (status != OK) {
452 ALOGW("failed to start effect processing thread: %s", strerror(-status));
453 _hidl_cb(Result::INVALID_ARGUMENTS, MQDescriptorSync<Result>());
454 return Void();
455 }
456
457 mStatusMQ = std::move(tempStatusMQ);
458 _hidl_cb(Result::OK, *mStatusMQ->getDesc());
459 return Void();
460}
461
Kevin Rocard22505e62017-12-14 18:50:12 -0800462Return<Result> Effect::setProcessBuffers(const AudioBuffer& inBuffer,
463 const AudioBuffer& outBuffer) {
Mikhail Naganova331de12017-01-04 16:33:55 -0800464 AudioBufferManager& manager = AudioBufferManager::getInstance();
465 sp<AudioBufferWrapper> tempInBuffer, tempOutBuffer;
466 if (!manager.wrap(inBuffer, &tempInBuffer)) {
467 ALOGE("Could not map memory of the input buffer");
468 return Result::INVALID_ARGUMENTS;
469 }
470 if (!manager.wrap(outBuffer, &tempOutBuffer)) {
471 ALOGE("Could not map memory of the output buffer");
472 return Result::INVALID_ARGUMENTS;
473 }
474 mInBuffer = tempInBuffer;
475 mOutBuffer = tempOutBuffer;
476 // The processing thread only reads these pointers after waking up by an event flag,
477 // so it's OK to update the pair non-atomically.
478 mHalInBufferPtr.store(mInBuffer->getHalBuffer(), std::memory_order_release);
479 mHalOutBufferPtr.store(mOutBuffer->getHalBuffer(), std::memory_order_release);
480 return Result::OK;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700481}
482
483Result Effect::sendCommand(int commandCode, const char* commandName) {
484 return sendCommand(commandCode, commandName, 0, NULL);
485}
486
Kevin Rocard22505e62017-12-14 18:50:12 -0800487Result Effect::sendCommand(int commandCode, const char* commandName, uint32_t size, void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700488 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, 0, NULL);
489 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
490}
491
Kevin Rocard22505e62017-12-14 18:50:12 -0800492Result Effect::sendCommandReturningData(int commandCode, const char* commandName,
493 uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700494 return sendCommandReturningData(commandCode, commandName, 0, NULL, replySize, replyData);
495}
496
Kevin Rocard22505e62017-12-14 18:50:12 -0800497Result Effect::sendCommandReturningData(int commandCode, const char* commandName, uint32_t size,
498 void* data, uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700499 uint32_t expectedReplySize = *replySize;
500 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
501 if (status == OK && *replySize != expectedReplySize) {
502 status = -ENODATA;
503 }
504 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
505}
506
507Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName) {
508 return sendCommandReturningStatus(commandCode, commandName, 0, NULL);
509}
510
Kevin Rocard22505e62017-12-14 18:50:12 -0800511Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName, uint32_t size,
512 void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700513 uint32_t replyCmdStatus;
514 uint32_t replySize = sizeof(uint32_t);
Kevin Rocard22505e62017-12-14 18:50:12 -0800515 return sendCommandReturningStatusAndData(commandCode, commandName, size, data, &replySize,
516 &replyCmdStatus, replySize, [] {});
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700517}
518
Kevin Rocard22505e62017-12-14 18:50:12 -0800519Result Effect::sendCommandReturningStatusAndData(int commandCode, const char* commandName,
520 uint32_t size, void* data, uint32_t* replySize,
521 void* replyData, uint32_t minReplySize,
522 CommandSuccessCallback onSuccess) {
523 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700524 Result retval;
525 if (status == OK && minReplySize >= sizeof(uint32_t) && *replySize >= minReplySize) {
526 uint32_t commandStatus = *reinterpret_cast<uint32_t*>(replyData);
527 retval = analyzeCommandStatus(commandName, sContextResultOfCommand, commandStatus);
528 if (commandStatus == OK) {
529 onSuccess();
530 }
531 } else {
532 retval = analyzeCommandStatus(commandName, sContextCallToCommand, status);
533 }
534 return retval;
535}
536
Kevin Rocard22505e62017-12-14 18:50:12 -0800537Result Effect::setConfigImpl(int commandCode, const char* commandName, const EffectConfig& config,
538 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
539 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700540 effect_config_t halConfig;
541 effectConfigToHal(config, &halConfig);
542 if (inputBufferProvider != 0) {
543 LOG_FATAL("Using input buffer provider is not supported");
544 }
545 if (outputBufferProvider != 0) {
546 LOG_FATAL("Using output buffer provider is not supported");
547 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800548 return sendCommandReturningStatus(commandCode, commandName, sizeof(effect_config_t),
549 &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700550}
551
Kevin Rocard22505e62017-12-14 18:50:12 -0800552Result Effect::setParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize,
553 const void* valueData) {
554 std::vector<uint8_t> halParamBuffer =
555 parameterToHal(paramSize, paramData, valueSize, &valueData);
556 return sendCommandReturningStatus(EFFECT_CMD_SET_PARAM, "SET_PARAM", halParamBuffer.size(),
557 &halParamBuffer[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700558}
559
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800560// Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow.
Kevin Rocard22505e62017-12-14 18:50:12 -0800561Return<Result> Effect::init() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700562 return sendCommandReturningStatus(EFFECT_CMD_INIT, "INIT");
563}
564
Kevin Rocard22505e62017-12-14 18:50:12 -0800565Return<Result> Effect::setConfig(const EffectConfig& config,
566 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
567 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
568 return setConfigImpl(EFFECT_CMD_SET_CONFIG, "SET_CONFIG", config, inputBufferProvider,
569 outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700570}
571
Kevin Rocard22505e62017-12-14 18:50:12 -0800572Return<Result> Effect::reset() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700573 return sendCommand(EFFECT_CMD_RESET, "RESET");
574}
575
Kevin Rocard22505e62017-12-14 18:50:12 -0800576Return<Result> Effect::enable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700577 return sendCommandReturningStatus(EFFECT_CMD_ENABLE, "ENABLE");
578}
579
Kevin Rocard22505e62017-12-14 18:50:12 -0800580Return<Result> Effect::disable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700581 return sendCommandReturningStatus(EFFECT_CMD_DISABLE, "DISABLE");
582}
583
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800584Return<Result> Effect::setAudioSource(
585#if MAJOR_VERSION <= 6
586 AudioSource source
587#else
588 const AudioSource& source
589#endif
590) {
591 audio_source_t halSource;
592 if (status_t status = HidlUtils::audioSourceToHal(source, &halSource); status == NO_ERROR) {
593 uint32_t halSourceParam = static_cast<uint32_t>(halSource);
594 return sendCommand(EFFECT_CMD_SET_AUDIO_SOURCE, "SET_AUDIO_SOURCE", sizeof(uint32_t),
595 &halSourceParam);
596 } else {
597 return analyzeStatus(__func__, "audioSourceToHal", sContextConversion, status);
598 }
599}
600
601#if MAJOR_VERSION <= 6
602
Kevin Rocard30a7fcc2018-03-01 15:08:07 -0800603Return<Result> Effect::setDevice(AudioDeviceBitfield device) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700604 uint32_t halDevice = static_cast<uint32_t>(device);
605 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDevice);
606}
607
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800608Return<Result> Effect::setInputDevice(AudioDeviceBitfield device) {
609 uint32_t halDevice = static_cast<uint32_t>(device);
610 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
611 &halDevice);
612}
613
614#else // MAJOR_VERSION <= 6
615
616Return<Result> Effect::setDevice(const DeviceAddress& device) {
617 audio_devices_t halDevice;
618 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
619 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
620 status == NO_ERROR) {
621 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
622 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDeviceParam);
623 } else {
624 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
625 }
626}
627
628Return<Result> Effect::setInputDevice(const DeviceAddress& device) {
629 audio_devices_t halDevice;
630 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
631 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
632 status == NO_ERROR) {
633 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
634 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
635 &halDeviceParam);
636 } else {
637 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
638 }
639}
640
641#endif // MAJOR_VERSION <= 6
642
Kevin Rocard22505e62017-12-14 18:50:12 -0800643Return<void> Effect::setAndGetVolume(const hidl_vec<uint32_t>& volumes,
644 setAndGetVolume_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700645 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800646 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700647 uint32_t halResultSize = halDataSize;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800648 std::vector<uint32_t> halResult(volumes.size(), 0);
Kevin Rocard22505e62017-12-14 18:50:12 -0800649 Result retval = sendCommandReturningData(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800650 &halData[0], &halResultSize, &halResult[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700651 hidl_vec<uint32_t> result;
652 if (retval == Result::OK) {
653 result.setToExternal(&halResult[0], halResultSize);
654 }
655 _hidl_cb(retval, result);
656 return Void();
657}
658
Kevin Rocard22505e62017-12-14 18:50:12 -0800659Return<Result> Effect::volumeChangeNotification(const hidl_vec<uint32_t>& volumes) {
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800660 uint32_t halDataSize;
661 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Kevin Rocard22505e62017-12-14 18:50:12 -0800662 return sendCommand(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize, &halData[0]);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800663}
664
Kevin Rocard22505e62017-12-14 18:50:12 -0800665Return<Result> Effect::setAudioMode(AudioMode mode) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700666 uint32_t halMode = static_cast<uint32_t>(mode);
Kevin Rocard22505e62017-12-14 18:50:12 -0800667 return sendCommand(EFFECT_CMD_SET_AUDIO_MODE, "SET_AUDIO_MODE", sizeof(uint32_t), &halMode);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700668}
669
670Return<Result> Effect::setConfigReverse(
Kevin Rocard22505e62017-12-14 18:50:12 -0800671 const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
672 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
673 return setConfigImpl(EFFECT_CMD_SET_CONFIG_REVERSE, "SET_CONFIG_REVERSE", config,
674 inputBufferProvider, outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700675}
676
Kevin Rocard22505e62017-12-14 18:50:12 -0800677Return<void> Effect::getConfig(getConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700678 getConfigImpl(EFFECT_CMD_GET_CONFIG, "GET_CONFIG", _hidl_cb);
679 return Void();
680}
681
Kevin Rocard22505e62017-12-14 18:50:12 -0800682Return<void> Effect::getConfigReverse(getConfigReverse_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700683 getConfigImpl(EFFECT_CMD_GET_CONFIG_REVERSE, "GET_CONFIG_REVERSE", _hidl_cb);
684 return Void();
685}
686
Kevin Rocard22505e62017-12-14 18:50:12 -0800687Return<void> Effect::getSupportedAuxChannelsConfigs(uint32_t maxConfigs,
688 getSupportedAuxChannelsConfigs_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700689 hidl_vec<EffectAuxChannelsConfig> result;
690 Result retval = getSupportedConfigsImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800691 EFFECT_FEATURE_AUX_CHANNELS, maxConfigs, sizeof(channel_config_t),
692 [&](uint32_t supportedConfigs, void* configsData) {
693 result.resize(supportedConfigs);
694 channel_config_t* config = reinterpret_cast<channel_config_t*>(configsData);
695 for (size_t i = 0; i < result.size(); ++i) {
696 effectAuxChannelsConfigFromHal(*config++, &result[i]);
697 }
698 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700699 _hidl_cb(retval, result);
700 return Void();
701}
702
Kevin Rocard22505e62017-12-14 18:50:12 -0800703Return<void> Effect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700704 EffectAuxChannelsConfig result;
705 Result retval = getCurrentConfigImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800706 EFFECT_FEATURE_AUX_CHANNELS, sizeof(channel_config_t), [&](void* configData) {
707 effectAuxChannelsConfigFromHal(*reinterpret_cast<channel_config_t*>(configData),
708 &result);
709 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700710 _hidl_cb(retval, result);
711 return Void();
712}
713
Kevin Rocard22505e62017-12-14 18:50:12 -0800714Return<Result> Effect::setAuxChannelsConfig(const EffectAuxChannelsConfig& config) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800715 std::vector<uint32_t> halCmd(
716 alignedSizeIn<uint32_t>(sizeof(uint32_t) + sizeof(channel_config_t)), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700717 halCmd[0] = EFFECT_FEATURE_AUX_CHANNELS;
718 effectAuxChannelsConfigToHal(config, reinterpret_cast<channel_config_t*>(&halCmd[1]));
719 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800720 "SET_FEATURE_CONFIG AUX_CHANNELS", halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700721}
722
Kevin Rocard22505e62017-12-14 18:50:12 -0800723Return<Result> Effect::offload(const EffectOffloadParameter& param) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700724 effect_offload_param_t halParam;
725 effectOffloadParamToHal(param, &halParam);
Kevin Rocard22505e62017-12-14 18:50:12 -0800726 return sendCommandReturningStatus(EFFECT_CMD_OFFLOAD, "OFFLOAD", sizeof(effect_offload_param_t),
727 &halParam);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700728}
729
Kevin Rocard22505e62017-12-14 18:50:12 -0800730Return<void> Effect::getDescriptor(getDescriptor_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700731 effect_descriptor_t halDescriptor;
732 memset(&halDescriptor, 0, sizeof(effect_descriptor_t));
733 status_t status = (*mHandle)->get_descriptor(mHandle, &halDescriptor);
734 EffectDescriptor descriptor;
735 if (status == OK) {
736 effectDescriptorFromHal(halDescriptor, &descriptor);
737 }
738 _hidl_cb(analyzeStatus("get_descriptor", "", sContextCallFunction, status), descriptor);
739 return Void();
740}
741
Kevin Rocard22505e62017-12-14 18:50:12 -0800742Return<void> Effect::command(uint32_t commandId, const hidl_vec<uint8_t>& data,
743 uint32_t resultMaxSize, command_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700744 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800745 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(data, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700746 uint32_t halResultSize = resultMaxSize;
747 std::unique_ptr<uint8_t[]> halResult(new uint8_t[halResultSize]);
748 memset(&halResult[0], 0, halResultSize);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800749
750 void* dataPtr = halDataSize > 0 ? &halData[0] : NULL;
751 void* resultPtr = halResultSize > 0 ? &halResult[0] : NULL;
Kevin Rocard22505e62017-12-14 18:50:12 -0800752 status_t status =
753 (*mHandle)->command(mHandle, commandId, halDataSize, dataPtr, &halResultSize, resultPtr);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700754 hidl_vec<uint8_t> result;
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800755 if (status == OK && resultPtr != NULL) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700756 result.setToExternal(&halResult[0], halResultSize);
757 }
758 _hidl_cb(status, result);
759 return Void();
760}
761
Kevin Rocard22505e62017-12-14 18:50:12 -0800762Return<Result> Effect::setParameter(const hidl_vec<uint8_t>& parameter,
763 const hidl_vec<uint8_t>& value) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700764 return setParameterImpl(parameter.size(), &parameter[0], value.size(), &value[0]);
765}
766
Kevin Rocard22505e62017-12-14 18:50:12 -0800767Return<void> Effect::getParameter(const hidl_vec<uint8_t>& parameter, uint32_t valueMaxSize,
768 getParameter_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700769 hidl_vec<uint8_t> value;
770 Result retval = getParameterImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800771 parameter.size(), &parameter[0], valueMaxSize,
772 [&](uint32_t valueSize, const void* valueData) {
773 value.setToExternal(reinterpret_cast<uint8_t*>(const_cast<void*>(valueData)),
774 valueSize);
775 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700776 _hidl_cb(retval, value);
777 return Void();
778}
779
Kevin Rocard22505e62017-12-14 18:50:12 -0800780Return<void> Effect::getSupportedConfigsForFeature(uint32_t featureId, uint32_t maxConfigs,
781 uint32_t configSize,
782 getSupportedConfigsForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700783 uint32_t configCount = 0;
784 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800785 Result retval = getSupportedConfigsImpl(featureId, maxConfigs, configSize,
786 [&](uint32_t supportedConfigs, void* configsData) {
787 configCount = supportedConfigs;
788 result.resize(configCount * configSize);
789 memcpy(&result[0], configsData, result.size());
790 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700791 _hidl_cb(retval, configCount, result);
792 return Void();
793}
794
Kevin Rocard22505e62017-12-14 18:50:12 -0800795Return<void> Effect::getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize,
796 getCurrentConfigForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700797 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800798 Result retval = getCurrentConfigImpl(featureId, configSize, [&](void* configData) {
799 result.resize(configSize);
800 memcpy(&result[0], configData, result.size());
801 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700802 _hidl_cb(retval, result);
803 return Void();
804}
805
Kevin Rocard22505e62017-12-14 18:50:12 -0800806Return<Result> Effect::setCurrentConfigForFeature(uint32_t featureId,
807 const hidl_vec<uint8_t>& configData) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800808 std::vector<uint32_t> halCmd(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configData.size()), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700809 halCmd[0] = featureId;
810 memcpy(&halCmd[1], &configData[0], configData.size());
Kevin Rocard22505e62017-12-14 18:50:12 -0800811 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG, "SET_FEATURE_CONFIG",
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800812 halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700813}
814
Mikhail Naganova331de12017-01-04 16:33:55 -0800815Return<Result> Effect::close() {
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800816 if (mStopProcessThread.load(std::memory_order_relaxed)) { // only this thread modifies
817 return Result::INVALID_STATE;
Mikhail Naganova331de12017-01-04 16:33:55 -0800818 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800819 mStopProcessThread.store(true, std::memory_order_release);
Mikhail Naganova331de12017-01-04 16:33:55 -0800820 if (mEfGroup) {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800821 mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT));
Mikhail Naganova331de12017-01-04 16:33:55 -0800822 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800823#if MAJOR_VERSION <= 5
Mikhail Naganova331de12017-01-04 16:33:55 -0800824 return Result::OK;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800825#elif MAJOR_VERSION >= 6
826 // No need to join the processing thread, it is part of the API contract that the client
827 // must finish processing before closing the effect.
Mikhail Naganov532240f2019-12-04 16:18:50 -0800828 Result retval =
829 analyzeStatus("EffectRelease", "", sContextCallFunction, EffectRelease(mHandle));
830 EffectMap::getInstance().remove(mHandle);
831 return retval;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800832#endif
Mikhail Naganova331de12017-01-04 16:33:55 -0800833}
834
Mikhail Naganovfa021442019-02-22 14:28:26 -0800835Return<void> Effect::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& /* options */) {
836 if (fd.getNativeHandle() != nullptr && fd->numFds == 1) {
837 uint32_t cmdData = fd->data[0];
838 (void)sendCommand(EFFECT_CMD_DUMP, "DUMP", sizeof(cmdData), &cmdData);
839 }
840 return Void();
841}
842
Kevin Rocard22505e62017-12-14 18:50:12 -0800843} // namespace implementation
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800844} // namespace CPP_VERSION
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700845} // namespace effect
846} // namespace audio
847} // namespace hardware
848} // namespace android