blob: ccfc6b22c966f00726e6fae03888ff62f354650a [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 Naganov7cbf2f12016-10-27 20:05:35 -070025#include <memory.h>
26
Mikhail Naganovb0abafb2017-01-31 17:24:48 -080027#define ATRACE_TAG ATRACE_TAG_AUDIO
28
Mikhail Naganovf363ed42020-12-10 18:47:51 -080029#include <HidlUtils.h>
Yifan Hongf9d30342016-11-30 13:45:34 -080030#include <android/log.h>
Mikhail Naganova331de12017-01-04 16:33:55 -080031#include <media/EffectsFactoryApi.h>
Mikhail Naganov5ec48c22021-01-15 19:05:04 +000032#include <util/EffectUtils.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
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800205#else // MAJOR_VERSION <= 6
206
207void Effect::effectAuxChannelsConfigFromHal(const channel_config_t& halConfig,
208 EffectAuxChannelsConfig* config) {
209 (void)HidlUtils::audioChannelMaskFromHal(halConfig.main_channels, mIsInput,
210 &config->mainChannels);
211 (void)HidlUtils::audioChannelMaskFromHal(halConfig.aux_channels, mIsInput,
212 &config->auxChannels);
213}
214
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700215// static
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800216void Effect::effectAuxChannelsConfigToHal(const EffectAuxChannelsConfig& config,
217 channel_config_t* halConfig) {
218 (void)HidlUtils::audioChannelMaskToHal(config.mainChannels, &halConfig->main_channels);
219 (void)HidlUtils::audioChannelMaskToHal(config.auxChannels, &halConfig->aux_channels);
220}
221
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800222#endif // MAJOR_VERSION <= 6
223
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700224// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800225void Effect::effectOffloadParamToHal(const EffectOffloadParameter& offload,
226 effect_offload_param_t* halOffload) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700227 halOffload->isOffload = offload.isOffload;
228 halOffload->ioHandle = offload.ioHandle;
229}
230
231// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800232std::vector<uint8_t> Effect::parameterToHal(uint32_t paramSize, const void* paramData,
233 uint32_t valueSize, const void** valueData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700234 size_t valueOffsetFromData = alignedSizeIn<uint32_t>(paramSize) * sizeof(uint32_t);
235 size_t halParamBufferSize = sizeof(effect_param_t) + valueOffsetFromData + valueSize;
236 std::vector<uint8_t> halParamBuffer(halParamBufferSize, 0);
Kevin Rocard22505e62017-12-14 18:50:12 -0800237 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700238 halParam->psize = paramSize;
239 halParam->vsize = valueSize;
240 memcpy(halParam->data, paramData, paramSize);
241 if (valueData) {
242 if (*valueData) {
243 // Value data is provided.
244 memcpy(halParam->data + valueOffsetFromData, *valueData, valueSize);
245 } else {
246 // The caller needs the pointer to the value data location.
247 *valueData = halParam->data + valueOffsetFromData;
248 }
249 }
250 return halParamBuffer;
251}
252
253Result Effect::analyzeCommandStatus(const char* commandName, const char* context, status_t status) {
254 return analyzeStatus("command", commandName, context, status);
255}
256
Kevin Rocard22505e62017-12-14 18:50:12 -0800257Result Effect::analyzeStatus(const char* funcName, const char* subFuncName,
258 const char* contextDescription, status_t status) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700259 if (status != OK) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800260 ALOGW("Effect %p %s %s %s: %s", mHandle, funcName, subFuncName, contextDescription,
261 strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700262 }
263 switch (status) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800264 case OK:
265 return Result::OK;
266 case -EINVAL:
267 return Result::INVALID_ARGUMENTS;
268 case -ENODATA:
269 return Result::INVALID_STATE;
270 case -ENODEV:
271 return Result::NOT_INITIALIZED;
272 case -ENOMEM:
273 return Result::RESULT_TOO_BIG;
274 case -ENOSYS:
275 return Result::NOT_SUPPORTED;
276 default:
277 return Result::INVALID_STATE;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700278 }
279}
280
281void Effect::getConfigImpl(int commandCode, const char* commandName, GetConfigCallback cb) {
282 uint32_t halResultSize = sizeof(effect_config_t);
Mikhail Naganov9f289042017-02-23 08:39:36 -0800283 effect_config_t halConfig{};
Kevin Rocard22505e62017-12-14 18:50:12 -0800284 status_t status =
285 (*mHandle)->command(mHandle, commandCode, 0, NULL, &halResultSize, &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700286 EffectConfig config;
287 if (status == OK) {
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000288 status = EffectUtils::effectConfigFromHal(halConfig, mIsInput, &config);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700289 }
290 cb(analyzeCommandStatus(commandName, sContextCallToCommand, status), config);
291}
292
Kevin Rocard22505e62017-12-14 18:50:12 -0800293Result Effect::getCurrentConfigImpl(uint32_t featureId, uint32_t configSize,
294 GetCurrentConfigSuccessCallback onSuccess) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700295 uint32_t halCmd = featureId;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800296 std::vector<uint32_t> halResult(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700297 uint32_t halResultSize = 0;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800298 return sendCommandReturningStatusAndData(
299 EFFECT_CMD_GET_FEATURE_CONFIG, "GET_FEATURE_CONFIG", sizeof(uint32_t), &halCmd,
300 &halResultSize, &halResult[0], sizeof(uint32_t), [&] { onSuccess(&halResult[1]); });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700301}
302
Kevin Rocard22505e62017-12-14 18:50:12 -0800303Result Effect::getParameterImpl(uint32_t paramSize, const void* paramData,
304 uint32_t requestValueSize, uint32_t replyValueSize,
305 GetParameterSuccessCallback onSuccess) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700306 // As it is unknown what method HAL uses for copying the provided parameter data,
307 // it is safer to make sure that input and output buffers do not overlap.
308 std::vector<uint8_t> halCmdBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800309 parameterToHal(paramSize, paramData, requestValueSize, nullptr);
310 const void* valueData = nullptr;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700311 std::vector<uint8_t> halParamBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800312 parameterToHal(paramSize, paramData, replyValueSize, &valueData);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700313 uint32_t halParamBufferSize = halParamBuffer.size();
314
315 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800316 EFFECT_CMD_GET_PARAM, "GET_PARAM", halCmdBuffer.size(), &halCmdBuffer[0],
317 &halParamBufferSize, &halParamBuffer[0], sizeof(effect_param_t), [&] {
318 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]);
319 onSuccess(halParam->vsize, valueData);
320 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700321}
322
Kevin Rocard22505e62017-12-14 18:50:12 -0800323Result Effect::getSupportedConfigsImpl(uint32_t featureId, uint32_t maxConfigs, uint32_t configSize,
324 GetSupportedConfigsSuccessCallback onSuccess) {
325 uint32_t halCmd[2] = {featureId, maxConfigs};
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700326 uint32_t halResultSize = 2 * sizeof(uint32_t) + maxConfigs * sizeof(configSize);
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800327 std::vector<uint8_t> halResult(static_cast<size_t>(halResultSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700328 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800329 EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS, "GET_FEATURE_SUPPORTED_CONFIGS", sizeof(halCmd),
330 halCmd, &halResultSize, &halResult[0], 2 * sizeof(uint32_t), [&] {
331 uint32_t* halResult32 = reinterpret_cast<uint32_t*>(&halResult[0]);
332 uint32_t supportedConfigs = *(++halResult32); // skip status field
333 if (supportedConfigs > maxConfigs) supportedConfigs = maxConfigs;
334 onSuccess(supportedConfigs, ++halResult32);
335 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700336}
337
Mikhail Naganova331de12017-01-04 16:33:55 -0800338Return<void> Effect::prepareForProcessing(prepareForProcessing_cb _hidl_cb) {
339 status_t status;
340 // Create message queue.
341 if (mStatusMQ) {
342 ALOGE("the client attempts to call prepareForProcessing_cb twice");
343 _hidl_cb(Result::INVALID_STATE, StatusMQ::Descriptor());
344 return Void();
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700345 }
Mikhail Naganova331de12017-01-04 16:33:55 -0800346 std::unique_ptr<StatusMQ> tempStatusMQ(new StatusMQ(1, true /*EventFlag*/));
347 if (!tempStatusMQ->isValid()) {
348 ALOGE_IF(!tempStatusMQ->isValid(), "status MQ is invalid");
349 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
350 return Void();
351 }
352 status = EventFlag::createEventFlag(tempStatusMQ->getEventFlagWord(), &mEfGroup);
353 if (status != OK || !mEfGroup) {
354 ALOGE("failed creating event flag for status MQ: %s", strerror(-status));
355 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
356 return Void();
357 }
358
359 // Create and launch the thread.
Kevin Rocard22505e62017-12-14 18:50:12 -0800360 mProcessThread = new ProcessThread(&mStopProcessThread, mHandle, &mHalInBufferPtr,
361 &mHalOutBufferPtr, tempStatusMQ.get(), mEfGroup);
Mikhail Naganova331de12017-01-04 16:33:55 -0800362 status = mProcessThread->run("effect", PRIORITY_URGENT_AUDIO);
363 if (status != OK) {
364 ALOGW("failed to start effect processing thread: %s", strerror(-status));
365 _hidl_cb(Result::INVALID_ARGUMENTS, MQDescriptorSync<Result>());
366 return Void();
367 }
368
369 mStatusMQ = std::move(tempStatusMQ);
370 _hidl_cb(Result::OK, *mStatusMQ->getDesc());
371 return Void();
372}
373
Kevin Rocard22505e62017-12-14 18:50:12 -0800374Return<Result> Effect::setProcessBuffers(const AudioBuffer& inBuffer,
375 const AudioBuffer& outBuffer) {
Mikhail Naganova331de12017-01-04 16:33:55 -0800376 AudioBufferManager& manager = AudioBufferManager::getInstance();
377 sp<AudioBufferWrapper> tempInBuffer, tempOutBuffer;
378 if (!manager.wrap(inBuffer, &tempInBuffer)) {
379 ALOGE("Could not map memory of the input buffer");
380 return Result::INVALID_ARGUMENTS;
381 }
382 if (!manager.wrap(outBuffer, &tempOutBuffer)) {
383 ALOGE("Could not map memory of the output buffer");
384 return Result::INVALID_ARGUMENTS;
385 }
386 mInBuffer = tempInBuffer;
387 mOutBuffer = tempOutBuffer;
388 // The processing thread only reads these pointers after waking up by an event flag,
389 // so it's OK to update the pair non-atomically.
390 mHalInBufferPtr.store(mInBuffer->getHalBuffer(), std::memory_order_release);
391 mHalOutBufferPtr.store(mOutBuffer->getHalBuffer(), std::memory_order_release);
392 return Result::OK;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700393}
394
395Result Effect::sendCommand(int commandCode, const char* commandName) {
396 return sendCommand(commandCode, commandName, 0, NULL);
397}
398
Kevin Rocard22505e62017-12-14 18:50:12 -0800399Result Effect::sendCommand(int commandCode, const char* commandName, uint32_t size, void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700400 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, 0, NULL);
401 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
402}
403
Kevin Rocard22505e62017-12-14 18:50:12 -0800404Result Effect::sendCommandReturningData(int commandCode, const char* commandName,
405 uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700406 return sendCommandReturningData(commandCode, commandName, 0, NULL, replySize, replyData);
407}
408
Kevin Rocard22505e62017-12-14 18:50:12 -0800409Result Effect::sendCommandReturningData(int commandCode, const char* commandName, uint32_t size,
410 void* data, uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700411 uint32_t expectedReplySize = *replySize;
412 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
413 if (status == OK && *replySize != expectedReplySize) {
414 status = -ENODATA;
415 }
416 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
417}
418
419Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName) {
420 return sendCommandReturningStatus(commandCode, commandName, 0, NULL);
421}
422
Kevin Rocard22505e62017-12-14 18:50:12 -0800423Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName, uint32_t size,
424 void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700425 uint32_t replyCmdStatus;
426 uint32_t replySize = sizeof(uint32_t);
Kevin Rocard22505e62017-12-14 18:50:12 -0800427 return sendCommandReturningStatusAndData(commandCode, commandName, size, data, &replySize,
428 &replyCmdStatus, replySize, [] {});
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700429}
430
Kevin Rocard22505e62017-12-14 18:50:12 -0800431Result Effect::sendCommandReturningStatusAndData(int commandCode, const char* commandName,
432 uint32_t size, void* data, uint32_t* replySize,
433 void* replyData, uint32_t minReplySize,
434 CommandSuccessCallback onSuccess) {
435 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700436 Result retval;
437 if (status == OK && minReplySize >= sizeof(uint32_t) && *replySize >= minReplySize) {
438 uint32_t commandStatus = *reinterpret_cast<uint32_t*>(replyData);
439 retval = analyzeCommandStatus(commandName, sContextResultOfCommand, commandStatus);
440 if (commandStatus == OK) {
441 onSuccess();
442 }
443 } else {
444 retval = analyzeCommandStatus(commandName, sContextCallToCommand, status);
445 }
446 return retval;
447}
448
Kevin Rocard22505e62017-12-14 18:50:12 -0800449Result Effect::setConfigImpl(int commandCode, const char* commandName, const EffectConfig& config,
450 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
451 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700452 effect_config_t halConfig;
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000453 EffectUtils::effectConfigToHal(config, &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700454 if (inputBufferProvider != 0) {
455 LOG_FATAL("Using input buffer provider is not supported");
456 }
457 if (outputBufferProvider != 0) {
458 LOG_FATAL("Using output buffer provider is not supported");
459 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800460 return sendCommandReturningStatus(commandCode, commandName, sizeof(effect_config_t),
461 &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700462}
463
Kevin Rocard22505e62017-12-14 18:50:12 -0800464Result Effect::setParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize,
465 const void* valueData) {
466 std::vector<uint8_t> halParamBuffer =
467 parameterToHal(paramSize, paramData, valueSize, &valueData);
468 return sendCommandReturningStatus(EFFECT_CMD_SET_PARAM, "SET_PARAM", halParamBuffer.size(),
469 &halParamBuffer[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700470}
471
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800472// Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow.
Kevin Rocard22505e62017-12-14 18:50:12 -0800473Return<Result> Effect::init() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700474 return sendCommandReturningStatus(EFFECT_CMD_INIT, "INIT");
475}
476
Kevin Rocard22505e62017-12-14 18:50:12 -0800477Return<Result> Effect::setConfig(const EffectConfig& config,
478 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
479 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
480 return setConfigImpl(EFFECT_CMD_SET_CONFIG, "SET_CONFIG", config, inputBufferProvider,
481 outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700482}
483
Kevin Rocard22505e62017-12-14 18:50:12 -0800484Return<Result> Effect::reset() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700485 return sendCommand(EFFECT_CMD_RESET, "RESET");
486}
487
Kevin Rocard22505e62017-12-14 18:50:12 -0800488Return<Result> Effect::enable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700489 return sendCommandReturningStatus(EFFECT_CMD_ENABLE, "ENABLE");
490}
491
Kevin Rocard22505e62017-12-14 18:50:12 -0800492Return<Result> Effect::disable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700493 return sendCommandReturningStatus(EFFECT_CMD_DISABLE, "DISABLE");
494}
495
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800496Return<Result> Effect::setAudioSource(
497#if MAJOR_VERSION <= 6
498 AudioSource source
499#else
500 const AudioSource& source
501#endif
502) {
503 audio_source_t halSource;
504 if (status_t status = HidlUtils::audioSourceToHal(source, &halSource); status == NO_ERROR) {
505 uint32_t halSourceParam = static_cast<uint32_t>(halSource);
506 return sendCommand(EFFECT_CMD_SET_AUDIO_SOURCE, "SET_AUDIO_SOURCE", sizeof(uint32_t),
507 &halSourceParam);
508 } else {
509 return analyzeStatus(__func__, "audioSourceToHal", sContextConversion, status);
510 }
511}
512
513#if MAJOR_VERSION <= 6
514
Kevin Rocard30a7fcc2018-03-01 15:08:07 -0800515Return<Result> Effect::setDevice(AudioDeviceBitfield device) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700516 uint32_t halDevice = static_cast<uint32_t>(device);
517 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDevice);
518}
519
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800520Return<Result> Effect::setInputDevice(AudioDeviceBitfield device) {
521 uint32_t halDevice = static_cast<uint32_t>(device);
522 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
523 &halDevice);
524}
525
526#else // MAJOR_VERSION <= 6
527
528Return<Result> Effect::setDevice(const DeviceAddress& device) {
529 audio_devices_t halDevice;
530 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
531 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
532 status == NO_ERROR) {
533 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
534 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDeviceParam);
535 } else {
536 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
537 }
538}
539
540Return<Result> Effect::setInputDevice(const DeviceAddress& device) {
541 audio_devices_t halDevice;
542 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
543 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
544 status == NO_ERROR) {
545 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
546 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
547 &halDeviceParam);
548 } else {
549 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
550 }
551}
552
553#endif // MAJOR_VERSION <= 6
554
Kevin Rocard22505e62017-12-14 18:50:12 -0800555Return<void> Effect::setAndGetVolume(const hidl_vec<uint32_t>& volumes,
556 setAndGetVolume_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700557 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800558 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700559 uint32_t halResultSize = halDataSize;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800560 std::vector<uint32_t> halResult(volumes.size(), 0);
Kevin Rocard22505e62017-12-14 18:50:12 -0800561 Result retval = sendCommandReturningData(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800562 &halData[0], &halResultSize, &halResult[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700563 hidl_vec<uint32_t> result;
564 if (retval == Result::OK) {
565 result.setToExternal(&halResult[0], halResultSize);
566 }
567 _hidl_cb(retval, result);
568 return Void();
569}
570
Kevin Rocard22505e62017-12-14 18:50:12 -0800571Return<Result> Effect::volumeChangeNotification(const hidl_vec<uint32_t>& volumes) {
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800572 uint32_t halDataSize;
573 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Kevin Rocard22505e62017-12-14 18:50:12 -0800574 return sendCommand(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize, &halData[0]);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800575}
576
Kevin Rocard22505e62017-12-14 18:50:12 -0800577Return<Result> Effect::setAudioMode(AudioMode mode) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700578 uint32_t halMode = static_cast<uint32_t>(mode);
Kevin Rocard22505e62017-12-14 18:50:12 -0800579 return sendCommand(EFFECT_CMD_SET_AUDIO_MODE, "SET_AUDIO_MODE", sizeof(uint32_t), &halMode);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700580}
581
582Return<Result> Effect::setConfigReverse(
Kevin Rocard22505e62017-12-14 18:50:12 -0800583 const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
584 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
585 return setConfigImpl(EFFECT_CMD_SET_CONFIG_REVERSE, "SET_CONFIG_REVERSE", config,
586 inputBufferProvider, outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700587}
588
Kevin Rocard22505e62017-12-14 18:50:12 -0800589Return<void> Effect::getConfig(getConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700590 getConfigImpl(EFFECT_CMD_GET_CONFIG, "GET_CONFIG", _hidl_cb);
591 return Void();
592}
593
Kevin Rocard22505e62017-12-14 18:50:12 -0800594Return<void> Effect::getConfigReverse(getConfigReverse_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700595 getConfigImpl(EFFECT_CMD_GET_CONFIG_REVERSE, "GET_CONFIG_REVERSE", _hidl_cb);
596 return Void();
597}
598
Kevin Rocard22505e62017-12-14 18:50:12 -0800599Return<void> Effect::getSupportedAuxChannelsConfigs(uint32_t maxConfigs,
600 getSupportedAuxChannelsConfigs_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700601 hidl_vec<EffectAuxChannelsConfig> result;
602 Result retval = getSupportedConfigsImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800603 EFFECT_FEATURE_AUX_CHANNELS, maxConfigs, sizeof(channel_config_t),
604 [&](uint32_t supportedConfigs, void* configsData) {
605 result.resize(supportedConfigs);
606 channel_config_t* config = reinterpret_cast<channel_config_t*>(configsData);
607 for (size_t i = 0; i < result.size(); ++i) {
608 effectAuxChannelsConfigFromHal(*config++, &result[i]);
609 }
610 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700611 _hidl_cb(retval, result);
612 return Void();
613}
614
Kevin Rocard22505e62017-12-14 18:50:12 -0800615Return<void> Effect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700616 EffectAuxChannelsConfig result;
617 Result retval = getCurrentConfigImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800618 EFFECT_FEATURE_AUX_CHANNELS, sizeof(channel_config_t), [&](void* configData) {
619 effectAuxChannelsConfigFromHal(*reinterpret_cast<channel_config_t*>(configData),
620 &result);
621 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700622 _hidl_cb(retval, result);
623 return Void();
624}
625
Kevin Rocard22505e62017-12-14 18:50:12 -0800626Return<Result> Effect::setAuxChannelsConfig(const EffectAuxChannelsConfig& config) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800627 std::vector<uint32_t> halCmd(
628 alignedSizeIn<uint32_t>(sizeof(uint32_t) + sizeof(channel_config_t)), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700629 halCmd[0] = EFFECT_FEATURE_AUX_CHANNELS;
630 effectAuxChannelsConfigToHal(config, reinterpret_cast<channel_config_t*>(&halCmd[1]));
631 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800632 "SET_FEATURE_CONFIG AUX_CHANNELS", halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700633}
634
Kevin Rocard22505e62017-12-14 18:50:12 -0800635Return<Result> Effect::offload(const EffectOffloadParameter& param) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700636 effect_offload_param_t halParam;
637 effectOffloadParamToHal(param, &halParam);
Kevin Rocard22505e62017-12-14 18:50:12 -0800638 return sendCommandReturningStatus(EFFECT_CMD_OFFLOAD, "OFFLOAD", sizeof(effect_offload_param_t),
639 &halParam);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700640}
641
Kevin Rocard22505e62017-12-14 18:50:12 -0800642Return<void> Effect::getDescriptor(getDescriptor_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700643 effect_descriptor_t halDescriptor;
644 memset(&halDescriptor, 0, sizeof(effect_descriptor_t));
645 status_t status = (*mHandle)->get_descriptor(mHandle, &halDescriptor);
646 EffectDescriptor descriptor;
647 if (status == OK) {
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000648 status = EffectUtils::effectDescriptorFromHal(halDescriptor, &descriptor);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700649 }
650 _hidl_cb(analyzeStatus("get_descriptor", "", sContextCallFunction, status), descriptor);
651 return Void();
652}
653
Kevin Rocard22505e62017-12-14 18:50:12 -0800654Return<void> Effect::command(uint32_t commandId, const hidl_vec<uint8_t>& data,
655 uint32_t resultMaxSize, command_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700656 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800657 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(data, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700658 uint32_t halResultSize = resultMaxSize;
659 std::unique_ptr<uint8_t[]> halResult(new uint8_t[halResultSize]);
660 memset(&halResult[0], 0, halResultSize);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800661
662 void* dataPtr = halDataSize > 0 ? &halData[0] : NULL;
663 void* resultPtr = halResultSize > 0 ? &halResult[0] : NULL;
Kevin Rocard22505e62017-12-14 18:50:12 -0800664 status_t status =
665 (*mHandle)->command(mHandle, commandId, halDataSize, dataPtr, &halResultSize, resultPtr);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700666 hidl_vec<uint8_t> result;
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800667 if (status == OK && resultPtr != NULL) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700668 result.setToExternal(&halResult[0], halResultSize);
669 }
670 _hidl_cb(status, result);
671 return Void();
672}
673
Kevin Rocard22505e62017-12-14 18:50:12 -0800674Return<Result> Effect::setParameter(const hidl_vec<uint8_t>& parameter,
675 const hidl_vec<uint8_t>& value) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700676 return setParameterImpl(parameter.size(), &parameter[0], value.size(), &value[0]);
677}
678
Kevin Rocard22505e62017-12-14 18:50:12 -0800679Return<void> Effect::getParameter(const hidl_vec<uint8_t>& parameter, uint32_t valueMaxSize,
680 getParameter_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700681 hidl_vec<uint8_t> value;
682 Result retval = getParameterImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800683 parameter.size(), &parameter[0], valueMaxSize,
684 [&](uint32_t valueSize, const void* valueData) {
685 value.setToExternal(reinterpret_cast<uint8_t*>(const_cast<void*>(valueData)),
686 valueSize);
687 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700688 _hidl_cb(retval, value);
689 return Void();
690}
691
Kevin Rocard22505e62017-12-14 18:50:12 -0800692Return<void> Effect::getSupportedConfigsForFeature(uint32_t featureId, uint32_t maxConfigs,
693 uint32_t configSize,
694 getSupportedConfigsForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700695 uint32_t configCount = 0;
696 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800697 Result retval = getSupportedConfigsImpl(featureId, maxConfigs, configSize,
698 [&](uint32_t supportedConfigs, void* configsData) {
699 configCount = supportedConfigs;
700 result.resize(configCount * configSize);
701 memcpy(&result[0], configsData, result.size());
702 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700703 _hidl_cb(retval, configCount, result);
704 return Void();
705}
706
Kevin Rocard22505e62017-12-14 18:50:12 -0800707Return<void> Effect::getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize,
708 getCurrentConfigForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700709 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800710 Result retval = getCurrentConfigImpl(featureId, configSize, [&](void* configData) {
711 result.resize(configSize);
712 memcpy(&result[0], configData, result.size());
713 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700714 _hidl_cb(retval, result);
715 return Void();
716}
717
Kevin Rocard22505e62017-12-14 18:50:12 -0800718Return<Result> Effect::setCurrentConfigForFeature(uint32_t featureId,
719 const hidl_vec<uint8_t>& configData) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800720 std::vector<uint32_t> halCmd(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configData.size()), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700721 halCmd[0] = featureId;
722 memcpy(&halCmd[1], &configData[0], configData.size());
Kevin Rocard22505e62017-12-14 18:50:12 -0800723 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG, "SET_FEATURE_CONFIG",
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800724 halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700725}
726
Mikhail Naganova331de12017-01-04 16:33:55 -0800727Return<Result> Effect::close() {
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800728 if (mStopProcessThread.load(std::memory_order_relaxed)) { // only this thread modifies
729 return Result::INVALID_STATE;
Mikhail Naganova331de12017-01-04 16:33:55 -0800730 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800731 mStopProcessThread.store(true, std::memory_order_release);
Mikhail Naganova331de12017-01-04 16:33:55 -0800732 if (mEfGroup) {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800733 mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT));
Mikhail Naganova331de12017-01-04 16:33:55 -0800734 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800735#if MAJOR_VERSION <= 5
Mikhail Naganova331de12017-01-04 16:33:55 -0800736 return Result::OK;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800737#elif MAJOR_VERSION >= 6
738 // No need to join the processing thread, it is part of the API contract that the client
739 // must finish processing before closing the effect.
Mikhail Naganov532240f2019-12-04 16:18:50 -0800740 Result retval =
741 analyzeStatus("EffectRelease", "", sContextCallFunction, EffectRelease(mHandle));
742 EffectMap::getInstance().remove(mHandle);
743 return retval;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800744#endif
Mikhail Naganova331de12017-01-04 16:33:55 -0800745}
746
Mikhail Naganovfa021442019-02-22 14:28:26 -0800747Return<void> Effect::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& /* options */) {
748 if (fd.getNativeHandle() != nullptr && fd->numFds == 1) {
749 uint32_t cmdData = fd->data[0];
750 (void)sendCommand(EFFECT_CMD_DUMP, "DUMP", sizeof(cmdData), &cmdData);
751 }
752 return Void();
753}
754
Kevin Rocard22505e62017-12-14 18:50:12 -0800755} // namespace implementation
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800756} // namespace CPP_VERSION
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700757} // namespace effect
758} // namespace audio
759} // namespace hardware
760} // namespace android