blob: 61f09ab25c52f492f082abc0ede3149aa51c8bc4 [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)
Mikhail Naganov69d41cc2020-07-31 17:36:08 -070039 : mOpPackageName(opPackageName)
Eric Laurent801a1182010-06-09 00:17:29 -070040{
41}
42
Eric Laurent801a1182010-06-09 00:17:29 -070043status_t AudioEffect::set(const effect_uuid_t *type,
44 const effect_uuid_t *uuid,
45 int32_t priority,
46 effect_callback_t cbf,
47 void* user,
Glenn Kastend848eb42016-03-08 13:42:11 -080048 audio_session_t sessionId,
Eric Laurentfefebb52019-11-13 12:45:28 -080049 audio_io_handle_t io,
50 const AudioDeviceTypeAddr& device)
Eric Laurent801a1182010-06-09 00:17:29 -070051{
52 sp<IEffect> iEffect;
53 sp<IMemory> cblk;
54 int enabled;
55
Steve Block3856b092011-10-20 11:56:00 +010056 ALOGV("set %p mUserData: %p uuid: %p timeLow %08x", this, user, type, type ? type->timeLow : 0);
Eric Laurent801a1182010-06-09 00:17:29 -070057
58 if (mIEffect != 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +000059 ALOGW("Effect already in use");
Eric Laurent801a1182010-06-09 00:17:29 -070060 return INVALID_OPERATION;
61 }
62
Eric Laurentfefebb52019-11-13 12:45:28 -080063 if (sessionId == AUDIO_SESSION_DEVICE && io != AUDIO_IO_HANDLE_NONE) {
64 ALOGW("IO handle should not be specified for device effect");
65 return BAD_VALUE;
66 }
Eric Laurent801a1182010-06-09 00:17:29 -070067 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
68 if (audioFlinger == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000069 ALOGE("set(): Could not get audioflinger");
Eric Laurent801a1182010-06-09 00:17:29 -070070 return NO_INIT;
71 }
72
73 if (type == NULL && uuid == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +000074 ALOGW("Must specify at least type or uuid");
Eric Laurent801a1182010-06-09 00:17:29 -070075 return BAD_VALUE;
76 }
77
78 mPriority = priority;
79 mCbf = cbf;
80 mUserData = user;
81 mSessionId = sessionId;
82
83 memset(&mDescriptor, 0, sizeof(effect_descriptor_t));
Glenn Kastena189a682012-02-20 12:16:30 -080084 mDescriptor.type = *(type != NULL ? type : EFFECT_UUID_NULL);
85 mDescriptor.uuid = *(uuid != NULL ? uuid : EFFECT_UUID_NULL);
Eric Laurent801a1182010-06-09 00:17:29 -070086
87 mIEffectClient = new EffectClient(this);
Eric Laurentb6436272016-12-07 19:24:50 -080088 mClientPid = IPCThreadState::self()->getCallingPid();
Eric Laurent801a1182010-06-09 00:17:29 -070089
Glenn Kasten8d6cc842012-02-03 11:06:53 -080090 iEffect = audioFlinger->createEffect((effect_descriptor_t *)&mDescriptor,
Eric Laurentfefebb52019-11-13 12:45:28 -080091 mIEffectClient, priority, io, mSessionId, device, mOpPackageName, mClientPid,
Eric Laurentb6436272016-12-07 19:24:50 -080092 &mStatus, &mId, &enabled);
Eric Laurent801a1182010-06-09 00:17:29 -070093
94 if (iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
Mikhail Naganov98389e32020-03-23 11:44:33 -070095 char typeBuffer[64] = {}, uuidBuffer[64] = {};
Mikhail Naganov424c4f52017-07-19 17:54:29 -070096 guidToString(type, typeBuffer, sizeof(typeBuffer));
97 guidToString(uuid, uuidBuffer, sizeof(uuidBuffer));
98 ALOGE("set(): AudioFlinger could not create effect %s / %s, status: %d",
Mikhail Naganov98389e32020-03-23 11:44:33 -070099 type != nullptr ? typeBuffer : "NULL",
100 uuid != nullptr ? uuidBuffer : "NULL",
101 mStatus);
Eric Laurenteecd7652015-06-04 16:20:16 -0700102 if (iEffect == 0) {
103 mStatus = NO_INIT;
104 }
Eric Laurent801a1182010-06-09 00:17:29 -0700105 return mStatus;
106 }
107
108 mEnabled = (volatile int32_t)enabled;
109
Eric Laurent801a1182010-06-09 00:17:29 -0700110 cblk = iEffect->getCblk();
111 if (cblk == 0) {
112 mStatus = NO_INIT;
Steve Block29357bc2012-01-06 19:20:56 +0000113 ALOGE("Could not get control block");
Eric Laurent801a1182010-06-09 00:17:29 -0700114 return mStatus;
115 }
116
Eric Laurenteecd7652015-06-04 16:20:16 -0700117 mIEffect = iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -0700118 mCblkMemory = cblk;
119 mCblk = static_cast<effect_param_cblk_t*>(cblk->pointer());
120 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
121 mCblk->buffer = (uint8_t *)mCblk + bufOffset;
122
Marco Nelissen06b46062014-11-14 07:58:25 -0800123 IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700124 ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId,
125 mStatus, mEnabled, mClientPid);
126
Eric Laurenta20c4e92019-11-12 15:55:51 -0800127 if (!audio_is_global_session(mSessionId)) {
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700128 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid);
129 }
Eric Laurent801a1182010-06-09 00:17:29 -0700130
131 return mStatus;
132}
133
Mikhail Naganov69d41cc2020-07-31 17:36:08 -0700134status_t AudioEffect::set(const char *typeStr,
135 const char *uuidStr,
136 int32_t priority,
137 effect_callback_t cbf,
138 void* user,
139 audio_session_t sessionId,
140 audio_io_handle_t io,
141 const AudioDeviceTypeAddr& device)
142{
143 effect_uuid_t type;
144 effect_uuid_t *pType = nullptr;
145 effect_uuid_t uuid;
146 effect_uuid_t *pUuid = nullptr;
147
148 ALOGV("AudioEffect::set string\n - type: %s\n - uuid: %s",
149 typeStr ? typeStr : "nullptr", uuidStr ? uuidStr : "nullptr");
150
151 if (stringToGuid(typeStr, &type) == NO_ERROR) {
152 pType = &type;
153 }
154 if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
155 pUuid = &uuid;
156 }
157
158 return set(pType, pUuid, priority, cbf, user, sessionId, io, device);
159}
160
Eric Laurent801a1182010-06-09 00:17:29 -0700161
162AudioEffect::~AudioEffect()
163{
Steve Block3856b092011-10-20 11:56:00 +0100164 ALOGV("Destructor %p", this);
Eric Laurent801a1182010-06-09 00:17:29 -0700165
166 if (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS) {
Eric Laurenta20c4e92019-11-12 15:55:51 -0800167 if (!audio_is_global_session(mSessionId)) {
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700168 AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
169 }
Eric Laurent801a1182010-06-09 00:17:29 -0700170 if (mIEffect != NULL) {
171 mIEffect->disconnect();
Marco Nelissen06b46062014-11-14 07:58:25 -0800172 IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient);
Eric Laurent801a1182010-06-09 00:17:29 -0700173 }
Eric Laurenteecd7652015-06-04 16:20:16 -0700174 mIEffect.clear();
175 mCblkMemory.clear();
176 mIEffectClient.clear();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700177 IPCThreadState::self()->flushCommands();
Eric Laurent801a1182010-06-09 00:17:29 -0700178 }
Eric Laurent801a1182010-06-09 00:17:29 -0700179}
180
181
182status_t AudioEffect::initCheck() const
183{
184 return mStatus;
185}
186
187// -------------------------------------------------------------------------
188
189effect_descriptor_t AudioEffect::descriptor() const
190{
191 return mDescriptor;
192}
193
Eric Laurentda7581b2010-07-02 08:12:41 -0700194bool AudioEffect::getEnabled() const
Eric Laurent801a1182010-06-09 00:17:29 -0700195{
196 return (mEnabled != 0);
197}
198
Eric Laurentda7581b2010-07-02 08:12:41 -0700199status_t AudioEffect::setEnabled(bool enabled)
Eric Laurent801a1182010-06-09 00:17:29 -0700200{
201 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800202 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700203 }
Eric Laurent801a1182010-06-09 00:17:29 -0700204
Eric Laurentf5aafb22010-11-18 08:40:16 -0800205 status_t status = NO_ERROR;
206
207 AutoMutex lock(mLock);
208 if (enabled != mEnabled) {
209 if (enabled) {
Steve Block3856b092011-10-20 11:56:00 +0100210 ALOGV("enable %p", this);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800211 status = mIEffect->enable();
212 } else {
Steve Block3856b092011-10-20 11:56:00 +0100213 ALOGV("disable %p", this);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800214 status = mIEffect->disable();
Eric Laurentda7581b2010-07-02 08:12:41 -0700215 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800216 if (status == NO_ERROR) {
217 mEnabled = enabled;
Eric Laurentda7581b2010-07-02 08:12:41 -0700218 }
Eric Laurent801a1182010-06-09 00:17:29 -0700219 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800220 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700221}
222
Eric Laurent25f43952010-07-28 05:40:18 -0700223status_t AudioEffect::command(uint32_t cmdCode,
224 uint32_t cmdSize,
225 void *cmdData,
226 uint32_t *replySize,
227 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700228{
229 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
Steve Block3856b092011-10-20 11:56:00 +0100230 ALOGV("command() bad status %d", mStatus);
John Grossmanaf7d8182012-01-11 12:23:42 -0800231 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700232 }
233
Eric Laurentf5aafb22010-11-18 08:40:16 -0800234 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
235 if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
236 return NO_ERROR;
237 }
238 if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) {
239 return BAD_VALUE;
240 }
241 mLock.lock();
Eric Laurent0fa449c2010-09-24 11:52:04 -0700242 }
243
Eric Laurent8569f0d2010-07-29 23:43:43 -0700244 status_t status = mIEffect->command(cmdCode, cmdSize, cmdData, replySize, replyData);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700245
246 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800247 if (status == NO_ERROR) {
248 status = *(status_t *)replyData;
Eric Laurent0fa449c2010-09-24 11:52:04 -0700249 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800250 if (status == NO_ERROR) {
251 mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700252 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800253 mLock.unlock();
Eric Laurent8569f0d2010-07-29 23:43:43 -0700254 }
255
Eric Laurent8569f0d2010-07-29 23:43:43 -0700256 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700257}
258
259
260status_t AudioEffect::setParameter(effect_param_t *param)
261{
262 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800263 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700264 }
265
266 if (param == NULL || param->psize == 0 || param->vsize == 0) {
267 return BAD_VALUE;
268 }
269
Eric Laurent25f43952010-07-28 05:40:18 -0700270 uint32_t size = sizeof(int);
271 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700272
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700273 ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data,
274 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700275
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700276 return mIEffect->command(EFFECT_CMD_SET_PARAM, sizeof (effect_param_t) + psize, param, &size,
277 &param->status);
Eric Laurent801a1182010-06-09 00:17:29 -0700278}
279
280status_t AudioEffect::setParameterDeferred(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
290 Mutex::Autolock _l(mCblk->lock);
291
292 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
293 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
294
295 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
296 return NO_MEMORY;
297 }
298 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
299 *p++ = size;
300 memcpy(p, param, sizeof(effect_param_t) + psize);
301 mCblk->clientIndex += size;
302
303 return NO_ERROR;
304}
305
306status_t AudioEffect::setParameterCommit()
307{
308 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800309 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700310 }
311
312 Mutex::Autolock _l(mCblk->lock);
313 if (mCblk->clientIndex == 0) {
314 return INVALID_OPERATION;
315 }
Eric Laurent25f43952010-07-28 05:40:18 -0700316 uint32_t size = 0;
Eric Laurent801a1182010-06-09 00:17:29 -0700317 return mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT, 0, NULL, &size, NULL);
318}
319
320status_t AudioEffect::getParameter(effect_param_t *param)
321{
322 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
John Grossmanaf7d8182012-01-11 12:23:42 -0800323 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700324 }
325
326 if (param == NULL || param->psize == 0 || param->vsize == 0) {
327 return BAD_VALUE;
328 }
329
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700330 ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data,
331 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700332
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700333 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
334 param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700335
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700336 return mIEffect->command(EFFECT_CMD_GET_PARAM, sizeof(effect_param_t) + param->psize, param,
337 &psize, param);
Eric Laurent801a1182010-06-09 00:17:29 -0700338}
339
340
341// -------------------------------------------------------------------------
342
343void AudioEffect::binderDied()
344{
Steve Block5ff1dd52012-01-05 23:22:43 +0000345 ALOGW("IEffect died");
John Grossmanaf7d8182012-01-11 12:23:42 -0800346 mStatus = DEAD_OBJECT;
Glenn Kastena0d68332012-01-27 16:47:15 -0800347 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700348 status_t status = DEAD_OBJECT;
349 mCbf(EVENT_ERROR, mUserData, &status);
350 }
351 mIEffect.clear();
352}
353
354// -------------------------------------------------------------------------
355
356void AudioEffect::controlStatusChanged(bool controlGranted)
357{
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700358 ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf,
359 mUserData);
Eric Laurent801a1182010-06-09 00:17:29 -0700360 if (controlGranted) {
361 if (mStatus == ALREADY_EXISTS) {
362 mStatus = NO_ERROR;
363 }
364 } else {
365 if (mStatus == NO_ERROR) {
366 mStatus = ALREADY_EXISTS;
367 }
368 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800369 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700370 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
371 }
372}
373
374void AudioEffect::enableStatusChanged(bool enabled)
375{
Steve Block3856b092011-10-20 11:56:00 +0100376 ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
Eric Laurent801a1182010-06-09 00:17:29 -0700377 if (mStatus == ALREADY_EXISTS) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800378 mEnabled = enabled;
Glenn Kastena0d68332012-01-27 16:47:15 -0800379 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700380 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
381 }
382 }
383}
384
Eric Laurent25f43952010-07-28 05:40:18 -0700385void AudioEffect::commandExecuted(uint32_t cmdCode,
Glenn Kasten0f11b512014-01-31 16:18:54 -0800386 uint32_t cmdSize __unused,
Eric Laurent25f43952010-07-28 05:40:18 -0700387 void *cmdData,
Glenn Kasten0f11b512014-01-31 16:18:54 -0800388 uint32_t replySize __unused,
Eric Laurent25f43952010-07-28 05:40:18 -0700389 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700390{
391 if (cmdData == NULL || replyData == NULL) {
392 return;
393 }
394
Glenn Kastena0d68332012-01-27 16:47:15 -0800395 if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
Eric Laurent801a1182010-06-09 00:17:29 -0700396 effect_param_t *cmd = (effect_param_t *)cmdData;
397 cmd->status = *(int32_t *)replyData;
398 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
399 }
400}
401
402// -------------------------------------------------------------------------
403
Eric Laurent801a1182010-06-09 00:17:29 -0700404status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
405{
406 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
407 if (af == 0) return PERMISSION_DENIED;
408 return af->queryNumberEffects(numEffects);
409}
410
Eric Laurentffe9c252010-06-23 17:38:20 -0700411status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700412{
413 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
414 if (af == 0) return PERMISSION_DENIED;
Eric Laurentffe9c252010-06-23 17:38:20 -0700415 return af->queryEffect(index, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700416}
417
Glenn Kasten5e92a782012-01-30 07:40:52 -0800418status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700419 const effect_uuid_t *type,
420 uint32_t preferredTypeFlag,
421 effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700422{
423 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
424 if (af == 0) return PERMISSION_DENIED;
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700425 return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700426}
427
Glenn Kastend848eb42016-03-08 13:42:11 -0800428status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700429 effect_descriptor_t *descriptors,
430 uint32_t *count)
431{
432 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
433 if (aps == 0) return PERMISSION_DENIED;
434 return aps->queryDefaultPreProcessing(audioSession, descriptors, count);
435}
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700436
437status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id)
438{
439 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
440 if (af == 0) return PERMISSION_DENIED;
441 *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
442 return NO_ERROR;
443}
444
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700445status_t AudioEffect::addSourceDefaultEffect(const char *typeStr,
446 const String16& opPackageName,
447 const char *uuidStr,
448 int32_t priority,
449 audio_source_t source,
450 audio_unique_id_t *id)
451{
452 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
453 if (aps == 0) return PERMISSION_DENIED;
454
455 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
456
457 // Convert type & uuid from string to effect_uuid_t.
458 effect_uuid_t type;
459 if (typeStr != NULL) {
460 status_t res = stringToGuid(typeStr, &type);
461 if (res != OK) return res;
462 } else {
463 type = *EFFECT_UUID_NULL;
464 }
465
466 effect_uuid_t uuid;
467 if (uuidStr != NULL) {
468 status_t res = stringToGuid(uuidStr, &uuid);
469 if (res != OK) return res;
470 } else {
471 uuid = *EFFECT_UUID_NULL;
472 }
473
474 return aps->addSourceDefaultEffect(&type, opPackageName, &uuid, priority, source, id);
475}
476
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700477status_t AudioEffect::addStreamDefaultEffect(const char *typeStr,
478 const String16& opPackageName,
479 const char *uuidStr,
480 int32_t priority,
481 audio_usage_t usage,
482 audio_unique_id_t *id)
483{
484 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
485 if (aps == 0) return PERMISSION_DENIED;
486
487 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
488
489 // Convert type & uuid from string to effect_uuid_t.
490 effect_uuid_t type;
491 if (typeStr != NULL) {
492 status_t res = stringToGuid(typeStr, &type);
493 if (res != OK) return res;
494 } else {
495 type = *EFFECT_UUID_NULL;
496 }
497
498 effect_uuid_t uuid;
499 if (uuidStr != NULL) {
500 status_t res = stringToGuid(uuidStr, &uuid);
501 if (res != OK) return res;
502 } else {
503 uuid = *EFFECT_UUID_NULL;
504 }
505
506 return aps->addStreamDefaultEffect(&type, opPackageName, &uuid, priority, usage, id);
507}
508
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700509status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id)
510{
511 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
512 if (aps == 0) return PERMISSION_DENIED;
513
514 return aps->removeSourceDefaultEffect(id);
515}
516
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700517status_t AudioEffect::removeStreamDefaultEffect(audio_unique_id_t id)
518{
519 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
520 if (aps == 0) return PERMISSION_DENIED;
521
522 return aps->removeStreamDefaultEffect(id);
523}
524
Eric Laurent801a1182010-06-09 00:17:29 -0700525// -------------------------------------------------------------------------
526
527status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
528{
529 if (str == NULL || guid == NULL) {
530 return BAD_VALUE;
531 }
532
533 int tmp[10];
534
535 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
536 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
537 return BAD_VALUE;
538 }
539 guid->timeLow = (uint32_t)tmp[0];
540 guid->timeMid = (uint16_t)tmp[1];
541 guid->timeHiAndVersion = (uint16_t)tmp[2];
542 guid->clockSeq = (uint16_t)tmp[3];
543 guid->node[0] = (uint8_t)tmp[4];
544 guid->node[1] = (uint8_t)tmp[5];
545 guid->node[2] = (uint8_t)tmp[6];
546 guid->node[3] = (uint8_t)tmp[7];
547 guid->node[4] = (uint8_t)tmp[8];
548 guid->node[5] = (uint8_t)tmp[9];
549
550 return NO_ERROR;
551}
552
553status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
554{
555 if (guid == NULL || str == NULL) {
556 return BAD_VALUE;
557 }
558
559 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
560 guid->timeLow,
561 guid->timeMid,
562 guid->timeHiAndVersion,
563 guid->clockSeq,
564 guid->node[0],
565 guid->node[1],
566 guid->node[2],
567 guid->node[3],
568 guid->node[4],
569 guid->node[5]);
570
571 return NO_ERROR;
572}
573
574
Glenn Kasten40bc9062015-03-20 09:09:33 -0700575} // namespace android