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