1 /*
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>
30 #include <binder/IPCThreadState.h>
31
32
33
34 namespace android {
35
36 // ---------------------------------------------------------------------------
37
AudioEffect()38 AudioEffect::AudioEffect()
39 : mStatus(NO_INIT)
40 {
41 }
42
43
AudioEffect(const effect_uuid_t * type,const effect_uuid_t * uuid,int32_t priority,effect_callback_t cbf,void * user,int sessionId,audio_io_handle_t io)44 AudioEffect::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,
50 audio_io_handle_t io
51 )
52 : mStatus(NO_INIT)
53 {
54 mStatus = set(type, uuid, priority, cbf, user, sessionId, io);
55 }
56
AudioEffect(const char * typeStr,const char * uuidStr,int32_t priority,effect_callback_t cbf,void * user,int sessionId,audio_io_handle_t io)57 AudioEffect::AudioEffect(const char *typeStr,
58 const char *uuidStr,
59 int32_t priority,
60 effect_callback_t cbf,
61 void* user,
62 int sessionId,
63 audio_io_handle_t io
64 )
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
72 ALOGV("Constructor string\n - type: %s\n - uuid: %s", typeStr, uuidStr);
73
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
86 mStatus = set(pType, pUuid, priority, cbf, user, sessionId, io);
87 }
88
set(const effect_uuid_t * type,const effect_uuid_t * uuid,int32_t priority,effect_callback_t cbf,void * user,int sessionId,audio_io_handle_t io)89 status_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,
95 audio_io_handle_t io)
96 {
97 sp<IEffect> iEffect;
98 sp<IMemory> cblk;
99 int enabled;
100
101 ALOGV("set %p mUserData: %p uuid: %p timeLow %08x", this, user, type, type ? type->timeLow : 0);
102
103 if (mIEffect != 0) {
104 ALOGW("Effect already in use");
105 return INVALID_OPERATION;
106 }
107
108 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
109 if (audioFlinger == 0) {
110 ALOGE("set(): Could not get audioflinger");
111 return NO_INIT;
112 }
113
114 if (type == NULL && uuid == NULL) {
115 ALOGW("Must specify at least type or uuid");
116 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));
125 mDescriptor.type = *(type != NULL ? type : EFFECT_UUID_NULL);
126 mDescriptor.uuid = *(uuid != NULL ? uuid : EFFECT_UUID_NULL);
127
128 mIEffectClient = new EffectClient(this);
129
130 iEffect = audioFlinger->createEffect(getpid(), &mDescriptor,
131 mIEffectClient, priority, io, mSessionId, &mStatus, &mId, &enabled);
132
133 if (iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
134 ALOGE("set(): AudioFlinger could not create effect, status: %d", mStatus);
135 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;
144 ALOGE("Could not get control block");
145 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);
155 ALOGV("set() %p OK effect: %s id: %d status %d enabled %d", this, mDescriptor.name, mId, mStatus, mEnabled);
156
157 return mStatus;
158 }
159
160
~AudioEffect()161 AudioEffect::~AudioEffect()
162 {
163 ALOGV("Destructor %p", this);
164
165 if (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS) {
166 if (mIEffect != NULL) {
167 mIEffect->disconnect();
168 mIEffect->asBinder()->unlinkToDeath(mIEffectClient);
169 }
170 IPCThreadState::self()->flushCommands();
171 }
172 mIEffect.clear();
173 mIEffectClient.clear();
174 mCblkMemory.clear();
175 }
176
177
initCheck() const178 status_t AudioEffect::initCheck() const
179 {
180 return mStatus;
181 }
182
183 // -------------------------------------------------------------------------
184
descriptor() const185 effect_descriptor_t AudioEffect::descriptor() const
186 {
187 return mDescriptor;
188 }
189
getEnabled() const190 bool AudioEffect::getEnabled() const
191 {
192 return (mEnabled != 0);
193 }
194
setEnabled(bool enabled)195 status_t AudioEffect::setEnabled(bool enabled)
196 {
197 if (mStatus != NO_ERROR) {
198 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
199 }
200
201 status_t status = NO_ERROR;
202
203 AutoMutex lock(mLock);
204 if (enabled != mEnabled) {
205 if (enabled) {
206 ALOGV("enable %p", this);
207 status = mIEffect->enable();
208 } else {
209 ALOGV("disable %p", this);
210 status = mIEffect->disable();
211 }
212 if (status == NO_ERROR) {
213 mEnabled = enabled;
214 }
215 }
216 return status;
217 }
218
command(uint32_t cmdCode,uint32_t cmdSize,void * cmdData,uint32_t * replySize,void * replyData)219 status_t AudioEffect::command(uint32_t cmdCode,
220 uint32_t cmdSize,
221 void *cmdData,
222 uint32_t *replySize,
223 void *replyData)
224 {
225 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
226 ALOGV("command() bad status %d", mStatus);
227 return mStatus;
228 }
229
230 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
231 if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
232 return NO_ERROR;
233 }
234 if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) {
235 return BAD_VALUE;
236 }
237 mLock.lock();
238 }
239
240 status_t status = mIEffect->command(cmdCode, cmdSize, cmdData, replySize, replyData);
241
242 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
243 if (status == NO_ERROR) {
244 status = *(status_t *)replyData;
245 }
246 if (status == NO_ERROR) {
247 mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
248 }
249 mLock.unlock();
250 }
251
252 return status;
253 }
254
255
setParameter(effect_param_t * param)256 status_t AudioEffect::setParameter(effect_param_t *param)
257 {
258 if (mStatus != NO_ERROR) {
259 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
260 }
261
262 if (param == NULL || param->psize == 0 || param->vsize == 0) {
263 return BAD_VALUE;
264 }
265
266 uint32_t size = sizeof(int);
267 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
268
269 ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data, (param->psize == 8) ? *((int *)param->data + 1): -1);
270
271 return mIEffect->command(EFFECT_CMD_SET_PARAM, sizeof (effect_param_t) + psize, param, &size, ¶m->status);
272 }
273
setParameterDeferred(effect_param_t * param)274 status_t AudioEffect::setParameterDeferred(effect_param_t *param)
275 {
276 if (mStatus != NO_ERROR) {
277 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
278 }
279
280 if (param == NULL || param->psize == 0 || param->vsize == 0) {
281 return BAD_VALUE;
282 }
283
284 Mutex::Autolock _l(mCblk->lock);
285
286 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
287 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
288
289 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
290 return NO_MEMORY;
291 }
292 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
293 *p++ = size;
294 memcpy(p, param, sizeof(effect_param_t) + psize);
295 mCblk->clientIndex += size;
296
297 return NO_ERROR;
298 }
299
setParameterCommit()300 status_t AudioEffect::setParameterCommit()
301 {
302 if (mStatus != NO_ERROR) {
303 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
304 }
305
306 Mutex::Autolock _l(mCblk->lock);
307 if (mCblk->clientIndex == 0) {
308 return INVALID_OPERATION;
309 }
310 uint32_t size = 0;
311 return mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT, 0, NULL, &size, NULL);
312 }
313
getParameter(effect_param_t * param)314 status_t AudioEffect::getParameter(effect_param_t *param)
315 {
316 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
317 return mStatus;
318 }
319
320 if (param == NULL || param->psize == 0 || param->vsize == 0) {
321 return BAD_VALUE;
322 }
323
324 ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data, (param->psize == 8) ? *((int *)param->data + 1): -1);
325
326 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
327
328 return mIEffect->command(EFFECT_CMD_GET_PARAM, sizeof(effect_param_t) + param->psize, param, &psize, param);
329 }
330
331
332 // -------------------------------------------------------------------------
333
binderDied()334 void AudioEffect::binderDied()
335 {
336 ALOGW("IEffect died");
337 mStatus = DEAD_OBJECT;
338 if (mCbf != NULL) {
339 status_t status = DEAD_OBJECT;
340 mCbf(EVENT_ERROR, mUserData, &status);
341 }
342 mIEffect.clear();
343 }
344
345 // -------------------------------------------------------------------------
346
controlStatusChanged(bool controlGranted)347 void AudioEffect::controlStatusChanged(bool controlGranted)
348 {
349 ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf, mUserData);
350 if (controlGranted) {
351 if (mStatus == ALREADY_EXISTS) {
352 mStatus = NO_ERROR;
353 }
354 } else {
355 if (mStatus == NO_ERROR) {
356 mStatus = ALREADY_EXISTS;
357 }
358 }
359 if (mCbf != NULL) {
360 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
361 }
362 }
363
enableStatusChanged(bool enabled)364 void AudioEffect::enableStatusChanged(bool enabled)
365 {
366 ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
367 if (mStatus == ALREADY_EXISTS) {
368 mEnabled = enabled;
369 if (mCbf != NULL) {
370 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
371 }
372 }
373 }
374
commandExecuted(uint32_t cmdCode,uint32_t cmdSize,void * cmdData,uint32_t replySize,void * replyData)375 void AudioEffect::commandExecuted(uint32_t cmdCode,
376 uint32_t cmdSize,
377 void *cmdData,
378 uint32_t replySize,
379 void *replyData)
380 {
381 if (cmdData == NULL || replyData == NULL) {
382 return;
383 }
384
385 if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
386 effect_param_t *cmd = (effect_param_t *)cmdData;
387 cmd->status = *(int32_t *)replyData;
388 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
389 }
390 }
391
392 // -------------------------------------------------------------------------
393
queryNumberEffects(uint32_t * numEffects)394 status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
395 {
396 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
397 if (af == 0) return PERMISSION_DENIED;
398 return af->queryNumberEffects(numEffects);
399 }
400
queryEffect(uint32_t index,effect_descriptor_t * descriptor)401 status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
402 {
403 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
404 if (af == 0) return PERMISSION_DENIED;
405 return af->queryEffect(index, descriptor);
406 }
407
getEffectDescriptor(const effect_uuid_t * uuid,effect_descriptor_t * descriptor)408 status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
409 effect_descriptor_t *descriptor) /*const*/
410 {
411 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
412 if (af == 0) return PERMISSION_DENIED;
413 return af->getEffectDescriptor(uuid, descriptor);
414 }
415
416
queryDefaultPreProcessing(int audioSession,effect_descriptor_t * descriptors,uint32_t * count)417 status_t AudioEffect::queryDefaultPreProcessing(int audioSession,
418 effect_descriptor_t *descriptors,
419 uint32_t *count)
420 {
421 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
422 if (aps == 0) return PERMISSION_DENIED;
423 return aps->queryDefaultPreProcessing(audioSession, descriptors, count);
424 }
425 // -------------------------------------------------------------------------
426
stringToGuid(const char * str,effect_uuid_t * guid)427 status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
428 {
429 if (str == NULL || guid == NULL) {
430 return BAD_VALUE;
431 }
432
433 int tmp[10];
434
435 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
436 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
437 return BAD_VALUE;
438 }
439 guid->timeLow = (uint32_t)tmp[0];
440 guid->timeMid = (uint16_t)tmp[1];
441 guid->timeHiAndVersion = (uint16_t)tmp[2];
442 guid->clockSeq = (uint16_t)tmp[3];
443 guid->node[0] = (uint8_t)tmp[4];
444 guid->node[1] = (uint8_t)tmp[5];
445 guid->node[2] = (uint8_t)tmp[6];
446 guid->node[3] = (uint8_t)tmp[7];
447 guid->node[4] = (uint8_t)tmp[8];
448 guid->node[5] = (uint8_t)tmp[9];
449
450 return NO_ERROR;
451 }
452
guidToString(const effect_uuid_t * guid,char * str,size_t maxLen)453 status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
454 {
455 if (guid == NULL || str == NULL) {
456 return BAD_VALUE;
457 }
458
459 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
460 guid->timeLow,
461 guid->timeMid,
462 guid->timeHiAndVersion,
463 guid->clockSeq,
464 guid->node[0],
465 guid->node[1],
466 guid->node[2],
467 guid->node[3],
468 guid->node[4],
469 guid->node[5]);
470
471 return NO_ERROR;
472 }
473
474
475 }; // namespace android
476