blob: 3ead6cb1acee6f3dd95e9c312c68f6c9e3e9f435 [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)
Eric Laurent2fe0acd2020-03-13 14:30:46 -070039 : mStatus(NO_INIT), mProbe(false), 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,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070052 const AudioDeviceTypeAddr& device,
53 bool probe
Eric Laurent801a1182010-06-09 00:17:29 -070054 )
Eric Laurent2fe0acd2020-03-13 14:30:46 -070055 : mStatus(NO_INIT), mProbe(false), mOpPackageName(opPackageName)
Eric Laurent801a1182010-06-09 00:17:29 -070056{
haobo101735293f51b542018-08-09 09:14:31 +080057 AutoMutex lock(mConstructLock);
Eric Laurent2fe0acd2020-03-13 14:30:46 -070058 mStatus = set(type, uuid, priority, cbf, user, sessionId, io, device, probe);
Eric Laurent801a1182010-06-09 00:17:29 -070059}
60
61AudioEffect::AudioEffect(const char *typeStr,
Svet Ganovbe71aa22015-04-28 12:06:02 -070062 const String16& opPackageName,
Eric Laurent801a1182010-06-09 00:17:29 -070063 const char *uuidStr,
64 int32_t priority,
65 effect_callback_t cbf,
66 void* user,
Glenn Kastend848eb42016-03-08 13:42:11 -080067 audio_session_t sessionId,
Eric Laurent94876032019-11-13 12:45:28 -080068 audio_io_handle_t io,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070069 const AudioDeviceTypeAddr& device,
70 bool probe
Eric Laurent801a1182010-06-09 00:17:29 -070071 )
Eric Laurent2fe0acd2020-03-13 14:30:46 -070072 : mStatus(NO_INIT), mProbe(false), mOpPackageName(opPackageName)
Eric Laurent801a1182010-06-09 00:17:29 -070073{
74 effect_uuid_t type;
75 effect_uuid_t *pType = NULL;
76 effect_uuid_t uuid;
77 effect_uuid_t *pUuid = NULL;
78
Steve Block3856b092011-10-20 11:56:00 +010079 ALOGV("Constructor string\n - type: %s\n - uuid: %s", typeStr, uuidStr);
Eric Laurent801a1182010-06-09 00:17:29 -070080
81 if (typeStr != NULL) {
82 if (stringToGuid(typeStr, &type) == NO_ERROR) {
83 pType = &type;
84 }
85 }
86
87 if (uuidStr != NULL) {
88 if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
89 pUuid = &uuid;
90 }
91 }
92
haobo101735293f51b542018-08-09 09:14:31 +080093 AutoMutex lock(mConstructLock);
Eric Laurent2fe0acd2020-03-13 14:30:46 -070094 mStatus = set(pType, pUuid, priority, cbf, user, sessionId, io, device, probe);
Eric Laurent801a1182010-06-09 00:17:29 -070095}
96
97status_t AudioEffect::set(const effect_uuid_t *type,
98 const effect_uuid_t *uuid,
99 int32_t priority,
100 effect_callback_t cbf,
101 void* user,
Glenn Kastend848eb42016-03-08 13:42:11 -0800102 audio_session_t sessionId,
Eric Laurent94876032019-11-13 12:45:28 -0800103 audio_io_handle_t io,
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700104 const AudioDeviceTypeAddr& device,
105 bool probe)
Eric Laurent801a1182010-06-09 00:17:29 -0700106{
107 sp<IEffect> iEffect;
108 sp<IMemory> cblk;
109 int enabled;
110
Steve Block3856b092011-10-20 11:56:00 +0100111 ALOGV("set %p mUserData: %p uuid: %p timeLow %08x", this, user, type, type ? type->timeLow : 0);
Eric Laurent801a1182010-06-09 00:17:29 -0700112
113 if (mIEffect != 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000114 ALOGW("Effect already in use");
Eric Laurent801a1182010-06-09 00:17:29 -0700115 return INVALID_OPERATION;
116 }
117
Eric Laurent94876032019-11-13 12:45:28 -0800118 if (sessionId == AUDIO_SESSION_DEVICE && io != AUDIO_IO_HANDLE_NONE) {
119 ALOGW("IO handle should not be specified for device effect");
120 return BAD_VALUE;
121 }
Eric Laurent801a1182010-06-09 00:17:29 -0700122 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
123 if (audioFlinger == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000124 ALOGE("set(): Could not get audioflinger");
Eric Laurent801a1182010-06-09 00:17:29 -0700125 return NO_INIT;
126 }
127
128 if (type == NULL && uuid == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000129 ALOGW("Must specify at least type or uuid");
Eric Laurent801a1182010-06-09 00:17:29 -0700130 return BAD_VALUE;
131 }
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700132 mProbe = probe;
Eric Laurent801a1182010-06-09 00:17:29 -0700133 mPriority = priority;
134 mCbf = cbf;
135 mUserData = user;
136 mSessionId = sessionId;
137
138 memset(&mDescriptor, 0, sizeof(effect_descriptor_t));
Glenn Kastena189a682012-02-20 12:16:30 -0800139 mDescriptor.type = *(type != NULL ? type : EFFECT_UUID_NULL);
140 mDescriptor.uuid = *(uuid != NULL ? uuid : EFFECT_UUID_NULL);
Eric Laurent801a1182010-06-09 00:17:29 -0700141
142 mIEffectClient = new EffectClient(this);
Eric Laurentb6436272016-12-07 19:24:50 -0800143 mClientPid = IPCThreadState::self()->getCallingPid();
Andy Hung8b0bfd92019-12-23 13:11:11 -0800144 mClientUid = IPCThreadState::self()->getCallingUid();
Eric Laurent801a1182010-06-09 00:17:29 -0700145
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800146 iEffect = audioFlinger->createEffect((effect_descriptor_t *)&mDescriptor,
Eric Laurent94876032019-11-13 12:45:28 -0800147 mIEffectClient, priority, io, mSessionId, device, mOpPackageName, mClientPid,
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700148 probe, &mStatus, &mId, &enabled);
Eric Laurent801a1182010-06-09 00:17:29 -0700149
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700150 // In probe mode, we stop here and return the status: the IEffect interface to
151 // audio flinger will not be retained. initCheck() will return the creation status
152 // but all other APIs will return invalid operation.
153 if (probe || iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000154 char typeBuffer[64] = {}, uuidBuffer[64] = {};
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700155 guidToString(type, typeBuffer, sizeof(typeBuffer));
156 guidToString(uuid, uuidBuffer, sizeof(uuidBuffer));
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700157 ALOGE_IF(!probe, "set(): AudioFlinger could not create effect %s / %s, status: %d",
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000158 type != nullptr ? typeBuffer : "NULL",
159 uuid != nullptr ? uuidBuffer : "NULL",
160 mStatus);
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700161 if (!probe && iEffect == 0) {
Eric Laurenteecd7652015-06-04 16:20:16 -0700162 mStatus = NO_INIT;
163 }
Eric Laurent801a1182010-06-09 00:17:29 -0700164 return mStatus;
165 }
166
167 mEnabled = (volatile int32_t)enabled;
168
Eric Laurent801a1182010-06-09 00:17:29 -0700169 cblk = iEffect->getCblk();
170 if (cblk == 0) {
171 mStatus = NO_INIT;
Steve Block29357bc2012-01-06 19:20:56 +0000172 ALOGE("Could not get control block");
Eric Laurent801a1182010-06-09 00:17:29 -0700173 return mStatus;
174 }
175
Eric Laurenteecd7652015-06-04 16:20:16 -0700176 mIEffect = iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -0700177 mCblkMemory = cblk;
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700178 // TODO: Using unsecurePointer() has some associated security pitfalls
179 // (see declaration for details).
180 // Either document why it is safe in this case or address the
181 // issue (e.g. by copying).
182 mCblk = static_cast<effect_param_cblk_t*>(cblk->unsecurePointer());
Eric Laurent801a1182010-06-09 00:17:29 -0700183 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
184 mCblk->buffer = (uint8_t *)mCblk + bufOffset;
185
Marco Nelissen06b46062014-11-14 07:58:25 -0800186 IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700187 ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId,
188 mStatus, mEnabled, mClientPid);
189
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800190 if (!audio_is_global_session(mSessionId)) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800191 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid, mClientUid);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700192 }
Eric Laurent801a1182010-06-09 00:17:29 -0700193
194 return mStatus;
195}
196
197
198AudioEffect::~AudioEffect()
199{
Steve Block3856b092011-10-20 11:56:00 +0100200 ALOGV("Destructor %p", this);
Eric Laurent801a1182010-06-09 00:17:29 -0700201
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700202 if (!mProbe && (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS)) {
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800203 if (!audio_is_global_session(mSessionId)) {
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700204 AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
205 }
Eric Laurent801a1182010-06-09 00:17:29 -0700206 if (mIEffect != NULL) {
207 mIEffect->disconnect();
Marco Nelissen06b46062014-11-14 07:58:25 -0800208 IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient);
Eric Laurent801a1182010-06-09 00:17:29 -0700209 }
Eric Laurenteecd7652015-06-04 16:20:16 -0700210 mIEffect.clear();
211 mCblkMemory.clear();
Eric Laurent801a1182010-06-09 00:17:29 -0700212 }
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700213 mIEffectClient.clear();
214 IPCThreadState::self()->flushCommands();
Eric Laurent801a1182010-06-09 00:17:29 -0700215}
216
217
218status_t AudioEffect::initCheck() const
219{
220 return mStatus;
221}
222
223// -------------------------------------------------------------------------
224
225effect_descriptor_t AudioEffect::descriptor() const
226{
227 return mDescriptor;
228}
229
Eric Laurentda7581b2010-07-02 08:12:41 -0700230bool AudioEffect::getEnabled() const
Eric Laurent801a1182010-06-09 00:17:29 -0700231{
232 return (mEnabled != 0);
233}
234
Eric Laurentda7581b2010-07-02 08:12:41 -0700235status_t AudioEffect::setEnabled(bool enabled)
Eric Laurent801a1182010-06-09 00:17:29 -0700236{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700237 if (mProbe) {
238 return INVALID_OPERATION;
239 }
Eric Laurent801a1182010-06-09 00:17:29 -0700240 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800241 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700242 }
Eric Laurent801a1182010-06-09 00:17:29 -0700243
Eric Laurentf5aafb22010-11-18 08:40:16 -0800244 status_t status = NO_ERROR;
245
246 AutoMutex lock(mLock);
247 if (enabled != mEnabled) {
248 if (enabled) {
Steve Block3856b092011-10-20 11:56:00 +0100249 ALOGV("enable %p", this);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800250 status = mIEffect->enable();
251 } else {
Steve Block3856b092011-10-20 11:56:00 +0100252 ALOGV("disable %p", this);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800253 status = mIEffect->disable();
Eric Laurentda7581b2010-07-02 08:12:41 -0700254 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800255 if (status == NO_ERROR) {
256 mEnabled = enabled;
Eric Laurentda7581b2010-07-02 08:12:41 -0700257 }
Eric Laurent801a1182010-06-09 00:17:29 -0700258 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800259 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700260}
261
Eric Laurent25f43952010-07-28 05:40:18 -0700262status_t AudioEffect::command(uint32_t cmdCode,
263 uint32_t cmdSize,
264 void *cmdData,
265 uint32_t *replySize,
266 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700267{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700268 if (mProbe) {
269 return INVALID_OPERATION;
270 }
Eric Laurent801a1182010-06-09 00:17:29 -0700271 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
Steve Block3856b092011-10-20 11:56:00 +0100272 ALOGV("command() bad status %d", mStatus);
John Grossmanaf7d8182012-01-11 12:23:42 -0800273 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700274 }
275
Eric Laurentf5aafb22010-11-18 08:40:16 -0800276 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
277 if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
278 return NO_ERROR;
279 }
280 if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) {
281 return BAD_VALUE;
282 }
283 mLock.lock();
Eric Laurent0fa449c2010-09-24 11:52:04 -0700284 }
285
Eric Laurent8569f0d2010-07-29 23:43:43 -0700286 status_t status = mIEffect->command(cmdCode, cmdSize, cmdData, replySize, replyData);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700287
288 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800289 if (status == NO_ERROR) {
290 status = *(status_t *)replyData;
Eric Laurent0fa449c2010-09-24 11:52:04 -0700291 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800292 if (status == NO_ERROR) {
293 mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700294 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800295 mLock.unlock();
Eric Laurent8569f0d2010-07-29 23:43:43 -0700296 }
297
Eric Laurent8569f0d2010-07-29 23:43:43 -0700298 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700299}
300
301
302status_t AudioEffect::setParameter(effect_param_t *param)
303{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700304 if (mProbe) {
305 return INVALID_OPERATION;
306 }
Eric Laurent801a1182010-06-09 00:17:29 -0700307 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800308 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700309 }
310
311 if (param == NULL || param->psize == 0 || param->vsize == 0) {
312 return BAD_VALUE;
313 }
314
Eric Laurent25f43952010-07-28 05:40:18 -0700315 uint32_t size = sizeof(int);
316 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700317
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700318 ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data,
319 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700320
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700321 return mIEffect->command(EFFECT_CMD_SET_PARAM, sizeof (effect_param_t) + psize, param, &size,
322 &param->status);
Eric Laurent801a1182010-06-09 00:17:29 -0700323}
324
325status_t AudioEffect::setParameterDeferred(effect_param_t *param)
326{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700327 if (mProbe) {
328 return INVALID_OPERATION;
329 }
Eric Laurent801a1182010-06-09 00:17:29 -0700330 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800331 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700332 }
333
334 if (param == NULL || param->psize == 0 || param->vsize == 0) {
335 return BAD_VALUE;
336 }
337
338 Mutex::Autolock _l(mCblk->lock);
339
340 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
341 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
342
343 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
344 return NO_MEMORY;
345 }
346 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
347 *p++ = size;
348 memcpy(p, param, sizeof(effect_param_t) + psize);
349 mCblk->clientIndex += size;
350
351 return NO_ERROR;
352}
353
354status_t AudioEffect::setParameterCommit()
355{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700356 if (mProbe) {
357 return INVALID_OPERATION;
358 }
Eric Laurent801a1182010-06-09 00:17:29 -0700359 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800360 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700361 }
362
363 Mutex::Autolock _l(mCblk->lock);
364 if (mCblk->clientIndex == 0) {
365 return INVALID_OPERATION;
366 }
Eric Laurent25f43952010-07-28 05:40:18 -0700367 uint32_t size = 0;
Eric Laurent801a1182010-06-09 00:17:29 -0700368 return mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT, 0, NULL, &size, NULL);
369}
370
371status_t AudioEffect::getParameter(effect_param_t *param)
372{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700373 if (mProbe) {
374 return INVALID_OPERATION;
375 }
Eric Laurent801a1182010-06-09 00:17:29 -0700376 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
John Grossmanaf7d8182012-01-11 12:23:42 -0800377 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700378 }
379
380 if (param == NULL || param->psize == 0 || param->vsize == 0) {
381 return BAD_VALUE;
382 }
383
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700384 ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data,
385 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700386
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700387 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
388 param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700389
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700390 return mIEffect->command(EFFECT_CMD_GET_PARAM, sizeof(effect_param_t) + param->psize, param,
391 &psize, param);
Eric Laurent801a1182010-06-09 00:17:29 -0700392}
393
394
395// -------------------------------------------------------------------------
396
397void AudioEffect::binderDied()
398{
Steve Block5ff1dd52012-01-05 23:22:43 +0000399 ALOGW("IEffect died");
John Grossmanaf7d8182012-01-11 12:23:42 -0800400 mStatus = DEAD_OBJECT;
Glenn Kastena0d68332012-01-27 16:47:15 -0800401 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700402 status_t status = DEAD_OBJECT;
403 mCbf(EVENT_ERROR, mUserData, &status);
404 }
405 mIEffect.clear();
406}
407
408// -------------------------------------------------------------------------
409
410void AudioEffect::controlStatusChanged(bool controlGranted)
411{
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700412 ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf,
413 mUserData);
Eric Laurent801a1182010-06-09 00:17:29 -0700414 if (controlGranted) {
415 if (mStatus == ALREADY_EXISTS) {
416 mStatus = NO_ERROR;
417 }
418 } else {
419 if (mStatus == NO_ERROR) {
420 mStatus = ALREADY_EXISTS;
421 }
422 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800423 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700424 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
425 }
426}
427
428void AudioEffect::enableStatusChanged(bool enabled)
429{
Steve Block3856b092011-10-20 11:56:00 +0100430 ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
Eric Laurent801a1182010-06-09 00:17:29 -0700431 if (mStatus == ALREADY_EXISTS) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800432 mEnabled = enabled;
Glenn Kastena0d68332012-01-27 16:47:15 -0800433 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700434 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
435 }
436 }
437}
438
Eric Laurent25f43952010-07-28 05:40:18 -0700439void AudioEffect::commandExecuted(uint32_t cmdCode,
Glenn Kasten0f11b512014-01-31 16:18:54 -0800440 uint32_t cmdSize __unused,
Eric Laurent25f43952010-07-28 05:40:18 -0700441 void *cmdData,
Glenn Kasten0f11b512014-01-31 16:18:54 -0800442 uint32_t replySize __unused,
Eric Laurent25f43952010-07-28 05:40:18 -0700443 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700444{
445 if (cmdData == NULL || replyData == NULL) {
446 return;
447 }
448
Glenn Kastena0d68332012-01-27 16:47:15 -0800449 if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
Eric Laurent801a1182010-06-09 00:17:29 -0700450 effect_param_t *cmd = (effect_param_t *)cmdData;
451 cmd->status = *(int32_t *)replyData;
452 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
453 }
454}
455
456// -------------------------------------------------------------------------
457
Eric Laurent801a1182010-06-09 00:17:29 -0700458status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
459{
460 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
461 if (af == 0) return PERMISSION_DENIED;
462 return af->queryNumberEffects(numEffects);
463}
464
Eric Laurentffe9c252010-06-23 17:38:20 -0700465status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700466{
467 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
468 if (af == 0) return PERMISSION_DENIED;
Eric Laurentffe9c252010-06-23 17:38:20 -0700469 return af->queryEffect(index, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700470}
471
Glenn Kasten5e92a782012-01-30 07:40:52 -0800472status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700473 const effect_uuid_t *type,
474 uint32_t preferredTypeFlag,
475 effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700476{
477 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
478 if (af == 0) return PERMISSION_DENIED;
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700479 return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700480}
481
Glenn Kastend848eb42016-03-08 13:42:11 -0800482status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700483 effect_descriptor_t *descriptors,
484 uint32_t *count)
485{
486 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
487 if (aps == 0) return PERMISSION_DENIED;
488 return aps->queryDefaultPreProcessing(audioSession, descriptors, count);
489}
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700490
491status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id)
492{
493 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
494 if (af == 0) return PERMISSION_DENIED;
495 *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
496 return NO_ERROR;
497}
498
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700499status_t AudioEffect::addSourceDefaultEffect(const char *typeStr,
500 const String16& opPackageName,
501 const char *uuidStr,
502 int32_t priority,
503 audio_source_t source,
504 audio_unique_id_t *id)
505{
506 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
507 if (aps == 0) return PERMISSION_DENIED;
508
509 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
510
511 // Convert type & uuid from string to effect_uuid_t.
512 effect_uuid_t type;
513 if (typeStr != NULL) {
514 status_t res = stringToGuid(typeStr, &type);
515 if (res != OK) return res;
516 } else {
517 type = *EFFECT_UUID_NULL;
518 }
519
520 effect_uuid_t uuid;
521 if (uuidStr != NULL) {
522 status_t res = stringToGuid(uuidStr, &uuid);
523 if (res != OK) return res;
524 } else {
525 uuid = *EFFECT_UUID_NULL;
526 }
527
528 return aps->addSourceDefaultEffect(&type, opPackageName, &uuid, priority, source, id);
529}
530
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700531status_t AudioEffect::addStreamDefaultEffect(const char *typeStr,
532 const String16& opPackageName,
533 const char *uuidStr,
534 int32_t priority,
535 audio_usage_t usage,
536 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 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
542
543 // Convert type & uuid from string to effect_uuid_t.
544 effect_uuid_t type;
545 if (typeStr != NULL) {
546 status_t res = stringToGuid(typeStr, &type);
547 if (res != OK) return res;
548 } else {
549 type = *EFFECT_UUID_NULL;
550 }
551
552 effect_uuid_t uuid;
553 if (uuidStr != NULL) {
554 status_t res = stringToGuid(uuidStr, &uuid);
555 if (res != OK) return res;
556 } else {
557 uuid = *EFFECT_UUID_NULL;
558 }
559
560 return aps->addStreamDefaultEffect(&type, opPackageName, &uuid, priority, usage, id);
561}
562
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700563status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id)
564{
565 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
566 if (aps == 0) return PERMISSION_DENIED;
567
568 return aps->removeSourceDefaultEffect(id);
569}
570
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700571status_t AudioEffect::removeStreamDefaultEffect(audio_unique_id_t id)
572{
573 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
574 if (aps == 0) return PERMISSION_DENIED;
575
576 return aps->removeStreamDefaultEffect(id);
577}
578
Eric Laurent801a1182010-06-09 00:17:29 -0700579// -------------------------------------------------------------------------
580
581status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
582{
583 if (str == NULL || guid == NULL) {
584 return BAD_VALUE;
585 }
586
587 int tmp[10];
588
589 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
590 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
591 return BAD_VALUE;
592 }
593 guid->timeLow = (uint32_t)tmp[0];
594 guid->timeMid = (uint16_t)tmp[1];
595 guid->timeHiAndVersion = (uint16_t)tmp[2];
596 guid->clockSeq = (uint16_t)tmp[3];
597 guid->node[0] = (uint8_t)tmp[4];
598 guid->node[1] = (uint8_t)tmp[5];
599 guid->node[2] = (uint8_t)tmp[6];
600 guid->node[3] = (uint8_t)tmp[7];
601 guid->node[4] = (uint8_t)tmp[8];
602 guid->node[5] = (uint8_t)tmp[9];
603
604 return NO_ERROR;
605}
606
607status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
608{
609 if (guid == NULL || str == NULL) {
610 return BAD_VALUE;
611 }
612
613 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
614 guid->timeLow,
615 guid->timeMid,
616 guid->timeHiAndVersion,
617 guid->clockSeq,
618 guid->node[0],
619 guid->node[1],
620 guid->node[2],
621 guid->node[3],
622 guid->node[4],
623 guid->node[5]);
624
625 return NO_ERROR;
626}
627
628
Glenn Kasten40bc9062015-03-20 09:09:33 -0700629} // namespace android