blob: 49f6bf2eceff162b3e3c58b68476d5a25acafafc [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
Mikhail Naganov7d015382022-01-15 01:15:12 +000045using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::
46 AudioChannelBitfield;
Mikhail Naganovf363ed42020-12-10 18:47:51 -080047#endif
Mikhail Naganov7d015382022-01-15 01:15:12 +000048using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::HidlUtils;
Mikhail Naganova331de12017-01-04 16:33:55 -080049
50namespace {
51
52class ProcessThread : public Thread {
Kevin Rocard22505e62017-12-14 18:50:12 -080053 public:
Mikhail Naganova331de12017-01-04 16:33:55 -080054 // ProcessThread's lifespan never exceeds Effect's lifespan.
Kevin Rocard22505e62017-12-14 18:50:12 -080055 ProcessThread(std::atomic<bool>* stop, effect_handle_t effect,
56 std::atomic<audio_buffer_t*>* inBuffer, std::atomic<audio_buffer_t*>* outBuffer,
57 Effect::StatusMQ* statusMQ, EventFlag* efGroup)
58 : Thread(false /*canCallJava*/),
59 mStop(stop),
60 mEffect(effect),
61 mHasProcessReverse((*mEffect)->process_reverse != NULL),
62 mInBuffer(inBuffer),
63 mOutBuffer(outBuffer),
64 mStatusMQ(statusMQ),
65 mEfGroup(efGroup) {}
Mikhail Naganova331de12017-01-04 16:33:55 -080066 virtual ~ProcessThread() {}
67
Kevin Rocard22505e62017-12-14 18:50:12 -080068 private:
Mikhail Naganova331de12017-01-04 16:33:55 -080069 std::atomic<bool>* mStop;
70 effect_handle_t mEffect;
71 bool mHasProcessReverse;
72 std::atomic<audio_buffer_t*>* mInBuffer;
73 std::atomic<audio_buffer_t*>* mOutBuffer;
74 Effect::StatusMQ* mStatusMQ;
75 EventFlag* mEfGroup;
76
77 bool threadLoop() override;
78};
79
80bool ProcessThread::threadLoop() {
81 // This implementation doesn't return control back to the Thread until it decides to stop,
82 // as the Thread uses mutexes, and this can lead to priority inversion.
Kevin Rocard22505e62017-12-14 18:50:12 -080083 while (!std::atomic_load_explicit(mStop, std::memory_order_acquire)) {
Mikhail Naganova331de12017-01-04 16:33:55 -080084 uint32_t efState = 0;
Mikhail Naganove8674562017-02-10 08:37:19 -080085 mEfGroup->wait(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_ALL), &efState);
Kevin Rocard22505e62017-12-14 18:50:12 -080086 if (!(efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_ALL)) ||
87 (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT))) {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -080088 continue; // Nothing to do or time to quit.
Mikhail Naganova331de12017-01-04 16:33:55 -080089 }
90 Result retval = Result::OK;
Kevin Rocard22505e62017-12-14 18:50:12 -080091 if (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_REVERSE) &&
92 !mHasProcessReverse) {
Mikhail Naganova331de12017-01-04 16:33:55 -080093 retval = Result::NOT_SUPPORTED;
94 }
95
96 if (retval == Result::OK) {
97 // affects both buffer pointers and their contents.
98 std::atomic_thread_fence(std::memory_order_acquire);
99 int32_t processResult;
100 audio_buffer_t* inBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800101 std::atomic_load_explicit(mInBuffer, std::memory_order_relaxed);
Mikhail Naganova331de12017-01-04 16:33:55 -0800102 audio_buffer_t* outBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800103 std::atomic_load_explicit(mOutBuffer, std::memory_order_relaxed);
Mikhail Naganova331de12017-01-04 16:33:55 -0800104 if (inBuffer != nullptr && outBuffer != nullptr) {
105 if (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS)) {
106 processResult = (*mEffect)->process(mEffect, inBuffer, outBuffer);
107 } else {
108 processResult = (*mEffect)->process_reverse(mEffect, inBuffer, outBuffer);
109 }
110 std::atomic_thread_fence(std::memory_order_release);
111 } else {
112 ALOGE("processing buffers were not set before calling 'process'");
113 processResult = -ENODEV;
114 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800115 switch (processResult) {
116 case 0:
117 retval = Result::OK;
118 break;
119 case -ENODATA:
120 retval = Result::INVALID_STATE;
121 break;
122 case -EINVAL:
123 retval = Result::INVALID_ARGUMENTS;
124 break;
125 default:
126 retval = Result::NOT_INITIALIZED;
Mikhail Naganova331de12017-01-04 16:33:55 -0800127 }
128 }
129 if (!mStatusMQ->write(&retval)) {
130 ALOGW("status message queue write failed");
131 }
132 mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::DONE_PROCESSING));
133 }
134
135 return false;
136}
137
138} // namespace
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700139
140// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800141const char* Effect::sContextResultOfCommand = "returned status";
142const char* Effect::sContextCallToCommand = "error";
143const char* Effect::sContextCallFunction = sContextCallToCommand;
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800144const char* Effect::sContextConversion = "conversion";
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700145
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800146Effect::Effect(bool isInput, effect_handle_t handle)
147 : mIsInput(isInput), mHandle(handle), mEfGroup(nullptr), mStopProcessThread(false) {
148 (void)mIsInput; // prevent 'unused field' warnings in pre-V7 versions.
149}
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700150
151Effect::~Effect() {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800152 ATRACE_CALL();
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800153 (void)close();
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800154 if (mProcessThread.get()) {
155 ATRACE_NAME("mProcessThread->join");
156 status_t status = mProcessThread->join();
157 ALOGE_IF(status, "processing thread exit error: %s", strerror(-status));
158 }
159 if (mEfGroup) {
160 status_t status = EventFlag::deleteEventFlag(&mEfGroup);
161 ALOGE_IF(status, "processing MQ event flag deletion error: %s", strerror(-status));
162 }
163 mInBuffer.clear();
164 mOutBuffer.clear();
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800165#if MAJOR_VERSION <= 5
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800166 int status = EffectRelease(mHandle);
167 ALOGW_IF(status, "Error releasing effect %p: %s", mHandle, strerror(-status));
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800168#endif
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800169 EffectMap::getInstance().remove(mHandle);
170 mHandle = 0;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700171}
172
173// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800174template <typename T>
175size_t Effect::alignedSizeIn(size_t s) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700176 return (s + sizeof(T) - 1) / sizeof(T);
177}
178
179// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800180template <typename T>
181std::unique_ptr<uint8_t[]> Effect::hidlVecToHal(const hidl_vec<T>& vec, uint32_t* halDataSize) {
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800182 // Due to bugs in HAL, they may attempt to write into the provided
183 // input buffer. The original binder buffer is r/o, thus it is needed
184 // to create a r/w version.
185 *halDataSize = vec.size() * sizeof(T);
186 std::unique_ptr<uint8_t[]> halData(new uint8_t[*halDataSize]);
187 memcpy(&halData[0], &vec[0], *halDataSize);
188 return halData;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700189}
190
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800191#if MAJOR_VERSION <= 6
192
Kevin Rocard22505e62017-12-14 18:50:12 -0800193void Effect::effectAuxChannelsConfigFromHal(const channel_config_t& halConfig,
194 EffectAuxChannelsConfig* config) {
Kevin Rocard30a7fcc2018-03-01 15:08:07 -0800195 config->mainChannels = AudioChannelBitfield(halConfig.main_channels);
196 config->auxChannels = AudioChannelBitfield(halConfig.aux_channels);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700197}
198
199// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800200void Effect::effectAuxChannelsConfigToHal(const EffectAuxChannelsConfig& config,
201 channel_config_t* halConfig) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700202 halConfig->main_channels = static_cast<audio_channel_mask_t>(config.mainChannels);
203 halConfig->aux_channels = static_cast<audio_channel_mask_t>(config.auxChannels);
204}
205
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800206#else // MAJOR_VERSION <= 6
207
208void Effect::effectAuxChannelsConfigFromHal(const channel_config_t& halConfig,
209 EffectAuxChannelsConfig* config) {
210 (void)HidlUtils::audioChannelMaskFromHal(halConfig.main_channels, mIsInput,
211 &config->mainChannels);
212 (void)HidlUtils::audioChannelMaskFromHal(halConfig.aux_channels, mIsInput,
213 &config->auxChannels);
214}
215
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700216// static
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800217void Effect::effectAuxChannelsConfigToHal(const EffectAuxChannelsConfig& config,
218 channel_config_t* halConfig) {
219 (void)HidlUtils::audioChannelMaskToHal(config.mainChannels, &halConfig->main_channels);
220 (void)HidlUtils::audioChannelMaskToHal(config.auxChannels, &halConfig->aux_channels);
221}
222
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800223#endif // MAJOR_VERSION <= 6
224
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700225// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800226void Effect::effectOffloadParamToHal(const EffectOffloadParameter& offload,
227 effect_offload_param_t* halOffload) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700228 halOffload->isOffload = offload.isOffload;
229 halOffload->ioHandle = offload.ioHandle;
230}
231
232// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800233std::vector<uint8_t> Effect::parameterToHal(uint32_t paramSize, const void* paramData,
234 uint32_t valueSize, const void** valueData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700235 size_t valueOffsetFromData = alignedSizeIn<uint32_t>(paramSize) * sizeof(uint32_t);
236 size_t halParamBufferSize = sizeof(effect_param_t) + valueOffsetFromData + valueSize;
237 std::vector<uint8_t> halParamBuffer(halParamBufferSize, 0);
Kevin Rocard22505e62017-12-14 18:50:12 -0800238 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700239 halParam->psize = paramSize;
240 halParam->vsize = valueSize;
241 memcpy(halParam->data, paramData, paramSize);
242 if (valueData) {
243 if (*valueData) {
244 // Value data is provided.
245 memcpy(halParam->data + valueOffsetFromData, *valueData, valueSize);
246 } else {
247 // The caller needs the pointer to the value data location.
248 *valueData = halParam->data + valueOffsetFromData;
249 }
250 }
251 return halParamBuffer;
252}
253
254Result Effect::analyzeCommandStatus(const char* commandName, const char* context, status_t status) {
255 return analyzeStatus("command", commandName, context, status);
256}
257
Kevin Rocard22505e62017-12-14 18:50:12 -0800258Result Effect::analyzeStatus(const char* funcName, const char* subFuncName,
259 const char* contextDescription, status_t status) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700260 if (status != OK) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800261 ALOGW("Effect %p %s %s %s: %s", mHandle, funcName, subFuncName, contextDescription,
262 strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700263 }
264 switch (status) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800265 case OK:
266 return Result::OK;
267 case -EINVAL:
268 return Result::INVALID_ARGUMENTS;
269 case -ENODATA:
270 return Result::INVALID_STATE;
271 case -ENODEV:
272 return Result::NOT_INITIALIZED;
273 case -ENOMEM:
274 return Result::RESULT_TOO_BIG;
275 case -ENOSYS:
276 return Result::NOT_SUPPORTED;
277 default:
278 return Result::INVALID_STATE;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700279 }
280}
281
282void Effect::getConfigImpl(int commandCode, const char* commandName, GetConfigCallback cb) {
283 uint32_t halResultSize = sizeof(effect_config_t);
Mikhail Naganov9f289042017-02-23 08:39:36 -0800284 effect_config_t halConfig{};
Kevin Rocard22505e62017-12-14 18:50:12 -0800285 status_t status =
286 (*mHandle)->command(mHandle, commandCode, 0, NULL, &halResultSize, &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700287 EffectConfig config;
288 if (status == OK) {
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000289 status = EffectUtils::effectConfigFromHal(halConfig, mIsInput, &config);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700290 }
291 cb(analyzeCommandStatus(commandName, sContextCallToCommand, status), config);
292}
293
Kevin Rocard22505e62017-12-14 18:50:12 -0800294Result Effect::getCurrentConfigImpl(uint32_t featureId, uint32_t configSize,
295 GetCurrentConfigSuccessCallback onSuccess) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700296 uint32_t halCmd = featureId;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800297 std::vector<uint32_t> halResult(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700298 uint32_t halResultSize = 0;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800299 return sendCommandReturningStatusAndData(
300 EFFECT_CMD_GET_FEATURE_CONFIG, "GET_FEATURE_CONFIG", sizeof(uint32_t), &halCmd,
301 &halResultSize, &halResult[0], sizeof(uint32_t), [&] { onSuccess(&halResult[1]); });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700302}
303
Kevin Rocard22505e62017-12-14 18:50:12 -0800304Result Effect::getParameterImpl(uint32_t paramSize, const void* paramData,
305 uint32_t requestValueSize, uint32_t replyValueSize,
306 GetParameterSuccessCallback onSuccess) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700307 // As it is unknown what method HAL uses for copying the provided parameter data,
308 // it is safer to make sure that input and output buffers do not overlap.
309 std::vector<uint8_t> halCmdBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800310 parameterToHal(paramSize, paramData, requestValueSize, nullptr);
311 const void* valueData = nullptr;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700312 std::vector<uint8_t> halParamBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800313 parameterToHal(paramSize, paramData, replyValueSize, &valueData);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700314 uint32_t halParamBufferSize = halParamBuffer.size();
315
316 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800317 EFFECT_CMD_GET_PARAM, "GET_PARAM", halCmdBuffer.size(), &halCmdBuffer[0],
318 &halParamBufferSize, &halParamBuffer[0], sizeof(effect_param_t), [&] {
319 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]);
320 onSuccess(halParam->vsize, valueData);
321 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700322}
323
Kevin Rocard22505e62017-12-14 18:50:12 -0800324Result Effect::getSupportedConfigsImpl(uint32_t featureId, uint32_t maxConfigs, uint32_t configSize,
325 GetSupportedConfigsSuccessCallback onSuccess) {
326 uint32_t halCmd[2] = {featureId, maxConfigs};
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700327 uint32_t halResultSize = 2 * sizeof(uint32_t) + maxConfigs * sizeof(configSize);
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800328 std::vector<uint8_t> halResult(static_cast<size_t>(halResultSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700329 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800330 EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS, "GET_FEATURE_SUPPORTED_CONFIGS", sizeof(halCmd),
331 halCmd, &halResultSize, &halResult[0], 2 * sizeof(uint32_t), [&] {
332 uint32_t* halResult32 = reinterpret_cast<uint32_t*>(&halResult[0]);
333 uint32_t supportedConfigs = *(++halResult32); // skip status field
334 if (supportedConfigs > maxConfigs) supportedConfigs = maxConfigs;
335 onSuccess(supportedConfigs, ++halResult32);
336 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700337}
338
Mikhail Naganova331de12017-01-04 16:33:55 -0800339Return<void> Effect::prepareForProcessing(prepareForProcessing_cb _hidl_cb) {
340 status_t status;
341 // Create message queue.
342 if (mStatusMQ) {
343 ALOGE("the client attempts to call prepareForProcessing_cb twice");
344 _hidl_cb(Result::INVALID_STATE, StatusMQ::Descriptor());
345 return Void();
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700346 }
Mikhail Naganova331de12017-01-04 16:33:55 -0800347 std::unique_ptr<StatusMQ> tempStatusMQ(new StatusMQ(1, true /*EventFlag*/));
348 if (!tempStatusMQ->isValid()) {
349 ALOGE_IF(!tempStatusMQ->isValid(), "status MQ is invalid");
350 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
351 return Void();
352 }
353 status = EventFlag::createEventFlag(tempStatusMQ->getEventFlagWord(), &mEfGroup);
354 if (status != OK || !mEfGroup) {
355 ALOGE("failed creating event flag for status MQ: %s", strerror(-status));
356 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
357 return Void();
358 }
359
360 // Create and launch the thread.
Kevin Rocard22505e62017-12-14 18:50:12 -0800361 mProcessThread = new ProcessThread(&mStopProcessThread, mHandle, &mHalInBufferPtr,
362 &mHalOutBufferPtr, tempStatusMQ.get(), mEfGroup);
Mikhail Naganova331de12017-01-04 16:33:55 -0800363 status = mProcessThread->run("effect", PRIORITY_URGENT_AUDIO);
364 if (status != OK) {
365 ALOGW("failed to start effect processing thread: %s", strerror(-status));
366 _hidl_cb(Result::INVALID_ARGUMENTS, MQDescriptorSync<Result>());
367 return Void();
368 }
369
370 mStatusMQ = std::move(tempStatusMQ);
371 _hidl_cb(Result::OK, *mStatusMQ->getDesc());
372 return Void();
373}
374
Kevin Rocard22505e62017-12-14 18:50:12 -0800375Return<Result> Effect::setProcessBuffers(const AudioBuffer& inBuffer,
376 const AudioBuffer& outBuffer) {
Mikhail Naganova331de12017-01-04 16:33:55 -0800377 AudioBufferManager& manager = AudioBufferManager::getInstance();
378 sp<AudioBufferWrapper> tempInBuffer, tempOutBuffer;
379 if (!manager.wrap(inBuffer, &tempInBuffer)) {
380 ALOGE("Could not map memory of the input buffer");
381 return Result::INVALID_ARGUMENTS;
382 }
383 if (!manager.wrap(outBuffer, &tempOutBuffer)) {
384 ALOGE("Could not map memory of the output buffer");
385 return Result::INVALID_ARGUMENTS;
386 }
387 mInBuffer = tempInBuffer;
388 mOutBuffer = tempOutBuffer;
389 // The processing thread only reads these pointers after waking up by an event flag,
390 // so it's OK to update the pair non-atomically.
391 mHalInBufferPtr.store(mInBuffer->getHalBuffer(), std::memory_order_release);
392 mHalOutBufferPtr.store(mOutBuffer->getHalBuffer(), std::memory_order_release);
393 return Result::OK;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700394}
395
396Result Effect::sendCommand(int commandCode, const char* commandName) {
397 return sendCommand(commandCode, commandName, 0, NULL);
398}
399
Kevin Rocard22505e62017-12-14 18:50:12 -0800400Result Effect::sendCommand(int commandCode, const char* commandName, uint32_t size, void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700401 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, 0, NULL);
402 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
403}
404
Kevin Rocard22505e62017-12-14 18:50:12 -0800405Result Effect::sendCommandReturningData(int commandCode, const char* commandName,
406 uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700407 return sendCommandReturningData(commandCode, commandName, 0, NULL, replySize, replyData);
408}
409
Kevin Rocard22505e62017-12-14 18:50:12 -0800410Result Effect::sendCommandReturningData(int commandCode, const char* commandName, uint32_t size,
411 void* data, uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700412 uint32_t expectedReplySize = *replySize;
413 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
414 if (status == OK && *replySize != expectedReplySize) {
415 status = -ENODATA;
416 }
417 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
418}
419
420Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName) {
421 return sendCommandReturningStatus(commandCode, commandName, 0, NULL);
422}
423
Kevin Rocard22505e62017-12-14 18:50:12 -0800424Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName, uint32_t size,
425 void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700426 uint32_t replyCmdStatus;
427 uint32_t replySize = sizeof(uint32_t);
Kevin Rocard22505e62017-12-14 18:50:12 -0800428 return sendCommandReturningStatusAndData(commandCode, commandName, size, data, &replySize,
429 &replyCmdStatus, replySize, [] {});
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700430}
431
Kevin Rocard22505e62017-12-14 18:50:12 -0800432Result Effect::sendCommandReturningStatusAndData(int commandCode, const char* commandName,
433 uint32_t size, void* data, uint32_t* replySize,
434 void* replyData, uint32_t minReplySize,
435 CommandSuccessCallback onSuccess) {
436 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700437 Result retval;
438 if (status == OK && minReplySize >= sizeof(uint32_t) && *replySize >= minReplySize) {
439 uint32_t commandStatus = *reinterpret_cast<uint32_t*>(replyData);
440 retval = analyzeCommandStatus(commandName, sContextResultOfCommand, commandStatus);
441 if (commandStatus == OK) {
442 onSuccess();
443 }
444 } else {
445 retval = analyzeCommandStatus(commandName, sContextCallToCommand, status);
446 }
447 return retval;
448}
449
Kevin Rocard22505e62017-12-14 18:50:12 -0800450Result Effect::setConfigImpl(int commandCode, const char* commandName, const EffectConfig& config,
451 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
452 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700453 effect_config_t halConfig;
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000454 EffectUtils::effectConfigToHal(config, &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700455 if (inputBufferProvider != 0) {
456 LOG_FATAL("Using input buffer provider is not supported");
457 }
458 if (outputBufferProvider != 0) {
459 LOG_FATAL("Using output buffer provider is not supported");
460 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800461 return sendCommandReturningStatus(commandCode, commandName, sizeof(effect_config_t),
462 &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700463}
464
Kevin Rocard22505e62017-12-14 18:50:12 -0800465Result Effect::setParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize,
466 const void* valueData) {
467 std::vector<uint8_t> halParamBuffer =
468 parameterToHal(paramSize, paramData, valueSize, &valueData);
469 return sendCommandReturningStatus(EFFECT_CMD_SET_PARAM, "SET_PARAM", halParamBuffer.size(),
470 &halParamBuffer[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700471}
472
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800473// Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow.
Kevin Rocard22505e62017-12-14 18:50:12 -0800474Return<Result> Effect::init() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700475 return sendCommandReturningStatus(EFFECT_CMD_INIT, "INIT");
476}
477
Kevin Rocard22505e62017-12-14 18:50:12 -0800478Return<Result> Effect::setConfig(const EffectConfig& config,
479 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
480 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
481 return setConfigImpl(EFFECT_CMD_SET_CONFIG, "SET_CONFIG", config, inputBufferProvider,
482 outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700483}
484
Kevin Rocard22505e62017-12-14 18:50:12 -0800485Return<Result> Effect::reset() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700486 return sendCommand(EFFECT_CMD_RESET, "RESET");
487}
488
Kevin Rocard22505e62017-12-14 18:50:12 -0800489Return<Result> Effect::enable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700490 return sendCommandReturningStatus(EFFECT_CMD_ENABLE, "ENABLE");
491}
492
Kevin Rocard22505e62017-12-14 18:50:12 -0800493Return<Result> Effect::disable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700494 return sendCommandReturningStatus(EFFECT_CMD_DISABLE, "DISABLE");
495}
496
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800497Return<Result> Effect::setAudioSource(
498#if MAJOR_VERSION <= 6
499 AudioSource source
500#else
501 const AudioSource& source
502#endif
503) {
504 audio_source_t halSource;
505 if (status_t status = HidlUtils::audioSourceToHal(source, &halSource); status == NO_ERROR) {
506 uint32_t halSourceParam = static_cast<uint32_t>(halSource);
507 return sendCommand(EFFECT_CMD_SET_AUDIO_SOURCE, "SET_AUDIO_SOURCE", sizeof(uint32_t),
508 &halSourceParam);
509 } else {
510 return analyzeStatus(__func__, "audioSourceToHal", sContextConversion, status);
511 }
512}
513
514#if MAJOR_VERSION <= 6
515
Kevin Rocard30a7fcc2018-03-01 15:08:07 -0800516Return<Result> Effect::setDevice(AudioDeviceBitfield device) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700517 uint32_t halDevice = static_cast<uint32_t>(device);
518 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDevice);
519}
520
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800521Return<Result> Effect::setInputDevice(AudioDeviceBitfield device) {
522 uint32_t halDevice = static_cast<uint32_t>(device);
523 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
524 &halDevice);
525}
526
527#else // MAJOR_VERSION <= 6
528
529Return<Result> Effect::setDevice(const DeviceAddress& device) {
530 audio_devices_t halDevice;
531 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
532 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
533 status == NO_ERROR) {
534 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
535 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDeviceParam);
536 } else {
537 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
538 }
539}
540
541Return<Result> Effect::setInputDevice(const DeviceAddress& device) {
542 audio_devices_t halDevice;
543 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
544 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
545 status == NO_ERROR) {
546 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
547 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
548 &halDeviceParam);
549 } else {
550 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
551 }
552}
553
554#endif // MAJOR_VERSION <= 6
555
Kevin Rocard22505e62017-12-14 18:50:12 -0800556Return<void> Effect::setAndGetVolume(const hidl_vec<uint32_t>& volumes,
557 setAndGetVolume_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700558 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800559 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700560 uint32_t halResultSize = halDataSize;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800561 std::vector<uint32_t> halResult(volumes.size(), 0);
Kevin Rocard22505e62017-12-14 18:50:12 -0800562 Result retval = sendCommandReturningData(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800563 &halData[0], &halResultSize, &halResult[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700564 hidl_vec<uint32_t> result;
565 if (retval == Result::OK) {
566 result.setToExternal(&halResult[0], halResultSize);
567 }
568 _hidl_cb(retval, result);
569 return Void();
570}
571
Kevin Rocard22505e62017-12-14 18:50:12 -0800572Return<Result> Effect::volumeChangeNotification(const hidl_vec<uint32_t>& volumes) {
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800573 uint32_t halDataSize;
574 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Kevin Rocard22505e62017-12-14 18:50:12 -0800575 return sendCommand(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize, &halData[0]);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800576}
577
Kevin Rocard22505e62017-12-14 18:50:12 -0800578Return<Result> Effect::setAudioMode(AudioMode mode) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700579 uint32_t halMode = static_cast<uint32_t>(mode);
Kevin Rocard22505e62017-12-14 18:50:12 -0800580 return sendCommand(EFFECT_CMD_SET_AUDIO_MODE, "SET_AUDIO_MODE", sizeof(uint32_t), &halMode);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700581}
582
583Return<Result> Effect::setConfigReverse(
Kevin Rocard22505e62017-12-14 18:50:12 -0800584 const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
585 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
586 return setConfigImpl(EFFECT_CMD_SET_CONFIG_REVERSE, "SET_CONFIG_REVERSE", config,
587 inputBufferProvider, outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700588}
589
Kevin Rocard22505e62017-12-14 18:50:12 -0800590Return<void> Effect::getConfig(getConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700591 getConfigImpl(EFFECT_CMD_GET_CONFIG, "GET_CONFIG", _hidl_cb);
592 return Void();
593}
594
Kevin Rocard22505e62017-12-14 18:50:12 -0800595Return<void> Effect::getConfigReverse(getConfigReverse_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700596 getConfigImpl(EFFECT_CMD_GET_CONFIG_REVERSE, "GET_CONFIG_REVERSE", _hidl_cb);
597 return Void();
598}
599
Kevin Rocard22505e62017-12-14 18:50:12 -0800600Return<void> Effect::getSupportedAuxChannelsConfigs(uint32_t maxConfigs,
601 getSupportedAuxChannelsConfigs_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700602 hidl_vec<EffectAuxChannelsConfig> result;
603 Result retval = getSupportedConfigsImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800604 EFFECT_FEATURE_AUX_CHANNELS, maxConfigs, sizeof(channel_config_t),
605 [&](uint32_t supportedConfigs, void* configsData) {
606 result.resize(supportedConfigs);
607 channel_config_t* config = reinterpret_cast<channel_config_t*>(configsData);
608 for (size_t i = 0; i < result.size(); ++i) {
609 effectAuxChannelsConfigFromHal(*config++, &result[i]);
610 }
611 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700612 _hidl_cb(retval, result);
613 return Void();
614}
615
Kevin Rocard22505e62017-12-14 18:50:12 -0800616Return<void> Effect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700617 EffectAuxChannelsConfig result;
618 Result retval = getCurrentConfigImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800619 EFFECT_FEATURE_AUX_CHANNELS, sizeof(channel_config_t), [&](void* configData) {
620 effectAuxChannelsConfigFromHal(*reinterpret_cast<channel_config_t*>(configData),
621 &result);
622 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700623 _hidl_cb(retval, result);
624 return Void();
625}
626
Kevin Rocard22505e62017-12-14 18:50:12 -0800627Return<Result> Effect::setAuxChannelsConfig(const EffectAuxChannelsConfig& config) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800628 std::vector<uint32_t> halCmd(
629 alignedSizeIn<uint32_t>(sizeof(uint32_t) + sizeof(channel_config_t)), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700630 halCmd[0] = EFFECT_FEATURE_AUX_CHANNELS;
631 effectAuxChannelsConfigToHal(config, reinterpret_cast<channel_config_t*>(&halCmd[1]));
632 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800633 "SET_FEATURE_CONFIG AUX_CHANNELS", halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700634}
635
Kevin Rocard22505e62017-12-14 18:50:12 -0800636Return<Result> Effect::offload(const EffectOffloadParameter& param) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700637 effect_offload_param_t halParam;
638 effectOffloadParamToHal(param, &halParam);
Kevin Rocard22505e62017-12-14 18:50:12 -0800639 return sendCommandReturningStatus(EFFECT_CMD_OFFLOAD, "OFFLOAD", sizeof(effect_offload_param_t),
640 &halParam);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700641}
642
Kevin Rocard22505e62017-12-14 18:50:12 -0800643Return<void> Effect::getDescriptor(getDescriptor_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700644 effect_descriptor_t halDescriptor;
645 memset(&halDescriptor, 0, sizeof(effect_descriptor_t));
646 status_t status = (*mHandle)->get_descriptor(mHandle, &halDescriptor);
647 EffectDescriptor descriptor;
648 if (status == OK) {
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000649 status = EffectUtils::effectDescriptorFromHal(halDescriptor, &descriptor);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700650 }
651 _hidl_cb(analyzeStatus("get_descriptor", "", sContextCallFunction, status), descriptor);
652 return Void();
653}
654
Kevin Rocard22505e62017-12-14 18:50:12 -0800655Return<void> Effect::command(uint32_t commandId, const hidl_vec<uint8_t>& data,
656 uint32_t resultMaxSize, command_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700657 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800658 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(data, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700659 uint32_t halResultSize = resultMaxSize;
660 std::unique_ptr<uint8_t[]> halResult(new uint8_t[halResultSize]);
661 memset(&halResult[0], 0, halResultSize);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800662
663 void* dataPtr = halDataSize > 0 ? &halData[0] : NULL;
664 void* resultPtr = halResultSize > 0 ? &halResult[0] : NULL;
Kevin Rocard22505e62017-12-14 18:50:12 -0800665 status_t status =
666 (*mHandle)->command(mHandle, commandId, halDataSize, dataPtr, &halResultSize, resultPtr);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700667 hidl_vec<uint8_t> result;
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800668 if (status == OK && resultPtr != NULL) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700669 result.setToExternal(&halResult[0], halResultSize);
670 }
671 _hidl_cb(status, result);
672 return Void();
673}
674
Kevin Rocard22505e62017-12-14 18:50:12 -0800675Return<Result> Effect::setParameter(const hidl_vec<uint8_t>& parameter,
676 const hidl_vec<uint8_t>& value) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700677 return setParameterImpl(parameter.size(), &parameter[0], value.size(), &value[0]);
678}
679
Kevin Rocard22505e62017-12-14 18:50:12 -0800680Return<void> Effect::getParameter(const hidl_vec<uint8_t>& parameter, uint32_t valueMaxSize,
681 getParameter_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700682 hidl_vec<uint8_t> value;
683 Result retval = getParameterImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800684 parameter.size(), &parameter[0], valueMaxSize,
685 [&](uint32_t valueSize, const void* valueData) {
686 value.setToExternal(reinterpret_cast<uint8_t*>(const_cast<void*>(valueData)),
687 valueSize);
688 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700689 _hidl_cb(retval, value);
690 return Void();
691}
692
Kevin Rocard22505e62017-12-14 18:50:12 -0800693Return<void> Effect::getSupportedConfigsForFeature(uint32_t featureId, uint32_t maxConfigs,
694 uint32_t configSize,
695 getSupportedConfigsForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700696 uint32_t configCount = 0;
697 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800698 Result retval = getSupportedConfigsImpl(featureId, maxConfigs, configSize,
699 [&](uint32_t supportedConfigs, void* configsData) {
700 configCount = supportedConfigs;
701 result.resize(configCount * configSize);
702 memcpy(&result[0], configsData, result.size());
703 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700704 _hidl_cb(retval, configCount, result);
705 return Void();
706}
707
Kevin Rocard22505e62017-12-14 18:50:12 -0800708Return<void> Effect::getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize,
709 getCurrentConfigForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700710 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800711 Result retval = getCurrentConfigImpl(featureId, configSize, [&](void* configData) {
712 result.resize(configSize);
713 memcpy(&result[0], configData, result.size());
714 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700715 _hidl_cb(retval, result);
716 return Void();
717}
718
Kevin Rocard22505e62017-12-14 18:50:12 -0800719Return<Result> Effect::setCurrentConfigForFeature(uint32_t featureId,
720 const hidl_vec<uint8_t>& configData) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800721 std::vector<uint32_t> halCmd(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configData.size()), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700722 halCmd[0] = featureId;
723 memcpy(&halCmd[1], &configData[0], configData.size());
Kevin Rocard22505e62017-12-14 18:50:12 -0800724 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG, "SET_FEATURE_CONFIG",
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800725 halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700726}
727
Mikhail Naganova331de12017-01-04 16:33:55 -0800728Return<Result> Effect::close() {
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800729 if (mStopProcessThread.load(std::memory_order_relaxed)) { // only this thread modifies
730 return Result::INVALID_STATE;
Mikhail Naganova331de12017-01-04 16:33:55 -0800731 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800732 mStopProcessThread.store(true, std::memory_order_release);
Mikhail Naganova331de12017-01-04 16:33:55 -0800733 if (mEfGroup) {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800734 mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT));
Mikhail Naganova331de12017-01-04 16:33:55 -0800735 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800736#if MAJOR_VERSION <= 5
Mikhail Naganova331de12017-01-04 16:33:55 -0800737 return Result::OK;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800738#elif MAJOR_VERSION >= 6
739 // No need to join the processing thread, it is part of the API contract that the client
740 // must finish processing before closing the effect.
Mikhail Naganov532240f2019-12-04 16:18:50 -0800741 Result retval =
742 analyzeStatus("EffectRelease", "", sContextCallFunction, EffectRelease(mHandle));
743 EffectMap::getInstance().remove(mHandle);
744 return retval;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800745#endif
Mikhail Naganova331de12017-01-04 16:33:55 -0800746}
747
Mikhail Naganovfa021442019-02-22 14:28:26 -0800748Return<void> Effect::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& /* options */) {
749 if (fd.getNativeHandle() != nullptr && fd->numFds == 1) {
750 uint32_t cmdData = fd->data[0];
751 (void)sendCommand(EFFECT_CMD_DUMP, "DUMP", sizeof(cmdData), &cmdData);
752 }
753 return Void();
754}
755
Kevin Rocard22505e62017-12-14 18:50:12 -0800756} // namespace implementation
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800757} // namespace CPP_VERSION
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700758} // namespace effect
759} // namespace audio
760} // namespace hardware
761} // namespace android