blob: a1b141b12f708557d46ff71175ed52dcc5833637 [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
26#include <private/media/AudioEffectShared.h>
27#include <media/AudioEffect.h>
28
29#include <utils/Log.h>
Eric Laurent801a1182010-06-09 00:17:29 -070030#include <binder/IPCThreadState.h>
31
32
33
34namespace android {
35
36// ---------------------------------------------------------------------------
37
Svet Ganovbe71aa22015-04-28 12:06:02 -070038AudioEffect::AudioEffect(const String16& opPackageName)
39 : mStatus(NO_INIT), mOpPackageName(opPackageName)
Eric Laurent801a1182010-06-09 00:17:29 -070040{
41}
42
43
44AudioEffect::AudioEffect(const effect_uuid_t *type,
Svet Ganovbe71aa22015-04-28 12:06:02 -070045 const String16& opPackageName,
Eric Laurent801a1182010-06-09 00:17:29 -070046 const effect_uuid_t *uuid,
47 int32_t priority,
48 effect_callback_t cbf,
49 void* user,
Glenn Kastend848eb42016-03-08 13:42:11 -080050 audio_session_t sessionId,
Eric Laurent94876032019-11-13 12:45:28 -080051 audio_io_handle_t io,
52 const AudioDeviceTypeAddr& device
Eric Laurent801a1182010-06-09 00:17:29 -070053 )
Svet Ganovbe71aa22015-04-28 12:06:02 -070054 : mStatus(NO_INIT), mOpPackageName(opPackageName)
Eric Laurent801a1182010-06-09 00:17:29 -070055{
haobo101735293f51b542018-08-09 09:14:31 +080056 AutoMutex lock(mConstructLock);
Eric Laurent94876032019-11-13 12:45:28 -080057 mStatus = set(type, uuid, priority, cbf, user, sessionId, io, device);
Eric Laurent801a1182010-06-09 00:17:29 -070058}
59
60AudioEffect::AudioEffect(const char *typeStr,
Svet Ganovbe71aa22015-04-28 12:06:02 -070061 const String16& opPackageName,
Eric Laurent801a1182010-06-09 00:17:29 -070062 const char *uuidStr,
63 int32_t priority,
64 effect_callback_t cbf,
65 void* user,
Glenn Kastend848eb42016-03-08 13:42:11 -080066 audio_session_t sessionId,
Eric Laurent94876032019-11-13 12:45:28 -080067 audio_io_handle_t io,
68 const AudioDeviceTypeAddr& device
Eric Laurent801a1182010-06-09 00:17:29 -070069 )
Svet Ganovbe71aa22015-04-28 12:06:02 -070070 : mStatus(NO_INIT), mOpPackageName(opPackageName)
Eric Laurent801a1182010-06-09 00:17:29 -070071{
72 effect_uuid_t type;
73 effect_uuid_t *pType = NULL;
74 effect_uuid_t uuid;
75 effect_uuid_t *pUuid = NULL;
76
Steve Block3856b092011-10-20 11:56:00 +010077 ALOGV("Constructor string\n - type: %s\n - uuid: %s", typeStr, uuidStr);
Eric Laurent801a1182010-06-09 00:17:29 -070078
79 if (typeStr != NULL) {
80 if (stringToGuid(typeStr, &type) == NO_ERROR) {
81 pType = &type;
82 }
83 }
84
85 if (uuidStr != NULL) {
86 if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
87 pUuid = &uuid;
88 }
89 }
90
haobo101735293f51b542018-08-09 09:14:31 +080091 AutoMutex lock(mConstructLock);
Eric Laurent94876032019-11-13 12:45:28 -080092 mStatus = set(pType, pUuid, priority, cbf, user, sessionId, io, device);
Eric Laurent801a1182010-06-09 00:17:29 -070093}
94
95status_t AudioEffect::set(const effect_uuid_t *type,
96 const effect_uuid_t *uuid,
97 int32_t priority,
98 effect_callback_t cbf,
99 void* user,
Glenn Kastend848eb42016-03-08 13:42:11 -0800100 audio_session_t sessionId,
Eric Laurent94876032019-11-13 12:45:28 -0800101 audio_io_handle_t io,
102 const AudioDeviceTypeAddr& device)
Eric Laurent801a1182010-06-09 00:17:29 -0700103{
104 sp<IEffect> iEffect;
105 sp<IMemory> cblk;
106 int enabled;
107
Steve Block3856b092011-10-20 11:56:00 +0100108 ALOGV("set %p mUserData: %p uuid: %p timeLow %08x", this, user, type, type ? type->timeLow : 0);
Eric Laurent801a1182010-06-09 00:17:29 -0700109
110 if (mIEffect != 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000111 ALOGW("Effect already in use");
Eric Laurent801a1182010-06-09 00:17:29 -0700112 return INVALID_OPERATION;
113 }
114
Eric Laurent94876032019-11-13 12:45:28 -0800115 if (sessionId == AUDIO_SESSION_DEVICE && io != AUDIO_IO_HANDLE_NONE) {
116 ALOGW("IO handle should not be specified for device effect");
117 return BAD_VALUE;
118 }
Eric Laurent801a1182010-06-09 00:17:29 -0700119 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
120 if (audioFlinger == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000121 ALOGE("set(): Could not get audioflinger");
Eric Laurent801a1182010-06-09 00:17:29 -0700122 return NO_INIT;
123 }
124
125 if (type == NULL && uuid == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000126 ALOGW("Must specify at least type or uuid");
Eric Laurent801a1182010-06-09 00:17:29 -0700127 return BAD_VALUE;
128 }
129
130 mPriority = priority;
131 mCbf = cbf;
132 mUserData = user;
133 mSessionId = sessionId;
134
135 memset(&mDescriptor, 0, sizeof(effect_descriptor_t));
Glenn Kastena189a682012-02-20 12:16:30 -0800136 mDescriptor.type = *(type != NULL ? type : EFFECT_UUID_NULL);
137 mDescriptor.uuid = *(uuid != NULL ? uuid : EFFECT_UUID_NULL);
Eric Laurent801a1182010-06-09 00:17:29 -0700138
139 mIEffectClient = new EffectClient(this);
Eric Laurentb6436272016-12-07 19:24:50 -0800140 mClientPid = IPCThreadState::self()->getCallingPid();
Andy Hung8b0bfd92019-12-23 13:11:11 -0800141 mClientUid = IPCThreadState::self()->getCallingUid();
Eric Laurent801a1182010-06-09 00:17:29 -0700142
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800143 iEffect = audioFlinger->createEffect((effect_descriptor_t *)&mDescriptor,
Eric Laurent94876032019-11-13 12:45:28 -0800144 mIEffectClient, priority, io, mSessionId, device, mOpPackageName, mClientPid,
Eric Laurentb6436272016-12-07 19:24:50 -0800145 &mStatus, &mId, &enabled);
Eric Laurent801a1182010-06-09 00:17:29 -0700146
147 if (iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700148 char typeBuffer[64], uuidBuffer[64];
149 guidToString(type, typeBuffer, sizeof(typeBuffer));
150 guidToString(uuid, uuidBuffer, sizeof(uuidBuffer));
151 ALOGE("set(): AudioFlinger could not create effect %s / %s, status: %d",
152 typeBuffer, uuidBuffer, mStatus);
Eric Laurenteecd7652015-06-04 16:20:16 -0700153 if (iEffect == 0) {
154 mStatus = NO_INIT;
155 }
Eric Laurent801a1182010-06-09 00:17:29 -0700156 return mStatus;
157 }
158
159 mEnabled = (volatile int32_t)enabled;
160
Eric Laurent801a1182010-06-09 00:17:29 -0700161 cblk = iEffect->getCblk();
162 if (cblk == 0) {
163 mStatus = NO_INIT;
Steve Block29357bc2012-01-06 19:20:56 +0000164 ALOGE("Could not get control block");
Eric Laurent801a1182010-06-09 00:17:29 -0700165 return mStatus;
166 }
167
Eric Laurenteecd7652015-06-04 16:20:16 -0700168 mIEffect = iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -0700169 mCblkMemory = cblk;
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700170 // TODO: Using unsecurePointer() has some associated security pitfalls
171 // (see declaration for details).
172 // Either document why it is safe in this case or address the
173 // issue (e.g. by copying).
174 mCblk = static_cast<effect_param_cblk_t*>(cblk->unsecurePointer());
Eric Laurent801a1182010-06-09 00:17:29 -0700175 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
176 mCblk->buffer = (uint8_t *)mCblk + bufOffset;
177
Marco Nelissen06b46062014-11-14 07:58:25 -0800178 IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700179 ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId,
180 mStatus, mEnabled, mClientPid);
181
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800182 if (!audio_is_global_session(mSessionId)) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800183 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid, mClientUid);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700184 }
Eric Laurent801a1182010-06-09 00:17:29 -0700185
186 return mStatus;
187}
188
189
190AudioEffect::~AudioEffect()
191{
Steve Block3856b092011-10-20 11:56:00 +0100192 ALOGV("Destructor %p", this);
Eric Laurent801a1182010-06-09 00:17:29 -0700193
194 if (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS) {
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800195 if (!audio_is_global_session(mSessionId)) {
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700196 AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
197 }
Eric Laurent801a1182010-06-09 00:17:29 -0700198 if (mIEffect != NULL) {
199 mIEffect->disconnect();
Marco Nelissen06b46062014-11-14 07:58:25 -0800200 IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient);
Eric Laurent801a1182010-06-09 00:17:29 -0700201 }
Eric Laurenteecd7652015-06-04 16:20:16 -0700202 mIEffect.clear();
203 mCblkMemory.clear();
204 mIEffectClient.clear();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700205 IPCThreadState::self()->flushCommands();
Eric Laurent801a1182010-06-09 00:17:29 -0700206 }
Eric Laurent801a1182010-06-09 00:17:29 -0700207}
208
209
210status_t AudioEffect::initCheck() const
211{
212 return mStatus;
213}
214
215// -------------------------------------------------------------------------
216
217effect_descriptor_t AudioEffect::descriptor() const
218{
219 return mDescriptor;
220}
221
Eric Laurentda7581b2010-07-02 08:12:41 -0700222bool AudioEffect::getEnabled() const
Eric Laurent801a1182010-06-09 00:17:29 -0700223{
224 return (mEnabled != 0);
225}
226
Eric Laurentda7581b2010-07-02 08:12:41 -0700227status_t AudioEffect::setEnabled(bool enabled)
Eric Laurent801a1182010-06-09 00:17:29 -0700228{
229 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800230 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700231 }
Eric Laurent801a1182010-06-09 00:17:29 -0700232
Eric Laurentf5aafb22010-11-18 08:40:16 -0800233 status_t status = NO_ERROR;
234
235 AutoMutex lock(mLock);
236 if (enabled != mEnabled) {
237 if (enabled) {
Steve Block3856b092011-10-20 11:56:00 +0100238 ALOGV("enable %p", this);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800239 status = mIEffect->enable();
240 } else {
Steve Block3856b092011-10-20 11:56:00 +0100241 ALOGV("disable %p", this);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800242 status = mIEffect->disable();
Eric Laurentda7581b2010-07-02 08:12:41 -0700243 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800244 if (status == NO_ERROR) {
245 mEnabled = enabled;
Eric Laurentda7581b2010-07-02 08:12:41 -0700246 }
Eric Laurent801a1182010-06-09 00:17:29 -0700247 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800248 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700249}
250
Eric Laurent25f43952010-07-28 05:40:18 -0700251status_t AudioEffect::command(uint32_t cmdCode,
252 uint32_t cmdSize,
253 void *cmdData,
254 uint32_t *replySize,
255 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700256{
257 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
Steve Block3856b092011-10-20 11:56:00 +0100258 ALOGV("command() bad status %d", mStatus);
John Grossmanaf7d8182012-01-11 12:23:42 -0800259 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700260 }
261
Eric Laurentf5aafb22010-11-18 08:40:16 -0800262 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
263 if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
264 return NO_ERROR;
265 }
266 if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) {
267 return BAD_VALUE;
268 }
269 mLock.lock();
Eric Laurent0fa449c2010-09-24 11:52:04 -0700270 }
271
Eric Laurent8569f0d2010-07-29 23:43:43 -0700272 status_t status = mIEffect->command(cmdCode, cmdSize, cmdData, replySize, replyData);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700273
274 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800275 if (status == NO_ERROR) {
276 status = *(status_t *)replyData;
Eric Laurent0fa449c2010-09-24 11:52:04 -0700277 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800278 if (status == NO_ERROR) {
279 mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700280 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800281 mLock.unlock();
Eric Laurent8569f0d2010-07-29 23:43:43 -0700282 }
283
Eric Laurent8569f0d2010-07-29 23:43:43 -0700284 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700285}
286
287
288status_t AudioEffect::setParameter(effect_param_t *param)
289{
290 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800291 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700292 }
293
294 if (param == NULL || param->psize == 0 || param->vsize == 0) {
295 return BAD_VALUE;
296 }
297
Eric Laurent25f43952010-07-28 05:40:18 -0700298 uint32_t size = sizeof(int);
299 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700300
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700301 ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data,
302 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700303
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700304 return mIEffect->command(EFFECT_CMD_SET_PARAM, sizeof (effect_param_t) + psize, param, &size,
305 &param->status);
Eric Laurent801a1182010-06-09 00:17:29 -0700306}
307
308status_t AudioEffect::setParameterDeferred(effect_param_t *param)
309{
310 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800311 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700312 }
313
314 if (param == NULL || param->psize == 0 || param->vsize == 0) {
315 return BAD_VALUE;
316 }
317
318 Mutex::Autolock _l(mCblk->lock);
319
320 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
321 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
322
323 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
324 return NO_MEMORY;
325 }
326 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
327 *p++ = size;
328 memcpy(p, param, sizeof(effect_param_t) + psize);
329 mCblk->clientIndex += size;
330
331 return NO_ERROR;
332}
333
334status_t AudioEffect::setParameterCommit()
335{
336 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800337 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700338 }
339
340 Mutex::Autolock _l(mCblk->lock);
341 if (mCblk->clientIndex == 0) {
342 return INVALID_OPERATION;
343 }
Eric Laurent25f43952010-07-28 05:40:18 -0700344 uint32_t size = 0;
Eric Laurent801a1182010-06-09 00:17:29 -0700345 return mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT, 0, NULL, &size, NULL);
346}
347
348status_t AudioEffect::getParameter(effect_param_t *param)
349{
350 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
John Grossmanaf7d8182012-01-11 12:23:42 -0800351 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700352 }
353
354 if (param == NULL || param->psize == 0 || param->vsize == 0) {
355 return BAD_VALUE;
356 }
357
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700358 ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data,
359 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700360
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700361 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
362 param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700363
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700364 return mIEffect->command(EFFECT_CMD_GET_PARAM, sizeof(effect_param_t) + param->psize, param,
365 &psize, param);
Eric Laurent801a1182010-06-09 00:17:29 -0700366}
367
368
369// -------------------------------------------------------------------------
370
371void AudioEffect::binderDied()
372{
Steve Block5ff1dd52012-01-05 23:22:43 +0000373 ALOGW("IEffect died");
John Grossmanaf7d8182012-01-11 12:23:42 -0800374 mStatus = DEAD_OBJECT;
Glenn Kastena0d68332012-01-27 16:47:15 -0800375 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700376 status_t status = DEAD_OBJECT;
377 mCbf(EVENT_ERROR, mUserData, &status);
378 }
379 mIEffect.clear();
380}
381
382// -------------------------------------------------------------------------
383
384void AudioEffect::controlStatusChanged(bool controlGranted)
385{
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700386 ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf,
387 mUserData);
Eric Laurent801a1182010-06-09 00:17:29 -0700388 if (controlGranted) {
389 if (mStatus == ALREADY_EXISTS) {
390 mStatus = NO_ERROR;
391 }
392 } else {
393 if (mStatus == NO_ERROR) {
394 mStatus = ALREADY_EXISTS;
395 }
396 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800397 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700398 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
399 }
400}
401
402void AudioEffect::enableStatusChanged(bool enabled)
403{
Steve Block3856b092011-10-20 11:56:00 +0100404 ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
Eric Laurent801a1182010-06-09 00:17:29 -0700405 if (mStatus == ALREADY_EXISTS) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800406 mEnabled = enabled;
Glenn Kastena0d68332012-01-27 16:47:15 -0800407 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700408 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
409 }
410 }
411}
412
Eric Laurent25f43952010-07-28 05:40:18 -0700413void AudioEffect::commandExecuted(uint32_t cmdCode,
Glenn Kasten0f11b512014-01-31 16:18:54 -0800414 uint32_t cmdSize __unused,
Eric Laurent25f43952010-07-28 05:40:18 -0700415 void *cmdData,
Glenn Kasten0f11b512014-01-31 16:18:54 -0800416 uint32_t replySize __unused,
Eric Laurent25f43952010-07-28 05:40:18 -0700417 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700418{
419 if (cmdData == NULL || replyData == NULL) {
420 return;
421 }
422
Glenn Kastena0d68332012-01-27 16:47:15 -0800423 if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
Eric Laurent801a1182010-06-09 00:17:29 -0700424 effect_param_t *cmd = (effect_param_t *)cmdData;
425 cmd->status = *(int32_t *)replyData;
426 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
427 }
428}
429
430// -------------------------------------------------------------------------
431
Eric Laurent801a1182010-06-09 00:17:29 -0700432status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
433{
434 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
435 if (af == 0) return PERMISSION_DENIED;
436 return af->queryNumberEffects(numEffects);
437}
438
Eric Laurentffe9c252010-06-23 17:38:20 -0700439status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700440{
441 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
442 if (af == 0) return PERMISSION_DENIED;
Eric Laurentffe9c252010-06-23 17:38:20 -0700443 return af->queryEffect(index, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700444}
445
Glenn Kasten5e92a782012-01-30 07:40:52 -0800446status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700447 const effect_uuid_t *type,
448 uint32_t preferredTypeFlag,
449 effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700450{
451 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
452 if (af == 0) return PERMISSION_DENIED;
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700453 return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700454}
455
Glenn Kastend848eb42016-03-08 13:42:11 -0800456status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700457 effect_descriptor_t *descriptors,
458 uint32_t *count)
459{
460 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
461 if (aps == 0) return PERMISSION_DENIED;
462 return aps->queryDefaultPreProcessing(audioSession, descriptors, count);
463}
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700464
465status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id)
466{
467 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
468 if (af == 0) return PERMISSION_DENIED;
469 *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
470 return NO_ERROR;
471}
472
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700473status_t AudioEffect::addSourceDefaultEffect(const char *typeStr,
474 const String16& opPackageName,
475 const char *uuidStr,
476 int32_t priority,
477 audio_source_t source,
478 audio_unique_id_t *id)
479{
480 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
481 if (aps == 0) return PERMISSION_DENIED;
482
483 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
484
485 // Convert type & uuid from string to effect_uuid_t.
486 effect_uuid_t type;
487 if (typeStr != NULL) {
488 status_t res = stringToGuid(typeStr, &type);
489 if (res != OK) return res;
490 } else {
491 type = *EFFECT_UUID_NULL;
492 }
493
494 effect_uuid_t uuid;
495 if (uuidStr != NULL) {
496 status_t res = stringToGuid(uuidStr, &uuid);
497 if (res != OK) return res;
498 } else {
499 uuid = *EFFECT_UUID_NULL;
500 }
501
502 return aps->addSourceDefaultEffect(&type, opPackageName, &uuid, priority, source, id);
503}
504
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700505status_t AudioEffect::addStreamDefaultEffect(const char *typeStr,
506 const String16& opPackageName,
507 const char *uuidStr,
508 int32_t priority,
509 audio_usage_t usage,
510 audio_unique_id_t *id)
511{
512 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
513 if (aps == 0) return PERMISSION_DENIED;
514
515 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
516
517 // Convert type & uuid from string to effect_uuid_t.
518 effect_uuid_t type;
519 if (typeStr != NULL) {
520 status_t res = stringToGuid(typeStr, &type);
521 if (res != OK) return res;
522 } else {
523 type = *EFFECT_UUID_NULL;
524 }
525
526 effect_uuid_t uuid;
527 if (uuidStr != NULL) {
528 status_t res = stringToGuid(uuidStr, &uuid);
529 if (res != OK) return res;
530 } else {
531 uuid = *EFFECT_UUID_NULL;
532 }
533
534 return aps->addStreamDefaultEffect(&type, opPackageName, &uuid, priority, usage, id);
535}
536
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700537status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id)
538{
539 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
540 if (aps == 0) return PERMISSION_DENIED;
541
542 return aps->removeSourceDefaultEffect(id);
543}
544
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700545status_t AudioEffect::removeStreamDefaultEffect(audio_unique_id_t id)
546{
547 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
548 if (aps == 0) return PERMISSION_DENIED;
549
550 return aps->removeStreamDefaultEffect(id);
551}
552
Eric Laurent801a1182010-06-09 00:17:29 -0700553// -------------------------------------------------------------------------
554
555status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
556{
557 if (str == NULL || guid == NULL) {
558 return BAD_VALUE;
559 }
560
561 int tmp[10];
562
563 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
564 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
565 return BAD_VALUE;
566 }
567 guid->timeLow = (uint32_t)tmp[0];
568 guid->timeMid = (uint16_t)tmp[1];
569 guid->timeHiAndVersion = (uint16_t)tmp[2];
570 guid->clockSeq = (uint16_t)tmp[3];
571 guid->node[0] = (uint8_t)tmp[4];
572 guid->node[1] = (uint8_t)tmp[5];
573 guid->node[2] = (uint8_t)tmp[6];
574 guid->node[3] = (uint8_t)tmp[7];
575 guid->node[4] = (uint8_t)tmp[8];
576 guid->node[5] = (uint8_t)tmp[9];
577
578 return NO_ERROR;
579}
580
581status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
582{
583 if (guid == NULL || str == NULL) {
584 return BAD_VALUE;
585 }
586
587 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
588 guid->timeLow,
589 guid->timeMid,
590 guid->timeHiAndVersion,
591 guid->clockSeq,
592 guid->node[0],
593 guid->node[1],
594 guid->node[2],
595 guid->node[3],
596 guid->node[4],
597 guid->node[5]);
598
599 return NO_ERROR;
600}
601
602
Glenn Kasten40bc9062015-03-20 09:09:33 -0700603} // namespace android