EffectProxy.cpp revision 5d6d86a4d102704f49b9235eaf282c428d7100b6
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 <cutils/log.h> 21#include <assert.h> 22#include <stdlib.h> 23#include <string.h> 24#include <new> 25#include <EffectProxy.h> 26#include <utils/threads.h> 27#include <media/EffectsFactoryApi.h> 28 29namespace android { 30// This is a dummy proxy descriptor just to return to Factory during the initial 31// GetDescriptor call. Later in the factory, it is replaced with the 32// SW sub effect descriptor 33const effect_descriptor_t gProxyDescriptor = { 34 EFFECT_UUID_INITIALIZER, // type 35 EFFECT_UUID_INITIALIZER, // uuid 36 EFFECT_CONTROL_API_VERSION, //version of effect control API 37 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_LAST | 38 EFFECT_FLAG_VOLUME_CTRL), // effect capability flags 39 0, // CPU load 40 1, // Data memory 41 "Proxy", //effect name 42 "AOSP", //implementor name 43}; 44 45 46static const effect_descriptor_t *const gDescriptors[] = 47{ 48 &gProxyDescriptor, 49}; 50 51 52int EffectProxyCreate(const effect_uuid_t *uuid, 53 int32_t sessionId, 54 int32_t ioId, 55 effect_handle_t *pHandle) { 56 57 effect_descriptor_t* desc; 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 pContext->desc = new effect_descriptor_t[SUB_FX_COUNT]; 77 int retValue = EffectGetSubEffects(uuid, desc, 78 sizeof(effect_descriptor_t) * SUB_FX_COUNT); 79 // EffectGetSubEffects returns the number of sub-effects copied. 80 if (retValue != SUB_FX_COUNT) { 81 ALOGE("EffectCreate() could not get the sub effects"); 82 delete desc; 83 delete pContext->desc; 84 return -EINVAL; 85 } 86 // Check which is the HW descriptor and copy the descriptors 87 // to the Context desc array 88 // Also check if there is only one HW and one SW descriptor. 89 // HW descriptor alone has the HW_TUNNEL flag. 90 if ((desc[0].flags & EFFECT_FLAG_HW_ACC_TUNNEL) && 91 !(desc[1].flags & EFFECT_FLAG_HW_ACC_TUNNEL)) { 92 pContext->desc[SUB_FX_OFFLOAD] = desc[0]; 93 pContext->desc[SUB_FX_HOST] = desc[1]; 94 } 95 else if ((desc[1].flags & EFFECT_FLAG_HW_ACC_TUNNEL) && 96 !(desc[0].flags & EFFECT_FLAG_HW_ACC_TUNNEL)) { 97 pContext->desc[SUB_FX_HOST] = desc[0]; 98 pContext->desc[SUB_FX_OFFLOAD] = desc[1]; 99 } 100 delete desc; 101#if (LOG_NDEBUG == 0) 102 effect_uuid_t uuid_print = pContext->desc[SUB_FX_HOST].uuid; 103 ALOGV("EffectCreate() UUID of HOST: %08X-%04X-%04X-%04X-%02X%02X%02X%02X" 104 "%02X%02X\n",uuid_print.timeLow, uuid_print.timeMid, 105 uuid_print.timeHiAndVersion, uuid_print.clockSeq, uuid_print.node[0], 106 uuid_print.node[1], uuid_print.node[2], uuid_print.node[3], 107 uuid_print.node[4], uuid_print.node[5]); 108 ALOGV("EffectCreate() UUID of OFFLOAD: %08X-%04X-%04X-%04X-%02X%02X%02X%02X" 109 "%02X%02X\n", uuid_print.timeLow, uuid_print.timeMid, 110 uuid_print.timeHiAndVersion, uuid_print.clockSeq, uuid_print.node[0], 111 uuid_print.node[1], uuid_print.node[2], uuid_print.node[3], 112 uuid_print.node[4], uuid_print.node[5]); 113#endif 114 115 pContext->replySize = PROXY_REPLY_SIZE_DEFAULT; 116 pContext->replyData = (char *)malloc(PROXY_REPLY_SIZE_DEFAULT); 117 118 *pHandle = (effect_handle_t)pContext; 119 ALOGV("EffectCreate end"); 120 return 0; 121} //end EffectProxyCreate 122 123int EffectProxyRelease(effect_handle_t handle) { 124 EffectContext * pContext = (EffectContext *)handle; 125 if (pContext == NULL) { 126 ALOGV("ERROR : EffectRelease called with NULL pointer"); 127 return -EINVAL; 128 } 129 ALOGV("EffectRelease"); 130 delete pContext->desc; 131 free(pContext->replyData); 132 133 if (pContext->eHandle[SUB_FX_HOST]) 134 EffectRelease(pContext->eHandle[SUB_FX_HOST]); 135 if (pContext->eHandle[SUB_FX_OFFLOAD]) 136 EffectRelease(pContext->eHandle[SUB_FX_OFFLOAD]); 137 delete pContext; 138 pContext = NULL; 139 return 0; 140} /*end EffectProxyRelease */ 141 142int EffectProxyGetDescriptor(const effect_uuid_t *uuid, 143 effect_descriptor_t *pDescriptor) { 144 const effect_descriptor_t *desc = NULL; 145 146 if (pDescriptor == NULL || uuid == NULL) { 147 ALOGV("EffectGetDescriptor() called with NULL pointer"); 148 return -EINVAL; 149 } 150 desc = &gProxyDescriptor; 151 *pDescriptor = *desc; 152 return 0; 153} /* end EffectProxyGetDescriptor */ 154 155/* Effect Control Interface Implementation: Process */ 156int Effect_process(effect_handle_t self, 157 audio_buffer_t *inBuffer, 158 audio_buffer_t *outBuffer) { 159 160 EffectContext *pContext = (EffectContext *) self; 161 int ret = 0; 162 if (pContext != NULL) { 163 int index = pContext->index; 164 // if the index refers to HW , do not do anything. Just return. 165 if (index == SUB_FX_HOST) { 166 ret = (*pContext->eHandle[index])->process(pContext->eHandle[index], 167 inBuffer, outBuffer); 168 } 169 } 170 return ret; 171} /* end Effect_process */ 172 173/* Effect Control Interface Implementation: Command */ 174int Effect_command(effect_handle_t self, 175 uint32_t cmdCode, 176 uint32_t cmdSize, 177 void *pCmdData, 178 uint32_t *replySize, 179 void *pReplyData) { 180 181 EffectContext *pContext = (EffectContext *) self; 182 int status = 0; 183 if (pContext == NULL) { 184 ALOGV("Effect_command() Proxy context is NULL"); 185 return -EINVAL; 186 } 187 if (pContext->eHandle[SUB_FX_HOST] == NULL) { 188 ALOGV("Effect_command() Calling HOST EffectCreate"); 189 status = EffectCreate(&pContext->desc[SUB_FX_HOST].uuid, 190 pContext->sessionId, pContext->ioId, 191 &(pContext->eHandle[SUB_FX_HOST])); 192 if (status != NO_ERROR || (pContext->eHandle[SUB_FX_HOST] == NULL)) { 193 ALOGV("Effect_command() Error creating SW sub effect"); 194 return status; 195 } 196 } 197 if (pContext->eHandle[SUB_FX_OFFLOAD] == NULL) { 198 ALOGV("Effect_command() Calling OFFLOAD EffectCreate"); 199 status = EffectCreate(&pContext->desc[SUB_FX_OFFLOAD].uuid, 200 pContext->sessionId, pContext->ioId, 201 &(pContext->eHandle[SUB_FX_OFFLOAD])); 202 if (status != NO_ERROR || (pContext->eHandle[SUB_FX_OFFLOAD] == NULL)) { 203 ALOGV("Effect_command() Error creating HW effect"); 204 // Do not return error here as SW effect is created 205 // Return error if the CMD_OFFLOAD sends the index as OFFLOAD 206 } 207 pContext->index = SUB_FX_HOST; 208 } 209 // EFFECT_CMD_OFFLOAD used to (1) send whether the thread is offload or not 210 // (2) Send the ioHandle of the effectThread when the effect 211 // is moved from one type of thread to another. 212 // pCmdData points to a memory holding effect_offload_param_t structure 213 if (cmdCode == EFFECT_CMD_OFFLOAD) { 214 ALOGV("Effect_command() cmdCode = EFFECT_CMD_OFFLOAD"); 215 if (cmdSize == 0 || pCmdData == NULL) { 216 ALOGV("effectsOffload: Effect_command: CMD_OFFLOAD has no data"); 217 *(int*)pReplyData = FAILED_TRANSACTION; 218 return FAILED_TRANSACTION; 219 } 220 effect_offload_param_t* offloadParam = (effect_offload_param_t*)pCmdData; 221 // Assign the effect context index based on isOffload field of the structure 222 pContext->index = offloadParam->isOffload ? SUB_FX_OFFLOAD : SUB_FX_HOST; 223 // if the index is HW and the HW effect is unavailable, return error 224 // and reset the index to SW 225 if (pContext->eHandle[pContext->index] == NULL) { 226 ALOGV("Effect_command()CMD_OFFLOAD sub effect unavailable"); 227 *(int*)pReplyData = FAILED_TRANSACTION; 228 return FAILED_TRANSACTION; 229 } 230 pContext->ioId = offloadParam->ioHandle; 231 ALOGV("Effect_command()CMD_OFFLOAD index:%d io %d", pContext->index, pContext->ioId); 232 // Update the DSP wrapper with the new ioHandle. 233 // Pass the OFFLOAD command to the wrapper. 234 // The DSP wrapper needs to handle this CMD 235 if (pContext->eHandle[SUB_FX_OFFLOAD]) 236 status = (*pContext->eHandle[SUB_FX_OFFLOAD])->command( 237 pContext->eHandle[SUB_FX_OFFLOAD], cmdCode, cmdSize, 238 pCmdData, replySize, pReplyData); 239 return status; 240 } 241 242 int index = pContext->index; 243 if (index != SUB_FX_HOST && index != SUB_FX_OFFLOAD) { 244 ALOGV("Effect_command: effect index is neither offload nor host"); 245 return -EINVAL; 246 } 247 248 // Getter commands are only sent to the active sub effect. 249 int *subStatus[SUB_FX_COUNT]; 250 uint32_t *subReplySize[SUB_FX_COUNT]; 251 void *subReplyData[SUB_FX_COUNT]; 252 uint32_t tmpSize; 253 int tmpStatus; 254 255 // grow temp reply buffer if needed 256 if (replySize != NULL) { 257 tmpSize = pContext->replySize; 258 while (tmpSize < *replySize && tmpSize < PROXY_REPLY_SIZE_MAX) { 259 tmpSize *= 2; 260 } 261 if (tmpSize > pContext->replySize) { 262 ALOGV("Effect_command grow reply buf to %d", tmpSize); 263 pContext->replyData = (char *)realloc(pContext->replyData, tmpSize); 264 pContext->replySize = tmpSize; 265 } 266 if (tmpSize > *replySize) { 267 tmpSize = *replySize; 268 } 269 } else { 270 tmpSize = 0; 271 } 272 // tmpSize is now the actual reply size for the non active sub effect 273 274 // Send command to sub effects. The command is sent to all sub effects so that their internal 275 // state is kept in sync. 276 // Only the reply from the active sub effect is returned to the caller. The reply from the 277 // other sub effect is lost in pContext->replyData 278 for (int i = 0; i < SUB_FX_COUNT; i++) { 279 if (pContext->eHandle[i] == NULL) { 280 continue; 281 } 282 if (i == index) { 283 subStatus[i] = &status; 284 subReplySize[i] = replySize; 285 subReplyData[i] = pReplyData; 286 } else { 287 subStatus[i] = &tmpStatus; 288 subReplySize[i] = replySize == NULL ? NULL : &tmpSize; 289 subReplyData[i] = pReplyData == NULL ? NULL : pContext->replyData; 290 } 291 *subStatus[i] = (*pContext->eHandle[i])->command( 292 pContext->eHandle[i], cmdCode, cmdSize, 293 pCmdData, subReplySize[i], subReplyData[i]); 294 } 295 296 return status; 297} /* end Effect_command */ 298 299 300/* Effect Control Interface Implementation: get_descriptor */ 301int Effect_getDescriptor(effect_handle_t self, 302 effect_descriptor_t *pDescriptor) { 303 304 EffectContext * pContext = (EffectContext *) self; 305 const effect_descriptor_t *desc; 306 307 ALOGV("Effect_getDescriptor"); 308 if (pContext == NULL || pDescriptor == NULL) { 309 ALOGV("Effect_getDescriptor() invalid param"); 310 return -EINVAL; 311 } 312 if (pContext->desc == NULL) { 313 ALOGV("Effect_getDescriptor() could not get descriptor"); 314 return -EINVAL; 315 } 316 desc = &pContext->desc[SUB_FX_HOST]; 317 *pDescriptor = *desc; 318 pDescriptor->uuid = pContext->uuid; // Replace the uuid with the Proxy UUID 319 // Also set/clear the EFFECT_FLAG_OFFLOAD_SUPPORTED flag based on the sub effects availability 320 if (pContext->eHandle[SUB_FX_OFFLOAD] != NULL) 321 pDescriptor->flags |= EFFECT_FLAG_OFFLOAD_SUPPORTED; 322 else 323 pDescriptor->flags &= ~EFFECT_FLAG_OFFLOAD_SUPPORTED; 324 return 0; 325} /* end Effect_getDescriptor */ 326 327} // namespace android 328 329__attribute__ ((visibility ("default"))) 330audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = { 331 tag : AUDIO_EFFECT_LIBRARY_TAG, 332 version : EFFECT_LIBRARY_API_VERSION, 333 name : "Effect Proxy", 334 implementor : "AOSP", 335 create_effect : android::EffectProxyCreate, 336 release_effect : android::EffectProxyRelease, 337 get_descriptor : android::EffectProxyGetDescriptor, 338}; 339