blob: 4a9e1446272c96d08841ecfd7d34507b4168438a [file] [log] [blame]
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -07001/*
Kevin Rocard96d2cd92018-11-14 16:22:07 -08002 * Copyright (C) 2018 The Android Open Source Project
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Kevin Rocard96d2cd92018-11-14 16:22:07 -080017#include <memory.h>
18
19#define LOG_TAG "EffectHAL"
20#define ATRACE_TAG ATRACE_TAG_AUDIO
21
Kevin Rocard96d2cd92018-11-14 16:22:07 -080022#include "Effect.h"
23#include "common/all-versions/default/EffectMap.h"
Kevin Rocard62588b62017-12-20 11:07:12 -080024
Mikhail Naganovb0abafb2017-01-31 17:24:48 -080025#define ATRACE_TAG ATRACE_TAG_AUDIO
Mikhail Naganovf363ed42020-12-10 18:47:51 -080026#include <HidlUtils.h>
Yifan Hongf9d30342016-11-30 13:45:34 -080027#include <android/log.h>
Andy Hungafc2da82022-12-01 13:46:55 -080028#include <cutils/properties.h>
Mikhail Naganova331de12017-01-04 16:33:55 -080029#include <media/EffectsFactoryApi.h>
Andy Hung502f9d02022-04-26 18:37:36 -070030#include <mediautils/ScopedStatistics.h>
Andy Hungafc2da82022-12-01 13:46:55 -080031#include <sys/syscall.h>
32#include <system/audio_effects/effect_spatializer.h>
Mikhail Naganov5ec48c22021-01-15 19:05:04 +000033#include <util/EffectUtils.h>
Mikhail Naganovb0abafb2017-01-31 17:24:48 -080034#include <utils/Trace.h>
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070035
Kevin Rocard30a7fcc2018-03-01 15:08:07 -080036#include "VersionUtils.h"
37
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070038namespace android {
39namespace hardware {
40namespace audio {
41namespace effect {
Kevin Rocard96d2cd92018-11-14 16:22:07 -080042namespace CPP_VERSION {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070043namespace implementation {
44
Mikhail Naganovf363ed42020-12-10 18:47:51 -080045#if MAJOR_VERSION <= 6
Mikhail Naganov7d015382022-01-15 01:15:12 +000046using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::
47 AudioChannelBitfield;
Mikhail Naganovf363ed42020-12-10 18:47:51 -080048#endif
Mikhail Naganov7d015382022-01-15 01:15:12 +000049using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::HidlUtils;
Mikhail Naganova331de12017-01-04 16:33:55 -080050
51namespace {
52
Andy Hungafc2da82022-12-01 13:46:55 -080053/**
54 * Some basic scheduling tools.
55 */
56namespace scheduler {
57
58int getCpu() {
59 return sched_getcpu();
60}
61
62uint64_t getAffinity(pid_t tid) {
63 cpu_set_t set;
64 CPU_ZERO_S(sizeof(set), &set);
65
66 if (sched_getaffinity(tid, sizeof(set), &set)) {
67 ALOGW("%s: for tid:%d returning 0, failed %s", __func__, tid, strerror(errno));
68 return 0;
69 }
70 const int count = CPU_COUNT_S(sizeof(set), &set);
71 uint64_t mask = 0;
72 for (int i = 0; i < CPU_SETSIZE; ++i) {
73 if (CPU_ISSET_S(i, sizeof(set), &set)) {
74 mask |= 1 << i;
75 }
76 }
77 ALOGV("%s: for tid:%d returning cpu count %d mask %llu", __func__, tid, count,
78 (unsigned long long)mask);
79 return mask;
80}
81
82status_t setAffinity(pid_t tid, uint64_t mask) {
83 cpu_set_t set;
84 CPU_ZERO_S(sizeof(set), &set);
85
86 for (uint64_t m = mask; m != 0;) {
87 uint64_t tz = __builtin_ctz(m);
88 CPU_SET_S(tz, sizeof(set), &set);
89 m &= ~(1 << tz);
90 }
91 if (sched_setaffinity(tid, sizeof(set), &set)) {
92 ALOGW("%s: for tid:%d setting cpu mask %llu failed %s", __func__, tid,
93 (unsigned long long)mask, strerror(errno));
94 return -errno;
95 }
96 ALOGV("%s: for tid:%d setting cpu mask %llu", __func__, tid, (unsigned long long)mask);
97 return OK;
98}
99
100__unused status_t setPriority(pid_t tid, int policy, int priority) {
101 struct sched_param param {
102 .sched_priority = priority,
103 };
104 if (sched_setscheduler(tid, policy, &param) != 0) {
105 ALOGW("%s: Cannot set FIFO priority for tid %d to policy %d priority %d %s", __func__, tid,
106 policy, priority, strerror(errno));
107 return -errno;
108 }
109 ALOGV("%s: Successfully set priority for tid %d to policy %d priority %d", __func__, tid,
110 policy, priority);
111 return NO_ERROR;
112}
113
114status_t setUtilMin(pid_t tid, uint32_t utilMin) {
115 // Currently, there is no wrapper in bionic: b/183240349.
116 struct {
117 uint32_t size;
118 uint32_t sched_policy;
119 uint64_t sched_flags;
120 int32_t sched_nice;
121 uint32_t sched_priority;
122 uint64_t sched_runtime;
123 uint64_t sched_deadline;
124 uint64_t sched_period;
125 uint32_t sched_util_min;
126 uint32_t sched_util_max;
127 } attr{
128 .size = sizeof(attr),
129 .sched_flags = SCHED_FLAG_KEEP_ALL | SCHED_FLAG_UTIL_CLAMP_MIN,
130 .sched_util_min = utilMin,
131 };
132
133 if (syscall(__NR_sched_setattr, tid, &attr, 0 /* flags */)) {
134 ALOGW("%s: Cannot set sched_util_min for pid %d to %u %s", __func__, tid, utilMin,
135 strerror(errno));
136 return -errno;
137 }
138 ALOGV("%s: Successfully set sched_util_min for pid %d to %u", __func__, tid, utilMin);
139 return NO_ERROR;
140}
141
142/*
143 Attempts to raise the priority and usage of tid for spatialization.
144 Returns OK if everything works.
145*/
146status_t updateSpatializerPriority(pid_t tid) {
147 status_t status = OK;
148
149 const int cpu = getCpu();
150 ALOGV("%s: current CPU:%d", __func__, cpu);
151
152 const auto currentAffinity = getAffinity(tid);
153 ALOGV("%s: current Affinity:%llx", __func__, (unsigned long long)currentAffinity);
154
155 // Set the desired CPU core affinity.
156 // Typically this would be done to move the Spatializer effect off of the little cores.
157 // The mid cores and large cores typically have more FP/NEON units
158 // and will advantageously reduce power and prevent glitches due CPU limitations.
159 //
160 // Since this is SOC dependent, we do not set the core affinity here but
161 // prefer to set the util_clamp_min below.
162 //
163 constexpr uint64_t kDefaultAffinity = 0;
164 const int32_t desiredAffinity =
165 property_get_int32("audio.spatializer.effect.affinity", kDefaultAffinity);
166 if (desiredAffinity != 0 && (desiredAffinity & ~currentAffinity) == 0) {
167 const status_t localStatus = setAffinity(tid, desiredAffinity);
168 status = status ? status : localStatus;
169 }
170
171 // Set the util_clamp_min.
172 // This is beneficial to reduce glitches when starting up, or due to scheduler
173 // thread statistics reset (e.g. core migration), which cause the CPU frequency to drop
174 // to minimum.
175 //
176 // Experimentation has found that moving to a mid core over a little core reduces
177 // power if the mid core (e.g. A76/78) has more (e.g. 2x) FP/NEON units
178 // than the little core (e.g. A55).
179 // A possible value is 300.
180 //
181 constexpr uint32_t kUtilMin = 0;
182 const int32_t utilMin = property_get_int32("audio.spatializer.effect.util_clamp_min", kUtilMin);
183 if (utilMin > 0 && utilMin <= 1024) {
184 const status_t localStatus = setUtilMin(tid, utilMin);
185 status = status ? status : localStatus;
186 }
187
188#if 0
189 // Provided for local vendor testing but not enabled as audioserver does this for us.
190 //
191 // Set priority if specified.
192 constexpr int32_t kRTPriorityMin = 1;
193 constexpr int32_t kRTPriorityMax = 3;
194 const int32_t priorityBoost =
195 property_get_int32("audio.spatializer.priority", kRTPriorityMin);
196 if (priorityBoost >= kRTPriorityMin && priorityBoost <= kRTPriorityMax) {
197 const status_t localStatus = scheduler::setPriority(threadId, SCHED_FIFO, priorityBoost);
198 status = status ? status : localStatus;
199 }
200#endif
201
202 return status;
203}
204
205} // namespace scheduler
206
Andy Hung502f9d02022-04-26 18:37:36 -0700207#define SCOPED_STATS() \
208 ::android::mediautils::ScopedStatistics scopedStatistics { \
209 std::string("EffectHal::").append(__func__), mEffectHal->mStatistics \
210 }
211
Mikhail Naganova331de12017-01-04 16:33:55 -0800212class ProcessThread : public Thread {
Kevin Rocard22505e62017-12-14 18:50:12 -0800213 public:
Mikhail Naganova331de12017-01-04 16:33:55 -0800214 // ProcessThread's lifespan never exceeds Effect's lifespan.
Andy Hung502f9d02022-04-26 18:37:36 -0700215 ProcessThread(std::atomic<bool>* stop, effect_handle_t effect,
216 std::atomic<audio_buffer_t*>* inBuffer, std::atomic<audio_buffer_t*>* outBuffer,
217 Effect::StatusMQ* statusMQ, EventFlag* efGroup, Effect* effectHal)
218 : Thread(false /*canCallJava*/),
219 mStop(stop),
220 mEffect(effect),
221 mHasProcessReverse((*mEffect)->process_reverse != NULL),
222 mInBuffer(inBuffer),
223 mOutBuffer(outBuffer),
224 mStatusMQ(statusMQ),
225 mEfGroup(efGroup),
226 mEffectHal(effectHal) {}
227 virtual ~ProcessThread() {}
Mikhail Naganova331de12017-01-04 16:33:55 -0800228
Kevin Rocard22505e62017-12-14 18:50:12 -0800229 private:
Mikhail Naganova331de12017-01-04 16:33:55 -0800230 std::atomic<bool>* mStop;
231 effect_handle_t mEffect;
232 bool mHasProcessReverse;
233 std::atomic<audio_buffer_t*>* mInBuffer;
234 std::atomic<audio_buffer_t*>* mOutBuffer;
235 Effect::StatusMQ* mStatusMQ;
236 EventFlag* mEfGroup;
Andy Hung502f9d02022-04-26 18:37:36 -0700237 Effect* const mEffectHal;
Mikhail Naganova331de12017-01-04 16:33:55 -0800238
239 bool threadLoop() override;
240};
241
242bool ProcessThread::threadLoop() {
243 // This implementation doesn't return control back to the Thread until it decides to stop,
244 // as the Thread uses mutexes, and this can lead to priority inversion.
Kevin Rocard22505e62017-12-14 18:50:12 -0800245 while (!std::atomic_load_explicit(mStop, std::memory_order_acquire)) {
Mikhail Naganova331de12017-01-04 16:33:55 -0800246 uint32_t efState = 0;
Mikhail Naganove8674562017-02-10 08:37:19 -0800247 mEfGroup->wait(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_ALL), &efState);
Kevin Rocard22505e62017-12-14 18:50:12 -0800248 if (!(efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_ALL)) ||
249 (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT))) {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800250 continue; // Nothing to do or time to quit.
Mikhail Naganova331de12017-01-04 16:33:55 -0800251 }
252 Result retval = Result::OK;
Kevin Rocard22505e62017-12-14 18:50:12 -0800253 if (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_REVERSE) &&
254 !mHasProcessReverse) {
Mikhail Naganova331de12017-01-04 16:33:55 -0800255 retval = Result::NOT_SUPPORTED;
256 }
257
258 if (retval == Result::OK) {
259 // affects both buffer pointers and their contents.
260 std::atomic_thread_fence(std::memory_order_acquire);
261 int32_t processResult;
262 audio_buffer_t* inBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800263 std::atomic_load_explicit(mInBuffer, std::memory_order_relaxed);
Mikhail Naganova331de12017-01-04 16:33:55 -0800264 audio_buffer_t* outBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800265 std::atomic_load_explicit(mOutBuffer, std::memory_order_relaxed);
Mikhail Naganova331de12017-01-04 16:33:55 -0800266 if (inBuffer != nullptr && outBuffer != nullptr) {
Andy Hung502f9d02022-04-26 18:37:36 -0700267 // Time this effect process
268 SCOPED_STATS();
269
Mikhail Naganova331de12017-01-04 16:33:55 -0800270 if (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS)) {
271 processResult = (*mEffect)->process(mEffect, inBuffer, outBuffer);
272 } else {
273 processResult = (*mEffect)->process_reverse(mEffect, inBuffer, outBuffer);
274 }
275 std::atomic_thread_fence(std::memory_order_release);
276 } else {
277 ALOGE("processing buffers were not set before calling 'process'");
278 processResult = -ENODEV;
279 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800280 switch (processResult) {
281 case 0:
282 retval = Result::OK;
283 break;
284 case -ENODATA:
285 retval = Result::INVALID_STATE;
286 break;
287 case -EINVAL:
288 retval = Result::INVALID_ARGUMENTS;
289 break;
290 default:
291 retval = Result::NOT_INITIALIZED;
Mikhail Naganova331de12017-01-04 16:33:55 -0800292 }
293 }
294 if (!mStatusMQ->write(&retval)) {
295 ALOGW("status message queue write failed");
296 }
297 mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::DONE_PROCESSING));
298 }
299
300 return false;
301}
302
303} // namespace
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700304
305// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800306const char* Effect::sContextResultOfCommand = "returned status";
307const char* Effect::sContextCallToCommand = "error";
308const char* Effect::sContextCallFunction = sContextCallToCommand;
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800309const char* Effect::sContextConversion = "conversion";
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700310
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800311Effect::Effect(bool isInput, effect_handle_t handle)
312 : mIsInput(isInput), mHandle(handle), mEfGroup(nullptr), mStopProcessThread(false) {
313 (void)mIsInput; // prevent 'unused field' warnings in pre-V7 versions.
314}
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700315
316Effect::~Effect() {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800317 ATRACE_CALL();
Mikhail Naganov0d920592023-08-25 11:10:37 -0700318 auto [_, handle] = closeImpl();
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800319 if (mProcessThread.get()) {
320 ATRACE_NAME("mProcessThread->join");
321 status_t status = mProcessThread->join();
322 ALOGE_IF(status, "processing thread exit error: %s", strerror(-status));
323 }
324 if (mEfGroup) {
325 status_t status = EventFlag::deleteEventFlag(&mEfGroup);
326 ALOGE_IF(status, "processing MQ event flag deletion error: %s", strerror(-status));
327 }
328 mInBuffer.clear();
329 mOutBuffer.clear();
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800330#if MAJOR_VERSION <= 5
Mikhail Naganov0d920592023-08-25 11:10:37 -0700331 int status = EffectRelease(handle);
332 ALOGW_IF(status, "Error releasing effect %p: %s", handle, strerror(-status));
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800333#endif
Mikhail Naganov0d920592023-08-25 11:10:37 -0700334 EffectMap::getInstance().remove(handle);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700335}
336
337// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800338template <typename T>
339size_t Effect::alignedSizeIn(size_t s) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700340 return (s + sizeof(T) - 1) / sizeof(T);
341}
342
343// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800344template <typename T>
345std::unique_ptr<uint8_t[]> Effect::hidlVecToHal(const hidl_vec<T>& vec, uint32_t* halDataSize) {
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800346 // Due to bugs in HAL, they may attempt to write into the provided
347 // input buffer. The original binder buffer is r/o, thus it is needed
348 // to create a r/w version.
349 *halDataSize = vec.size() * sizeof(T);
350 std::unique_ptr<uint8_t[]> halData(new uint8_t[*halDataSize]);
351 memcpy(&halData[0], &vec[0], *halDataSize);
352 return halData;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700353}
354
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800355#if MAJOR_VERSION <= 6
356
Kevin Rocard22505e62017-12-14 18:50:12 -0800357void Effect::effectAuxChannelsConfigFromHal(const channel_config_t& halConfig,
358 EffectAuxChannelsConfig* config) {
Kevin Rocard30a7fcc2018-03-01 15:08:07 -0800359 config->mainChannels = AudioChannelBitfield(halConfig.main_channels);
360 config->auxChannels = AudioChannelBitfield(halConfig.aux_channels);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700361}
362
363// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800364void Effect::effectAuxChannelsConfigToHal(const EffectAuxChannelsConfig& config,
365 channel_config_t* halConfig) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700366 halConfig->main_channels = static_cast<audio_channel_mask_t>(config.mainChannels);
367 halConfig->aux_channels = static_cast<audio_channel_mask_t>(config.auxChannels);
368}
369
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800370#else // MAJOR_VERSION <= 6
371
372void Effect::effectAuxChannelsConfigFromHal(const channel_config_t& halConfig,
373 EffectAuxChannelsConfig* config) {
374 (void)HidlUtils::audioChannelMaskFromHal(halConfig.main_channels, mIsInput,
375 &config->mainChannels);
376 (void)HidlUtils::audioChannelMaskFromHal(halConfig.aux_channels, mIsInput,
377 &config->auxChannels);
378}
379
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700380// static
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800381void Effect::effectAuxChannelsConfigToHal(const EffectAuxChannelsConfig& config,
382 channel_config_t* halConfig) {
383 (void)HidlUtils::audioChannelMaskToHal(config.mainChannels, &halConfig->main_channels);
384 (void)HidlUtils::audioChannelMaskToHal(config.auxChannels, &halConfig->aux_channels);
385}
386
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800387#endif // MAJOR_VERSION <= 6
388
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700389// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800390void Effect::effectOffloadParamToHal(const EffectOffloadParameter& offload,
391 effect_offload_param_t* halOffload) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700392 halOffload->isOffload = offload.isOffload;
393 halOffload->ioHandle = offload.ioHandle;
394}
395
396// static
Mikhail Naganov4f110342022-07-07 20:37:42 +0000397bool Effect::parameterToHal(uint32_t paramSize, const void* paramData, uint32_t valueSize,
398 const void** valueData, std::vector<uint8_t>* halParamBuffer) {
399 constexpr size_t kMaxSize = EFFECT_PARAM_SIZE_MAX - sizeof(effect_param_t);
400 if (paramSize > kMaxSize) {
401 ALOGE("%s: Parameter size is too big: %" PRIu32, __func__, paramSize);
402 return false;
403 }
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700404 size_t valueOffsetFromData = alignedSizeIn<uint32_t>(paramSize) * sizeof(uint32_t);
Mikhail Naganov4f110342022-07-07 20:37:42 +0000405 if (valueOffsetFromData > kMaxSize) {
406 ALOGE("%s: Aligned parameter size is too big: %zu", __func__, valueOffsetFromData);
407 return false;
408 }
409 if (valueSize > kMaxSize - valueOffsetFromData) {
410 ALOGE("%s: Value size is too big: %" PRIu32 ", max size is %zu", __func__, valueSize,
411 kMaxSize - valueOffsetFromData);
412 android_errorWriteLog(0x534e4554, "237291425");
413 return false;
414 }
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700415 size_t halParamBufferSize = sizeof(effect_param_t) + valueOffsetFromData + valueSize;
Mikhail Naganov4f110342022-07-07 20:37:42 +0000416 halParamBuffer->resize(halParamBufferSize, 0);
417 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(halParamBuffer->data());
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700418 halParam->psize = paramSize;
419 halParam->vsize = valueSize;
420 memcpy(halParam->data, paramData, paramSize);
421 if (valueData) {
422 if (*valueData) {
423 // Value data is provided.
424 memcpy(halParam->data + valueOffsetFromData, *valueData, valueSize);
425 } else {
426 // The caller needs the pointer to the value data location.
427 *valueData = halParam->data + valueOffsetFromData;
428 }
429 }
Mikhail Naganov4f110342022-07-07 20:37:42 +0000430 return true;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700431}
432
433Result Effect::analyzeCommandStatus(const char* commandName, const char* context, status_t status) {
434 return analyzeStatus("command", commandName, context, status);
435}
436
Kevin Rocard22505e62017-12-14 18:50:12 -0800437Result Effect::analyzeStatus(const char* funcName, const char* subFuncName,
438 const char* contextDescription, status_t status) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700439 if (status != OK) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800440 ALOGW("Effect %p %s %s %s: %s", mHandle, funcName, subFuncName, contextDescription,
441 strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700442 }
443 switch (status) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800444 case OK:
445 return Result::OK;
446 case -EINVAL:
447 return Result::INVALID_ARGUMENTS;
448 case -ENODATA:
449 return Result::INVALID_STATE;
450 case -ENODEV:
451 return Result::NOT_INITIALIZED;
452 case -ENOMEM:
453 return Result::RESULT_TOO_BIG;
454 case -ENOSYS:
455 return Result::NOT_SUPPORTED;
456 default:
457 return Result::INVALID_STATE;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700458 }
459}
460
Mikhail Naganov0d920592023-08-25 11:10:37 -0700461#define RETURN_IF_EFFECT_CLOSED() \
462 if (mHandle == kInvalidEffectHandle) { \
463 return Result::INVALID_STATE; \
464 }
465#define RETURN_RESULT_IF_EFFECT_CLOSED(result) \
466 if (mHandle == kInvalidEffectHandle) { \
467 _hidl_cb(Result::INVALID_STATE, result); \
468 return Void(); \
469 }
470
471Return<void> Effect::getConfigImpl(int commandCode, const char* commandName,
472 GetConfigCallback _hidl_cb) {
473 RETURN_RESULT_IF_EFFECT_CLOSED(EffectConfig());
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700474 uint32_t halResultSize = sizeof(effect_config_t);
Mikhail Naganov9f289042017-02-23 08:39:36 -0800475 effect_config_t halConfig{};
Kevin Rocard22505e62017-12-14 18:50:12 -0800476 status_t status =
477 (*mHandle)->command(mHandle, commandCode, 0, NULL, &halResultSize, &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700478 EffectConfig config;
479 if (status == OK) {
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000480 status = EffectUtils::effectConfigFromHal(halConfig, mIsInput, &config);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700481 }
Mikhail Naganov0d920592023-08-25 11:10:37 -0700482 _hidl_cb(analyzeCommandStatus(commandName, sContextCallToCommand, status), config);
483 return Void();
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700484}
485
Kevin Rocard22505e62017-12-14 18:50:12 -0800486Result Effect::getCurrentConfigImpl(uint32_t featureId, uint32_t configSize,
487 GetCurrentConfigSuccessCallback onSuccess) {
Mikhail Naganov8e3480e2022-09-01 00:31:43 +0000488 if (configSize > kMaxDataSize - sizeof(uint32_t)) {
489 ALOGE("%s: Config size is too big: %" PRIu32, __func__, configSize);
490 android_errorWriteLog(0x534e4554, "240266798");
491 return Result::INVALID_ARGUMENTS;
492 }
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700493 uint32_t halCmd = featureId;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800494 std::vector<uint32_t> halResult(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700495 uint32_t halResultSize = 0;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800496 return sendCommandReturningStatusAndData(
497 EFFECT_CMD_GET_FEATURE_CONFIG, "GET_FEATURE_CONFIG", sizeof(uint32_t), &halCmd,
498 &halResultSize, &halResult[0], sizeof(uint32_t), [&] { onSuccess(&halResult[1]); });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700499}
500
Kevin Rocard22505e62017-12-14 18:50:12 -0800501Result Effect::getParameterImpl(uint32_t paramSize, const void* paramData,
502 uint32_t requestValueSize, uint32_t replyValueSize,
503 GetParameterSuccessCallback onSuccess) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700504 // As it is unknown what method HAL uses for copying the provided parameter data,
505 // it is safer to make sure that input and output buffers do not overlap.
Mikhail Naganov4f110342022-07-07 20:37:42 +0000506 std::vector<uint8_t> halCmdBuffer;
507 if (!parameterToHal(paramSize, paramData, requestValueSize, nullptr, &halCmdBuffer)) {
508 return Result::INVALID_ARGUMENTS;
509 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800510 const void* valueData = nullptr;
Mikhail Naganov4f110342022-07-07 20:37:42 +0000511 std::vector<uint8_t> halParamBuffer;
512 if (!parameterToHal(paramSize, paramData, replyValueSize, &valueData, &halParamBuffer)) {
513 return Result::INVALID_ARGUMENTS;
514 }
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700515 uint32_t halParamBufferSize = halParamBuffer.size();
516
517 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800518 EFFECT_CMD_GET_PARAM, "GET_PARAM", halCmdBuffer.size(), &halCmdBuffer[0],
519 &halParamBufferSize, &halParamBuffer[0], sizeof(effect_param_t), [&] {
520 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]);
521 onSuccess(halParam->vsize, valueData);
522 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700523}
524
Kevin Rocard22505e62017-12-14 18:50:12 -0800525Result Effect::getSupportedConfigsImpl(uint32_t featureId, uint32_t maxConfigs, uint32_t configSize,
526 GetSupportedConfigsSuccessCallback onSuccess) {
Mikhail Naganov8e3480e2022-09-01 00:31:43 +0000527 if (maxConfigs != 0 && configSize > (kMaxDataSize - 2 * sizeof(uint32_t)) / maxConfigs) {
528 ALOGE("%s: Config size is too big: %" PRIu32, __func__, configSize);
529 return Result::INVALID_ARGUMENTS;
530 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800531 uint32_t halCmd[2] = {featureId, maxConfigs};
Mikhail Naganov8e3480e2022-09-01 00:31:43 +0000532 uint32_t halResultSize = 2 * sizeof(uint32_t) + maxConfigs * configSize;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800533 std::vector<uint8_t> halResult(static_cast<size_t>(halResultSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700534 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800535 EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS, "GET_FEATURE_SUPPORTED_CONFIGS", sizeof(halCmd),
536 halCmd, &halResultSize, &halResult[0], 2 * sizeof(uint32_t), [&] {
537 uint32_t* halResult32 = reinterpret_cast<uint32_t*>(&halResult[0]);
538 uint32_t supportedConfigs = *(++halResult32); // skip status field
539 if (supportedConfigs > maxConfigs) supportedConfigs = maxConfigs;
540 onSuccess(supportedConfigs, ++halResult32);
541 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700542}
543
Mikhail Naganova331de12017-01-04 16:33:55 -0800544Return<void> Effect::prepareForProcessing(prepareForProcessing_cb _hidl_cb) {
Mikhail Naganov0d920592023-08-25 11:10:37 -0700545 RETURN_RESULT_IF_EFFECT_CLOSED(StatusMQ::Descriptor());
Mikhail Naganova331de12017-01-04 16:33:55 -0800546 status_t status;
547 // Create message queue.
548 if (mStatusMQ) {
549 ALOGE("the client attempts to call prepareForProcessing_cb twice");
550 _hidl_cb(Result::INVALID_STATE, StatusMQ::Descriptor());
551 return Void();
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700552 }
Mikhail Naganova331de12017-01-04 16:33:55 -0800553 std::unique_ptr<StatusMQ> tempStatusMQ(new StatusMQ(1, true /*EventFlag*/));
554 if (!tempStatusMQ->isValid()) {
555 ALOGE_IF(!tempStatusMQ->isValid(), "status MQ is invalid");
556 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
557 return Void();
558 }
559 status = EventFlag::createEventFlag(tempStatusMQ->getEventFlagWord(), &mEfGroup);
560 if (status != OK || !mEfGroup) {
561 ALOGE("failed creating event flag for status MQ: %s", strerror(-status));
562 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
563 return Void();
564 }
565
566 // Create and launch the thread.
Kevin Rocard22505e62017-12-14 18:50:12 -0800567 mProcessThread = new ProcessThread(&mStopProcessThread, mHandle, &mHalInBufferPtr,
Andy Hung502f9d02022-04-26 18:37:36 -0700568 &mHalOutBufferPtr, tempStatusMQ.get(), mEfGroup, this);
Mikhail Naganova331de12017-01-04 16:33:55 -0800569 status = mProcessThread->run("effect", PRIORITY_URGENT_AUDIO);
570 if (status != OK) {
571 ALOGW("failed to start effect processing thread: %s", strerror(-status));
572 _hidl_cb(Result::INVALID_ARGUMENTS, MQDescriptorSync<Result>());
573 return Void();
574 }
575
Andy Hungcbde2d02023-01-11 12:08:43 -0800576 // For a spatializer effect, we perform scheduler adjustments to reduce glitches and power.
577 // We do it here instead of the ProcessThread::threadLoop to ensure that mHandle is valid.
578 if (effect_descriptor_t halDescriptor{};
579 (*mHandle)->get_descriptor(mHandle, &halDescriptor) == NO_ERROR &&
580 memcmp(&halDescriptor.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0) {
581 const status_t status = scheduler::updateSpatializerPriority(mProcessThread->getTid());
582 ALOGW_IF(status != OK, "Failed to update Spatializer priority");
583 }
584
Mikhail Naganova331de12017-01-04 16:33:55 -0800585 mStatusMQ = std::move(tempStatusMQ);
586 _hidl_cb(Result::OK, *mStatusMQ->getDesc());
587 return Void();
588}
589
Kevin Rocard22505e62017-12-14 18:50:12 -0800590Return<Result> Effect::setProcessBuffers(const AudioBuffer& inBuffer,
591 const AudioBuffer& outBuffer) {
Mikhail Naganov0d920592023-08-25 11:10:37 -0700592 RETURN_IF_EFFECT_CLOSED();
Mikhail Naganova331de12017-01-04 16:33:55 -0800593 AudioBufferManager& manager = AudioBufferManager::getInstance();
594 sp<AudioBufferWrapper> tempInBuffer, tempOutBuffer;
595 if (!manager.wrap(inBuffer, &tempInBuffer)) {
596 ALOGE("Could not map memory of the input buffer");
597 return Result::INVALID_ARGUMENTS;
598 }
599 if (!manager.wrap(outBuffer, &tempOutBuffer)) {
600 ALOGE("Could not map memory of the output buffer");
601 return Result::INVALID_ARGUMENTS;
602 }
603 mInBuffer = tempInBuffer;
604 mOutBuffer = tempOutBuffer;
605 // The processing thread only reads these pointers after waking up by an event flag,
606 // so it's OK to update the pair non-atomically.
607 mHalInBufferPtr.store(mInBuffer->getHalBuffer(), std::memory_order_release);
608 mHalOutBufferPtr.store(mOutBuffer->getHalBuffer(), std::memory_order_release);
609 return Result::OK;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700610}
611
612Result Effect::sendCommand(int commandCode, const char* commandName) {
613 return sendCommand(commandCode, commandName, 0, NULL);
614}
615
Kevin Rocard22505e62017-12-14 18:50:12 -0800616Result Effect::sendCommand(int commandCode, const char* commandName, uint32_t size, void* data) {
Mikhail Naganov0d920592023-08-25 11:10:37 -0700617 RETURN_IF_EFFECT_CLOSED();
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700618 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, 0, NULL);
619 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
620}
621
Kevin Rocard22505e62017-12-14 18:50:12 -0800622Result Effect::sendCommandReturningData(int commandCode, const char* commandName,
623 uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700624 return sendCommandReturningData(commandCode, commandName, 0, NULL, replySize, replyData);
625}
626
Kevin Rocard22505e62017-12-14 18:50:12 -0800627Result Effect::sendCommandReturningData(int commandCode, const char* commandName, uint32_t size,
628 void* data, uint32_t* replySize, void* replyData) {
Mikhail Naganov0d920592023-08-25 11:10:37 -0700629 RETURN_IF_EFFECT_CLOSED();
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700630 uint32_t expectedReplySize = *replySize;
631 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
632 if (status == OK && *replySize != expectedReplySize) {
633 status = -ENODATA;
634 }
635 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
636}
637
638Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName) {
639 return sendCommandReturningStatus(commandCode, commandName, 0, NULL);
640}
641
Kevin Rocard22505e62017-12-14 18:50:12 -0800642Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName, uint32_t size,
643 void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700644 uint32_t replyCmdStatus;
645 uint32_t replySize = sizeof(uint32_t);
Kevin Rocard22505e62017-12-14 18:50:12 -0800646 return sendCommandReturningStatusAndData(commandCode, commandName, size, data, &replySize,
647 &replyCmdStatus, replySize, [] {});
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700648}
649
Kevin Rocard22505e62017-12-14 18:50:12 -0800650Result Effect::sendCommandReturningStatusAndData(int commandCode, const char* commandName,
651 uint32_t size, void* data, uint32_t* replySize,
652 void* replyData, uint32_t minReplySize,
653 CommandSuccessCallback onSuccess) {
Mikhail Naganov0d920592023-08-25 11:10:37 -0700654 RETURN_IF_EFFECT_CLOSED();
Kevin Rocard22505e62017-12-14 18:50:12 -0800655 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700656 Result retval;
657 if (status == OK && minReplySize >= sizeof(uint32_t) && *replySize >= minReplySize) {
658 uint32_t commandStatus = *reinterpret_cast<uint32_t*>(replyData);
659 retval = analyzeCommandStatus(commandName, sContextResultOfCommand, commandStatus);
660 if (commandStatus == OK) {
661 onSuccess();
662 }
663 } else {
664 retval = analyzeCommandStatus(commandName, sContextCallToCommand, status);
665 }
666 return retval;
667}
668
Kevin Rocard22505e62017-12-14 18:50:12 -0800669Result Effect::setConfigImpl(int commandCode, const char* commandName, const EffectConfig& config,
670 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
671 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700672 effect_config_t halConfig;
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000673 EffectUtils::effectConfigToHal(config, &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700674 if (inputBufferProvider != 0) {
675 LOG_FATAL("Using input buffer provider is not supported");
676 }
677 if (outputBufferProvider != 0) {
678 LOG_FATAL("Using output buffer provider is not supported");
679 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800680 return sendCommandReturningStatus(commandCode, commandName, sizeof(effect_config_t),
681 &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700682}
683
Kevin Rocard22505e62017-12-14 18:50:12 -0800684Result Effect::setParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize,
685 const void* valueData) {
Mikhail Naganov4f110342022-07-07 20:37:42 +0000686 std::vector<uint8_t> halParamBuffer;
687 if (!parameterToHal(paramSize, paramData, valueSize, &valueData, &halParamBuffer)) {
688 return Result::INVALID_ARGUMENTS;
689 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800690 return sendCommandReturningStatus(EFFECT_CMD_SET_PARAM, "SET_PARAM", halParamBuffer.size(),
691 &halParamBuffer[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700692}
693
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800694// Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow.
Kevin Rocard22505e62017-12-14 18:50:12 -0800695Return<Result> Effect::init() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700696 return sendCommandReturningStatus(EFFECT_CMD_INIT, "INIT");
697}
698
Kevin Rocard22505e62017-12-14 18:50:12 -0800699Return<Result> Effect::setConfig(const EffectConfig& config,
700 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
701 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
702 return setConfigImpl(EFFECT_CMD_SET_CONFIG, "SET_CONFIG", config, inputBufferProvider,
703 outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700704}
705
Kevin Rocard22505e62017-12-14 18:50:12 -0800706Return<Result> Effect::reset() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700707 return sendCommand(EFFECT_CMD_RESET, "RESET");
708}
709
Kevin Rocard22505e62017-12-14 18:50:12 -0800710Return<Result> Effect::enable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700711 return sendCommandReturningStatus(EFFECT_CMD_ENABLE, "ENABLE");
712}
713
Kevin Rocard22505e62017-12-14 18:50:12 -0800714Return<Result> Effect::disable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700715 return sendCommandReturningStatus(EFFECT_CMD_DISABLE, "DISABLE");
716}
717
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800718Return<Result> Effect::setAudioSource(
719#if MAJOR_VERSION <= 6
720 AudioSource source
721#else
722 const AudioSource& source
723#endif
724) {
725 audio_source_t halSource;
726 if (status_t status = HidlUtils::audioSourceToHal(source, &halSource); status == NO_ERROR) {
727 uint32_t halSourceParam = static_cast<uint32_t>(halSource);
728 return sendCommand(EFFECT_CMD_SET_AUDIO_SOURCE, "SET_AUDIO_SOURCE", sizeof(uint32_t),
729 &halSourceParam);
730 } else {
731 return analyzeStatus(__func__, "audioSourceToHal", sContextConversion, status);
732 }
733}
734
735#if MAJOR_VERSION <= 6
736
Kevin Rocard30a7fcc2018-03-01 15:08:07 -0800737Return<Result> Effect::setDevice(AudioDeviceBitfield device) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700738 uint32_t halDevice = static_cast<uint32_t>(device);
739 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDevice);
740}
741
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800742Return<Result> Effect::setInputDevice(AudioDeviceBitfield device) {
743 uint32_t halDevice = static_cast<uint32_t>(device);
744 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
745 &halDevice);
746}
747
748#else // MAJOR_VERSION <= 6
749
750Return<Result> Effect::setDevice(const DeviceAddress& device) {
751 audio_devices_t halDevice;
752 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
753 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
754 status == NO_ERROR) {
755 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
756 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDeviceParam);
757 } else {
758 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
759 }
760}
761
762Return<Result> Effect::setInputDevice(const DeviceAddress& device) {
763 audio_devices_t halDevice;
764 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
765 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
766 status == NO_ERROR) {
767 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
768 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
769 &halDeviceParam);
770 } else {
771 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
772 }
773}
774
775#endif // MAJOR_VERSION <= 6
776
Kevin Rocard22505e62017-12-14 18:50:12 -0800777Return<void> Effect::setAndGetVolume(const hidl_vec<uint32_t>& volumes,
778 setAndGetVolume_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700779 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800780 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700781 uint32_t halResultSize = halDataSize;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800782 std::vector<uint32_t> halResult(volumes.size(), 0);
Kevin Rocard22505e62017-12-14 18:50:12 -0800783 Result retval = sendCommandReturningData(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800784 &halData[0], &halResultSize, &halResult[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700785 hidl_vec<uint32_t> result;
786 if (retval == Result::OK) {
787 result.setToExternal(&halResult[0], halResultSize);
788 }
789 _hidl_cb(retval, result);
790 return Void();
791}
792
Kevin Rocard22505e62017-12-14 18:50:12 -0800793Return<Result> Effect::volumeChangeNotification(const hidl_vec<uint32_t>& volumes) {
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800794 uint32_t halDataSize;
795 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Kevin Rocard22505e62017-12-14 18:50:12 -0800796 return sendCommand(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize, &halData[0]);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800797}
798
Kevin Rocard22505e62017-12-14 18:50:12 -0800799Return<Result> Effect::setAudioMode(AudioMode mode) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700800 uint32_t halMode = static_cast<uint32_t>(mode);
Kevin Rocard22505e62017-12-14 18:50:12 -0800801 return sendCommand(EFFECT_CMD_SET_AUDIO_MODE, "SET_AUDIO_MODE", sizeof(uint32_t), &halMode);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700802}
803
804Return<Result> Effect::setConfigReverse(
Kevin Rocard22505e62017-12-14 18:50:12 -0800805 const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
806 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
807 return setConfigImpl(EFFECT_CMD_SET_CONFIG_REVERSE, "SET_CONFIG_REVERSE", config,
808 inputBufferProvider, outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700809}
810
Kevin Rocard22505e62017-12-14 18:50:12 -0800811Return<void> Effect::getConfig(getConfig_cb _hidl_cb) {
Mikhail Naganov0d920592023-08-25 11:10:37 -0700812 return getConfigImpl(EFFECT_CMD_GET_CONFIG, "GET_CONFIG", _hidl_cb);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700813}
814
Kevin Rocard22505e62017-12-14 18:50:12 -0800815Return<void> Effect::getConfigReverse(getConfigReverse_cb _hidl_cb) {
Mikhail Naganov0d920592023-08-25 11:10:37 -0700816 return getConfigImpl(EFFECT_CMD_GET_CONFIG_REVERSE, "GET_CONFIG_REVERSE", _hidl_cb);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700817}
818
Kevin Rocard22505e62017-12-14 18:50:12 -0800819Return<void> Effect::getSupportedAuxChannelsConfigs(uint32_t maxConfigs,
820 getSupportedAuxChannelsConfigs_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700821 hidl_vec<EffectAuxChannelsConfig> result;
822 Result retval = getSupportedConfigsImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800823 EFFECT_FEATURE_AUX_CHANNELS, maxConfigs, sizeof(channel_config_t),
824 [&](uint32_t supportedConfigs, void* configsData) {
825 result.resize(supportedConfigs);
826 channel_config_t* config = reinterpret_cast<channel_config_t*>(configsData);
827 for (size_t i = 0; i < result.size(); ++i) {
828 effectAuxChannelsConfigFromHal(*config++, &result[i]);
829 }
830 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700831 _hidl_cb(retval, result);
832 return Void();
833}
834
Kevin Rocard22505e62017-12-14 18:50:12 -0800835Return<void> Effect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700836 EffectAuxChannelsConfig result;
837 Result retval = getCurrentConfigImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800838 EFFECT_FEATURE_AUX_CHANNELS, sizeof(channel_config_t), [&](void* configData) {
839 effectAuxChannelsConfigFromHal(*reinterpret_cast<channel_config_t*>(configData),
840 &result);
841 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700842 _hidl_cb(retval, result);
843 return Void();
844}
845
Kevin Rocard22505e62017-12-14 18:50:12 -0800846Return<Result> Effect::setAuxChannelsConfig(const EffectAuxChannelsConfig& config) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800847 std::vector<uint32_t> halCmd(
848 alignedSizeIn<uint32_t>(sizeof(uint32_t) + sizeof(channel_config_t)), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700849 halCmd[0] = EFFECT_FEATURE_AUX_CHANNELS;
850 effectAuxChannelsConfigToHal(config, reinterpret_cast<channel_config_t*>(&halCmd[1]));
851 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800852 "SET_FEATURE_CONFIG AUX_CHANNELS", halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700853}
854
Kevin Rocard22505e62017-12-14 18:50:12 -0800855Return<Result> Effect::offload(const EffectOffloadParameter& param) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700856 effect_offload_param_t halParam;
857 effectOffloadParamToHal(param, &halParam);
Kevin Rocard22505e62017-12-14 18:50:12 -0800858 return sendCommandReturningStatus(EFFECT_CMD_OFFLOAD, "OFFLOAD", sizeof(effect_offload_param_t),
859 &halParam);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700860}
861
Kevin Rocard22505e62017-12-14 18:50:12 -0800862Return<void> Effect::getDescriptor(getDescriptor_cb _hidl_cb) {
Mikhail Naganov0d920592023-08-25 11:10:37 -0700863 RETURN_RESULT_IF_EFFECT_CLOSED(EffectDescriptor());
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700864 effect_descriptor_t halDescriptor;
865 memset(&halDescriptor, 0, sizeof(effect_descriptor_t));
866 status_t status = (*mHandle)->get_descriptor(mHandle, &halDescriptor);
867 EffectDescriptor descriptor;
868 if (status == OK) {
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000869 status = EffectUtils::effectDescriptorFromHal(halDescriptor, &descriptor);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700870 }
871 _hidl_cb(analyzeStatus("get_descriptor", "", sContextCallFunction, status), descriptor);
872 return Void();
873}
874
Kevin Rocard22505e62017-12-14 18:50:12 -0800875Return<void> Effect::command(uint32_t commandId, const hidl_vec<uint8_t>& data,
876 uint32_t resultMaxSize, command_cb _hidl_cb) {
Mikhail Naganov0d920592023-08-25 11:10:37 -0700877 if (mHandle == kInvalidEffectHandle) {
878 _hidl_cb(-ENODATA, hidl_vec<uint8_t>());
879 return Void();
880 }
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700881 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800882 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(data, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700883 uint32_t halResultSize = resultMaxSize;
884 std::unique_ptr<uint8_t[]> halResult(new uint8_t[halResultSize]);
885 memset(&halResult[0], 0, halResultSize);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800886
887 void* dataPtr = halDataSize > 0 ? &halData[0] : NULL;
888 void* resultPtr = halResultSize > 0 ? &halResult[0] : NULL;
Andy Hung7d5eb5c2022-10-18 18:03:54 -0700889 status_t status = BAD_VALUE;
890 switch (commandId) {
891 case 'gtid': // retrieve the tid, used for spatializer priority boost
892 if (halDataSize == 0 && resultMaxSize == sizeof(int32_t)) {
893 auto ptid = (int32_t*)resultPtr;
894 ptid[0] = mProcessThread ? mProcessThread->getTid() : -1;
895 status = OK;
896 break; // we have handled 'gtid' here.
897 }
898 [[fallthrough]]; // allow 'gtid' overload (checked halDataSize and resultMaxSize).
899 default:
900 status = (*mHandle)->command(mHandle, commandId, halDataSize, dataPtr, &halResultSize,
901 resultPtr);
902 break;
903 }
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700904 hidl_vec<uint8_t> result;
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800905 if (status == OK && resultPtr != NULL) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700906 result.setToExternal(&halResult[0], halResultSize);
907 }
908 _hidl_cb(status, result);
909 return Void();
910}
911
Kevin Rocard22505e62017-12-14 18:50:12 -0800912Return<Result> Effect::setParameter(const hidl_vec<uint8_t>& parameter,
913 const hidl_vec<uint8_t>& value) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700914 return setParameterImpl(parameter.size(), &parameter[0], value.size(), &value[0]);
915}
916
Kevin Rocard22505e62017-12-14 18:50:12 -0800917Return<void> Effect::getParameter(const hidl_vec<uint8_t>& parameter, uint32_t valueMaxSize,
918 getParameter_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700919 hidl_vec<uint8_t> value;
920 Result retval = getParameterImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800921 parameter.size(), &parameter[0], valueMaxSize,
922 [&](uint32_t valueSize, const void* valueData) {
923 value.setToExternal(reinterpret_cast<uint8_t*>(const_cast<void*>(valueData)),
924 valueSize);
925 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700926 _hidl_cb(retval, value);
927 return Void();
928}
929
Kevin Rocard22505e62017-12-14 18:50:12 -0800930Return<void> Effect::getSupportedConfigsForFeature(uint32_t featureId, uint32_t maxConfigs,
931 uint32_t configSize,
932 getSupportedConfigsForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700933 uint32_t configCount = 0;
934 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800935 Result retval = getSupportedConfigsImpl(featureId, maxConfigs, configSize,
936 [&](uint32_t supportedConfigs, void* configsData) {
937 configCount = supportedConfigs;
938 result.resize(configCount * configSize);
939 memcpy(&result[0], configsData, result.size());
940 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700941 _hidl_cb(retval, configCount, result);
942 return Void();
943}
944
Kevin Rocard22505e62017-12-14 18:50:12 -0800945Return<void> Effect::getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize,
946 getCurrentConfigForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700947 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800948 Result retval = getCurrentConfigImpl(featureId, configSize, [&](void* configData) {
949 result.resize(configSize);
950 memcpy(&result[0], configData, result.size());
951 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700952 _hidl_cb(retval, result);
953 return Void();
954}
955
Kevin Rocard22505e62017-12-14 18:50:12 -0800956Return<Result> Effect::setCurrentConfigForFeature(uint32_t featureId,
957 const hidl_vec<uint8_t>& configData) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800958 std::vector<uint32_t> halCmd(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configData.size()), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700959 halCmd[0] = featureId;
960 memcpy(&halCmd[1], &configData[0], configData.size());
Kevin Rocard22505e62017-12-14 18:50:12 -0800961 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG, "SET_FEATURE_CONFIG",
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800962 halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700963}
964
Mikhail Naganov0d920592023-08-25 11:10:37 -0700965std::tuple<Result, effect_handle_t> Effect::closeImpl() {
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800966 if (mStopProcessThread.load(std::memory_order_relaxed)) { // only this thread modifies
Mikhail Naganov0d920592023-08-25 11:10:37 -0700967 return {Result::INVALID_STATE, kInvalidEffectHandle};
Mikhail Naganova331de12017-01-04 16:33:55 -0800968 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800969 mStopProcessThread.store(true, std::memory_order_release);
Mikhail Naganova331de12017-01-04 16:33:55 -0800970 if (mEfGroup) {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800971 mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT));
Mikhail Naganova331de12017-01-04 16:33:55 -0800972 }
Mikhail Naganov0d920592023-08-25 11:10:37 -0700973 effect_handle_t handle = mHandle;
974 mHandle = kInvalidEffectHandle;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800975#if MAJOR_VERSION <= 5
Mikhail Naganov0d920592023-08-25 11:10:37 -0700976 return {Result::OK, handle};
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800977#elif MAJOR_VERSION >= 6
978 // No need to join the processing thread, it is part of the API contract that the client
979 // must finish processing before closing the effect.
Mikhail Naganov0d920592023-08-25 11:10:37 -0700980 Result retval = analyzeStatus("EffectRelease", "", sContextCallFunction, EffectRelease(handle));
981 EffectMap::getInstance().remove(handle);
982 return {retval, handle};
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800983#endif
Mikhail Naganova331de12017-01-04 16:33:55 -0800984}
985
Mikhail Naganov0d920592023-08-25 11:10:37 -0700986Return<Result> Effect::close() {
987 RETURN_IF_EFFECT_CLOSED();
988 auto [result, _] = closeImpl();
989 return result;
990}
991
Mikhail Naganovfa021442019-02-22 14:28:26 -0800992Return<void> Effect::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& /* options */) {
993 if (fd.getNativeHandle() != nullptr && fd->numFds == 1) {
994 uint32_t cmdData = fd->data[0];
995 (void)sendCommand(EFFECT_CMD_DUMP, "DUMP", sizeof(cmdData), &cmdData);
Andy Hung502f9d02022-04-26 18:37:36 -0700996 const std::string s = mStatistics->dump();
997 if (s.size() != 0) write(cmdData, s.c_str(), s.size());
Mikhail Naganovfa021442019-02-22 14:28:26 -0800998 }
999 return Void();
1000}
1001
Kevin Rocard22505e62017-12-14 18:50:12 -08001002} // namespace implementation
Kevin Rocard96d2cd92018-11-14 16:22:07 -08001003} // namespace CPP_VERSION
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -07001004} // namespace effect
1005} // namespace audio
1006} // namespace hardware
1007} // namespace android