3 ** Copyright 2010, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
19 //#define LOG_NDEBUG 0
20 #define LOG_TAG "AudioEffect"
23 #include <sys/types.h>
26 #include <private/media/AudioEffectShared.h>
27 #include <media/AudioEffect.h>
29 #include <utils/Log.h>
30 #include <cutils/atomic.h>
31 #include <binder/IPCThreadState.h>
37 // ---------------------------------------------------------------------------
39 AudioEffect::AudioEffect()
45 AudioEffect::AudioEffect(const effect_uuid_t *type,
46 const effect_uuid_t *uuid,
48 effect_callback_t cbf,
51 audio_io_handle_t output
55 mStatus = set(type, uuid, priority, cbf, user, sessionId, output);
58 AudioEffect::AudioEffect(const char *typeStr,
61 effect_callback_t cbf,
64 audio_io_handle_t output
69 effect_uuid_t *pType = NULL;
71 effect_uuid_t *pUuid = NULL;
73 LOGV("Constructor string\n - type: %s\n - uuid: %s", typeStr, uuidStr);
75 if (typeStr != NULL) {
76 if (stringToGuid(typeStr, &type) == NO_ERROR) {
81 if (uuidStr != NULL) {
82 if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
87 mStatus = set(pType, pUuid, priority, cbf, user, sessionId, output);
90 status_t AudioEffect::set(const effect_uuid_t *type,
91 const effect_uuid_t *uuid,
93 effect_callback_t cbf,
96 audio_io_handle_t output)
102 LOGV("set %p mUserData: %p", this, user);
105 LOGW("Effect already in use");
106 return INVALID_OPERATION;
109 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
110 if (audioFlinger == 0) {
111 LOGE("set(): Could not get audioflinger");
115 if (type == NULL && uuid == NULL) {
116 LOGW("Must specify at least type or uuid");
120 mPriority = priority;
123 mSessionId = sessionId;
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));
130 memcpy(&mDescriptor.type, type, sizeof(effect_uuid_t));
133 memcpy(&mDescriptor.uuid, uuid, sizeof(effect_uuid_t));
136 mIEffectClient = new EffectClient(this);
138 iEffect = audioFlinger->createEffect(getpid(), (effect_descriptor_t *)&mDescriptor,
139 mIEffectClient, priority, output, mSessionId, &mStatus, &mId, &enabled);
141 if (iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
142 LOGE("set(): AudioFlinger could not create effect, status: %d", mStatus);
146 mEnabled = (volatile int32_t)enabled;
149 cblk = iEffect->getCblk();
152 LOGE("Could not get control block");
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;
162 iEffect->asBinder()->linkToDeath(mIEffectClient);
163 LOGV("set() %p OK effect: %s id: %d status %d enabled %d, ", this, mDescriptor.name, mId, mStatus, mEnabled);
169 AudioEffect::~AudioEffect()
171 LOGV("Destructor %p", this);
173 if (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS) {
175 if (mIEffect != NULL) {
176 mIEffect->disconnect();
177 mIEffect->asBinder()->unlinkToDeath(mIEffectClient);
179 IPCThreadState::self()->flushCommands();
182 mIEffectClient.clear();
187 status_t AudioEffect::initCheck() const
192 // -------------------------------------------------------------------------
194 effect_descriptor_t AudioEffect::descriptor() const
199 bool AudioEffect::getEnabled() const
201 return (mEnabled != 0);
204 status_t AudioEffect::setEnabled(bool enabled)
206 if (mStatus != NO_ERROR) {
207 return INVALID_OPERATION;
211 LOGV("enable %p", this);
212 if (android_atomic_or(1, &mEnabled) == 0) {
213 return mIEffect->enable();
216 LOGV("disable %p", this);
217 if (android_atomic_and(~1, &mEnabled) == 1) {
218 return mIEffect->disable();
224 status_t AudioEffect::command(uint32_t cmdCode,
230 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
231 LOGV("command() bad status %d", mStatus);
232 return INVALID_OPERATION;
235 if ((cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) &&
236 (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL)) {
240 status_t status = mIEffect->command(cmdCode, cmdSize, cmdData, replySize, replyData);
241 if (status != NO_ERROR) {
245 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
246 status = *(status_t *)replyData;
247 if (status != NO_ERROR) {
250 if (cmdCode == EFFECT_CMD_ENABLE) {
251 android_atomic_or(1, &mEnabled);
253 android_atomic_and(~1, &mEnabled);
261 status_t AudioEffect::setParameter(effect_param_t *param)
263 if (mStatus != NO_ERROR) {
264 return INVALID_OPERATION;
267 if (param == NULL || param->psize == 0 || param->vsize == 0) {
271 uint32_t size = sizeof(int);
272 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
274 LOGV("setParameter: param: %d, param2: %d", *(int *)param->data, (param->psize == 8) ? *((int *)param->data + 1): -1);
276 return mIEffect->command(EFFECT_CMD_SET_PARAM, sizeof (effect_param_t) + psize, param, &size, ¶m->status);
279 status_t AudioEffect::setParameterDeferred(effect_param_t *param)
281 if (mStatus != NO_ERROR) {
282 return INVALID_OPERATION;
285 if (param == NULL || param->psize == 0 || param->vsize == 0) {
289 Mutex::Autolock _l(mCblk->lock);
291 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
292 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
294 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
297 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
299 memcpy(p, param, sizeof(effect_param_t) + psize);
300 mCblk->clientIndex += size;
305 status_t AudioEffect::setParameterCommit()
307 if (mStatus != NO_ERROR) {
308 return INVALID_OPERATION;
311 Mutex::Autolock _l(mCblk->lock);
312 if (mCblk->clientIndex == 0) {
313 return INVALID_OPERATION;
316 return mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT, 0, NULL, &size, NULL);
319 status_t AudioEffect::getParameter(effect_param_t *param)
321 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
322 return INVALID_OPERATION;
325 if (param == NULL || param->psize == 0 || param->vsize == 0) {
329 LOGV("getParameter: param: %d, param2: %d", *(int *)param->data, (param->psize == 8) ? *((int *)param->data + 1): -1);
331 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
333 return mIEffect->command(EFFECT_CMD_GET_PARAM, sizeof(effect_param_t) + param->psize, param, &psize, param);
337 // -------------------------------------------------------------------------
339 void AudioEffect::binderDied()
341 LOGW("IEffect died");
344 status_t status = DEAD_OBJECT;
345 mCbf(EVENT_ERROR, mUserData, &status);
350 // -------------------------------------------------------------------------
352 void AudioEffect::controlStatusChanged(bool controlGranted)
354 LOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf, mUserData);
355 if (controlGranted) {
356 if (mStatus == ALREADY_EXISTS) {
360 if (mStatus == NO_ERROR) {
361 mStatus = ALREADY_EXISTS;
365 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
369 void AudioEffect::enableStatusChanged(bool enabled)
371 LOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
372 if (mStatus == ALREADY_EXISTS) {
374 android_atomic_or(1, &mEnabled);
376 android_atomic_and(~1, &mEnabled);
379 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
384 void AudioEffect::commandExecuted(uint32_t cmdCode,
390 if (cmdData == NULL || replyData == NULL) {
394 if (mCbf && cmdCode == EFFECT_CMD_SET_PARAM) {
395 effect_param_t *cmd = (effect_param_t *)cmdData;
396 cmd->status = *(int32_t *)replyData;
397 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
401 // -------------------------------------------------------------------------
403 status_t AudioEffect::loadEffectLibrary(const char *libPath, int *handle)
405 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
406 if (af == 0) return PERMISSION_DENIED;
407 return af->loadEffectLibrary(libPath, handle);
410 status_t AudioEffect::unloadEffectLibrary(int handle)
412 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
413 if (af == 0) return PERMISSION_DENIED;
414 return af->unloadEffectLibrary(handle);
417 status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
419 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
420 if (af == 0) return PERMISSION_DENIED;
421 return af->queryNumberEffects(numEffects);
424 status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
426 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
427 if (af == 0) return PERMISSION_DENIED;
428 return af->queryEffect(index, descriptor);
431 status_t AudioEffect::getEffectDescriptor(effect_uuid_t *uuid, effect_descriptor_t *descriptor)
433 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
434 if (af == 0) return PERMISSION_DENIED;
435 return af->getEffectDescriptor(uuid, descriptor);
438 // -------------------------------------------------------------------------
440 status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
442 if (str == NULL || guid == NULL) {
448 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
449 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
452 guid->timeLow = (uint32_t)tmp[0];
453 guid->timeMid = (uint16_t)tmp[1];
454 guid->timeHiAndVersion = (uint16_t)tmp[2];
455 guid->clockSeq = (uint16_t)tmp[3];
456 guid->node[0] = (uint8_t)tmp[4];
457 guid->node[1] = (uint8_t)tmp[5];
458 guid->node[2] = (uint8_t)tmp[6];
459 guid->node[3] = (uint8_t)tmp[7];
460 guid->node[4] = (uint8_t)tmp[8];
461 guid->node[5] = (uint8_t)tmp[9];
466 status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
468 if (guid == NULL || str == NULL) {
472 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
475 guid->timeHiAndVersion,
488 }; // namespace android