AudioEffect.cpp revision ffe9c25ce85e1af55d58ec025adc6367d70db7e8
1801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent/*
2801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent**
3801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent** Copyright 2010, The Android Open Source Project
4801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent**
5801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent** Licensed under the Apache License, Version 2.0 (the "License");
6801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent** you may not use this file except in compliance with the License.
7801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent** You may obtain a copy of the License at
8801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent**
9801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent**     http://www.apache.org/licenses/LICENSE-2.0
10801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent**
11801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent** Unless required by applicable law or agreed to in writing, software
12801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent** distributed under the License is distributed on an "AS IS" BASIS,
13801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent** See the License for the specific language governing permissions and
15801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent** limitations under the License.
16801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent*/
17801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
18801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
19801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent//#define LOG_NDEBUG 0
20801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent#define LOG_TAG "AudioEffect"
21801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
22801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent#include <stdint.h>
23801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent#include <sys/types.h>
24801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent#include <limits.h>
25801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
26801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent#include <private/media/AudioEffectShared.h>
27801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent#include <media/AudioEffect.h>
28801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
29801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent#include <utils/Log.h>
30801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent#include <cutils/atomic.h>
31801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent#include <binder/IPCThreadState.h>
32801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
33801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
34801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
35801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentnamespace android {
36801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
37801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent// ---------------------------------------------------------------------------
38801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
39801a1186eb1d2ce195b15222701865932e08f3dcEric LaurentAudioEffect::AudioEffect()
40801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    : mStatus(NO_INIT)
41801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
42801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
43801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
44801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
45801a1186eb1d2ce195b15222701865932e08f3dcEric LaurentAudioEffect::AudioEffect(const effect_uuid_t *type,
46801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                const effect_uuid_t *uuid,
47801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                int32_t priority,
48801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                effect_callback_t cbf,
49801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                void* user,
50801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                int sessionId,
51801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                audio_io_handle_t output
52801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                )
53801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    : mStatus(NO_INIT)
54801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
55801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mStatus = set(type, uuid, priority, cbf, user, output, sessionId);
56801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
57801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
58801a1186eb1d2ce195b15222701865932e08f3dcEric LaurentAudioEffect::AudioEffect(const char *typeStr,
59801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                const char *uuidStr,
60801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                int32_t priority,
61801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                effect_callback_t cbf,
62801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                void* user,
63801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                int sessionId,
64801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                audio_io_handle_t output
65801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                )
66801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    : mStatus(NO_INIT)
67801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
68801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    effect_uuid_t type;
69801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    effect_uuid_t *pType = NULL;
70801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    effect_uuid_t uuid;
71801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    effect_uuid_t *pUuid = NULL;
72801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
73801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    LOGV("Constructor string\n - type: %s\n - uuid: %s", typeStr, uuidStr);
74801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
75801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (typeStr != NULL) {
76801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        if (stringToGuid(typeStr, &type) == NO_ERROR) {
77801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            pType = &type;
78801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        }
79801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
80801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
81801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (uuidStr != NULL) {
82801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
83801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            pUuid = &uuid;
84801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        }
85801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
86801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
87801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mStatus = set(pType, pUuid, priority, cbf, user, output, sessionId);
88801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
89801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
90801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::set(const effect_uuid_t *type,
91801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                const effect_uuid_t *uuid,
92801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                int32_t priority,
93801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                effect_callback_t cbf,
94801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                void* user,
95801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                int sessionId,
96801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent                audio_io_handle_t output)
97801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
98801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    sp<IEffect> iEffect;
99801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    sp<IMemory> cblk;
100801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    int enabled;
101801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
102801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    LOGV("set %p mUserData: %p", this, user);
103801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
104801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mIEffect != 0) {
105801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        LOGW("Effect already in use");
106801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return INVALID_OPERATION;
107801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
108801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
109801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
110801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (audioFlinger == 0) {
111801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        LOGE("set(): Could not get audioflinger");
112801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return NO_INIT;
113801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
114801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
115801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (type == NULL && uuid == NULL) {
116801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        LOGW("Must specify at least type or uuid");
117801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return BAD_VALUE;
118801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
119801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
120801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mPriority = priority;
121801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mCbf = cbf;
122801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mUserData = user;
123801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mSessionId = sessionId;
124801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
125801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    memset(&mDescriptor, 0, sizeof(effect_descriptor_t));
126801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    memcpy(&mDescriptor.type, EFFECT_UUID_NULL, sizeof(effect_uuid_t));
127801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    memcpy(&mDescriptor.uuid, EFFECT_UUID_NULL, sizeof(effect_uuid_t));
128801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
129801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (type != NULL) {
130801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        memcpy(&mDescriptor.type, type, sizeof(effect_uuid_t));
131801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
132801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (uuid != NULL) {
133801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        memcpy(&mDescriptor.uuid, uuid, sizeof(effect_uuid_t));
134801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
135801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
136801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mIEffectClient = new EffectClient(this);
137801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
138801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    iEffect = audioFlinger->createEffect(getpid(), (effect_descriptor_t *)&mDescriptor,
139801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            mIEffectClient, priority, output, mSessionId, &mStatus, &mId, &enabled);
140801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
141801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
142801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        LOGE("set(): AudioFlinger could not create effect, status: %d", mStatus);
143801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return mStatus;
144801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
145801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
146801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mEnabled = (volatile int32_t)enabled;
147801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
148801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mIEffect = iEffect;
149801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    cblk = iEffect->getCblk();
150801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (cblk == 0) {
151801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        mStatus = NO_INIT;
152801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        LOGE("Could not get control block");
153801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return mStatus;
154801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
155801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
156801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mIEffect = iEffect;
157801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mCblkMemory = cblk;
158801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mCblk = static_cast<effect_param_cblk_t*>(cblk->pointer());
159801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
160801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mCblk->buffer = (uint8_t *)mCblk + bufOffset;
161801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
162801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    iEffect->asBinder()->linkToDeath(mIEffectClient);
163801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    LOGV("set() %p OK effect: %s id: %d status %d enabled %d, ", this, mDescriptor.name, mId, mStatus, mEnabled);
164801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
165801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return mStatus;
166801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
167801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
168801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
169801a1186eb1d2ce195b15222701865932e08f3dcEric LaurentAudioEffect::~AudioEffect()
170801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
171801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    LOGV("Destructor %p", this);
172801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
173801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS) {
174801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        disable();
175801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        if (mIEffect != NULL) {
176801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            mIEffect->disconnect();
177801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            mIEffect->asBinder()->unlinkToDeath(mIEffectClient);
178801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        }
179801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent         IPCThreadState::self()->flushCommands();
180801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
181801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mIEffect.clear();
182801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mIEffectClient.clear();
183801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mCblkMemory.clear();
184801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
185801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
186801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
187801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::initCheck() const
188801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
189801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return mStatus;
190801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
191801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
192801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent// -------------------------------------------------------------------------
193801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
194801a1186eb1d2ce195b15222701865932e08f3dcEric Laurenteffect_descriptor_t AudioEffect::descriptor() const
195801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
196801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return mDescriptor;
197801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
198801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
199801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentbool AudioEffect::isEnabled() const
200801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
201801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return (mEnabled != 0);
202801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
203801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
204801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::enable()
205801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
206801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mStatus != NO_ERROR) {
207801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return INVALID_OPERATION;
208801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
209801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    LOGV("enable %p", this);
210801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
211801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (android_atomic_or(1, &mEnabled) == 0) {
212801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent       return mIEffect->enable();
213801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
214801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
215801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return INVALID_OPERATION;
216801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
217801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
218801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::disable()
219801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
220801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mStatus != NO_ERROR) {
221801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return INVALID_OPERATION;
222801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
223801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    LOGV("disable %p", this);
224801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
225801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (android_atomic_and(~1, &mEnabled) == 1) {
226801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent       return mIEffect->disable();
227801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
228801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
229801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return INVALID_OPERATION;
230801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
231801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
232801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::command(int32_t cmdCode, int32_t cmdSize, void *cmdData, int32_t *replySize, void *replyData)
233801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
234801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
235801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return INVALID_OPERATION;
236801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
237801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
238801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return mIEffect->command(cmdCode, cmdSize, cmdData, replySize, replyData);
239801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
240801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
241801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
242801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::setParameter(effect_param_t *param)
243801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
244801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mStatus != NO_ERROR) {
245801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return INVALID_OPERATION;
246801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
247801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
248801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (param == NULL || param->psize == 0 || param->vsize == 0) {
249801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return BAD_VALUE;
250801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
251801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
252801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    int size = sizeof(int);
253801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
254801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
255801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    LOGV("setParameter: param: %d, param2: %d", *(int *)param->data, (param->psize == 8) ? *((int *)param->data + 1): -1);
256801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
257801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return mIEffect->command(EFFECT_CMD_SET_PARAM, sizeof (effect_param_t) + psize, param, &size, &param->status);
258801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
259801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
260801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::setParameterDeferred(effect_param_t *param)
261801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
262801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mStatus != NO_ERROR) {
263801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return INVALID_OPERATION;
264801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
265801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
266801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (param == NULL || param->psize == 0 || param->vsize == 0) {
267801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return BAD_VALUE;
268801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
269801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
270801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    Mutex::Autolock _l(mCblk->lock);
271801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
272801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
273801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
274801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
275801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
276801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return NO_MEMORY;
277801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
278801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
279801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    *p++ = size;
280801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    memcpy(p, param, sizeof(effect_param_t) + psize);
281801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mCblk->clientIndex += size;
282801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
283801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return NO_ERROR;
284801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
285801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
286801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::setParameterCommit()
287801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
288801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mStatus != NO_ERROR) {
289801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return INVALID_OPERATION;
290801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
291801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
292801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    Mutex::Autolock _l(mCblk->lock);
293801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mCblk->clientIndex == 0) {
294801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return INVALID_OPERATION;
295801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
296801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    int size = 0;
297801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT, 0, NULL, &size, NULL);
298801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
299801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
300801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::getParameter(effect_param_t *param)
301801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
302801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
303801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return INVALID_OPERATION;
304801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
305801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
306801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (param == NULL || param->psize == 0 || param->vsize == 0) {
307801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return BAD_VALUE;
308801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
309801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
310801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    LOGV("getParameter: param: %d, param2: %d", *(int *)param->data, (param->psize == 8) ? *((int *)param->data + 1): -1);
311801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
312801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    int psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
313801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
314801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return mIEffect->command(EFFECT_CMD_GET_PARAM, sizeof(effect_param_t) + param->psize, param, &psize, param);
315801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
316801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
317801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
318801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent// -------------------------------------------------------------------------
319801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
320801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentvoid AudioEffect::binderDied()
321801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
322801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    LOGW("IEffect died");
323801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mStatus = NO_INIT;
324801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mCbf) {
325801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        status_t status = DEAD_OBJECT;
326801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        mCbf(EVENT_ERROR, mUserData, &status);
327801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
328801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    mIEffect.clear();
329801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
330801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
331801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent// -------------------------------------------------------------------------
332801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
333801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentvoid AudioEffect::controlStatusChanged(bool controlGranted)
334801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
335801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    LOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf, mUserData);
336801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (controlGranted) {
337801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        if (mStatus == ALREADY_EXISTS) {
338801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            mStatus = NO_ERROR;
339801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        }
340801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    } else {
341801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        if (mStatus == NO_ERROR) {
342801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            mStatus = ALREADY_EXISTS;
343801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        }
344801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
345801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mCbf) {
346801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
347801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
348801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
349801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
350801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentvoid AudioEffect::enableStatusChanged(bool enabled)
351801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
352801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    LOGV("enableStatusChanged %p enabled %d", this, enabled);
353801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mStatus == ALREADY_EXISTS) {
354801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        mEnabled = enabled;
355801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        if (mCbf) {
356801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
357801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        }
358801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
359801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
360801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
361801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentvoid AudioEffect::commandExecuted(int cmdCode, int cmdSize, void *cmdData, int replySize, void *replyData)
362801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
363801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (cmdData == NULL || replyData == NULL) {
364801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return;
365801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
366801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
367801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (mCbf && cmdCode == EFFECT_CMD_SET_PARAM) {
368801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        effect_param_t *cmd = (effect_param_t *)cmdData;
369801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        cmd->status = *(int32_t *)replyData;
370801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
371801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
372801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
373801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
374801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent// -------------------------------------------------------------------------
375801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
376801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::loadEffectLibrary(const char *libPath, int *handle)
377801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
378801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
379801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (af == 0) return PERMISSION_DENIED;
380801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return af->loadEffectLibrary(libPath, handle);
381801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
382801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
383801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::unloadEffectLibrary(int handle)
384801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
385801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
386801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (af == 0) return PERMISSION_DENIED;
387801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return af->unloadEffectLibrary(handle);
388801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
389801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
390801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
391801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
392801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
393801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (af == 0) return PERMISSION_DENIED;
394801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return af->queryNumberEffects(numEffects);
395801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
396801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
397ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurentstatus_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
398801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
399801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
400801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (af == 0) return PERMISSION_DENIED;
401ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent    return af->queryEffect(index, descriptor);
402801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
403801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
404801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::getEffectDescriptor(effect_uuid_t *uuid, effect_descriptor_t *descriptor)
405801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
406801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
407801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (af == 0) return PERMISSION_DENIED;
408801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return af->getEffectDescriptor(uuid, descriptor);
409801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
410801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
411801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent// -------------------------------------------------------------------------
412801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
413801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
414801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
415801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (str == NULL || guid == NULL) {
416801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return BAD_VALUE;
417801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
418801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
419801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    int tmp[10];
420801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
421801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
422801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
423801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return BAD_VALUE;
424801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
425801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    guid->timeLow = (uint32_t)tmp[0];
426801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    guid->timeMid = (uint16_t)tmp[1];
427801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    guid->timeHiAndVersion = (uint16_t)tmp[2];
428801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    guid->clockSeq = (uint16_t)tmp[3];
429801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    guid->node[0] = (uint8_t)tmp[4];
430801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    guid->node[1] = (uint8_t)tmp[5];
431801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    guid->node[2] = (uint8_t)tmp[6];
432801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    guid->node[3] = (uint8_t)tmp[7];
433801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    guid->node[4] = (uint8_t)tmp[8];
434801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    guid->node[5] = (uint8_t)tmp[9];
435801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
436801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return NO_ERROR;
437801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
438801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
439801a1186eb1d2ce195b15222701865932e08f3dcEric Laurentstatus_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
440801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent{
441801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    if (guid == NULL || str == NULL) {
442801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent        return BAD_VALUE;
443801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    }
444801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
445801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
446801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            guid->timeLow,
447801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            guid->timeMid,
448801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            guid->timeHiAndVersion,
449801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            guid->clockSeq,
450801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            guid->node[0],
451801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            guid->node[1],
452801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            guid->node[2],
453801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            guid->node[3],
454801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            guid->node[4],
455801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent            guid->node[5]);
456801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
457801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent    return NO_ERROR;
458801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}
459801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
460801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
461801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent}; // namespace android
462801a1186eb1d2ce195b15222701865932e08f3dcEric Laurent
463