blob: ae899c0f68135d5ffdd788f56ddcd8bbf5803698 [file] [log] [blame]
Eric Laurent801a1182010-06-09 00:17:29 -07001/*
2**
3** Copyright 2010, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioEffect"
21
22#include <stdint.h>
23#include <sys/types.h>
24#include <limits.h>
25
Eric Laurent801a1182010-06-09 00:17:29 -070026#include <binder/IPCThreadState.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070027#include <media/AudioEffect.h>
28#include <media/ShmemCompat.h>
29#include <private/media/AudioEffectShared.h>
30#include <utils/Log.h>
Eric Laurent801a1182010-06-09 00:17:29 -070031
32namespace android {
33
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070034using binder::Status;
35
36namespace {
37
38// Copy from a raw pointer + size into a vector of bytes.
39void appendToBuffer(const void* data,
40 size_t size,
41 std::vector<uint8_t>* buffer) {
42 const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
43 buffer->insert(buffer->end(), p, p + size);
44}
45
46} // namespace
47
Eric Laurent801a1182010-06-09 00:17:29 -070048// ---------------------------------------------------------------------------
49
Svet Ganovbe71aa22015-04-28 12:06:02 -070050AudioEffect::AudioEffect(const String16& opPackageName)
Mikhail Naganov416fffe2020-07-31 17:36:08 -070051 : mOpPackageName(opPackageName)
Eric Laurent801a1182010-06-09 00:17:29 -070052{
53}
54
Eric Laurent801a1182010-06-09 00:17:29 -070055status_t AudioEffect::set(const effect_uuid_t *type,
56 const effect_uuid_t *uuid,
57 int32_t priority,
58 effect_callback_t cbf,
59 void* user,
Glenn Kastend848eb42016-03-08 13:42:11 -080060 audio_session_t sessionId,
Eric Laurent94876032019-11-13 12:45:28 -080061 audio_io_handle_t io,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070062 const AudioDeviceTypeAddr& device,
63 bool probe)
Eric Laurent801a1182010-06-09 00:17:29 -070064{
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070065 sp<media::IEffect> iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -070066 sp<IMemory> cblk;
67 int enabled;
68
Steve Block3856b092011-10-20 11:56:00 +010069 ALOGV("set %p mUserData: %p uuid: %p timeLow %08x", this, user, type, type ? type->timeLow : 0);
Eric Laurent801a1182010-06-09 00:17:29 -070070
71 if (mIEffect != 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +000072 ALOGW("Effect already in use");
Eric Laurent801a1182010-06-09 00:17:29 -070073 return INVALID_OPERATION;
74 }
75
Eric Laurent94876032019-11-13 12:45:28 -080076 if (sessionId == AUDIO_SESSION_DEVICE && io != AUDIO_IO_HANDLE_NONE) {
77 ALOGW("IO handle should not be specified for device effect");
78 return BAD_VALUE;
79 }
Eric Laurent801a1182010-06-09 00:17:29 -070080 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
81 if (audioFlinger == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000082 ALOGE("set(): Could not get audioflinger");
Eric Laurent801a1182010-06-09 00:17:29 -070083 return NO_INIT;
84 }
85
86 if (type == NULL && uuid == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +000087 ALOGW("Must specify at least type or uuid");
Eric Laurent801a1182010-06-09 00:17:29 -070088 return BAD_VALUE;
89 }
Eric Laurent2fe0acd2020-03-13 14:30:46 -070090 mProbe = probe;
Eric Laurent801a1182010-06-09 00:17:29 -070091 mPriority = priority;
92 mCbf = cbf;
93 mUserData = user;
94 mSessionId = sessionId;
95
96 memset(&mDescriptor, 0, sizeof(effect_descriptor_t));
Glenn Kastena189a682012-02-20 12:16:30 -080097 mDescriptor.type = *(type != NULL ? type : EFFECT_UUID_NULL);
98 mDescriptor.uuid = *(uuid != NULL ? uuid : EFFECT_UUID_NULL);
Eric Laurent801a1182010-06-09 00:17:29 -070099
100 mIEffectClient = new EffectClient(this);
Eric Laurentb6436272016-12-07 19:24:50 -0800101 mClientPid = IPCThreadState::self()->getCallingPid();
Andy Hung8b0bfd92019-12-23 13:11:11 -0800102 mClientUid = IPCThreadState::self()->getCallingUid();
Eric Laurent801a1182010-06-09 00:17:29 -0700103
Ytai Ben-Tsvice182942020-11-04 14:48:01 -0800104 media::CreateEffectRequest request;
105 request.desc = VALUE_OR_RETURN_STATUS(
106 legacy2aidl_effect_descriptor_t_EffectDescriptor(mDescriptor));
107 request.client = mIEffectClient;
108 request.priority = priority;
109 request.output = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
110 request.sessionId = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(mSessionId));
111 request.device = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioDeviceTypeAddress(device));
112 request.opPackageName = VALUE_OR_RETURN_STATUS(legacy2aidl_String16_string(mOpPackageName));
113 request.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(mClientPid));
114 request.probe = probe;
115
116 media::CreateEffectResponse response;
117
118 mStatus = audioFlinger->createEffect(request, &response);
119
120 if (mStatus == OK) {
121 mId = response.id;
122 enabled = response.enabled;
123 iEffect = response.effect;
124 mDescriptor = VALUE_OR_RETURN_STATUS(
125 aidl2legacy_EffectDescriptor_effect_descriptor_t(response.desc));
126 }
Eric Laurent801a1182010-06-09 00:17:29 -0700127
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700128 // In probe mode, we stop here and return the status: the IEffect interface to
129 // audio flinger will not be retained. initCheck() will return the creation status
130 // but all other APIs will return invalid operation.
131 if (probe || iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000132 char typeBuffer[64] = {}, uuidBuffer[64] = {};
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700133 guidToString(type, typeBuffer, sizeof(typeBuffer));
134 guidToString(uuid, uuidBuffer, sizeof(uuidBuffer));
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700135 ALOGE_IF(!probe, "set(): AudioFlinger could not create effect %s / %s, status: %d",
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000136 type != nullptr ? typeBuffer : "NULL",
137 uuid != nullptr ? uuidBuffer : "NULL",
138 mStatus);
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700139 if (!probe && iEffect == 0) {
Eric Laurenteecd7652015-06-04 16:20:16 -0700140 mStatus = NO_INIT;
141 }
Eric Laurent801a1182010-06-09 00:17:29 -0700142 return mStatus;
143 }
144
145 mEnabled = (volatile int32_t)enabled;
146
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700147 if (media::SharedFileRegion shmem;
148 !iEffect->getCblk(&shmem).isOk()
149 || !convertSharedFileRegionToIMemory(shmem, &cblk)
150 || cblk == 0) {
Eric Laurent801a1182010-06-09 00:17:29 -0700151 mStatus = NO_INIT;
Steve Block29357bc2012-01-06 19:20:56 +0000152 ALOGE("Could not get control block");
Eric Laurent801a1182010-06-09 00:17:29 -0700153 return mStatus;
154 }
155
Eric Laurenteecd7652015-06-04 16:20:16 -0700156 mIEffect = iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -0700157 mCblkMemory = cblk;
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700158 // TODO: Using unsecurePointer() has some associated security pitfalls
159 // (see declaration for details).
160 // Either document why it is safe in this case or address the
161 // issue (e.g. by copying).
162 mCblk = static_cast<effect_param_cblk_t*>(cblk->unsecurePointer());
Eric Laurent801a1182010-06-09 00:17:29 -0700163 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
164 mCblk->buffer = (uint8_t *)mCblk + bufOffset;
165
Marco Nelissen06b46062014-11-14 07:58:25 -0800166 IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700167 ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId,
168 mStatus, mEnabled, mClientPid);
169
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800170 if (!audio_is_global_session(mSessionId)) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800171 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid, mClientUid);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700172 }
Eric Laurent801a1182010-06-09 00:17:29 -0700173
174 return mStatus;
175}
176
Mikhail Naganov416fffe2020-07-31 17:36:08 -0700177status_t AudioEffect::set(const char *typeStr,
178 const char *uuidStr,
179 int32_t priority,
180 effect_callback_t cbf,
181 void* user,
182 audio_session_t sessionId,
183 audio_io_handle_t io,
184 const AudioDeviceTypeAddr& device,
185 bool probe)
186{
187 effect_uuid_t type;
188 effect_uuid_t *pType = nullptr;
189 effect_uuid_t uuid;
190 effect_uuid_t *pUuid = nullptr;
191
192 ALOGV("AudioEffect::set string\n - type: %s\n - uuid: %s",
193 typeStr ? typeStr : "nullptr", uuidStr ? uuidStr : "nullptr");
194
195 if (stringToGuid(typeStr, &type) == NO_ERROR) {
196 pType = &type;
197 }
198 if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
199 pUuid = &uuid;
200 }
201
202 return set(pType, pUuid, priority, cbf, user, sessionId, io, device, probe);
203}
204
Eric Laurent801a1182010-06-09 00:17:29 -0700205
206AudioEffect::~AudioEffect()
207{
Steve Block3856b092011-10-20 11:56:00 +0100208 ALOGV("Destructor %p", this);
Eric Laurent801a1182010-06-09 00:17:29 -0700209
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700210 if (!mProbe && (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS)) {
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800211 if (!audio_is_global_session(mSessionId)) {
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700212 AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
213 }
Eric Laurent801a1182010-06-09 00:17:29 -0700214 if (mIEffect != NULL) {
215 mIEffect->disconnect();
Marco Nelissen06b46062014-11-14 07:58:25 -0800216 IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient);
Eric Laurent801a1182010-06-09 00:17:29 -0700217 }
Eric Laurenteecd7652015-06-04 16:20:16 -0700218 mIEffect.clear();
219 mCblkMemory.clear();
Eric Laurent801a1182010-06-09 00:17:29 -0700220 }
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700221 mIEffectClient.clear();
222 IPCThreadState::self()->flushCommands();
Eric Laurent801a1182010-06-09 00:17:29 -0700223}
224
225
226status_t AudioEffect::initCheck() const
227{
228 return mStatus;
229}
230
231// -------------------------------------------------------------------------
232
233effect_descriptor_t AudioEffect::descriptor() const
234{
235 return mDescriptor;
236}
237
Eric Laurentda7581b2010-07-02 08:12:41 -0700238bool AudioEffect::getEnabled() const
Eric Laurent801a1182010-06-09 00:17:29 -0700239{
240 return (mEnabled != 0);
241}
242
Eric Laurentda7581b2010-07-02 08:12:41 -0700243status_t AudioEffect::setEnabled(bool enabled)
Eric Laurent801a1182010-06-09 00:17:29 -0700244{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700245 if (mProbe) {
246 return INVALID_OPERATION;
247 }
Eric Laurent801a1182010-06-09 00:17:29 -0700248 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800249 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700250 }
Eric Laurent801a1182010-06-09 00:17:29 -0700251
Eric Laurentf5aafb22010-11-18 08:40:16 -0800252 status_t status = NO_ERROR;
Eric Laurentf5aafb22010-11-18 08:40:16 -0800253 AutoMutex lock(mLock);
254 if (enabled != mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700255 Status bs;
256
Eric Laurentf5aafb22010-11-18 08:40:16 -0800257 if (enabled) {
Steve Block3856b092011-10-20 11:56:00 +0100258 ALOGV("enable %p", this);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700259 bs = mIEffect->enable(&status);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800260 } else {
Steve Block3856b092011-10-20 11:56:00 +0100261 ALOGV("disable %p", this);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700262 bs = mIEffect->disable(&status);
263 }
264 if (!bs.isOk()) {
265 status = bs.transactionError();
Eric Laurentda7581b2010-07-02 08:12:41 -0700266 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800267 if (status == NO_ERROR) {
268 mEnabled = enabled;
Eric Laurentda7581b2010-07-02 08:12:41 -0700269 }
Eric Laurent801a1182010-06-09 00:17:29 -0700270 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800271 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700272}
273
Eric Laurent25f43952010-07-28 05:40:18 -0700274status_t AudioEffect::command(uint32_t cmdCode,
275 uint32_t cmdSize,
276 void *cmdData,
277 uint32_t *replySize,
278 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700279{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700280 if (mProbe) {
281 return INVALID_OPERATION;
282 }
Eric Laurent801a1182010-06-09 00:17:29 -0700283 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
Steve Block3856b092011-10-20 11:56:00 +0100284 ALOGV("command() bad status %d", mStatus);
John Grossmanaf7d8182012-01-11 12:23:42 -0800285 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700286 }
287
Eric Laurentf5aafb22010-11-18 08:40:16 -0800288 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
289 if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
290 return NO_ERROR;
291 }
292 if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) {
293 return BAD_VALUE;
294 }
295 mLock.lock();
Eric Laurent0fa449c2010-09-24 11:52:04 -0700296 }
297
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700298 std::vector<uint8_t> data;
299 appendToBuffer(cmdData, cmdSize, &data);
300
301 status_t status;
302 std::vector<uint8_t> response;
303
304 Status bs = mIEffect->command(cmdCode, data, *replySize, &response, &status);
305 if (!bs.isOk()) {
306 status = bs.transactionError();
307 }
308 if (status == NO_ERROR) {
309 memcpy(replyData, response.data(), response.size());
310 *replySize = response.size();
311 }
Eric Laurent0fa449c2010-09-24 11:52:04 -0700312
313 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800314 if (status == NO_ERROR) {
315 status = *(status_t *)replyData;
Eric Laurent0fa449c2010-09-24 11:52:04 -0700316 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800317 if (status == NO_ERROR) {
318 mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700319 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800320 mLock.unlock();
Eric Laurent8569f0d2010-07-29 23:43:43 -0700321 }
322
Eric Laurent8569f0d2010-07-29 23:43:43 -0700323 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700324}
325
Eric Laurent801a1182010-06-09 00:17:29 -0700326status_t AudioEffect::setParameter(effect_param_t *param)
327{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700328 if (mProbe) {
329 return INVALID_OPERATION;
330 }
Eric Laurent801a1182010-06-09 00:17:29 -0700331 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800332 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700333 }
334
335 if (param == NULL || param->psize == 0 || param->vsize == 0) {
336 return BAD_VALUE;
337 }
338
Eric Laurent25f43952010-07-28 05:40:18 -0700339 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700340
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700341 ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data,
342 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700343
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700344 std::vector<uint8_t> cmd;
345 appendToBuffer(param, sizeof(effect_param_t) + psize, &cmd);
346 std::vector<uint8_t> response;
347 status_t status;
348 Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM,
349 cmd,
350 sizeof(int),
351 &response,
352 &status);
353 if (!bs.isOk()) {
354 status = bs.transactionError();
355 return status;
356 }
357 assert(response.size() == sizeof(int));
358 memcpy(&param->status, response.data(), response.size());
359 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700360}
361
362status_t AudioEffect::setParameterDeferred(effect_param_t *param)
363{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700364 if (mProbe) {
365 return INVALID_OPERATION;
366 }
Eric Laurent801a1182010-06-09 00:17:29 -0700367 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800368 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700369 }
370
371 if (param == NULL || param->psize == 0 || param->vsize == 0) {
372 return BAD_VALUE;
373 }
374
375 Mutex::Autolock _l(mCblk->lock);
376
377 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
378 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
379
380 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
381 return NO_MEMORY;
382 }
383 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
384 *p++ = size;
385 memcpy(p, param, sizeof(effect_param_t) + psize);
386 mCblk->clientIndex += size;
387
388 return NO_ERROR;
389}
390
391status_t AudioEffect::setParameterCommit()
392{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700393 if (mProbe) {
394 return INVALID_OPERATION;
395 }
Eric Laurent801a1182010-06-09 00:17:29 -0700396 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800397 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700398 }
399
400 Mutex::Autolock _l(mCblk->lock);
401 if (mCblk->clientIndex == 0) {
402 return INVALID_OPERATION;
403 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700404 std::vector<uint8_t> cmd;
405 std::vector<uint8_t> response;
406 status_t status;
407 Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT,
408 cmd,
409 0,
410 &response,
411 &status);
412 if (!bs.isOk()) {
413 status = bs.transactionError();
414 }
415 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700416}
417
418status_t AudioEffect::getParameter(effect_param_t *param)
419{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700420 if (mProbe) {
421 return INVALID_OPERATION;
422 }
Eric Laurent801a1182010-06-09 00:17:29 -0700423 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
John Grossmanaf7d8182012-01-11 12:23:42 -0800424 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700425 }
426
427 if (param == NULL || param->psize == 0 || param->vsize == 0) {
428 return BAD_VALUE;
429 }
430
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700431 ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data,
432 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700433
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700434 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
435 param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700436
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700437 status_t status;
438 std::vector<uint8_t> cmd;
439 std::vector<uint8_t> response;
440 appendToBuffer(param, sizeof(effect_param_t) + param->psize, &cmd);
441
442 Status bs = mIEffect->command(EFFECT_CMD_GET_PARAM, cmd, psize, &response, &status);
443 if (!bs.isOk()) {
444 status = bs.transactionError();
445 return status;
446 }
447 memcpy(param, response.data(), response.size());
448 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700449}
450
451
452// -------------------------------------------------------------------------
453
454void AudioEffect::binderDied()
455{
Steve Block5ff1dd52012-01-05 23:22:43 +0000456 ALOGW("IEffect died");
John Grossmanaf7d8182012-01-11 12:23:42 -0800457 mStatus = DEAD_OBJECT;
Glenn Kastena0d68332012-01-27 16:47:15 -0800458 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700459 status_t status = DEAD_OBJECT;
460 mCbf(EVENT_ERROR, mUserData, &status);
461 }
462 mIEffect.clear();
463}
464
465// -------------------------------------------------------------------------
466
467void AudioEffect::controlStatusChanged(bool controlGranted)
468{
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700469 ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf,
470 mUserData);
Eric Laurent801a1182010-06-09 00:17:29 -0700471 if (controlGranted) {
472 if (mStatus == ALREADY_EXISTS) {
473 mStatus = NO_ERROR;
474 }
475 } else {
476 if (mStatus == NO_ERROR) {
477 mStatus = ALREADY_EXISTS;
478 }
479 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800480 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700481 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
482 }
483}
484
485void AudioEffect::enableStatusChanged(bool enabled)
486{
Steve Block3856b092011-10-20 11:56:00 +0100487 ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
Eric Laurent801a1182010-06-09 00:17:29 -0700488 if (mStatus == ALREADY_EXISTS) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800489 mEnabled = enabled;
Glenn Kastena0d68332012-01-27 16:47:15 -0800490 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700491 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
492 }
493 }
494}
495
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700496void AudioEffect::commandExecuted(int32_t cmdCode,
497 const std::vector<uint8_t>& cmdData,
498 const std::vector<uint8_t>& replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700499{
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700500 if (cmdData.empty() || replyData.empty()) {
Eric Laurent801a1182010-06-09 00:17:29 -0700501 return;
502 }
503
Glenn Kastena0d68332012-01-27 16:47:15 -0800504 if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700505 std::vector<uint8_t> cmdDataCopy(cmdData);
506 effect_param_t* cmd = reinterpret_cast<effect_param_t *>(cmdDataCopy.data());
507 cmd->status = *reinterpret_cast<const int32_t *>(replyData.data());
Eric Laurent801a1182010-06-09 00:17:29 -0700508 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
509 }
510}
511
512// -------------------------------------------------------------------------
513
Eric Laurent801a1182010-06-09 00:17:29 -0700514status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
515{
516 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
517 if (af == 0) return PERMISSION_DENIED;
518 return af->queryNumberEffects(numEffects);
519}
520
Eric Laurentffe9c252010-06-23 17:38:20 -0700521status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700522{
523 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
524 if (af == 0) return PERMISSION_DENIED;
Eric Laurentffe9c252010-06-23 17:38:20 -0700525 return af->queryEffect(index, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700526}
527
Glenn Kasten5e92a782012-01-30 07:40:52 -0800528status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700529 const effect_uuid_t *type,
530 uint32_t preferredTypeFlag,
531 effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700532{
533 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
534 if (af == 0) return PERMISSION_DENIED;
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700535 return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700536}
537
Glenn Kastend848eb42016-03-08 13:42:11 -0800538status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700539 effect_descriptor_t *descriptors,
540 uint32_t *count)
541{
542 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
543 if (aps == 0) return PERMISSION_DENIED;
544 return aps->queryDefaultPreProcessing(audioSession, descriptors, count);
545}
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700546
547status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id)
548{
549 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
550 if (af == 0) return PERMISSION_DENIED;
551 *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
552 return NO_ERROR;
553}
554
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700555status_t AudioEffect::addSourceDefaultEffect(const char *typeStr,
556 const String16& opPackageName,
557 const char *uuidStr,
558 int32_t priority,
559 audio_source_t source,
560 audio_unique_id_t *id)
561{
562 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
563 if (aps == 0) return PERMISSION_DENIED;
564
565 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
566
567 // Convert type & uuid from string to effect_uuid_t.
568 effect_uuid_t type;
569 if (typeStr != NULL) {
570 status_t res = stringToGuid(typeStr, &type);
571 if (res != OK) return res;
572 } else {
573 type = *EFFECT_UUID_NULL;
574 }
575
576 effect_uuid_t uuid;
577 if (uuidStr != NULL) {
578 status_t res = stringToGuid(uuidStr, &uuid);
579 if (res != OK) return res;
580 } else {
581 uuid = *EFFECT_UUID_NULL;
582 }
583
584 return aps->addSourceDefaultEffect(&type, opPackageName, &uuid, priority, source, id);
585}
586
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700587status_t AudioEffect::addStreamDefaultEffect(const char *typeStr,
588 const String16& opPackageName,
589 const char *uuidStr,
590 int32_t priority,
591 audio_usage_t usage,
592 audio_unique_id_t *id)
593{
594 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
595 if (aps == 0) return PERMISSION_DENIED;
596
597 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
598
599 // Convert type & uuid from string to effect_uuid_t.
600 effect_uuid_t type;
601 if (typeStr != NULL) {
602 status_t res = stringToGuid(typeStr, &type);
603 if (res != OK) return res;
604 } else {
605 type = *EFFECT_UUID_NULL;
606 }
607
608 effect_uuid_t uuid;
609 if (uuidStr != NULL) {
610 status_t res = stringToGuid(uuidStr, &uuid);
611 if (res != OK) return res;
612 } else {
613 uuid = *EFFECT_UUID_NULL;
614 }
615
616 return aps->addStreamDefaultEffect(&type, opPackageName, &uuid, priority, usage, id);
617}
618
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700619status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id)
620{
621 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
622 if (aps == 0) return PERMISSION_DENIED;
623
624 return aps->removeSourceDefaultEffect(id);
625}
626
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700627status_t AudioEffect::removeStreamDefaultEffect(audio_unique_id_t id)
628{
629 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
630 if (aps == 0) return PERMISSION_DENIED;
631
632 return aps->removeStreamDefaultEffect(id);
633}
634
Eric Laurent801a1182010-06-09 00:17:29 -0700635// -------------------------------------------------------------------------
636
637status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
638{
639 if (str == NULL || guid == NULL) {
640 return BAD_VALUE;
641 }
642
643 int tmp[10];
644
645 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
646 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
647 return BAD_VALUE;
648 }
649 guid->timeLow = (uint32_t)tmp[0];
650 guid->timeMid = (uint16_t)tmp[1];
651 guid->timeHiAndVersion = (uint16_t)tmp[2];
652 guid->clockSeq = (uint16_t)tmp[3];
653 guid->node[0] = (uint8_t)tmp[4];
654 guid->node[1] = (uint8_t)tmp[5];
655 guid->node[2] = (uint8_t)tmp[6];
656 guid->node[3] = (uint8_t)tmp[7];
657 guid->node[4] = (uint8_t)tmp[8];
658 guid->node[5] = (uint8_t)tmp[9];
659
660 return NO_ERROR;
661}
662
663status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
664{
665 if (guid == NULL || str == NULL) {
666 return BAD_VALUE;
667 }
668
669 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
670 guid->timeLow,
671 guid->timeMid,
672 guid->timeHiAndVersion,
673 guid->clockSeq,
674 guid->node[0],
675 guid->node[1],
676 guid->node[2],
677 guid->node[3],
678 guid->node[4],
679 guid->node[5]);
680
681 return NO_ERROR;
682}
683
684
Glenn Kasten40bc9062015-03-20 09:09:33 -0700685} // namespace android