blob: 7d5b690b49a56406879232cd95db629b7a16bc75 [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();
Eric Laurent801a1182010-06-09 00:17:29 -0700141
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800142 iEffect = audioFlinger->createEffect((effect_descriptor_t *)&mDescriptor,
Eric Laurent94876032019-11-13 12:45:28 -0800143 mIEffectClient, priority, io, mSessionId, device, mOpPackageName, mClientPid,
Eric Laurentb6436272016-12-07 19:24:50 -0800144 &mStatus, &mId, &enabled);
Eric Laurent801a1182010-06-09 00:17:29 -0700145
146 if (iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700147 char typeBuffer[64], uuidBuffer[64];
148 guidToString(type, typeBuffer, sizeof(typeBuffer));
149 guidToString(uuid, uuidBuffer, sizeof(uuidBuffer));
150 ALOGE("set(): AudioFlinger could not create effect %s / %s, status: %d",
151 typeBuffer, uuidBuffer, mStatus);
Eric Laurenteecd7652015-06-04 16:20:16 -0700152 if (iEffect == 0) {
153 mStatus = NO_INIT;
154 }
Eric Laurent801a1182010-06-09 00:17:29 -0700155 return mStatus;
156 }
157
158 mEnabled = (volatile int32_t)enabled;
159
Eric Laurent801a1182010-06-09 00:17:29 -0700160 cblk = iEffect->getCblk();
161 if (cblk == 0) {
162 mStatus = NO_INIT;
Steve Block29357bc2012-01-06 19:20:56 +0000163 ALOGE("Could not get control block");
Eric Laurent801a1182010-06-09 00:17:29 -0700164 return mStatus;
165 }
166
Eric Laurenteecd7652015-06-04 16:20:16 -0700167 mIEffect = iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -0700168 mCblkMemory = cblk;
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700169 // TODO: Using unsecurePointer() has some associated security pitfalls
170 // (see declaration for details).
171 // Either document why it is safe in this case or address the
172 // issue (e.g. by copying).
173 mCblk = static_cast<effect_param_cblk_t*>(cblk->unsecurePointer());
Eric Laurent801a1182010-06-09 00:17:29 -0700174 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
175 mCblk->buffer = (uint8_t *)mCblk + bufOffset;
176
Marco Nelissen06b46062014-11-14 07:58:25 -0800177 IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700178 ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId,
179 mStatus, mEnabled, mClientPid);
180
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800181 if (!audio_is_global_session(mSessionId)) {
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700182 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid);
183 }
Eric Laurent801a1182010-06-09 00:17:29 -0700184
185 return mStatus;
186}
187
188
189AudioEffect::~AudioEffect()
190{
Steve Block3856b092011-10-20 11:56:00 +0100191 ALOGV("Destructor %p", this);
Eric Laurent801a1182010-06-09 00:17:29 -0700192
193 if (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS) {
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800194 if (!audio_is_global_session(mSessionId)) {
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700195 AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
196 }
Eric Laurent801a1182010-06-09 00:17:29 -0700197 if (mIEffect != NULL) {
198 mIEffect->disconnect();
Marco Nelissen06b46062014-11-14 07:58:25 -0800199 IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient);
Eric Laurent801a1182010-06-09 00:17:29 -0700200 }
Eric Laurenteecd7652015-06-04 16:20:16 -0700201 mIEffect.clear();
202 mCblkMemory.clear();
203 mIEffectClient.clear();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700204 IPCThreadState::self()->flushCommands();
Eric Laurent801a1182010-06-09 00:17:29 -0700205 }
Eric Laurent801a1182010-06-09 00:17:29 -0700206}
207
208
209status_t AudioEffect::initCheck() const
210{
211 return mStatus;
212}
213
214// -------------------------------------------------------------------------
215
216effect_descriptor_t AudioEffect::descriptor() const
217{
218 return mDescriptor;
219}
220
Eric Laurentda7581b2010-07-02 08:12:41 -0700221bool AudioEffect::getEnabled() const
Eric Laurent801a1182010-06-09 00:17:29 -0700222{
223 return (mEnabled != 0);
224}
225
Eric Laurentda7581b2010-07-02 08:12:41 -0700226status_t AudioEffect::setEnabled(bool enabled)
Eric Laurent801a1182010-06-09 00:17:29 -0700227{
228 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800229 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700230 }
Eric Laurent801a1182010-06-09 00:17:29 -0700231
Eric Laurentf5aafb22010-11-18 08:40:16 -0800232 status_t status = NO_ERROR;
233
234 AutoMutex lock(mLock);
235 if (enabled != mEnabled) {
236 if (enabled) {
Steve Block3856b092011-10-20 11:56:00 +0100237 ALOGV("enable %p", this);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800238 status = mIEffect->enable();
239 } else {
Steve Block3856b092011-10-20 11:56:00 +0100240 ALOGV("disable %p", this);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800241 status = mIEffect->disable();
Eric Laurentda7581b2010-07-02 08:12:41 -0700242 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800243 if (status == NO_ERROR) {
244 mEnabled = enabled;
Eric Laurentda7581b2010-07-02 08:12:41 -0700245 }
Eric Laurent801a1182010-06-09 00:17:29 -0700246 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800247 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700248}
249
Eric Laurent25f43952010-07-28 05:40:18 -0700250status_t AudioEffect::command(uint32_t cmdCode,
251 uint32_t cmdSize,
252 void *cmdData,
253 uint32_t *replySize,
254 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700255{
256 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
Steve Block3856b092011-10-20 11:56:00 +0100257 ALOGV("command() bad status %d", mStatus);
John Grossmanaf7d8182012-01-11 12:23:42 -0800258 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700259 }
260
Eric Laurentf5aafb22010-11-18 08:40:16 -0800261 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
262 if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
263 return NO_ERROR;
264 }
265 if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) {
266 return BAD_VALUE;
267 }
268 mLock.lock();
Eric Laurent0fa449c2010-09-24 11:52:04 -0700269 }
270
Eric Laurent8569f0d2010-07-29 23:43:43 -0700271 status_t status = mIEffect->command(cmdCode, cmdSize, cmdData, replySize, replyData);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700272
273 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800274 if (status == NO_ERROR) {
275 status = *(status_t *)replyData;
Eric Laurent0fa449c2010-09-24 11:52:04 -0700276 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800277 if (status == NO_ERROR) {
278 mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700279 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800280 mLock.unlock();
Eric Laurent8569f0d2010-07-29 23:43:43 -0700281 }
282
Eric Laurent8569f0d2010-07-29 23:43:43 -0700283 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700284}
285
286
287status_t AudioEffect::setParameter(effect_param_t *param)
288{
289 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800290 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700291 }
292
293 if (param == NULL || param->psize == 0 || param->vsize == 0) {
294 return BAD_VALUE;
295 }
296
Eric Laurent25f43952010-07-28 05:40:18 -0700297 uint32_t size = sizeof(int);
298 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700299
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700300 ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data,
301 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700302
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700303 return mIEffect->command(EFFECT_CMD_SET_PARAM, sizeof (effect_param_t) + psize, param, &size,
304 &param->status);
Eric Laurent801a1182010-06-09 00:17:29 -0700305}
306
307status_t AudioEffect::setParameterDeferred(effect_param_t *param)
308{
309 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800310 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700311 }
312
313 if (param == NULL || param->psize == 0 || param->vsize == 0) {
314 return BAD_VALUE;
315 }
316
317 Mutex::Autolock _l(mCblk->lock);
318
319 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
320 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
321
322 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
323 return NO_MEMORY;
324 }
325 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
326 *p++ = size;
327 memcpy(p, param, sizeof(effect_param_t) + psize);
328 mCblk->clientIndex += size;
329
330 return NO_ERROR;
331}
332
333status_t AudioEffect::setParameterCommit()
334{
335 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800336 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700337 }
338
339 Mutex::Autolock _l(mCblk->lock);
340 if (mCblk->clientIndex == 0) {
341 return INVALID_OPERATION;
342 }
Eric Laurent25f43952010-07-28 05:40:18 -0700343 uint32_t size = 0;
Eric Laurent801a1182010-06-09 00:17:29 -0700344 return mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT, 0, NULL, &size, NULL);
345}
346
347status_t AudioEffect::getParameter(effect_param_t *param)
348{
349 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
John Grossmanaf7d8182012-01-11 12:23:42 -0800350 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700351 }
352
353 if (param == NULL || param->psize == 0 || param->vsize == 0) {
354 return BAD_VALUE;
355 }
356
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700357 ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data,
358 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700359
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700360 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
361 param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700362
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700363 return mIEffect->command(EFFECT_CMD_GET_PARAM, sizeof(effect_param_t) + param->psize, param,
364 &psize, param);
Eric Laurent801a1182010-06-09 00:17:29 -0700365}
366
367
368// -------------------------------------------------------------------------
369
370void AudioEffect::binderDied()
371{
Steve Block5ff1dd52012-01-05 23:22:43 +0000372 ALOGW("IEffect died");
John Grossmanaf7d8182012-01-11 12:23:42 -0800373 mStatus = DEAD_OBJECT;
Glenn Kastena0d68332012-01-27 16:47:15 -0800374 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700375 status_t status = DEAD_OBJECT;
376 mCbf(EVENT_ERROR, mUserData, &status);
377 }
378 mIEffect.clear();
379}
380
381// -------------------------------------------------------------------------
382
383void AudioEffect::controlStatusChanged(bool controlGranted)
384{
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700385 ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf,
386 mUserData);
Eric Laurent801a1182010-06-09 00:17:29 -0700387 if (controlGranted) {
388 if (mStatus == ALREADY_EXISTS) {
389 mStatus = NO_ERROR;
390 }
391 } else {
392 if (mStatus == NO_ERROR) {
393 mStatus = ALREADY_EXISTS;
394 }
395 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800396 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700397 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
398 }
399}
400
401void AudioEffect::enableStatusChanged(bool enabled)
402{
Steve Block3856b092011-10-20 11:56:00 +0100403 ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
Eric Laurent801a1182010-06-09 00:17:29 -0700404 if (mStatus == ALREADY_EXISTS) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800405 mEnabled = enabled;
Glenn Kastena0d68332012-01-27 16:47:15 -0800406 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700407 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
408 }
409 }
410}
411
Eric Laurent25f43952010-07-28 05:40:18 -0700412void AudioEffect::commandExecuted(uint32_t cmdCode,
Glenn Kasten0f11b512014-01-31 16:18:54 -0800413 uint32_t cmdSize __unused,
Eric Laurent25f43952010-07-28 05:40:18 -0700414 void *cmdData,
Glenn Kasten0f11b512014-01-31 16:18:54 -0800415 uint32_t replySize __unused,
Eric Laurent25f43952010-07-28 05:40:18 -0700416 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700417{
418 if (cmdData == NULL || replyData == NULL) {
419 return;
420 }
421
Glenn Kastena0d68332012-01-27 16:47:15 -0800422 if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
Eric Laurent801a1182010-06-09 00:17:29 -0700423 effect_param_t *cmd = (effect_param_t *)cmdData;
424 cmd->status = *(int32_t *)replyData;
425 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
426 }
427}
428
429// -------------------------------------------------------------------------
430
Eric Laurent801a1182010-06-09 00:17:29 -0700431status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
432{
433 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
434 if (af == 0) return PERMISSION_DENIED;
435 return af->queryNumberEffects(numEffects);
436}
437
Eric Laurentffe9c252010-06-23 17:38:20 -0700438status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700439{
440 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
441 if (af == 0) return PERMISSION_DENIED;
Eric Laurentffe9c252010-06-23 17:38:20 -0700442 return af->queryEffect(index, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700443}
444
Glenn Kasten5e92a782012-01-30 07:40:52 -0800445status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700446 const effect_uuid_t *type,
447 uint32_t preferredTypeFlag,
448 effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700449{
450 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
451 if (af == 0) return PERMISSION_DENIED;
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700452 return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700453}
454
Glenn Kastend848eb42016-03-08 13:42:11 -0800455status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700456 effect_descriptor_t *descriptors,
457 uint32_t *count)
458{
459 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
460 if (aps == 0) return PERMISSION_DENIED;
461 return aps->queryDefaultPreProcessing(audioSession, descriptors, count);
462}
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700463
464status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id)
465{
466 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
467 if (af == 0) return PERMISSION_DENIED;
468 *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
469 return NO_ERROR;
470}
471
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700472status_t AudioEffect::addSourceDefaultEffect(const char *typeStr,
473 const String16& opPackageName,
474 const char *uuidStr,
475 int32_t priority,
476 audio_source_t source,
477 audio_unique_id_t *id)
478{
479 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
480 if (aps == 0) return PERMISSION_DENIED;
481
482 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
483
484 // Convert type & uuid from string to effect_uuid_t.
485 effect_uuid_t type;
486 if (typeStr != NULL) {
487 status_t res = stringToGuid(typeStr, &type);
488 if (res != OK) return res;
489 } else {
490 type = *EFFECT_UUID_NULL;
491 }
492
493 effect_uuid_t uuid;
494 if (uuidStr != NULL) {
495 status_t res = stringToGuid(uuidStr, &uuid);
496 if (res != OK) return res;
497 } else {
498 uuid = *EFFECT_UUID_NULL;
499 }
500
501 return aps->addSourceDefaultEffect(&type, opPackageName, &uuid, priority, source, id);
502}
503
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700504status_t AudioEffect::addStreamDefaultEffect(const char *typeStr,
505 const String16& opPackageName,
506 const char *uuidStr,
507 int32_t priority,
508 audio_usage_t usage,
509 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 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
515
516 // Convert type & uuid from string to effect_uuid_t.
517 effect_uuid_t type;
518 if (typeStr != NULL) {
519 status_t res = stringToGuid(typeStr, &type);
520 if (res != OK) return res;
521 } else {
522 type = *EFFECT_UUID_NULL;
523 }
524
525 effect_uuid_t uuid;
526 if (uuidStr != NULL) {
527 status_t res = stringToGuid(uuidStr, &uuid);
528 if (res != OK) return res;
529 } else {
530 uuid = *EFFECT_UUID_NULL;
531 }
532
533 return aps->addStreamDefaultEffect(&type, opPackageName, &uuid, priority, usage, id);
534}
535
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700536status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id)
537{
538 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
539 if (aps == 0) return PERMISSION_DENIED;
540
541 return aps->removeSourceDefaultEffect(id);
542}
543
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700544status_t AudioEffect::removeStreamDefaultEffect(audio_unique_id_t id)
545{
546 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
547 if (aps == 0) return PERMISSION_DENIED;
548
549 return aps->removeStreamDefaultEffect(id);
550}
551
Eric Laurent801a1182010-06-09 00:17:29 -0700552// -------------------------------------------------------------------------
553
554status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
555{
556 if (str == NULL || guid == NULL) {
557 return BAD_VALUE;
558 }
559
560 int tmp[10];
561
562 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
563 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
564 return BAD_VALUE;
565 }
566 guid->timeLow = (uint32_t)tmp[0];
567 guid->timeMid = (uint16_t)tmp[1];
568 guid->timeHiAndVersion = (uint16_t)tmp[2];
569 guid->clockSeq = (uint16_t)tmp[3];
570 guid->node[0] = (uint8_t)tmp[4];
571 guid->node[1] = (uint8_t)tmp[5];
572 guid->node[2] = (uint8_t)tmp[6];
573 guid->node[3] = (uint8_t)tmp[7];
574 guid->node[4] = (uint8_t)tmp[8];
575 guid->node[5] = (uint8_t)tmp[9];
576
577 return NO_ERROR;
578}
579
580status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
581{
582 if (guid == NULL || str == NULL) {
583 return BAD_VALUE;
584 }
585
586 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
587 guid->timeLow,
588 guid->timeMid,
589 guid->timeHiAndVersion,
590 guid->clockSeq,
591 guid->node[0],
592 guid->node[1],
593 guid->node[2],
594 guid->node[3],
595 guid->node[4],
596 guid->node[5]);
597
598 return NO_ERROR;
599}
600
601
Glenn Kasten40bc9062015-03-20 09:09:33 -0700602} // namespace android