1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "EffectProxy"
18//#define LOG_NDEBUG 0
19
20#include <assert.h>
21#include <stdlib.h>
22#include <string.h>
23#include <new>
24
25#include <EffectProxy.h>
26
27#include <log/log.h>
28#include <utils/threads.h>
29
30#include <media/EffectsFactoryApi.h>
31
32namespace android {
33// This is a dummy proxy descriptor just to return to Factory during the initial
34// GetDescriptor call. Later in the factory, it is replaced with the
35// SW sub effect descriptor
36// proxy UUID af8da7e0-2ca1-11e3-b71d-0002a5d5c51b
37const effect_descriptor_t gProxyDescriptor = {
38        EFFECT_UUID_INITIALIZER, // type
39        {0xaf8da7e0, 0x2ca1, 0x11e3, 0xb71d, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b }}, // uuid
40        EFFECT_CONTROL_API_VERSION, //version of effect control API
41        (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_LAST |
42         EFFECT_FLAG_VOLUME_CTRL), // effect capability flags
43        0, // CPU load
44        1, // Data memory
45        "Proxy", //effect name
46        "AOSP", //implementor name
47};
48
49
50int EffectProxyCreate(const effect_uuid_t *uuid,
51                            int32_t             sessionId,
52                            int32_t             ioId,
53                           effect_handle_t  *pHandle) {
54
55    effect_descriptor_t* desc;
56    audio_effect_library_t** aeli;
57    sub_effect_entry_t** sube;
58    EffectContext* pContext;
59    if (pHandle == NULL || uuid == NULL) {
60        ALOGE("EffectProxyCreate() called with NULL pointer");
61        return -EINVAL;
62    }
63    ALOGV("EffectProxyCreate start..");
64    pContext = new EffectContext;
65    pContext->sessionId = sessionId;
66    pContext->ioId = ioId;
67    pContext->uuid = *uuid;
68    pContext->common_itfe = &gEffectInterface;
69
70    // The sub effects will be created in effect_command when the first command
71    // for the effect is received
72    pContext->eHandle[SUB_FX_HOST] = pContext->eHandle[SUB_FX_OFFLOAD] = NULL;
73
74    // Get the HW and SW sub effect descriptors from the effects factory
75    desc = new effect_descriptor_t[SUB_FX_COUNT];
76    aeli = new audio_effect_library_t*[SUB_FX_COUNT];
77    sube = new sub_effect_entry_t*[SUB_FX_COUNT];
78    pContext->sube = new sub_effect_entry_t*[SUB_FX_COUNT];
79    pContext->desc = new effect_descriptor_t[SUB_FX_COUNT];
80    pContext->aeli = new audio_effect_library_t*[SUB_FX_COUNT];
81    int retValue = EffectGetSubEffects(uuid, sube, SUB_FX_COUNT);
82    // EffectGetSubEffects returns the number of sub-effects copied.
83    if (retValue != SUB_FX_COUNT) {
84       ALOGE("EffectCreate() could not get the sub effects");
85       delete[] sube;
86       delete[] desc;
87       delete[] aeli;
88       delete[] pContext->sube;
89       delete[] pContext->desc;
90       delete[] pContext->aeli;
91       delete pContext;
92       return -EINVAL;
93    }
94    // Check which is the HW descriptor and copy the descriptors
95    // to the Context desc array
96    // Also check if there is only one HW and one SW descriptor.
97    // HW descriptor alone has the HW_TUNNEL flag.
98    desc[0] = *(effect_descriptor_t*)(sube[0])->object;
99    desc[1] = *(effect_descriptor_t*)(sube[1])->object;
100    aeli[0] = sube[0]->lib->desc;
101    aeli[1] = sube[1]->lib->desc;
102    if ((desc[0].flags & EFFECT_FLAG_HW_ACC_TUNNEL) &&
103       !(desc[1].flags & EFFECT_FLAG_HW_ACC_TUNNEL)) {
104        pContext->sube[SUB_FX_OFFLOAD] = sube[0];
105        pContext->desc[SUB_FX_OFFLOAD] = desc[0];
106        pContext->aeli[SUB_FX_OFFLOAD] = aeli[0];
107        pContext->sube[SUB_FX_HOST] = sube[1];
108        pContext->desc[SUB_FX_HOST] = desc[1];
109        pContext->aeli[SUB_FX_HOST] = aeli[1];
110    }
111    else if ((desc[1].flags & EFFECT_FLAG_HW_ACC_TUNNEL) &&
112             !(desc[0].flags & EFFECT_FLAG_HW_ACC_TUNNEL)) {
113        pContext->sube[SUB_FX_HOST] = sube[0];
114        pContext->desc[SUB_FX_HOST] = desc[0];
115        pContext->aeli[SUB_FX_HOST] = aeli[0];
116        pContext->sube[SUB_FX_OFFLOAD] = sube[1];
117        pContext->desc[SUB_FX_OFFLOAD] = desc[1];
118        pContext->aeli[SUB_FX_OFFLOAD] = aeli[1];
119    }
120    delete[] desc;
121    delete[] aeli;
122    delete[] sube;
123#if (LOG_NDEBUG == 0)
124    effect_uuid_t uuid_print = pContext->desc[SUB_FX_HOST].uuid;
125    ALOGV("EffectCreate() UUID of HOST: %08X-%04X-%04X-%04X-%02X%02X%02X%02X"
126        "%02X%02X\n",uuid_print.timeLow, uuid_print.timeMid,
127        uuid_print.timeHiAndVersion, uuid_print.clockSeq, uuid_print.node[0],
128        uuid_print.node[1], uuid_print.node[2], uuid_print.node[3],
129        uuid_print.node[4], uuid_print.node[5]);
130    ALOGV("EffectCreate() UUID of OFFLOAD: %08X-%04X-%04X-%04X-%02X%02X%02X%02X"
131        "%02X%02X\n", uuid_print.timeLow, uuid_print.timeMid,
132        uuid_print.timeHiAndVersion, uuid_print.clockSeq, uuid_print.node[0],
133        uuid_print.node[1], uuid_print.node[2], uuid_print.node[3],
134        uuid_print.node[4], uuid_print.node[5]);
135#endif
136
137    pContext->replySize = PROXY_REPLY_SIZE_DEFAULT;
138    pContext->replyData = (char *)malloc(PROXY_REPLY_SIZE_DEFAULT);
139
140    *pHandle = (effect_handle_t)pContext;
141    ALOGV("EffectCreate end");
142    return 0;
143} //end EffectProxyCreate
144
145int EffectProxyRelease(effect_handle_t handle) {
146    EffectContext * pContext = (EffectContext *)handle;
147    if (pContext == NULL) {
148        ALOGV("ERROR : EffectRelease called with NULL pointer");
149        return -EINVAL;
150    }
151    ALOGV("EffectRelease");
152    delete[] pContext->desc;
153    free(pContext->replyData);
154
155    if (pContext->eHandle[SUB_FX_HOST])
156       pContext->aeli[SUB_FX_HOST]->release_effect(pContext->eHandle[SUB_FX_HOST]);
157    if (pContext->eHandle[SUB_FX_OFFLOAD])
158       pContext->aeli[SUB_FX_OFFLOAD]->release_effect(pContext->eHandle[SUB_FX_OFFLOAD]);
159    delete[] pContext->aeli;
160    delete[] pContext->sube;
161    delete pContext;
162    pContext = NULL;
163    return 0;
164} /*end EffectProxyRelease */
165
166int EffectProxyGetDescriptor(const effect_uuid_t *uuid,
167                                   effect_descriptor_t *pDescriptor) {
168    const effect_descriptor_t *desc = NULL;
169
170    if (pDescriptor == NULL || uuid == NULL) {
171        ALOGV("EffectGetDescriptor() called with NULL pointer");
172        return -EINVAL;
173    }
174    desc = &gProxyDescriptor;
175    *pDescriptor = *desc;
176    return 0;
177} /* end EffectProxyGetDescriptor */
178
179/* Effect Control Interface Implementation: Process */
180int Effect_process(effect_handle_t     self,
181                              audio_buffer_t         *inBuffer,
182                              audio_buffer_t         *outBuffer) {
183
184    EffectContext *pContext = (EffectContext *) self;
185    int ret = 0;
186    if (pContext != NULL) {
187        int index = pContext->index;
188        // if the index refers to HW , do not do anything. Just return.
189        if (index == SUB_FX_HOST) {
190            ret = (*pContext->eHandle[index])->process(pContext->eHandle[index],
191                                                       inBuffer, outBuffer);
192        }
193    }
194    return ret;
195}   /* end Effect_process */
196
197/* Effect Control Interface Implementation: Command */
198int Effect_command(effect_handle_t  self,
199                              uint32_t            cmdCode,
200                              uint32_t            cmdSize,
201                              void                *pCmdData,
202                              uint32_t            *replySize,
203                              void                *pReplyData) {
204
205    EffectContext *pContext = (EffectContext *) self;
206    int status = 0;
207    if (pContext == NULL) {
208        ALOGV("Effect_command() Proxy context is NULL");
209        return -EINVAL;
210    }
211    if (pContext->eHandle[SUB_FX_HOST] == NULL) {
212        ALOGV("Effect_command() Calling HOST EffectCreate");
213        status = pContext->aeli[SUB_FX_HOST]->create_effect(
214                              &pContext->desc[SUB_FX_HOST].uuid,
215                              pContext->sessionId, pContext->ioId,
216                              &(pContext->eHandle[SUB_FX_HOST]));
217        if (status != NO_ERROR || (pContext->eHandle[SUB_FX_HOST] == NULL)) {
218            ALOGV("Effect_command() Error creating SW sub effect");
219            return status;
220        }
221    }
222    if (pContext->eHandle[SUB_FX_OFFLOAD] == NULL) {
223        ALOGV("Effect_command() Calling OFFLOAD EffectCreate");
224        status = pContext->aeli[SUB_FX_OFFLOAD]->create_effect(
225                              &pContext->desc[SUB_FX_OFFLOAD].uuid,
226                              pContext->sessionId, pContext->ioId,
227                              &(pContext->eHandle[SUB_FX_OFFLOAD]));
228        if (status != NO_ERROR || (pContext->eHandle[SUB_FX_OFFLOAD] == NULL)) {
229            ALOGV("Effect_command() Error creating HW effect");
230            pContext->eHandle[SUB_FX_OFFLOAD] = NULL;
231            // Do not return error here as SW effect is created
232            // Return error if the CMD_OFFLOAD sends the index as OFFLOAD
233        }
234        pContext->index = SUB_FX_HOST;
235    }
236    // EFFECT_CMD_OFFLOAD used to (1) send whether the thread is offload or not
237    // (2) Send the ioHandle of the effectThread when the effect
238    // is moved from one type of thread to another.
239    // pCmdData points to a memory holding effect_offload_param_t structure
240    if (cmdCode == EFFECT_CMD_OFFLOAD) {
241        ALOGV("Effect_command() cmdCode = EFFECT_CMD_OFFLOAD");
242        if (replySize == NULL || *replySize < sizeof(int)) {
243            ALOGV("effectsOffload: Effect_command: CMD_OFFLOAD has no reply");
244            android_errorWriteLog(0x534e4554, "32448121");
245            return FAILED_TRANSACTION;
246        }
247        if (cmdSize == 0 || pCmdData == NULL) {
248            ALOGV("effectsOffload: Effect_command: CMD_OFFLOAD has no data");
249             *(int*)pReplyData = FAILED_TRANSACTION;
250            return FAILED_TRANSACTION;
251        }
252        effect_offload_param_t* offloadParam = (effect_offload_param_t*)pCmdData;
253        // Assign the effect context index based on isOffload field of the structure
254        pContext->index = offloadParam->isOffload ? SUB_FX_OFFLOAD : SUB_FX_HOST;
255        // if the index is HW and the HW effect is unavailable, return error
256        // and reset the index to SW
257        if (pContext->eHandle[pContext->index] == NULL) {
258            ALOGV("Effect_command()CMD_OFFLOAD sub effect unavailable");
259            *(int*)pReplyData = FAILED_TRANSACTION;
260            return FAILED_TRANSACTION;
261        }
262        pContext->ioId = offloadParam->ioHandle;
263        ALOGV("Effect_command()CMD_OFFLOAD index:%d io %d", pContext->index, pContext->ioId);
264        // Update the DSP wrapper with the new ioHandle.
265        // Pass the OFFLOAD command to the wrapper.
266        // The DSP wrapper needs to handle this CMD
267        if (pContext->eHandle[SUB_FX_OFFLOAD]) {
268            ALOGV("Effect_command: Calling OFFLOAD command");
269            return (*pContext->eHandle[SUB_FX_OFFLOAD])->command(
270                           pContext->eHandle[SUB_FX_OFFLOAD], cmdCode, cmdSize,
271                           pCmdData, replySize, pReplyData);
272        }
273        *(int*)pReplyData = NO_ERROR;
274        ALOGV("Effect_command OFFLOAD return 0, replyData %d",
275                                                *(int*)pReplyData);
276
277        return NO_ERROR;
278    }
279
280    int index = pContext->index;
281    if (index != SUB_FX_HOST && index != SUB_FX_OFFLOAD) {
282        ALOGV("Effect_command: effect index is neither offload nor host");
283        return -EINVAL;
284    }
285
286    // Getter commands are only sent to the active sub effect.
287    int *subStatus[SUB_FX_COUNT];
288    uint32_t *subReplySize[SUB_FX_COUNT];
289    void *subReplyData[SUB_FX_COUNT];
290    uint32_t tmpSize;
291    int tmpStatus;
292
293    // grow temp reply buffer if needed
294    if (replySize != NULL) {
295        tmpSize = pContext->replySize;
296        while (tmpSize < *replySize && tmpSize < PROXY_REPLY_SIZE_MAX) {
297            tmpSize *= 2;
298        }
299        if (tmpSize > pContext->replySize) {
300            ALOGV("Effect_command grow reply buf to %d", tmpSize);
301            pContext->replyData = (char *)realloc(pContext->replyData, tmpSize);
302            pContext->replySize = tmpSize;
303        }
304        if (tmpSize > *replySize) {
305            tmpSize = *replySize;
306        }
307    } else {
308        tmpSize = 0;
309    }
310    // tmpSize is now the actual reply size for the non active sub effect
311
312    // Send command to sub effects. The command is sent to all sub effects so that their internal
313    // state is kept in sync.
314    // Only the reply from the active sub effect is returned to the caller. The reply from the
315    // other sub effect is lost in pContext->replyData
316    for (int i = 0; i < SUB_FX_COUNT; i++) {
317        if (pContext->eHandle[i] == NULL) {
318            continue;
319        }
320        if (i == index) {
321            subStatus[i] = &status;
322            subReplySize[i] = replySize;
323            subReplyData[i] = pReplyData;
324        } else {
325            subStatus[i] = &tmpStatus;
326            subReplySize[i] = replySize == NULL ? NULL : &tmpSize;
327            subReplyData[i] = pReplyData == NULL ? NULL : pContext->replyData;
328        }
329        *subStatus[i] = (*pContext->eHandle[i])->command(
330                             pContext->eHandle[i], cmdCode, cmdSize,
331                             pCmdData, subReplySize[i], subReplyData[i]);
332    }
333
334    return status;
335}    /* end Effect_command */
336
337
338/* Effect Control Interface Implementation: get_descriptor */
339int Effect_getDescriptor(effect_handle_t   self,
340                         effect_descriptor_t *pDescriptor) {
341
342    EffectContext * pContext = (EffectContext *) self;
343    const effect_descriptor_t *desc;
344
345    ALOGV("Effect_getDescriptor");
346    if (pContext == NULL || pDescriptor == NULL) {
347        ALOGV("Effect_getDescriptor() invalid param");
348        return -EINVAL;
349    }
350    if (pContext->desc == NULL) {
351        ALOGV("Effect_getDescriptor() could not get descriptor");
352        return -EINVAL;
353    }
354    desc = &pContext->desc[SUB_FX_HOST];
355    *pDescriptor = *desc;
356    pDescriptor->uuid = pContext->uuid; // Replace the uuid with the Proxy UUID
357    // Also set/clear the EFFECT_FLAG_OFFLOAD_SUPPORTED flag based on the sub effects availability
358    if (pContext->eHandle[SUB_FX_OFFLOAD] != NULL)
359        pDescriptor->flags |= EFFECT_FLAG_OFFLOAD_SUPPORTED;
360    else
361        pDescriptor->flags &= ~EFFECT_FLAG_OFFLOAD_SUPPORTED;
362    return 0;
363} /* end Effect_getDescriptor */
364
365} // namespace android
366
367__attribute__ ((visibility ("default")))
368audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
369    .tag = AUDIO_EFFECT_LIBRARY_TAG,
370    .version = EFFECT_LIBRARY_API_VERSION,
371    .name = "Effect Proxy",
372    .implementor = "AOSP",
373    .create_effect = android::EffectProxyCreate,
374    .release_effect = android::EffectProxyRelease,
375    .get_descriptor = android::EffectProxyGetDescriptor,
376};
377