blob: be9f8c0204af74b8bc000afc8d8831b3bda464b3 [file] [log] [blame]
jpadmanafaca05e2013-06-04 16:03:29 +05301/*
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
jpadmanafaca05e2013-06-04 16:03:29 +053020#include <assert.h>
21#include <stdlib.h>
22#include <string.h>
23#include <new>
Mark Salyzyn60d02072016-09-29 08:48:48 -070024
jpadmanafaca05e2013-06-04 16:03:29 +053025#include <EffectProxy.h>
Mark Salyzyn60d02072016-09-29 08:48:48 -070026
27#include <log/log.h>
jpadmanafaca05e2013-06-04 16:03:29 +053028#include <utils/threads.h>
Mark Salyzyn60d02072016-09-29 08:48:48 -070029
jpadmanafaca05e2013-06-04 16:03:29 +053030#include <media/EffectsFactoryApi.h>
31
32namespace android {
Jiabin Huangbe0988a2020-07-29 00:56:31 +000033// This is a stub proxy descriptor just to return to Factory during the initial
jpadmanafaca05e2013-06-04 16:03:29 +053034// GetDescriptor call. Later in the factory, it is replaced with the
35// SW sub effect descriptor
Eric Laurent385e7502013-10-04 08:36:52 -070036// proxy UUID af8da7e0-2ca1-11e3-b71d-0002a5d5c51b
jpadmanafaca05e2013-06-04 16:03:29 +053037const effect_descriptor_t gProxyDescriptor = {
38 EFFECT_UUID_INITIALIZER, // type
Eric Laurent385e7502013-10-04 08:36:52 -070039 {0xaf8da7e0, 0x2ca1, 0x11e3, 0xb71d, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b }}, // uuid
jpadmanafaca05e2013-06-04 16:03:29 +053040 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
jpadmanafaca05e2013-06-04 16:03:29 +053050int 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;
jpadmanaf90c7e02013-11-14 17:20:52 +053056 audio_effect_library_t** aeli;
57 sub_effect_entry_t** sube;
jpadmanafaca05e2013-06-04 16:03:29 +053058 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;
Eric Laurent5d6d86a2013-09-20 12:27:32 -070069
jpadmanafaca05e2013-06-04 16:03:29 +053070 // 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];
jpadmanaf90c7e02013-11-14 17:20:52 +053076 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];
jpadmanafaca05e2013-06-04 16:03:29 +053079 pContext->desc = new effect_descriptor_t[SUB_FX_COUNT];
jpadmanaf90c7e02013-11-14 17:20:52 +053080 pContext->aeli = new audio_effect_library_t*[SUB_FX_COUNT];
81 int retValue = EffectGetSubEffects(uuid, sube, SUB_FX_COUNT);
jpadmanafaca05e2013-06-04 16:03:29 +053082 // EffectGetSubEffects returns the number of sub-effects copied.
83 if (retValue != SUB_FX_COUNT) {
84 ALOGE("EffectCreate() could not get the sub effects");
jpadmanaf90c7e02013-11-14 17:20:52 +053085 delete[] sube;
86 delete[] desc;
87 delete[] aeli;
88 delete[] pContext->sube;
89 delete[] pContext->desc;
90 delete[] pContext->aeli;
Caroline Ticef8dd1bd2016-10-18 16:32:13 -070091 delete pContext;
jpadmanafaca05e2013-06-04 16:03:29 +053092 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.
jpadmanaf90c7e02013-11-14 17:20:52 +053098 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;
jpadmanafaca05e2013-06-04 16:03:29 +0530102 if ((desc[0].flags & EFFECT_FLAG_HW_ACC_TUNNEL) &&
103 !(desc[1].flags & EFFECT_FLAG_HW_ACC_TUNNEL)) {
jpadmanaf90c7e02013-11-14 17:20:52 +0530104 pContext->sube[SUB_FX_OFFLOAD] = sube[0];
jpadmanafaca05e2013-06-04 16:03:29 +0530105 pContext->desc[SUB_FX_OFFLOAD] = desc[0];
jpadmanaf90c7e02013-11-14 17:20:52 +0530106 pContext->aeli[SUB_FX_OFFLOAD] = aeli[0];
107 pContext->sube[SUB_FX_HOST] = sube[1];
jpadmanafaca05e2013-06-04 16:03:29 +0530108 pContext->desc[SUB_FX_HOST] = desc[1];
jpadmanaf90c7e02013-11-14 17:20:52 +0530109 pContext->aeli[SUB_FX_HOST] = aeli[1];
jpadmanafaca05e2013-06-04 16:03:29 +0530110 }
111 else if ((desc[1].flags & EFFECT_FLAG_HW_ACC_TUNNEL) &&
112 !(desc[0].flags & EFFECT_FLAG_HW_ACC_TUNNEL)) {
jpadmanaf90c7e02013-11-14 17:20:52 +0530113 pContext->sube[SUB_FX_HOST] = sube[0];
jpadmanafaca05e2013-06-04 16:03:29 +0530114 pContext->desc[SUB_FX_HOST] = desc[0];
jpadmanaf90c7e02013-11-14 17:20:52 +0530115 pContext->aeli[SUB_FX_HOST] = aeli[0];
116 pContext->sube[SUB_FX_OFFLOAD] = sube[1];
jpadmanafaca05e2013-06-04 16:03:29 +0530117 pContext->desc[SUB_FX_OFFLOAD] = desc[1];
jpadmanaf90c7e02013-11-14 17:20:52 +0530118 pContext->aeli[SUB_FX_OFFLOAD] = aeli[1];
David Li3b247162021-03-19 07:01:40 +0000119 } else {
120 ALOGE("Both effects have (or don't have) EFFECT_FLAG_HW_ACC_TUNNEL flag");
121 delete[] sube;
122 delete[] desc;
123 delete[] aeli;
124 delete[] pContext->sube;
125 delete[] pContext->desc;
126 delete[] pContext->aeli;
127 delete pContext;
128 return -EINVAL;
jpadmanafaca05e2013-06-04 16:03:29 +0530129 }
jpadmanaf90c7e02013-11-14 17:20:52 +0530130 delete[] desc;
131 delete[] aeli;
132 delete[] sube;
jpadmanafaca05e2013-06-04 16:03:29 +0530133#if (LOG_NDEBUG == 0)
134 effect_uuid_t uuid_print = pContext->desc[SUB_FX_HOST].uuid;
135 ALOGV("EffectCreate() UUID of HOST: %08X-%04X-%04X-%04X-%02X%02X%02X%02X"
136 "%02X%02X\n",uuid_print.timeLow, uuid_print.timeMid,
137 uuid_print.timeHiAndVersion, uuid_print.clockSeq, uuid_print.node[0],
138 uuid_print.node[1], uuid_print.node[2], uuid_print.node[3],
139 uuid_print.node[4], uuid_print.node[5]);
140 ALOGV("EffectCreate() UUID of OFFLOAD: %08X-%04X-%04X-%04X-%02X%02X%02X%02X"
141 "%02X%02X\n", uuid_print.timeLow, uuid_print.timeMid,
142 uuid_print.timeHiAndVersion, uuid_print.clockSeq, uuid_print.node[0],
143 uuid_print.node[1], uuid_print.node[2], uuid_print.node[3],
144 uuid_print.node[4], uuid_print.node[5]);
145#endif
Eric Laurent5d6d86a2013-09-20 12:27:32 -0700146
147 pContext->replySize = PROXY_REPLY_SIZE_DEFAULT;
148 pContext->replyData = (char *)malloc(PROXY_REPLY_SIZE_DEFAULT);
149
jpadmanafaca05e2013-06-04 16:03:29 +0530150 *pHandle = (effect_handle_t)pContext;
151 ALOGV("EffectCreate end");
152 return 0;
153} //end EffectProxyCreate
154
155int EffectProxyRelease(effect_handle_t handle) {
156 EffectContext * pContext = (EffectContext *)handle;
157 if (pContext == NULL) {
158 ALOGV("ERROR : EffectRelease called with NULL pointer");
159 return -EINVAL;
160 }
161 ALOGV("EffectRelease");
jpadmanaf90c7e02013-11-14 17:20:52 +0530162 delete[] pContext->desc;
Eric Laurent5d6d86a2013-09-20 12:27:32 -0700163 free(pContext->replyData);
164
jpadmanafaca05e2013-06-04 16:03:29 +0530165 if (pContext->eHandle[SUB_FX_HOST])
jpadmanaf90c7e02013-11-14 17:20:52 +0530166 pContext->aeli[SUB_FX_HOST]->release_effect(pContext->eHandle[SUB_FX_HOST]);
jpadmanafaca05e2013-06-04 16:03:29 +0530167 if (pContext->eHandle[SUB_FX_OFFLOAD])
jpadmanaf90c7e02013-11-14 17:20:52 +0530168 pContext->aeli[SUB_FX_OFFLOAD]->release_effect(pContext->eHandle[SUB_FX_OFFLOAD]);
169 delete[] pContext->aeli;
170 delete[] pContext->sube;
jpadmanafaca05e2013-06-04 16:03:29 +0530171 delete pContext;
172 pContext = NULL;
173 return 0;
174} /*end EffectProxyRelease */
175
176int EffectProxyGetDescriptor(const effect_uuid_t *uuid,
177 effect_descriptor_t *pDescriptor) {
178 const effect_descriptor_t *desc = NULL;
179
180 if (pDescriptor == NULL || uuid == NULL) {
181 ALOGV("EffectGetDescriptor() called with NULL pointer");
182 return -EINVAL;
183 }
184 desc = &gProxyDescriptor;
185 *pDescriptor = *desc;
186 return 0;
187} /* end EffectProxyGetDescriptor */
188
189/* Effect Control Interface Implementation: Process */
190int Effect_process(effect_handle_t self,
191 audio_buffer_t *inBuffer,
192 audio_buffer_t *outBuffer) {
193
194 EffectContext *pContext = (EffectContext *) self;
195 int ret = 0;
196 if (pContext != NULL) {
197 int index = pContext->index;
198 // if the index refers to HW , do not do anything. Just return.
199 if (index == SUB_FX_HOST) {
jpadmanafaca05e2013-06-04 16:03:29 +0530200 ret = (*pContext->eHandle[index])->process(pContext->eHandle[index],
201 inBuffer, outBuffer);
202 }
203 }
204 return ret;
205} /* end Effect_process */
206
207/* Effect Control Interface Implementation: Command */
208int Effect_command(effect_handle_t self,
209 uint32_t cmdCode,
210 uint32_t cmdSize,
211 void *pCmdData,
212 uint32_t *replySize,
213 void *pReplyData) {
214
215 EffectContext *pContext = (EffectContext *) self;
Eric Laurenteba9bf72013-09-27 15:04:26 -0700216 int status = 0;
jpadmanafaca05e2013-06-04 16:03:29 +0530217 if (pContext == NULL) {
218 ALOGV("Effect_command() Proxy context is NULL");
219 return -EINVAL;
220 }
221 if (pContext->eHandle[SUB_FX_HOST] == NULL) {
222 ALOGV("Effect_command() Calling HOST EffectCreate");
jpadmanaf90c7e02013-11-14 17:20:52 +0530223 status = pContext->aeli[SUB_FX_HOST]->create_effect(
224 &pContext->desc[SUB_FX_HOST].uuid,
jpadmanafaca05e2013-06-04 16:03:29 +0530225 pContext->sessionId, pContext->ioId,
226 &(pContext->eHandle[SUB_FX_HOST]));
227 if (status != NO_ERROR || (pContext->eHandle[SUB_FX_HOST] == NULL)) {
228 ALOGV("Effect_command() Error creating SW sub effect");
229 return status;
230 }
231 }
232 if (pContext->eHandle[SUB_FX_OFFLOAD] == NULL) {
233 ALOGV("Effect_command() Calling OFFLOAD EffectCreate");
jpadmanaf90c7e02013-11-14 17:20:52 +0530234 status = pContext->aeli[SUB_FX_OFFLOAD]->create_effect(
235 &pContext->desc[SUB_FX_OFFLOAD].uuid,
jpadmanafaca05e2013-06-04 16:03:29 +0530236 pContext->sessionId, pContext->ioId,
237 &(pContext->eHandle[SUB_FX_OFFLOAD]));
238 if (status != NO_ERROR || (pContext->eHandle[SUB_FX_OFFLOAD] == NULL)) {
239 ALOGV("Effect_command() Error creating HW effect");
jpadmanaf90c7e02013-11-14 17:20:52 +0530240 pContext->eHandle[SUB_FX_OFFLOAD] = NULL;
jpadmanafaca05e2013-06-04 16:03:29 +0530241 // Do not return error here as SW effect is created
242 // Return error if the CMD_OFFLOAD sends the index as OFFLOAD
243 }
244 pContext->index = SUB_FX_HOST;
245 }
246 // EFFECT_CMD_OFFLOAD used to (1) send whether the thread is offload or not
247 // (2) Send the ioHandle of the effectThread when the effect
248 // is moved from one type of thread to another.
249 // pCmdData points to a memory holding effect_offload_param_t structure
250 if (cmdCode == EFFECT_CMD_OFFLOAD) {
251 ALOGV("Effect_command() cmdCode = EFFECT_CMD_OFFLOAD");
Andy Hung48946cd2016-11-04 16:45:06 -0700252 if (replySize == NULL || *replySize < sizeof(int)) {
253 ALOGV("effectsOffload: Effect_command: CMD_OFFLOAD has no reply");
254 android_errorWriteLog(0x534e4554, "32448121");
255 return FAILED_TRANSACTION;
256 }
jpadmanafaca05e2013-06-04 16:03:29 +0530257 if (cmdSize == 0 || pCmdData == NULL) {
258 ALOGV("effectsOffload: Effect_command: CMD_OFFLOAD has no data");
259 *(int*)pReplyData = FAILED_TRANSACTION;
260 return FAILED_TRANSACTION;
261 }
262 effect_offload_param_t* offloadParam = (effect_offload_param_t*)pCmdData;
263 // Assign the effect context index based on isOffload field of the structure
264 pContext->index = offloadParam->isOffload ? SUB_FX_OFFLOAD : SUB_FX_HOST;
265 // if the index is HW and the HW effect is unavailable, return error
266 // and reset the index to SW
267 if (pContext->eHandle[pContext->index] == NULL) {
268 ALOGV("Effect_command()CMD_OFFLOAD sub effect unavailable");
269 *(int*)pReplyData = FAILED_TRANSACTION;
270 return FAILED_TRANSACTION;
271 }
272 pContext->ioId = offloadParam->ioHandle;
273 ALOGV("Effect_command()CMD_OFFLOAD index:%d io %d", pContext->index, pContext->ioId);
274 // Update the DSP wrapper with the new ioHandle.
275 // Pass the OFFLOAD command to the wrapper.
276 // The DSP wrapper needs to handle this CMD
jpadmanaf90c7e02013-11-14 17:20:52 +0530277 if (pContext->eHandle[SUB_FX_OFFLOAD]) {
278 ALOGV("Effect_command: Calling OFFLOAD command");
279 return (*pContext->eHandle[SUB_FX_OFFLOAD])->command(
280 pContext->eHandle[SUB_FX_OFFLOAD], cmdCode, cmdSize,
281 pCmdData, replySize, pReplyData);
282 }
283 *(int*)pReplyData = NO_ERROR;
284 ALOGV("Effect_command OFFLOAD return 0, replyData %d",
285 *(int*)pReplyData);
286
287 return NO_ERROR;
jpadmanafaca05e2013-06-04 16:03:29 +0530288 }
289
290 int index = pContext->index;
291 if (index != SUB_FX_HOST && index != SUB_FX_OFFLOAD) {
292 ALOGV("Effect_command: effect index is neither offload nor host");
293 return -EINVAL;
294 }
Eric Laurenteba9bf72013-09-27 15:04:26 -0700295
296 // Getter commands are only sent to the active sub effect.
Eric Laurent5d6d86a2013-09-20 12:27:32 -0700297 int *subStatus[SUB_FX_COUNT];
298 uint32_t *subReplySize[SUB_FX_COUNT];
299 void *subReplyData[SUB_FX_COUNT];
300 uint32_t tmpSize;
301 int tmpStatus;
Eric Laurenteba9bf72013-09-27 15:04:26 -0700302
Eric Laurent5d6d86a2013-09-20 12:27:32 -0700303 // grow temp reply buffer if needed
304 if (replySize != NULL) {
305 tmpSize = pContext->replySize;
306 while (tmpSize < *replySize && tmpSize < PROXY_REPLY_SIZE_MAX) {
307 tmpSize *= 2;
Eric Laurenteba9bf72013-09-27 15:04:26 -0700308 }
Eric Laurent5d6d86a2013-09-20 12:27:32 -0700309 if (tmpSize > pContext->replySize) {
310 ALOGV("Effect_command grow reply buf to %d", tmpSize);
311 pContext->replyData = (char *)realloc(pContext->replyData, tmpSize);
312 pContext->replySize = tmpSize;
Eric Laurenteba9bf72013-09-27 15:04:26 -0700313 }
Eric Laurent5d6d86a2013-09-20 12:27:32 -0700314 if (tmpSize > *replySize) {
315 tmpSize = *replySize;
316 }
317 } else {
318 tmpSize = 0;
Eric Laurenteba9bf72013-09-27 15:04:26 -0700319 }
Eric Laurent5d6d86a2013-09-20 12:27:32 -0700320 // tmpSize is now the actual reply size for the non active sub effect
321
322 // Send command to sub effects. The command is sent to all sub effects so that their internal
323 // state is kept in sync.
324 // Only the reply from the active sub effect is returned to the caller. The reply from the
325 // other sub effect is lost in pContext->replyData
326 for (int i = 0; i < SUB_FX_COUNT; i++) {
327 if (pContext->eHandle[i] == NULL) {
328 continue;
329 }
330 if (i == index) {
331 subStatus[i] = &status;
332 subReplySize[i] = replySize;
333 subReplyData[i] = pReplyData;
334 } else {
335 subStatus[i] = &tmpStatus;
336 subReplySize[i] = replySize == NULL ? NULL : &tmpSize;
337 subReplyData[i] = pReplyData == NULL ? NULL : pContext->replyData;
338 }
339 *subStatus[i] = (*pContext->eHandle[i])->command(
340 pContext->eHandle[i], cmdCode, cmdSize,
341 pCmdData, subReplySize[i], subReplyData[i]);
342 }
343
Eric Laurenteba9bf72013-09-27 15:04:26 -0700344 return status;
jpadmanafaca05e2013-06-04 16:03:29 +0530345} /* end Effect_command */
346
347
348/* Effect Control Interface Implementation: get_descriptor */
349int Effect_getDescriptor(effect_handle_t self,
350 effect_descriptor_t *pDescriptor) {
351
352 EffectContext * pContext = (EffectContext *) self;
353 const effect_descriptor_t *desc;
354
355 ALOGV("Effect_getDescriptor");
356 if (pContext == NULL || pDescriptor == NULL) {
357 ALOGV("Effect_getDescriptor() invalid param");
358 return -EINVAL;
359 }
360 if (pContext->desc == NULL) {
361 ALOGV("Effect_getDescriptor() could not get descriptor");
362 return -EINVAL;
363 }
364 desc = &pContext->desc[SUB_FX_HOST];
365 *pDescriptor = *desc;
366 pDescriptor->uuid = pContext->uuid; // Replace the uuid with the Proxy UUID
367 // Also set/clear the EFFECT_FLAG_OFFLOAD_SUPPORTED flag based on the sub effects availability
368 if (pContext->eHandle[SUB_FX_OFFLOAD] != NULL)
369 pDescriptor->flags |= EFFECT_FLAG_OFFLOAD_SUPPORTED;
370 else
371 pDescriptor->flags &= ~EFFECT_FLAG_OFFLOAD_SUPPORTED;
372 return 0;
373} /* end Effect_getDescriptor */
374
375} // namespace android
376
377__attribute__ ((visibility ("default")))
378audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
synergy dev9803acb2013-12-17 17:48:51 -0800379 .tag = AUDIO_EFFECT_LIBRARY_TAG,
380 .version = EFFECT_LIBRARY_API_VERSION,
381 .name = "Effect Proxy",
382 .implementor = "AOSP",
383 .create_effect = android::EffectProxyCreate,
384 .release_effect = android::EffectProxyRelease,
385 .get_descriptor = android::EffectProxyGetDescriptor,
jpadmanafaca05e2013-06-04 16:03:29 +0530386};