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(const String16 & opPackageName)38 AudioEffect::AudioEffect(const String16& opPackageName)
39     : mStatus(NO_INIT), mOpPackageName(opPackageName)
40 {
41 }
42 
43 
AudioEffect(const effect_uuid_t * type,const String16 & opPackageName,const effect_uuid_t * uuid,int32_t priority,effect_callback_t cbf,void * user,audio_session_t sessionId,audio_io_handle_t io,const AudioDeviceTypeAddr & device)44 AudioEffect::AudioEffect(const effect_uuid_t *type,
45                 const String16& opPackageName,
46                 const effect_uuid_t *uuid,
47                 int32_t priority,
48                 effect_callback_t cbf,
49                 void* user,
50                 audio_session_t sessionId,
51                 audio_io_handle_t io,
52                 const AudioDeviceTypeAddr& device
53                 )
54     : mStatus(NO_INIT), mOpPackageName(opPackageName)
55 {
56     AutoMutex lock(mConstructLock);
57     mStatus = set(type, uuid, priority, cbf, user, sessionId, io, device);
58 }
59 
AudioEffect(const char * typeStr,const String16 & opPackageName,const char * uuidStr,int32_t priority,effect_callback_t cbf,void * user,audio_session_t sessionId,audio_io_handle_t io,const AudioDeviceTypeAddr & device)60 AudioEffect::AudioEffect(const char *typeStr,
61                 const String16& opPackageName,
62                 const char *uuidStr,
63                 int32_t priority,
64                 effect_callback_t cbf,
65                 void* user,
66                 audio_session_t sessionId,
67                 audio_io_handle_t io,
68                 const AudioDeviceTypeAddr& device
69                 )
70     : mStatus(NO_INIT), mOpPackageName(opPackageName)
71 {
72     effect_uuid_t type;
73     effect_uuid_t *pType = NULL;
74     effect_uuid_t uuid;
75     effect_uuid_t *pUuid = NULL;
76 
77     ALOGV("Constructor string\n - type: %s\n - uuid: %s", typeStr, uuidStr);
78 
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 
91     AutoMutex lock(mConstructLock);
92     mStatus = set(pType, pUuid, priority, cbf, user, sessionId, io, device);
93 }
94 
set(const effect_uuid_t * type,const effect_uuid_t * uuid,int32_t priority,effect_callback_t cbf,void * user,audio_session_t sessionId,audio_io_handle_t io,const AudioDeviceTypeAddr & device)95 status_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,
100                 audio_session_t sessionId,
101                 audio_io_handle_t io,
102                 const AudioDeviceTypeAddr& device)
103 {
104     sp<IEffect> iEffect;
105     sp<IMemory> cblk;
106     int enabled;
107 
108     ALOGV("set %p mUserData: %p uuid: %p timeLow %08x", this, user, type, type ? type->timeLow : 0);
109 
110     if (mIEffect != 0) {
111         ALOGW("Effect already in use");
112         return INVALID_OPERATION;
113     }
114 
115     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     }
119     const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
120     if (audioFlinger == 0) {
121         ALOGE("set(): Could not get audioflinger");
122         return NO_INIT;
123     }
124 
125     if (type == NULL && uuid == NULL) {
126         ALOGW("Must specify at least type or uuid");
127         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));
136     mDescriptor.type = *(type != NULL ? type : EFFECT_UUID_NULL);
137     mDescriptor.uuid = *(uuid != NULL ? uuid : EFFECT_UUID_NULL);
138 
139     mIEffectClient = new EffectClient(this);
140     mClientPid = IPCThreadState::self()->getCallingPid();
141 
142     iEffect = audioFlinger->createEffect((effect_descriptor_t *)&mDescriptor,
143             mIEffectClient, priority, io, mSessionId, device, mOpPackageName, mClientPid,
144             &mStatus, &mId, &enabled);
145 
146     if (iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
147         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                 type != nullptr ? typeBuffer : "NULL",
152                 uuid != nullptr ? uuidBuffer : "NULL",
153                 mStatus);
154         if (iEffect == 0) {
155             mStatus = NO_INIT;
156         }
157         return mStatus;
158     }
159 
160     mEnabled = (volatile int32_t)enabled;
161 
162     cblk = iEffect->getCblk();
163     if (cblk == 0) {
164         mStatus = NO_INIT;
165         ALOGE("Could not get control block");
166         return mStatus;
167     }
168 
169     mIEffect = iEffect;
170     mCblkMemory = cblk;
171     mCblk = static_cast<effect_param_cblk_t*>(cblk->pointer());
172     int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
173     mCblk->buffer = (uint8_t *)mCblk + bufOffset;
174 
175     IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient);
176     ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId,
177             mStatus, mEnabled, mClientPid);
178 
179     if (!audio_is_global_session(mSessionId)) {
180         AudioSystem::acquireAudioSessionId(mSessionId, mClientPid);
181     }
182 
183     return mStatus;
184 }
185 
186 
~AudioEffect()187 AudioEffect::~AudioEffect()
188 {
189     ALOGV("Destructor %p", this);
190 
191     if (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS) {
192         if (!audio_is_global_session(mSessionId)) {
193             AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
194         }
195         if (mIEffect != NULL) {
196             mIEffect->disconnect();
197             IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient);
198         }
199         mIEffect.clear();
200         mCblkMemory.clear();
201         mIEffectClient.clear();
202         IPCThreadState::self()->flushCommands();
203     }
204 }
205 
206 
initCheck() const207 status_t AudioEffect::initCheck() const
208 {
209     return mStatus;
210 }
211 
212 // -------------------------------------------------------------------------
213 
descriptor() const214 effect_descriptor_t AudioEffect::descriptor() const
215 {
216     return mDescriptor;
217 }
218 
getEnabled() const219 bool AudioEffect::getEnabled() const
220 {
221     return (mEnabled != 0);
222 }
223 
setEnabled(bool enabled)224 status_t AudioEffect::setEnabled(bool enabled)
225 {
226     if (mStatus != NO_ERROR) {
227         return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
228     }
229 
230     status_t status = NO_ERROR;
231 
232     AutoMutex lock(mLock);
233     if (enabled != mEnabled) {
234         if (enabled) {
235             ALOGV("enable %p", this);
236             status = mIEffect->enable();
237         } else {
238             ALOGV("disable %p", this);
239             status = mIEffect->disable();
240         }
241         if (status == NO_ERROR) {
242             mEnabled = enabled;
243         }
244     }
245     return status;
246 }
247 
command(uint32_t cmdCode,uint32_t cmdSize,void * cmdData,uint32_t * replySize,void * replyData)248 status_t AudioEffect::command(uint32_t cmdCode,
249                               uint32_t cmdSize,
250                               void *cmdData,
251                               uint32_t *replySize,
252                               void *replyData)
253 {
254     if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
255         ALOGV("command() bad status %d", mStatus);
256         return mStatus;
257     }
258 
259     if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
260         if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
261             return NO_ERROR;
262         }
263         if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) {
264             return BAD_VALUE;
265         }
266         mLock.lock();
267     }
268 
269     status_t status = mIEffect->command(cmdCode, cmdSize, cmdData, replySize, replyData);
270 
271     if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
272         if (status == NO_ERROR) {
273             status = *(status_t *)replyData;
274         }
275         if (status == NO_ERROR) {
276             mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
277         }
278         mLock.unlock();
279     }
280 
281     return status;
282 }
283 
284 
setParameter(effect_param_t * param)285 status_t AudioEffect::setParameter(effect_param_t *param)
286 {
287     if (mStatus != NO_ERROR) {
288         return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
289     }
290 
291     if (param == NULL || param->psize == 0 || param->vsize == 0) {
292         return BAD_VALUE;
293     }
294 
295     uint32_t size = sizeof(int);
296     uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
297 
298     ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data,
299             (param->psize == 8) ? *((int *)param->data + 1): -1);
300 
301     return mIEffect->command(EFFECT_CMD_SET_PARAM, sizeof (effect_param_t) + psize, param, &size,
302             &param->status);
303 }
304 
setParameterDeferred(effect_param_t * param)305 status_t AudioEffect::setParameterDeferred(effect_param_t *param)
306 {
307     if (mStatus != NO_ERROR) {
308         return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
309     }
310 
311     if (param == NULL || param->psize == 0 || param->vsize == 0) {
312         return BAD_VALUE;
313     }
314 
315     Mutex::Autolock _l(mCblk->lock);
316 
317     int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
318     int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
319 
320     if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
321         return NO_MEMORY;
322     }
323     int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
324     *p++ = size;
325     memcpy(p, param, sizeof(effect_param_t) + psize);
326     mCblk->clientIndex += size;
327 
328     return NO_ERROR;
329 }
330 
setParameterCommit()331 status_t AudioEffect::setParameterCommit()
332 {
333     if (mStatus != NO_ERROR) {
334         return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
335     }
336 
337     Mutex::Autolock _l(mCblk->lock);
338     if (mCblk->clientIndex == 0) {
339         return INVALID_OPERATION;
340     }
341     uint32_t size = 0;
342     return mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT, 0, NULL, &size, NULL);
343 }
344 
getParameter(effect_param_t * param)345 status_t AudioEffect::getParameter(effect_param_t *param)
346 {
347     if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
348         return mStatus;
349     }
350 
351     if (param == NULL || param->psize == 0 || param->vsize == 0) {
352         return BAD_VALUE;
353     }
354 
355     ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data,
356             (param->psize == 8) ? *((int *)param->data + 1): -1);
357 
358     uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
359             param->vsize;
360 
361     return mIEffect->command(EFFECT_CMD_GET_PARAM, sizeof(effect_param_t) + param->psize, param,
362             &psize, param);
363 }
364 
365 
366 // -------------------------------------------------------------------------
367 
binderDied()368 void AudioEffect::binderDied()
369 {
370     ALOGW("IEffect died");
371     mStatus = DEAD_OBJECT;
372     if (mCbf != NULL) {
373         status_t status = DEAD_OBJECT;
374         mCbf(EVENT_ERROR, mUserData, &status);
375     }
376     mIEffect.clear();
377 }
378 
379 // -------------------------------------------------------------------------
380 
controlStatusChanged(bool controlGranted)381 void AudioEffect::controlStatusChanged(bool controlGranted)
382 {
383     ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf,
384             mUserData);
385     if (controlGranted) {
386         if (mStatus == ALREADY_EXISTS) {
387             mStatus = NO_ERROR;
388         }
389     } else {
390         if (mStatus == NO_ERROR) {
391             mStatus = ALREADY_EXISTS;
392         }
393     }
394     if (mCbf != NULL) {
395         mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
396     }
397 }
398 
enableStatusChanged(bool enabled)399 void AudioEffect::enableStatusChanged(bool enabled)
400 {
401     ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
402     if (mStatus == ALREADY_EXISTS) {
403         mEnabled = enabled;
404         if (mCbf != NULL) {
405             mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
406         }
407     }
408 }
409 
commandExecuted(uint32_t cmdCode,uint32_t cmdSize __unused,void * cmdData,uint32_t replySize __unused,void * replyData)410 void AudioEffect::commandExecuted(uint32_t cmdCode,
411                                   uint32_t cmdSize __unused,
412                                   void *cmdData,
413                                   uint32_t replySize __unused,
414                                   void *replyData)
415 {
416     if (cmdData == NULL || replyData == NULL) {
417         return;
418     }
419 
420     if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
421         effect_param_t *cmd = (effect_param_t *)cmdData;
422         cmd->status = *(int32_t *)replyData;
423         mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
424     }
425 }
426 
427 // -------------------------------------------------------------------------
428 
queryNumberEffects(uint32_t * numEffects)429 status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
430 {
431     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
432     if (af == 0) return PERMISSION_DENIED;
433     return af->queryNumberEffects(numEffects);
434 }
435 
queryEffect(uint32_t index,effect_descriptor_t * descriptor)436 status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
437 {
438     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
439     if (af == 0) return PERMISSION_DENIED;
440     return af->queryEffect(index, descriptor);
441 }
442 
getEffectDescriptor(const effect_uuid_t * uuid,const effect_uuid_t * type,uint32_t preferredTypeFlag,effect_descriptor_t * descriptor)443 status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
444                                           const effect_uuid_t *type,
445                                           uint32_t preferredTypeFlag,
446                                           effect_descriptor_t *descriptor)
447 {
448     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
449     if (af == 0) return PERMISSION_DENIED;
450     return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor);
451 }
452 
queryDefaultPreProcessing(audio_session_t audioSession,effect_descriptor_t * descriptors,uint32_t * count)453 status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession,
454                                           effect_descriptor_t *descriptors,
455                                           uint32_t *count)
456 {
457     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
458     if (aps == 0) return PERMISSION_DENIED;
459     return aps->queryDefaultPreProcessing(audioSession, descriptors, count);
460 }
461 
newEffectUniqueId(audio_unique_id_t * id)462 status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id)
463 {
464     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
465     if (af == 0) return PERMISSION_DENIED;
466     *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
467     return NO_ERROR;
468 }
469 
addSourceDefaultEffect(const char * typeStr,const String16 & opPackageName,const char * uuidStr,int32_t priority,audio_source_t source,audio_unique_id_t * id)470 status_t AudioEffect::addSourceDefaultEffect(const char *typeStr,
471                                              const String16& opPackageName,
472                                              const char *uuidStr,
473                                              int32_t priority,
474                                              audio_source_t source,
475                                              audio_unique_id_t *id)
476 {
477     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
478     if (aps == 0) return PERMISSION_DENIED;
479 
480     if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
481 
482     // Convert type & uuid from string to effect_uuid_t.
483     effect_uuid_t type;
484     if (typeStr != NULL) {
485         status_t res = stringToGuid(typeStr, &type);
486         if (res != OK) return res;
487     } else {
488         type = *EFFECT_UUID_NULL;
489     }
490 
491     effect_uuid_t uuid;
492     if (uuidStr != NULL) {
493         status_t res = stringToGuid(uuidStr, &uuid);
494         if (res != OK) return res;
495     } else {
496         uuid = *EFFECT_UUID_NULL;
497     }
498 
499     return aps->addSourceDefaultEffect(&type, opPackageName, &uuid, priority, source, id);
500 }
501 
addStreamDefaultEffect(const char * typeStr,const String16 & opPackageName,const char * uuidStr,int32_t priority,audio_usage_t usage,audio_unique_id_t * id)502 status_t AudioEffect::addStreamDefaultEffect(const char *typeStr,
503                                              const String16& opPackageName,
504                                              const char *uuidStr,
505                                              int32_t priority,
506                                              audio_usage_t usage,
507                                              audio_unique_id_t *id)
508 {
509     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
510     if (aps == 0) return PERMISSION_DENIED;
511 
512     if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
513 
514     // Convert type & uuid from string to effect_uuid_t.
515     effect_uuid_t type;
516     if (typeStr != NULL) {
517         status_t res = stringToGuid(typeStr, &type);
518         if (res != OK) return res;
519     } else {
520         type = *EFFECT_UUID_NULL;
521     }
522 
523     effect_uuid_t uuid;
524     if (uuidStr != NULL) {
525         status_t res = stringToGuid(uuidStr, &uuid);
526         if (res != OK) return res;
527     } else {
528         uuid = *EFFECT_UUID_NULL;
529     }
530 
531     return aps->addStreamDefaultEffect(&type, opPackageName, &uuid, priority, usage, id);
532 }
533 
removeSourceDefaultEffect(audio_unique_id_t id)534 status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id)
535 {
536     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
537     if (aps == 0) return PERMISSION_DENIED;
538 
539     return aps->removeSourceDefaultEffect(id);
540 }
541 
removeStreamDefaultEffect(audio_unique_id_t id)542 status_t AudioEffect::removeStreamDefaultEffect(audio_unique_id_t id)
543 {
544     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
545     if (aps == 0) return PERMISSION_DENIED;
546 
547     return aps->removeStreamDefaultEffect(id);
548 }
549 
550 // -------------------------------------------------------------------------
551 
stringToGuid(const char * str,effect_uuid_t * guid)552 status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
553 {
554     if (str == NULL || guid == NULL) {
555         return BAD_VALUE;
556     }
557 
558     int tmp[10];
559 
560     if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
561             tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
562         return BAD_VALUE;
563     }
564     guid->timeLow = (uint32_t)tmp[0];
565     guid->timeMid = (uint16_t)tmp[1];
566     guid->timeHiAndVersion = (uint16_t)tmp[2];
567     guid->clockSeq = (uint16_t)tmp[3];
568     guid->node[0] = (uint8_t)tmp[4];
569     guid->node[1] = (uint8_t)tmp[5];
570     guid->node[2] = (uint8_t)tmp[6];
571     guid->node[3] = (uint8_t)tmp[7];
572     guid->node[4] = (uint8_t)tmp[8];
573     guid->node[5] = (uint8_t)tmp[9];
574 
575     return NO_ERROR;
576 }
577 
guidToString(const effect_uuid_t * guid,char * str,size_t maxLen)578 status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
579 {
580     if (guid == NULL || str == NULL) {
581         return BAD_VALUE;
582     }
583 
584     snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
585             guid->timeLow,
586             guid->timeMid,
587             guid->timeHiAndVersion,
588             guid->clockSeq,
589             guid->node[0],
590             guid->node[1],
591             guid->node[2],
592             guid->node[3],
593             guid->node[4],
594             guid->node[5]);
595 
596     return NO_ERROR;
597 }
598 
599 
600 } // namespace android
601