blob: 12824744e432f1b15c012daf2142dbfbdb7a9dd7 [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
Eric Laurent801a1182010-06-09 00:17:29 -070026#include <binder/IPCThreadState.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070027#include <media/AudioEffect.h>
28#include <media/ShmemCompat.h>
29#include <private/media/AudioEffectShared.h>
30#include <utils/Log.h>
Eric Laurent801a1182010-06-09 00:17:29 -070031
32namespace android {
33
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070034using binder::Status;
35
36namespace {
37
38// Copy from a raw pointer + size into a vector of bytes.
39void appendToBuffer(const void* data,
40 size_t size,
41 std::vector<uint8_t>* buffer) {
42 const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
43 buffer->insert(buffer->end(), p, p + size);
44}
45
46} // namespace
47
Eric Laurent801a1182010-06-09 00:17:29 -070048// ---------------------------------------------------------------------------
49
Svet Ganovbe71aa22015-04-28 12:06:02 -070050AudioEffect::AudioEffect(const String16& opPackageName)
Mikhail Naganov416fffe2020-07-31 17:36:08 -070051 : mOpPackageName(opPackageName)
Eric Laurent801a1182010-06-09 00:17:29 -070052{
53}
54
Eric Laurent801a1182010-06-09 00:17:29 -070055status_t AudioEffect::set(const effect_uuid_t *type,
56 const effect_uuid_t *uuid,
57 int32_t priority,
58 effect_callback_t cbf,
59 void* user,
Glenn Kastend848eb42016-03-08 13:42:11 -080060 audio_session_t sessionId,
Eric Laurent94876032019-11-13 12:45:28 -080061 audio_io_handle_t io,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070062 const AudioDeviceTypeAddr& device,
63 bool probe)
Eric Laurent801a1182010-06-09 00:17:29 -070064{
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070065 sp<media::IEffect> iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -070066 sp<IMemory> cblk;
67 int enabled;
68
Steve Block3856b092011-10-20 11:56:00 +010069 ALOGV("set %p mUserData: %p uuid: %p timeLow %08x", this, user, type, type ? type->timeLow : 0);
Eric Laurent801a1182010-06-09 00:17:29 -070070
71 if (mIEffect != 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +000072 ALOGW("Effect already in use");
Eric Laurent801a1182010-06-09 00:17:29 -070073 return INVALID_OPERATION;
74 }
75
Eric Laurent94876032019-11-13 12:45:28 -080076 if (sessionId == AUDIO_SESSION_DEVICE && io != AUDIO_IO_HANDLE_NONE) {
77 ALOGW("IO handle should not be specified for device effect");
78 return BAD_VALUE;
79 }
Eric Laurent801a1182010-06-09 00:17:29 -070080 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
81 if (audioFlinger == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000082 ALOGE("set(): Could not get audioflinger");
Eric Laurent801a1182010-06-09 00:17:29 -070083 return NO_INIT;
84 }
85
86 if (type == NULL && uuid == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +000087 ALOGW("Must specify at least type or uuid");
Eric Laurent801a1182010-06-09 00:17:29 -070088 return BAD_VALUE;
89 }
Eric Laurent2fe0acd2020-03-13 14:30:46 -070090 mProbe = probe;
Eric Laurent801a1182010-06-09 00:17:29 -070091 mPriority = priority;
92 mCbf = cbf;
93 mUserData = user;
94 mSessionId = sessionId;
95
96 memset(&mDescriptor, 0, sizeof(effect_descriptor_t));
Glenn Kastena189a682012-02-20 12:16:30 -080097 mDescriptor.type = *(type != NULL ? type : EFFECT_UUID_NULL);
98 mDescriptor.uuid = *(uuid != NULL ? uuid : EFFECT_UUID_NULL);
Eric Laurent801a1182010-06-09 00:17:29 -070099
100 mIEffectClient = new EffectClient(this);
Eric Laurentb6436272016-12-07 19:24:50 -0800101 mClientPid = IPCThreadState::self()->getCallingPid();
Andy Hung8b0bfd92019-12-23 13:11:11 -0800102 mClientUid = IPCThreadState::self()->getCallingUid();
Eric Laurent801a1182010-06-09 00:17:29 -0700103
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800104 iEffect = audioFlinger->createEffect((effect_descriptor_t *)&mDescriptor,
Eric Laurent94876032019-11-13 12:45:28 -0800105 mIEffectClient, priority, io, mSessionId, device, mOpPackageName, mClientPid,
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700106 probe, &mStatus, &mId, &enabled);
Eric Laurent801a1182010-06-09 00:17:29 -0700107
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700108 // In probe mode, we stop here and return the status: the IEffect interface to
109 // audio flinger will not be retained. initCheck() will return the creation status
110 // but all other APIs will return invalid operation.
111 if (probe || iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000112 char typeBuffer[64] = {}, uuidBuffer[64] = {};
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700113 guidToString(type, typeBuffer, sizeof(typeBuffer));
114 guidToString(uuid, uuidBuffer, sizeof(uuidBuffer));
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700115 ALOGE_IF(!probe, "set(): AudioFlinger could not create effect %s / %s, status: %d",
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000116 type != nullptr ? typeBuffer : "NULL",
117 uuid != nullptr ? uuidBuffer : "NULL",
118 mStatus);
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700119 if (!probe && iEffect == 0) {
Eric Laurenteecd7652015-06-04 16:20:16 -0700120 mStatus = NO_INIT;
121 }
Eric Laurent801a1182010-06-09 00:17:29 -0700122 return mStatus;
123 }
124
125 mEnabled = (volatile int32_t)enabled;
126
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700127 if (media::SharedFileRegion shmem;
128 !iEffect->getCblk(&shmem).isOk()
129 || !convertSharedFileRegionToIMemory(shmem, &cblk)
130 || cblk == 0) {
Eric Laurent801a1182010-06-09 00:17:29 -0700131 mStatus = NO_INIT;
Steve Block29357bc2012-01-06 19:20:56 +0000132 ALOGE("Could not get control block");
Eric Laurent801a1182010-06-09 00:17:29 -0700133 return mStatus;
134 }
135
Eric Laurenteecd7652015-06-04 16:20:16 -0700136 mIEffect = iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -0700137 mCblkMemory = cblk;
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700138 // TODO: Using unsecurePointer() has some associated security pitfalls
139 // (see declaration for details).
140 // Either document why it is safe in this case or address the
141 // issue (e.g. by copying).
142 mCblk = static_cast<effect_param_cblk_t*>(cblk->unsecurePointer());
Eric Laurent801a1182010-06-09 00:17:29 -0700143 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
144 mCblk->buffer = (uint8_t *)mCblk + bufOffset;
145
Marco Nelissen06b46062014-11-14 07:58:25 -0800146 IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700147 ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId,
148 mStatus, mEnabled, mClientPid);
149
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800150 if (!audio_is_global_session(mSessionId)) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800151 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid, mClientUid);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700152 }
Eric Laurent801a1182010-06-09 00:17:29 -0700153
154 return mStatus;
155}
156
Mikhail Naganov416fffe2020-07-31 17:36:08 -0700157status_t AudioEffect::set(const char *typeStr,
158 const char *uuidStr,
159 int32_t priority,
160 effect_callback_t cbf,
161 void* user,
162 audio_session_t sessionId,
163 audio_io_handle_t io,
164 const AudioDeviceTypeAddr& device,
165 bool probe)
166{
167 effect_uuid_t type;
168 effect_uuid_t *pType = nullptr;
169 effect_uuid_t uuid;
170 effect_uuid_t *pUuid = nullptr;
171
172 ALOGV("AudioEffect::set string\n - type: %s\n - uuid: %s",
173 typeStr ? typeStr : "nullptr", uuidStr ? uuidStr : "nullptr");
174
175 if (stringToGuid(typeStr, &type) == NO_ERROR) {
176 pType = &type;
177 }
178 if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
179 pUuid = &uuid;
180 }
181
182 return set(pType, pUuid, priority, cbf, user, sessionId, io, device, probe);
183}
184
Eric Laurent801a1182010-06-09 00:17:29 -0700185
186AudioEffect::~AudioEffect()
187{
Steve Block3856b092011-10-20 11:56:00 +0100188 ALOGV("Destructor %p", this);
Eric Laurent801a1182010-06-09 00:17:29 -0700189
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700190 if (!mProbe && (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS)) {
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800191 if (!audio_is_global_session(mSessionId)) {
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700192 AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
193 }
Eric Laurent801a1182010-06-09 00:17:29 -0700194 if (mIEffect != NULL) {
195 mIEffect->disconnect();
Marco Nelissen06b46062014-11-14 07:58:25 -0800196 IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient);
Eric Laurent801a1182010-06-09 00:17:29 -0700197 }
Eric Laurenteecd7652015-06-04 16:20:16 -0700198 mIEffect.clear();
199 mCblkMemory.clear();
Eric Laurent801a1182010-06-09 00:17:29 -0700200 }
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700201 mIEffectClient.clear();
202 IPCThreadState::self()->flushCommands();
Eric Laurent801a1182010-06-09 00:17:29 -0700203}
204
205
206status_t AudioEffect::initCheck() const
207{
208 return mStatus;
209}
210
211// -------------------------------------------------------------------------
212
213effect_descriptor_t AudioEffect::descriptor() const
214{
215 return mDescriptor;
216}
217
Eric Laurentda7581b2010-07-02 08:12:41 -0700218bool AudioEffect::getEnabled() const
Eric Laurent801a1182010-06-09 00:17:29 -0700219{
220 return (mEnabled != 0);
221}
222
Eric Laurentda7581b2010-07-02 08:12:41 -0700223status_t AudioEffect::setEnabled(bool enabled)
Eric Laurent801a1182010-06-09 00:17:29 -0700224{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700225 if (mProbe) {
226 return INVALID_OPERATION;
227 }
Eric Laurent801a1182010-06-09 00:17:29 -0700228 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;
Eric Laurentf5aafb22010-11-18 08:40:16 -0800233 AutoMutex lock(mLock);
234 if (enabled != mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700235 Status bs;
236
Eric Laurentf5aafb22010-11-18 08:40:16 -0800237 if (enabled) {
Steve Block3856b092011-10-20 11:56:00 +0100238 ALOGV("enable %p", this);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700239 bs = mIEffect->enable(&status);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800240 } else {
Steve Block3856b092011-10-20 11:56:00 +0100241 ALOGV("disable %p", this);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700242 bs = mIEffect->disable(&status);
243 }
244 if (!bs.isOk()) {
245 status = bs.transactionError();
Eric Laurentda7581b2010-07-02 08:12:41 -0700246 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800247 if (status == NO_ERROR) {
248 mEnabled = enabled;
Eric Laurentda7581b2010-07-02 08:12:41 -0700249 }
Eric Laurent801a1182010-06-09 00:17:29 -0700250 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800251 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700252}
253
Eric Laurent25f43952010-07-28 05:40:18 -0700254status_t AudioEffect::command(uint32_t cmdCode,
255 uint32_t cmdSize,
256 void *cmdData,
257 uint32_t *replySize,
258 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700259{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700260 if (mProbe) {
261 return INVALID_OPERATION;
262 }
Eric Laurent801a1182010-06-09 00:17:29 -0700263 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
Steve Block3856b092011-10-20 11:56:00 +0100264 ALOGV("command() bad status %d", mStatus);
John Grossmanaf7d8182012-01-11 12:23:42 -0800265 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700266 }
267
Eric Laurentf5aafb22010-11-18 08:40:16 -0800268 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
269 if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
270 return NO_ERROR;
271 }
272 if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) {
273 return BAD_VALUE;
274 }
275 mLock.lock();
Eric Laurent0fa449c2010-09-24 11:52:04 -0700276 }
277
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700278 std::vector<uint8_t> data;
279 appendToBuffer(cmdData, cmdSize, &data);
280
281 status_t status;
282 std::vector<uint8_t> response;
283
284 Status bs = mIEffect->command(cmdCode, data, *replySize, &response, &status);
285 if (!bs.isOk()) {
286 status = bs.transactionError();
287 }
288 if (status == NO_ERROR) {
289 memcpy(replyData, response.data(), response.size());
290 *replySize = response.size();
291 }
Eric Laurent0fa449c2010-09-24 11:52:04 -0700292
293 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800294 if (status == NO_ERROR) {
295 status = *(status_t *)replyData;
Eric Laurent0fa449c2010-09-24 11:52:04 -0700296 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800297 if (status == NO_ERROR) {
298 mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700299 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800300 mLock.unlock();
Eric Laurent8569f0d2010-07-29 23:43:43 -0700301 }
302
Eric Laurent8569f0d2010-07-29 23:43:43 -0700303 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700304}
305
Eric Laurent801a1182010-06-09 00:17:29 -0700306status_t AudioEffect::setParameter(effect_param_t *param)
307{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700308 if (mProbe) {
309 return INVALID_OPERATION;
310 }
Eric Laurent801a1182010-06-09 00:17:29 -0700311 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800312 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700313 }
314
315 if (param == NULL || param->psize == 0 || param->vsize == 0) {
316 return BAD_VALUE;
317 }
318
Eric Laurent25f43952010-07-28 05:40:18 -0700319 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700320
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700321 ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data,
322 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700323
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700324 std::vector<uint8_t> cmd;
325 appendToBuffer(param, sizeof(effect_param_t) + psize, &cmd);
326 std::vector<uint8_t> response;
327 status_t status;
328 Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM,
329 cmd,
330 sizeof(int),
331 &response,
332 &status);
333 if (!bs.isOk()) {
334 status = bs.transactionError();
335 return status;
336 }
337 assert(response.size() == sizeof(int));
338 memcpy(&param->status, response.data(), response.size());
339 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700340}
341
342status_t AudioEffect::setParameterDeferred(effect_param_t *param)
343{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700344 if (mProbe) {
345 return INVALID_OPERATION;
346 }
Eric Laurent801a1182010-06-09 00:17:29 -0700347 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800348 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700349 }
350
351 if (param == NULL || param->psize == 0 || param->vsize == 0) {
352 return BAD_VALUE;
353 }
354
355 Mutex::Autolock _l(mCblk->lock);
356
357 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
358 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
359
360 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
361 return NO_MEMORY;
362 }
363 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
364 *p++ = size;
365 memcpy(p, param, sizeof(effect_param_t) + psize);
366 mCblk->clientIndex += size;
367
368 return NO_ERROR;
369}
370
371status_t AudioEffect::setParameterCommit()
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) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800377 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700378 }
379
380 Mutex::Autolock _l(mCblk->lock);
381 if (mCblk->clientIndex == 0) {
382 return INVALID_OPERATION;
383 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700384 std::vector<uint8_t> cmd;
385 std::vector<uint8_t> response;
386 status_t status;
387 Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT,
388 cmd,
389 0,
390 &response,
391 &status);
392 if (!bs.isOk()) {
393 status = bs.transactionError();
394 }
395 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700396}
397
398status_t AudioEffect::getParameter(effect_param_t *param)
399{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700400 if (mProbe) {
401 return INVALID_OPERATION;
402 }
Eric Laurent801a1182010-06-09 00:17:29 -0700403 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
John Grossmanaf7d8182012-01-11 12:23:42 -0800404 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700405 }
406
407 if (param == NULL || param->psize == 0 || param->vsize == 0) {
408 return BAD_VALUE;
409 }
410
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700411 ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data,
412 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700413
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700414 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
415 param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700416
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700417 status_t status;
418 std::vector<uint8_t> cmd;
419 std::vector<uint8_t> response;
420 appendToBuffer(param, sizeof(effect_param_t) + param->psize, &cmd);
421
422 Status bs = mIEffect->command(EFFECT_CMD_GET_PARAM, cmd, psize, &response, &status);
423 if (!bs.isOk()) {
424 status = bs.transactionError();
425 return status;
426 }
427 memcpy(param, response.data(), response.size());
428 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700429}
430
431
432// -------------------------------------------------------------------------
433
434void AudioEffect::binderDied()
435{
Steve Block5ff1dd52012-01-05 23:22:43 +0000436 ALOGW("IEffect died");
John Grossmanaf7d8182012-01-11 12:23:42 -0800437 mStatus = DEAD_OBJECT;
Glenn Kastena0d68332012-01-27 16:47:15 -0800438 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700439 status_t status = DEAD_OBJECT;
440 mCbf(EVENT_ERROR, mUserData, &status);
441 }
442 mIEffect.clear();
443}
444
445// -------------------------------------------------------------------------
446
447void AudioEffect::controlStatusChanged(bool controlGranted)
448{
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700449 ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf,
450 mUserData);
Eric Laurent801a1182010-06-09 00:17:29 -0700451 if (controlGranted) {
452 if (mStatus == ALREADY_EXISTS) {
453 mStatus = NO_ERROR;
454 }
455 } else {
456 if (mStatus == NO_ERROR) {
457 mStatus = ALREADY_EXISTS;
458 }
459 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800460 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700461 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
462 }
463}
464
465void AudioEffect::enableStatusChanged(bool enabled)
466{
Steve Block3856b092011-10-20 11:56:00 +0100467 ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
Eric Laurent801a1182010-06-09 00:17:29 -0700468 if (mStatus == ALREADY_EXISTS) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800469 mEnabled = enabled;
Glenn Kastena0d68332012-01-27 16:47:15 -0800470 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700471 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
472 }
473 }
474}
475
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700476void AudioEffect::commandExecuted(int32_t cmdCode,
477 const std::vector<uint8_t>& cmdData,
478 const std::vector<uint8_t>& replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700479{
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700480 if (cmdData.empty() || replyData.empty()) {
Eric Laurent801a1182010-06-09 00:17:29 -0700481 return;
482 }
483
Glenn Kastena0d68332012-01-27 16:47:15 -0800484 if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700485 std::vector<uint8_t> cmdDataCopy(cmdData);
486 effect_param_t* cmd = reinterpret_cast<effect_param_t *>(cmdDataCopy.data());
487 cmd->status = *reinterpret_cast<const int32_t *>(replyData.data());
Eric Laurent801a1182010-06-09 00:17:29 -0700488 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
489 }
490}
491
492// -------------------------------------------------------------------------
493
Eric Laurent801a1182010-06-09 00:17:29 -0700494status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
495{
496 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
497 if (af == 0) return PERMISSION_DENIED;
498 return af->queryNumberEffects(numEffects);
499}
500
Eric Laurentffe9c252010-06-23 17:38:20 -0700501status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700502{
503 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
504 if (af == 0) return PERMISSION_DENIED;
Eric Laurentffe9c252010-06-23 17:38:20 -0700505 return af->queryEffect(index, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700506}
507
Glenn Kasten5e92a782012-01-30 07:40:52 -0800508status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700509 const effect_uuid_t *type,
510 uint32_t preferredTypeFlag,
511 effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700512{
513 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
514 if (af == 0) return PERMISSION_DENIED;
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700515 return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700516}
517
Glenn Kastend848eb42016-03-08 13:42:11 -0800518status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700519 effect_descriptor_t *descriptors,
520 uint32_t *count)
521{
522 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
523 if (aps == 0) return PERMISSION_DENIED;
524 return aps->queryDefaultPreProcessing(audioSession, descriptors, count);
525}
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700526
527status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id)
528{
529 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
530 if (af == 0) return PERMISSION_DENIED;
531 *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
532 return NO_ERROR;
533}
534
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700535status_t AudioEffect::addSourceDefaultEffect(const char *typeStr,
536 const String16& opPackageName,
537 const char *uuidStr,
538 int32_t priority,
539 audio_source_t source,
540 audio_unique_id_t *id)
541{
542 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
543 if (aps == 0) return PERMISSION_DENIED;
544
545 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
546
547 // Convert type & uuid from string to effect_uuid_t.
548 effect_uuid_t type;
549 if (typeStr != NULL) {
550 status_t res = stringToGuid(typeStr, &type);
551 if (res != OK) return res;
552 } else {
553 type = *EFFECT_UUID_NULL;
554 }
555
556 effect_uuid_t uuid;
557 if (uuidStr != NULL) {
558 status_t res = stringToGuid(uuidStr, &uuid);
559 if (res != OK) return res;
560 } else {
561 uuid = *EFFECT_UUID_NULL;
562 }
563
564 return aps->addSourceDefaultEffect(&type, opPackageName, &uuid, priority, source, id);
565}
566
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700567status_t AudioEffect::addStreamDefaultEffect(const char *typeStr,
568 const String16& opPackageName,
569 const char *uuidStr,
570 int32_t priority,
571 audio_usage_t usage,
572 audio_unique_id_t *id)
573{
574 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
575 if (aps == 0) return PERMISSION_DENIED;
576
577 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
578
579 // Convert type & uuid from string to effect_uuid_t.
580 effect_uuid_t type;
581 if (typeStr != NULL) {
582 status_t res = stringToGuid(typeStr, &type);
583 if (res != OK) return res;
584 } else {
585 type = *EFFECT_UUID_NULL;
586 }
587
588 effect_uuid_t uuid;
589 if (uuidStr != NULL) {
590 status_t res = stringToGuid(uuidStr, &uuid);
591 if (res != OK) return res;
592 } else {
593 uuid = *EFFECT_UUID_NULL;
594 }
595
596 return aps->addStreamDefaultEffect(&type, opPackageName, &uuid, priority, usage, id);
597}
598
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700599status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id)
600{
601 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
602 if (aps == 0) return PERMISSION_DENIED;
603
604 return aps->removeSourceDefaultEffect(id);
605}
606
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700607status_t AudioEffect::removeStreamDefaultEffect(audio_unique_id_t id)
608{
609 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
610 if (aps == 0) return PERMISSION_DENIED;
611
612 return aps->removeStreamDefaultEffect(id);
613}
614
Eric Laurent801a1182010-06-09 00:17:29 -0700615// -------------------------------------------------------------------------
616
617status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
618{
619 if (str == NULL || guid == NULL) {
620 return BAD_VALUE;
621 }
622
623 int tmp[10];
624
625 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
626 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
627 return BAD_VALUE;
628 }
629 guid->timeLow = (uint32_t)tmp[0];
630 guid->timeMid = (uint16_t)tmp[1];
631 guid->timeHiAndVersion = (uint16_t)tmp[2];
632 guid->clockSeq = (uint16_t)tmp[3];
633 guid->node[0] = (uint8_t)tmp[4];
634 guid->node[1] = (uint8_t)tmp[5];
635 guid->node[2] = (uint8_t)tmp[6];
636 guid->node[3] = (uint8_t)tmp[7];
637 guid->node[4] = (uint8_t)tmp[8];
638 guid->node[5] = (uint8_t)tmp[9];
639
640 return NO_ERROR;
641}
642
643status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
644{
645 if (guid == NULL || str == NULL) {
646 return BAD_VALUE;
647 }
648
649 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
650 guid->timeLow,
651 guid->timeMid,
652 guid->timeHiAndVersion,
653 guid->clockSeq,
654 guid->node[0],
655 guid->node[1],
656 guid->node[2],
657 guid->node[3],
658 guid->node[4],
659 guid->node[5]);
660
661 return NO_ERROR;
662}
663
664
Glenn Kasten40bc9062015-03-20 09:09:33 -0700665} // namespace android