blob: 8dfffb3a39e20b9d4f523b5aa9a857ecb1d7a37a [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
38AudioEffect::AudioEffect()
39 : mStatus(NO_INIT)
40{
41}
42
43
44AudioEffect::AudioEffect(const effect_uuid_t *type,
45 const effect_uuid_t *uuid,
46 int32_t priority,
47 effect_callback_t cbf,
48 void* user,
49 int sessionId,
Eric Laurent7c7f10b2011-06-17 21:29:58 -070050 audio_io_handle_t io
Eric Laurent801a1182010-06-09 00:17:29 -070051 )
52 : mStatus(NO_INIT)
53{
Eric Laurent7c7f10b2011-06-17 21:29:58 -070054 mStatus = set(type, uuid, priority, cbf, user, sessionId, io);
Eric Laurent801a1182010-06-09 00:17:29 -070055}
56
57AudioEffect::AudioEffect(const char *typeStr,
58 const char *uuidStr,
59 int32_t priority,
60 effect_callback_t cbf,
61 void* user,
62 int sessionId,
Eric Laurent7c7f10b2011-06-17 21:29:58 -070063 audio_io_handle_t io
Eric Laurent801a1182010-06-09 00:17:29 -070064 )
65 : mStatus(NO_INIT)
66{
67 effect_uuid_t type;
68 effect_uuid_t *pType = NULL;
69 effect_uuid_t uuid;
70 effect_uuid_t *pUuid = NULL;
71
Steve Block3856b092011-10-20 11:56:00 +010072 ALOGV("Constructor string\n - type: %s\n - uuid: %s", typeStr, uuidStr);
Eric Laurent801a1182010-06-09 00:17:29 -070073
74 if (typeStr != NULL) {
75 if (stringToGuid(typeStr, &type) == NO_ERROR) {
76 pType = &type;
77 }
78 }
79
80 if (uuidStr != NULL) {
81 if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
82 pUuid = &uuid;
83 }
84 }
85
Eric Laurent7c7f10b2011-06-17 21:29:58 -070086 mStatus = set(pType, pUuid, priority, cbf, user, sessionId, io);
Eric Laurent801a1182010-06-09 00:17:29 -070087}
88
89status_t AudioEffect::set(const effect_uuid_t *type,
90 const effect_uuid_t *uuid,
91 int32_t priority,
92 effect_callback_t cbf,
93 void* user,
94 int sessionId,
Eric Laurent7c7f10b2011-06-17 21:29:58 -070095 audio_io_handle_t io)
Eric Laurent801a1182010-06-09 00:17:29 -070096{
97 sp<IEffect> iEffect;
98 sp<IMemory> cblk;
99 int enabled;
100
Steve Block3856b092011-10-20 11:56:00 +0100101 ALOGV("set %p mUserData: %p uuid: %p timeLow %08x", this, user, type, type ? type->timeLow : 0);
Eric Laurent801a1182010-06-09 00:17:29 -0700102
103 if (mIEffect != 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000104 ALOGW("Effect already in use");
Eric Laurent801a1182010-06-09 00:17:29 -0700105 return INVALID_OPERATION;
106 }
107
108 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
109 if (audioFlinger == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000110 ALOGE("set(): Could not get audioflinger");
Eric Laurent801a1182010-06-09 00:17:29 -0700111 return NO_INIT;
112 }
113
114 if (type == NULL && uuid == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000115 ALOGW("Must specify at least type or uuid");
Eric Laurent801a1182010-06-09 00:17:29 -0700116 return BAD_VALUE;
117 }
118
119 mPriority = priority;
120 mCbf = cbf;
121 mUserData = user;
122 mSessionId = sessionId;
123
124 memset(&mDescriptor, 0, sizeof(effect_descriptor_t));
Glenn Kastena189a682012-02-20 12:16:30 -0800125 mDescriptor.type = *(type != NULL ? type : EFFECT_UUID_NULL);
126 mDescriptor.uuid = *(uuid != NULL ? uuid : EFFECT_UUID_NULL);
Eric Laurent801a1182010-06-09 00:17:29 -0700127
128 mIEffectClient = new EffectClient(this);
129
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800130 iEffect = audioFlinger->createEffect((effect_descriptor_t *)&mDescriptor,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700131 mIEffectClient, priority, io, mSessionId, &mStatus, &mId, &enabled);
Eric Laurent801a1182010-06-09 00:17:29 -0700132
133 if (iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
Steve Block29357bc2012-01-06 19:20:56 +0000134 ALOGE("set(): AudioFlinger could not create effect, status: %d", mStatus);
Eric Laurent801a1182010-06-09 00:17:29 -0700135 return mStatus;
136 }
137
138 mEnabled = (volatile int32_t)enabled;
139
140 mIEffect = iEffect;
141 cblk = iEffect->getCblk();
142 if (cblk == 0) {
143 mStatus = NO_INIT;
Steve Block29357bc2012-01-06 19:20:56 +0000144 ALOGE("Could not get control block");
Eric Laurent801a1182010-06-09 00:17:29 -0700145 return mStatus;
146 }
147
148 mIEffect = iEffect;
149 mCblkMemory = cblk;
150 mCblk = static_cast<effect_param_cblk_t*>(cblk->pointer());
151 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
152 mCblk->buffer = (uint8_t *)mCblk + bufOffset;
153
154 iEffect->asBinder()->linkToDeath(mIEffectClient);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700155 ALOGV("set() %p OK effect: %s id: %d status %d enabled %d", this, mDescriptor.name, mId,
156 mStatus, mEnabled);
Eric Laurent801a1182010-06-09 00:17:29 -0700157
158 return mStatus;
159}
160
161
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 Laurent801a1182010-06-09 00:17:29 -0700167 if (mIEffect != NULL) {
168 mIEffect->disconnect();
169 mIEffect->asBinder()->unlinkToDeath(mIEffectClient);
170 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700171 IPCThreadState::self()->flushCommands();
Eric Laurent801a1182010-06-09 00:17:29 -0700172 }
173 mIEffect.clear();
174 mIEffectClient.clear();
175 mCblkMemory.clear();
176}
177
178
179status_t AudioEffect::initCheck() const
180{
181 return mStatus;
182}
183
184// -------------------------------------------------------------------------
185
186effect_descriptor_t AudioEffect::descriptor() const
187{
188 return mDescriptor;
189}
190
Eric Laurentda7581b2010-07-02 08:12:41 -0700191bool AudioEffect::getEnabled() const
Eric Laurent801a1182010-06-09 00:17:29 -0700192{
193 return (mEnabled != 0);
194}
195
Eric Laurentda7581b2010-07-02 08:12:41 -0700196status_t AudioEffect::setEnabled(bool enabled)
Eric Laurent801a1182010-06-09 00:17:29 -0700197{
198 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800199 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700200 }
Eric Laurent801a1182010-06-09 00:17:29 -0700201
Eric Laurentf5aafb22010-11-18 08:40:16 -0800202 status_t status = NO_ERROR;
203
204 AutoMutex lock(mLock);
205 if (enabled != mEnabled) {
206 if (enabled) {
Steve Block3856b092011-10-20 11:56:00 +0100207 ALOGV("enable %p", this);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800208 status = mIEffect->enable();
209 } else {
Steve Block3856b092011-10-20 11:56:00 +0100210 ALOGV("disable %p", this);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800211 status = mIEffect->disable();
Eric Laurentda7581b2010-07-02 08:12:41 -0700212 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800213 if (status == NO_ERROR) {
214 mEnabled = enabled;
Eric Laurentda7581b2010-07-02 08:12:41 -0700215 }
Eric Laurent801a1182010-06-09 00:17:29 -0700216 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800217 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700218}
219
Eric Laurent25f43952010-07-28 05:40:18 -0700220status_t AudioEffect::command(uint32_t cmdCode,
221 uint32_t cmdSize,
222 void *cmdData,
223 uint32_t *replySize,
224 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700225{
226 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
Steve Block3856b092011-10-20 11:56:00 +0100227 ALOGV("command() bad status %d", mStatus);
John Grossmanaf7d8182012-01-11 12:23:42 -0800228 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700229 }
230
Eric Laurentf5aafb22010-11-18 08:40:16 -0800231 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
232 if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
233 return NO_ERROR;
234 }
235 if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) {
236 return BAD_VALUE;
237 }
238 mLock.lock();
Eric Laurent0fa449c2010-09-24 11:52:04 -0700239 }
240
Eric Laurent8569f0d2010-07-29 23:43:43 -0700241 status_t status = mIEffect->command(cmdCode, cmdSize, cmdData, replySize, replyData);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700242
243 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800244 if (status == NO_ERROR) {
245 status = *(status_t *)replyData;
Eric Laurent0fa449c2010-09-24 11:52:04 -0700246 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800247 if (status == NO_ERROR) {
248 mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700249 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800250 mLock.unlock();
Eric Laurent8569f0d2010-07-29 23:43:43 -0700251 }
252
Eric Laurent8569f0d2010-07-29 23:43:43 -0700253 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700254}
255
256
257status_t AudioEffect::setParameter(effect_param_t *param)
258{
259 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800260 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700261 }
262
263 if (param == NULL || param->psize == 0 || param->vsize == 0) {
264 return BAD_VALUE;
265 }
266
Eric Laurent25f43952010-07-28 05:40:18 -0700267 uint32_t size = sizeof(int);
268 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700269
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700270 ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data,
271 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700272
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700273 return mIEffect->command(EFFECT_CMD_SET_PARAM, sizeof (effect_param_t) + psize, param, &size,
274 &param->status);
Eric Laurent801a1182010-06-09 00:17:29 -0700275}
276
277status_t AudioEffect::setParameterDeferred(effect_param_t *param)
278{
279 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800280 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700281 }
282
283 if (param == NULL || param->psize == 0 || param->vsize == 0) {
284 return BAD_VALUE;
285 }
286
287 Mutex::Autolock _l(mCblk->lock);
288
289 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
290 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
291
292 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
293 return NO_MEMORY;
294 }
295 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
296 *p++ = size;
297 memcpy(p, param, sizeof(effect_param_t) + psize);
298 mCblk->clientIndex += size;
299
300 return NO_ERROR;
301}
302
303status_t AudioEffect::setParameterCommit()
304{
305 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800306 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700307 }
308
309 Mutex::Autolock _l(mCblk->lock);
310 if (mCblk->clientIndex == 0) {
311 return INVALID_OPERATION;
312 }
Eric Laurent25f43952010-07-28 05:40:18 -0700313 uint32_t size = 0;
Eric Laurent801a1182010-06-09 00:17:29 -0700314 return mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT, 0, NULL, &size, NULL);
315}
316
317status_t AudioEffect::getParameter(effect_param_t *param)
318{
319 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
John Grossmanaf7d8182012-01-11 12:23:42 -0800320 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700321 }
322
323 if (param == NULL || param->psize == 0 || param->vsize == 0) {
324 return BAD_VALUE;
325 }
326
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700327 ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data,
328 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700329
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700330 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
331 param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700332
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700333 return mIEffect->command(EFFECT_CMD_GET_PARAM, sizeof(effect_param_t) + param->psize, param,
334 &psize, param);
Eric Laurent801a1182010-06-09 00:17:29 -0700335}
336
337
338// -------------------------------------------------------------------------
339
340void AudioEffect::binderDied()
341{
Steve Block5ff1dd52012-01-05 23:22:43 +0000342 ALOGW("IEffect died");
John Grossmanaf7d8182012-01-11 12:23:42 -0800343 mStatus = DEAD_OBJECT;
Glenn Kastena0d68332012-01-27 16:47:15 -0800344 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700345 status_t status = DEAD_OBJECT;
346 mCbf(EVENT_ERROR, mUserData, &status);
347 }
348 mIEffect.clear();
349}
350
351// -------------------------------------------------------------------------
352
353void AudioEffect::controlStatusChanged(bool controlGranted)
354{
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700355 ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf,
356 mUserData);
Eric Laurent801a1182010-06-09 00:17:29 -0700357 if (controlGranted) {
358 if (mStatus == ALREADY_EXISTS) {
359 mStatus = NO_ERROR;
360 }
361 } else {
362 if (mStatus == NO_ERROR) {
363 mStatus = ALREADY_EXISTS;
364 }
365 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800366 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700367 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
368 }
369}
370
371void AudioEffect::enableStatusChanged(bool enabled)
372{
Steve Block3856b092011-10-20 11:56:00 +0100373 ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
Eric Laurent801a1182010-06-09 00:17:29 -0700374 if (mStatus == ALREADY_EXISTS) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800375 mEnabled = enabled;
Glenn Kastena0d68332012-01-27 16:47:15 -0800376 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700377 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
378 }
379 }
380}
381
Eric Laurent25f43952010-07-28 05:40:18 -0700382void AudioEffect::commandExecuted(uint32_t cmdCode,
383 uint32_t cmdSize,
384 void *cmdData,
385 uint32_t replySize,
386 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700387{
388 if (cmdData == NULL || replyData == NULL) {
389 return;
390 }
391
Glenn Kastena0d68332012-01-27 16:47:15 -0800392 if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
Eric Laurent801a1182010-06-09 00:17:29 -0700393 effect_param_t *cmd = (effect_param_t *)cmdData;
394 cmd->status = *(int32_t *)replyData;
395 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
396 }
397}
398
399// -------------------------------------------------------------------------
400
Eric Laurent801a1182010-06-09 00:17:29 -0700401status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
402{
403 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
404 if (af == 0) return PERMISSION_DENIED;
405 return af->queryNumberEffects(numEffects);
406}
407
Eric Laurentffe9c252010-06-23 17:38:20 -0700408status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700409{
410 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
411 if (af == 0) return PERMISSION_DENIED;
Eric Laurentffe9c252010-06-23 17:38:20 -0700412 return af->queryEffect(index, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700413}
414
Glenn Kasten5e92a782012-01-30 07:40:52 -0800415status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -0800416 effect_descriptor_t *descriptor) /*const*/
Eric Laurent801a1182010-06-09 00:17:29 -0700417{
418 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
419 if (af == 0) return PERMISSION_DENIED;
420 return af->getEffectDescriptor(uuid, descriptor);
421}
422
Eric Laurent57dae992011-07-24 13:36:09 -0700423
424status_t AudioEffect::queryDefaultPreProcessing(int audioSession,
425 effect_descriptor_t *descriptors,
426 uint32_t *count)
427{
428 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
429 if (aps == 0) return PERMISSION_DENIED;
430 return aps->queryDefaultPreProcessing(audioSession, descriptors, count);
431}
Eric Laurent801a1182010-06-09 00:17:29 -0700432// -------------------------------------------------------------------------
433
434status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
435{
436 if (str == NULL || guid == NULL) {
437 return BAD_VALUE;
438 }
439
440 int tmp[10];
441
442 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
443 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
444 return BAD_VALUE;
445 }
446 guid->timeLow = (uint32_t)tmp[0];
447 guid->timeMid = (uint16_t)tmp[1];
448 guid->timeHiAndVersion = (uint16_t)tmp[2];
449 guid->clockSeq = (uint16_t)tmp[3];
450 guid->node[0] = (uint8_t)tmp[4];
451 guid->node[1] = (uint8_t)tmp[5];
452 guid->node[2] = (uint8_t)tmp[6];
453 guid->node[3] = (uint8_t)tmp[7];
454 guid->node[4] = (uint8_t)tmp[8];
455 guid->node[5] = (uint8_t)tmp[9];
456
457 return NO_ERROR;
458}
459
460status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
461{
462 if (guid == NULL || str == NULL) {
463 return BAD_VALUE;
464 }
465
466 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
467 guid->timeLow,
468 guid->timeMid,
469 guid->timeHiAndVersion,
470 guid->clockSeq,
471 guid->node[0],
472 guid->node[1],
473 guid->node[2],
474 guid->node[3],
475 guid->node[4],
476 guid->node[5]);
477
478 return NO_ERROR;
479}
480
481
482}; // namespace android