blob: b3862beecae22b42bdbaa61781bbd123d0f04489 [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 Laurent7c7f10b2011-06-17 21:29:58 -070051 audio_io_handle_t io
Eric Laurent801a1182010-06-09 00:17:29 -070052 )
Svet Ganovbe71aa22015-04-28 12:06:02 -070053 : mStatus(NO_INIT), mOpPackageName(opPackageName)
Eric Laurent801a1182010-06-09 00:17:29 -070054{
haobo101735293f51b542018-08-09 09:14:31 +080055 AutoMutex lock(mConstructLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -070056 mStatus = set(type, uuid, priority, cbf, user, sessionId, io);
Eric Laurent801a1182010-06-09 00:17:29 -070057}
58
59AudioEffect::AudioEffect(const char *typeStr,
Svet Ganovbe71aa22015-04-28 12:06:02 -070060 const String16& opPackageName,
Eric Laurent801a1182010-06-09 00:17:29 -070061 const char *uuidStr,
62 int32_t priority,
63 effect_callback_t cbf,
64 void* user,
Glenn Kastend848eb42016-03-08 13:42:11 -080065 audio_session_t sessionId,
Eric Laurent7c7f10b2011-06-17 21:29:58 -070066 audio_io_handle_t io
Eric Laurent801a1182010-06-09 00:17:29 -070067 )
Svet Ganovbe71aa22015-04-28 12:06:02 -070068 : mStatus(NO_INIT), mOpPackageName(opPackageName)
Eric Laurent801a1182010-06-09 00:17:29 -070069{
70 effect_uuid_t type;
71 effect_uuid_t *pType = NULL;
72 effect_uuid_t uuid;
73 effect_uuid_t *pUuid = NULL;
74
Steve Block3856b092011-10-20 11:56:00 +010075 ALOGV("Constructor string\n - type: %s\n - uuid: %s", typeStr, uuidStr);
Eric Laurent801a1182010-06-09 00:17:29 -070076
77 if (typeStr != NULL) {
78 if (stringToGuid(typeStr, &type) == NO_ERROR) {
79 pType = &type;
80 }
81 }
82
83 if (uuidStr != NULL) {
84 if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
85 pUuid = &uuid;
86 }
87 }
88
haobo101735293f51b542018-08-09 09:14:31 +080089 AutoMutex lock(mConstructLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -070090 mStatus = set(pType, pUuid, priority, cbf, user, sessionId, io);
Eric Laurent801a1182010-06-09 00:17:29 -070091}
92
93status_t AudioEffect::set(const effect_uuid_t *type,
94 const effect_uuid_t *uuid,
95 int32_t priority,
96 effect_callback_t cbf,
97 void* user,
Glenn Kastend848eb42016-03-08 13:42:11 -080098 audio_session_t sessionId,
Eric Laurent7c7f10b2011-06-17 21:29:58 -070099 audio_io_handle_t io)
Eric Laurent801a1182010-06-09 00:17:29 -0700100{
101 sp<IEffect> iEffect;
102 sp<IMemory> cblk;
103 int enabled;
104
Steve Block3856b092011-10-20 11:56:00 +0100105 ALOGV("set %p mUserData: %p uuid: %p timeLow %08x", this, user, type, type ? type->timeLow : 0);
Eric Laurent801a1182010-06-09 00:17:29 -0700106
107 if (mIEffect != 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000108 ALOGW("Effect already in use");
Eric Laurent801a1182010-06-09 00:17:29 -0700109 return INVALID_OPERATION;
110 }
111
112 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
113 if (audioFlinger == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000114 ALOGE("set(): Could not get audioflinger");
Eric Laurent801a1182010-06-09 00:17:29 -0700115 return NO_INIT;
116 }
117
118 if (type == NULL && uuid == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000119 ALOGW("Must specify at least type or uuid");
Eric Laurent801a1182010-06-09 00:17:29 -0700120 return BAD_VALUE;
121 }
122
123 mPriority = priority;
124 mCbf = cbf;
125 mUserData = user;
126 mSessionId = sessionId;
127
128 memset(&mDescriptor, 0, sizeof(effect_descriptor_t));
Glenn Kastena189a682012-02-20 12:16:30 -0800129 mDescriptor.type = *(type != NULL ? type : EFFECT_UUID_NULL);
130 mDescriptor.uuid = *(uuid != NULL ? uuid : EFFECT_UUID_NULL);
Eric Laurent801a1182010-06-09 00:17:29 -0700131
132 mIEffectClient = new EffectClient(this);
Eric Laurentb6436272016-12-07 19:24:50 -0800133 mClientPid = IPCThreadState::self()->getCallingPid();
Eric Laurent801a1182010-06-09 00:17:29 -0700134
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800135 iEffect = audioFlinger->createEffect((effect_descriptor_t *)&mDescriptor,
Eric Laurentb6436272016-12-07 19:24:50 -0800136 mIEffectClient, priority, io, mSessionId, mOpPackageName, mClientPid,
137 &mStatus, &mId, &enabled);
Eric Laurent801a1182010-06-09 00:17:29 -0700138
139 if (iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700140 char typeBuffer[64], uuidBuffer[64];
141 guidToString(type, typeBuffer, sizeof(typeBuffer));
142 guidToString(uuid, uuidBuffer, sizeof(uuidBuffer));
143 ALOGE("set(): AudioFlinger could not create effect %s / %s, status: %d",
144 typeBuffer, uuidBuffer, mStatus);
Eric Laurenteecd7652015-06-04 16:20:16 -0700145 if (iEffect == 0) {
146 mStatus = NO_INIT;
147 }
Eric Laurent801a1182010-06-09 00:17:29 -0700148 return mStatus;
149 }
150
151 mEnabled = (volatile int32_t)enabled;
152
Eric Laurent801a1182010-06-09 00:17:29 -0700153 cblk = iEffect->getCblk();
154 if (cblk == 0) {
155 mStatus = NO_INIT;
Steve Block29357bc2012-01-06 19:20:56 +0000156 ALOGE("Could not get control block");
Eric Laurent801a1182010-06-09 00:17:29 -0700157 return mStatus;
158 }
159
Eric Laurenteecd7652015-06-04 16:20:16 -0700160 mIEffect = iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -0700161 mCblkMemory = cblk;
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700162 // TODO: Using unsecurePointer() has some associated security pitfalls
163 // (see declaration for details).
164 // Either document why it is safe in this case or address the
165 // issue (e.g. by copying).
166 mCblk = static_cast<effect_param_cblk_t*>(cblk->unsecurePointer());
Eric Laurent801a1182010-06-09 00:17:29 -0700167 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
168 mCblk->buffer = (uint8_t *)mCblk + bufOffset;
169
Marco Nelissen06b46062014-11-14 07:58:25 -0800170 IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700171 ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId,
172 mStatus, mEnabled, mClientPid);
173
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800174 if (!audio_is_global_session(mSessionId)) {
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700175 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid);
176 }
Eric Laurent801a1182010-06-09 00:17:29 -0700177
178 return mStatus;
179}
180
181
182AudioEffect::~AudioEffect()
183{
Steve Block3856b092011-10-20 11:56:00 +0100184 ALOGV("Destructor %p", this);
Eric Laurent801a1182010-06-09 00:17:29 -0700185
186 if (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS) {
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800187 if (!audio_is_global_session(mSessionId)) {
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700188 AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
189 }
Eric Laurent801a1182010-06-09 00:17:29 -0700190 if (mIEffect != NULL) {
191 mIEffect->disconnect();
Marco Nelissen06b46062014-11-14 07:58:25 -0800192 IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient);
Eric Laurent801a1182010-06-09 00:17:29 -0700193 }
Eric Laurenteecd7652015-06-04 16:20:16 -0700194 mIEffect.clear();
195 mCblkMemory.clear();
196 mIEffectClient.clear();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700197 IPCThreadState::self()->flushCommands();
Eric Laurent801a1182010-06-09 00:17:29 -0700198 }
Eric Laurent801a1182010-06-09 00:17:29 -0700199}
200
201
202status_t AudioEffect::initCheck() const
203{
204 return mStatus;
205}
206
207// -------------------------------------------------------------------------
208
209effect_descriptor_t AudioEffect::descriptor() const
210{
211 return mDescriptor;
212}
213
Eric Laurentda7581b2010-07-02 08:12:41 -0700214bool AudioEffect::getEnabled() const
Eric Laurent801a1182010-06-09 00:17:29 -0700215{
216 return (mEnabled != 0);
217}
218
Eric Laurentda7581b2010-07-02 08:12:41 -0700219status_t AudioEffect::setEnabled(bool enabled)
Eric Laurent801a1182010-06-09 00:17:29 -0700220{
221 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800222 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700223 }
Eric Laurent801a1182010-06-09 00:17:29 -0700224
Eric Laurentf5aafb22010-11-18 08:40:16 -0800225 status_t status = NO_ERROR;
226
227 AutoMutex lock(mLock);
228 if (enabled != mEnabled) {
229 if (enabled) {
Steve Block3856b092011-10-20 11:56:00 +0100230 ALOGV("enable %p", this);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800231 status = mIEffect->enable();
232 } else {
Steve Block3856b092011-10-20 11:56:00 +0100233 ALOGV("disable %p", this);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800234 status = mIEffect->disable();
Eric Laurentda7581b2010-07-02 08:12:41 -0700235 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800236 if (status == NO_ERROR) {
237 mEnabled = enabled;
Eric Laurentda7581b2010-07-02 08:12:41 -0700238 }
Eric Laurent801a1182010-06-09 00:17:29 -0700239 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800240 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700241}
242
Eric Laurent25f43952010-07-28 05:40:18 -0700243status_t AudioEffect::command(uint32_t cmdCode,
244 uint32_t cmdSize,
245 void *cmdData,
246 uint32_t *replySize,
247 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700248{
249 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
Steve Block3856b092011-10-20 11:56:00 +0100250 ALOGV("command() bad status %d", mStatus);
John Grossmanaf7d8182012-01-11 12:23:42 -0800251 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700252 }
253
Eric Laurentf5aafb22010-11-18 08:40:16 -0800254 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
255 if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
256 return NO_ERROR;
257 }
258 if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) {
259 return BAD_VALUE;
260 }
261 mLock.lock();
Eric Laurent0fa449c2010-09-24 11:52:04 -0700262 }
263
Eric Laurent8569f0d2010-07-29 23:43:43 -0700264 status_t status = mIEffect->command(cmdCode, cmdSize, cmdData, replySize, replyData);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700265
266 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800267 if (status == NO_ERROR) {
268 status = *(status_t *)replyData;
Eric Laurent0fa449c2010-09-24 11:52:04 -0700269 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800270 if (status == NO_ERROR) {
271 mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700272 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800273 mLock.unlock();
Eric Laurent8569f0d2010-07-29 23:43:43 -0700274 }
275
Eric Laurent8569f0d2010-07-29 23:43:43 -0700276 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700277}
278
279
280status_t AudioEffect::setParameter(effect_param_t *param)
281{
282 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800283 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700284 }
285
286 if (param == NULL || param->psize == 0 || param->vsize == 0) {
287 return BAD_VALUE;
288 }
289
Eric Laurent25f43952010-07-28 05:40:18 -0700290 uint32_t size = sizeof(int);
291 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700292
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700293 ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data,
294 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700295
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700296 return mIEffect->command(EFFECT_CMD_SET_PARAM, sizeof (effect_param_t) + psize, param, &size,
297 &param->status);
Eric Laurent801a1182010-06-09 00:17:29 -0700298}
299
300status_t AudioEffect::setParameterDeferred(effect_param_t *param)
301{
302 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800303 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700304 }
305
306 if (param == NULL || param->psize == 0 || param->vsize == 0) {
307 return BAD_VALUE;
308 }
309
310 Mutex::Autolock _l(mCblk->lock);
311
312 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
313 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
314
315 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
316 return NO_MEMORY;
317 }
318 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
319 *p++ = size;
320 memcpy(p, param, sizeof(effect_param_t) + psize);
321 mCblk->clientIndex += size;
322
323 return NO_ERROR;
324}
325
326status_t AudioEffect::setParameterCommit()
327{
328 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800329 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700330 }
331
332 Mutex::Autolock _l(mCblk->lock);
333 if (mCblk->clientIndex == 0) {
334 return INVALID_OPERATION;
335 }
Eric Laurent25f43952010-07-28 05:40:18 -0700336 uint32_t size = 0;
Eric Laurent801a1182010-06-09 00:17:29 -0700337 return mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT, 0, NULL, &size, NULL);
338}
339
340status_t AudioEffect::getParameter(effect_param_t *param)
341{
342 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
John Grossmanaf7d8182012-01-11 12:23:42 -0800343 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700344 }
345
346 if (param == NULL || param->psize == 0 || param->vsize == 0) {
347 return BAD_VALUE;
348 }
349
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700350 ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data,
351 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700352
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700353 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
354 param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700355
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700356 return mIEffect->command(EFFECT_CMD_GET_PARAM, sizeof(effect_param_t) + param->psize, param,
357 &psize, param);
Eric Laurent801a1182010-06-09 00:17:29 -0700358}
359
360
361// -------------------------------------------------------------------------
362
363void AudioEffect::binderDied()
364{
Steve Block5ff1dd52012-01-05 23:22:43 +0000365 ALOGW("IEffect died");
John Grossmanaf7d8182012-01-11 12:23:42 -0800366 mStatus = DEAD_OBJECT;
Glenn Kastena0d68332012-01-27 16:47:15 -0800367 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700368 status_t status = DEAD_OBJECT;
369 mCbf(EVENT_ERROR, mUserData, &status);
370 }
371 mIEffect.clear();
372}
373
374// -------------------------------------------------------------------------
375
376void AudioEffect::controlStatusChanged(bool controlGranted)
377{
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700378 ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf,
379 mUserData);
Eric Laurent801a1182010-06-09 00:17:29 -0700380 if (controlGranted) {
381 if (mStatus == ALREADY_EXISTS) {
382 mStatus = NO_ERROR;
383 }
384 } else {
385 if (mStatus == NO_ERROR) {
386 mStatus = ALREADY_EXISTS;
387 }
388 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800389 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700390 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
391 }
392}
393
394void AudioEffect::enableStatusChanged(bool enabled)
395{
Steve Block3856b092011-10-20 11:56:00 +0100396 ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
Eric Laurent801a1182010-06-09 00:17:29 -0700397 if (mStatus == ALREADY_EXISTS) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800398 mEnabled = enabled;
Glenn Kastena0d68332012-01-27 16:47:15 -0800399 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700400 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
401 }
402 }
403}
404
Eric Laurent25f43952010-07-28 05:40:18 -0700405void AudioEffect::commandExecuted(uint32_t cmdCode,
Glenn Kasten0f11b512014-01-31 16:18:54 -0800406 uint32_t cmdSize __unused,
Eric Laurent25f43952010-07-28 05:40:18 -0700407 void *cmdData,
Glenn Kasten0f11b512014-01-31 16:18:54 -0800408 uint32_t replySize __unused,
Eric Laurent25f43952010-07-28 05:40:18 -0700409 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700410{
411 if (cmdData == NULL || replyData == NULL) {
412 return;
413 }
414
Glenn Kastena0d68332012-01-27 16:47:15 -0800415 if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
Eric Laurent801a1182010-06-09 00:17:29 -0700416 effect_param_t *cmd = (effect_param_t *)cmdData;
417 cmd->status = *(int32_t *)replyData;
418 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
419 }
420}
421
422// -------------------------------------------------------------------------
423
Eric Laurent801a1182010-06-09 00:17:29 -0700424status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
425{
426 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
427 if (af == 0) return PERMISSION_DENIED;
428 return af->queryNumberEffects(numEffects);
429}
430
Eric Laurentffe9c252010-06-23 17:38:20 -0700431status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700432{
433 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
434 if (af == 0) return PERMISSION_DENIED;
Eric Laurentffe9c252010-06-23 17:38:20 -0700435 return af->queryEffect(index, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700436}
437
Glenn Kasten5e92a782012-01-30 07:40:52 -0800438status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700439 const effect_uuid_t *type,
440 uint32_t preferredTypeFlag,
441 effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700442{
443 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
444 if (af == 0) return PERMISSION_DENIED;
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700445 return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700446}
447
Glenn Kastend848eb42016-03-08 13:42:11 -0800448status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700449 effect_descriptor_t *descriptors,
450 uint32_t *count)
451{
452 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
453 if (aps == 0) return PERMISSION_DENIED;
454 return aps->queryDefaultPreProcessing(audioSession, descriptors, count);
455}
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700456
457status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id)
458{
459 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
460 if (af == 0) return PERMISSION_DENIED;
461 *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
462 return NO_ERROR;
463}
464
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700465status_t AudioEffect::addSourceDefaultEffect(const char *typeStr,
466 const String16& opPackageName,
467 const char *uuidStr,
468 int32_t priority,
469 audio_source_t source,
470 audio_unique_id_t *id)
471{
472 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
473 if (aps == 0) return PERMISSION_DENIED;
474
475 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
476
477 // Convert type & uuid from string to effect_uuid_t.
478 effect_uuid_t type;
479 if (typeStr != NULL) {
480 status_t res = stringToGuid(typeStr, &type);
481 if (res != OK) return res;
482 } else {
483 type = *EFFECT_UUID_NULL;
484 }
485
486 effect_uuid_t uuid;
487 if (uuidStr != NULL) {
488 status_t res = stringToGuid(uuidStr, &uuid);
489 if (res != OK) return res;
490 } else {
491 uuid = *EFFECT_UUID_NULL;
492 }
493
494 return aps->addSourceDefaultEffect(&type, opPackageName, &uuid, priority, source, id);
495}
496
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700497status_t AudioEffect::addStreamDefaultEffect(const char *typeStr,
498 const String16& opPackageName,
499 const char *uuidStr,
500 int32_t priority,
501 audio_usage_t usage,
502 audio_unique_id_t *id)
503{
504 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
505 if (aps == 0) return PERMISSION_DENIED;
506
507 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
508
509 // Convert type & uuid from string to effect_uuid_t.
510 effect_uuid_t type;
511 if (typeStr != NULL) {
512 status_t res = stringToGuid(typeStr, &type);
513 if (res != OK) return res;
514 } else {
515 type = *EFFECT_UUID_NULL;
516 }
517
518 effect_uuid_t uuid;
519 if (uuidStr != NULL) {
520 status_t res = stringToGuid(uuidStr, &uuid);
521 if (res != OK) return res;
522 } else {
523 uuid = *EFFECT_UUID_NULL;
524 }
525
526 return aps->addStreamDefaultEffect(&type, opPackageName, &uuid, priority, usage, id);
527}
528
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700529status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id)
530{
531 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
532 if (aps == 0) return PERMISSION_DENIED;
533
534 return aps->removeSourceDefaultEffect(id);
535}
536
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700537status_t AudioEffect::removeStreamDefaultEffect(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->removeStreamDefaultEffect(id);
543}
544
Eric Laurent801a1182010-06-09 00:17:29 -0700545// -------------------------------------------------------------------------
546
547status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
548{
549 if (str == NULL || guid == NULL) {
550 return BAD_VALUE;
551 }
552
553 int tmp[10];
554
555 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
556 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
557 return BAD_VALUE;
558 }
559 guid->timeLow = (uint32_t)tmp[0];
560 guid->timeMid = (uint16_t)tmp[1];
561 guid->timeHiAndVersion = (uint16_t)tmp[2];
562 guid->clockSeq = (uint16_t)tmp[3];
563 guid->node[0] = (uint8_t)tmp[4];
564 guid->node[1] = (uint8_t)tmp[5];
565 guid->node[2] = (uint8_t)tmp[6];
566 guid->node[3] = (uint8_t)tmp[7];
567 guid->node[4] = (uint8_t)tmp[8];
568 guid->node[5] = (uint8_t)tmp[9];
569
570 return NO_ERROR;
571}
572
573status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
574{
575 if (guid == NULL || str == NULL) {
576 return BAD_VALUE;
577 }
578
579 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
580 guid->timeLow,
581 guid->timeMid,
582 guid->timeHiAndVersion,
583 guid->clockSeq,
584 guid->node[0],
585 guid->node[1],
586 guid->node[2],
587 guid->node[3],
588 guid->node[4],
589 guid->node[5]);
590
591 return NO_ERROR;
592}
593
594
Glenn Kasten40bc9062015-03-20 09:09:33 -0700595} // namespace android