jpadmana | 60c60df | 2013-06-04 16:03:29 +0530 | [diff] [blame] | 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 | |
| 29 | namespace 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 |
| 33 | const 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 | |
| 46 | static const effect_descriptor_t *const gDescriptors[] = |
| 47 | { |
| 48 | &gProxyDescriptor, |
| 49 | }; |
| 50 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame^] | 51 | static inline bool isGetterCmd(uint32_t cmdCode) |
| 52 | { |
| 53 | switch (cmdCode) { |
| 54 | case EFFECT_CMD_GET_PARAM: |
| 55 | case EFFECT_CMD_GET_CONFIG: |
| 56 | case EFFECT_CMD_GET_CONFIG_REVERSE: |
| 57 | case EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS: |
| 58 | case EFFECT_CMD_GET_FEATURE_CONFIG: |
| 59 | return true; |
| 60 | default: |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
jpadmana | 60c60df | 2013-06-04 16:03:29 +0530 | [diff] [blame] | 66 | int EffectProxyCreate(const effect_uuid_t *uuid, |
| 67 | int32_t sessionId, |
| 68 | int32_t ioId, |
| 69 | effect_handle_t *pHandle) { |
| 70 | |
| 71 | effect_descriptor_t* desc; |
| 72 | EffectContext* pContext; |
| 73 | if (pHandle == NULL || uuid == NULL) { |
| 74 | ALOGE("EffectProxyCreate() called with NULL pointer"); |
| 75 | return -EINVAL; |
| 76 | } |
| 77 | ALOGV("EffectProxyCreate start.."); |
| 78 | pContext = new EffectContext; |
| 79 | pContext->sessionId = sessionId; |
| 80 | pContext->ioId = ioId; |
| 81 | pContext->uuid = *uuid; |
| 82 | pContext->common_itfe = &gEffectInterface; |
| 83 | // The sub effects will be created in effect_command when the first command |
| 84 | // for the effect is received |
| 85 | pContext->eHandle[SUB_FX_HOST] = pContext->eHandle[SUB_FX_OFFLOAD] = NULL; |
| 86 | |
| 87 | // Get the HW and SW sub effect descriptors from the effects factory |
| 88 | desc = new effect_descriptor_t[SUB_FX_COUNT]; |
| 89 | pContext->desc = new effect_descriptor_t[SUB_FX_COUNT]; |
| 90 | int retValue = EffectGetSubEffects(uuid, desc, |
| 91 | sizeof(effect_descriptor_t) * SUB_FX_COUNT); |
| 92 | // EffectGetSubEffects returns the number of sub-effects copied. |
| 93 | if (retValue != SUB_FX_COUNT) { |
| 94 | ALOGE("EffectCreate() could not get the sub effects"); |
| 95 | delete desc; |
| 96 | delete pContext->desc; |
| 97 | return -EINVAL; |
| 98 | } |
| 99 | // Check which is the HW descriptor and copy the descriptors |
| 100 | // to the Context desc array |
| 101 | // Also check if there is only one HW and one SW descriptor. |
| 102 | // HW descriptor alone has the HW_TUNNEL flag. |
| 103 | if ((desc[0].flags & EFFECT_FLAG_HW_ACC_TUNNEL) && |
| 104 | !(desc[1].flags & EFFECT_FLAG_HW_ACC_TUNNEL)) { |
| 105 | pContext->desc[SUB_FX_OFFLOAD] = desc[0]; |
| 106 | pContext->desc[SUB_FX_HOST] = desc[1]; |
| 107 | } |
| 108 | else if ((desc[1].flags & EFFECT_FLAG_HW_ACC_TUNNEL) && |
| 109 | !(desc[0].flags & EFFECT_FLAG_HW_ACC_TUNNEL)) { |
| 110 | pContext->desc[SUB_FX_HOST] = desc[0]; |
| 111 | pContext->desc[SUB_FX_OFFLOAD] = desc[1]; |
| 112 | } |
| 113 | delete desc; |
| 114 | #if (LOG_NDEBUG == 0) |
| 115 | effect_uuid_t uuid_print = pContext->desc[SUB_FX_HOST].uuid; |
| 116 | ALOGV("EffectCreate() UUID of HOST: %08X-%04X-%04X-%04X-%02X%02X%02X%02X" |
| 117 | "%02X%02X\n",uuid_print.timeLow, uuid_print.timeMid, |
| 118 | uuid_print.timeHiAndVersion, uuid_print.clockSeq, uuid_print.node[0], |
| 119 | uuid_print.node[1], uuid_print.node[2], uuid_print.node[3], |
| 120 | uuid_print.node[4], uuid_print.node[5]); |
| 121 | ALOGV("EffectCreate() UUID of OFFLOAD: %08X-%04X-%04X-%04X-%02X%02X%02X%02X" |
| 122 | "%02X%02X\n", uuid_print.timeLow, uuid_print.timeMid, |
| 123 | uuid_print.timeHiAndVersion, uuid_print.clockSeq, uuid_print.node[0], |
| 124 | uuid_print.node[1], uuid_print.node[2], uuid_print.node[3], |
| 125 | uuid_print.node[4], uuid_print.node[5]); |
| 126 | #endif |
| 127 | *pHandle = (effect_handle_t)pContext; |
| 128 | ALOGV("EffectCreate end"); |
| 129 | return 0; |
| 130 | } //end EffectProxyCreate |
| 131 | |
| 132 | int EffectProxyRelease(effect_handle_t handle) { |
| 133 | EffectContext * pContext = (EffectContext *)handle; |
| 134 | if (pContext == NULL) { |
| 135 | ALOGV("ERROR : EffectRelease called with NULL pointer"); |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | ALOGV("EffectRelease"); |
| 139 | delete pContext->desc; |
| 140 | if (pContext->eHandle[SUB_FX_HOST]) |
| 141 | EffectRelease(pContext->eHandle[SUB_FX_HOST]); |
| 142 | if (pContext->eHandle[SUB_FX_OFFLOAD]) |
| 143 | EffectRelease(pContext->eHandle[SUB_FX_OFFLOAD]); |
| 144 | delete pContext; |
| 145 | pContext = NULL; |
| 146 | return 0; |
| 147 | } /*end EffectProxyRelease */ |
| 148 | |
| 149 | int EffectProxyGetDescriptor(const effect_uuid_t *uuid, |
| 150 | effect_descriptor_t *pDescriptor) { |
| 151 | const effect_descriptor_t *desc = NULL; |
| 152 | |
| 153 | if (pDescriptor == NULL || uuid == NULL) { |
| 154 | ALOGV("EffectGetDescriptor() called with NULL pointer"); |
| 155 | return -EINVAL; |
| 156 | } |
| 157 | desc = &gProxyDescriptor; |
| 158 | *pDescriptor = *desc; |
| 159 | return 0; |
| 160 | } /* end EffectProxyGetDescriptor */ |
| 161 | |
| 162 | /* Effect Control Interface Implementation: Process */ |
| 163 | int Effect_process(effect_handle_t self, |
| 164 | audio_buffer_t *inBuffer, |
| 165 | audio_buffer_t *outBuffer) { |
| 166 | |
| 167 | EffectContext *pContext = (EffectContext *) self; |
| 168 | int ret = 0; |
| 169 | if (pContext != NULL) { |
| 170 | int index = pContext->index; |
| 171 | // if the index refers to HW , do not do anything. Just return. |
| 172 | if (index == SUB_FX_HOST) { |
jpadmana | 60c60df | 2013-06-04 16:03:29 +0530 | [diff] [blame] | 173 | ret = (*pContext->eHandle[index])->process(pContext->eHandle[index], |
| 174 | inBuffer, outBuffer); |
| 175 | } |
| 176 | } |
| 177 | return ret; |
| 178 | } /* end Effect_process */ |
| 179 | |
| 180 | /* Effect Control Interface Implementation: Command */ |
| 181 | int Effect_command(effect_handle_t self, |
| 182 | uint32_t cmdCode, |
| 183 | uint32_t cmdSize, |
| 184 | void *pCmdData, |
| 185 | uint32_t *replySize, |
| 186 | void *pReplyData) { |
| 187 | |
| 188 | EffectContext *pContext = (EffectContext *) self; |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame^] | 189 | int status = 0; |
jpadmana | 60c60df | 2013-06-04 16:03:29 +0530 | [diff] [blame] | 190 | if (pContext == NULL) { |
| 191 | ALOGV("Effect_command() Proxy context is NULL"); |
| 192 | return -EINVAL; |
| 193 | } |
| 194 | if (pContext->eHandle[SUB_FX_HOST] == NULL) { |
| 195 | ALOGV("Effect_command() Calling HOST EffectCreate"); |
| 196 | status = EffectCreate(&pContext->desc[SUB_FX_HOST].uuid, |
| 197 | pContext->sessionId, pContext->ioId, |
| 198 | &(pContext->eHandle[SUB_FX_HOST])); |
| 199 | if (status != NO_ERROR || (pContext->eHandle[SUB_FX_HOST] == NULL)) { |
| 200 | ALOGV("Effect_command() Error creating SW sub effect"); |
| 201 | return status; |
| 202 | } |
| 203 | } |
| 204 | if (pContext->eHandle[SUB_FX_OFFLOAD] == NULL) { |
| 205 | ALOGV("Effect_command() Calling OFFLOAD EffectCreate"); |
| 206 | status = EffectCreate(&pContext->desc[SUB_FX_OFFLOAD].uuid, |
| 207 | pContext->sessionId, pContext->ioId, |
| 208 | &(pContext->eHandle[SUB_FX_OFFLOAD])); |
| 209 | if (status != NO_ERROR || (pContext->eHandle[SUB_FX_OFFLOAD] == NULL)) { |
| 210 | ALOGV("Effect_command() Error creating HW effect"); |
| 211 | // Do not return error here as SW effect is created |
| 212 | // Return error if the CMD_OFFLOAD sends the index as OFFLOAD |
| 213 | } |
| 214 | pContext->index = SUB_FX_HOST; |
| 215 | } |
| 216 | // EFFECT_CMD_OFFLOAD used to (1) send whether the thread is offload or not |
| 217 | // (2) Send the ioHandle of the effectThread when the effect |
| 218 | // is moved from one type of thread to another. |
| 219 | // pCmdData points to a memory holding effect_offload_param_t structure |
| 220 | if (cmdCode == EFFECT_CMD_OFFLOAD) { |
| 221 | ALOGV("Effect_command() cmdCode = EFFECT_CMD_OFFLOAD"); |
| 222 | if (cmdSize == 0 || pCmdData == NULL) { |
| 223 | ALOGV("effectsOffload: Effect_command: CMD_OFFLOAD has no data"); |
| 224 | *(int*)pReplyData = FAILED_TRANSACTION; |
| 225 | return FAILED_TRANSACTION; |
| 226 | } |
| 227 | effect_offload_param_t* offloadParam = (effect_offload_param_t*)pCmdData; |
| 228 | // Assign the effect context index based on isOffload field of the structure |
| 229 | pContext->index = offloadParam->isOffload ? SUB_FX_OFFLOAD : SUB_FX_HOST; |
| 230 | // if the index is HW and the HW effect is unavailable, return error |
| 231 | // and reset the index to SW |
| 232 | if (pContext->eHandle[pContext->index] == NULL) { |
| 233 | ALOGV("Effect_command()CMD_OFFLOAD sub effect unavailable"); |
| 234 | *(int*)pReplyData = FAILED_TRANSACTION; |
| 235 | return FAILED_TRANSACTION; |
| 236 | } |
| 237 | pContext->ioId = offloadParam->ioHandle; |
| 238 | ALOGV("Effect_command()CMD_OFFLOAD index:%d io %d", pContext->index, pContext->ioId); |
| 239 | // Update the DSP wrapper with the new ioHandle. |
| 240 | // Pass the OFFLOAD command to the wrapper. |
| 241 | // The DSP wrapper needs to handle this CMD |
| 242 | if (pContext->eHandle[SUB_FX_OFFLOAD]) |
| 243 | status = (*pContext->eHandle[SUB_FX_OFFLOAD])->command( |
| 244 | pContext->eHandle[SUB_FX_OFFLOAD], cmdCode, cmdSize, |
| 245 | pCmdData, replySize, pReplyData); |
| 246 | return status; |
| 247 | } |
| 248 | |
| 249 | int index = pContext->index; |
| 250 | if (index != SUB_FX_HOST && index != SUB_FX_OFFLOAD) { |
| 251 | ALOGV("Effect_command: effect index is neither offload nor host"); |
| 252 | return -EINVAL; |
| 253 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame^] | 254 | |
| 255 | // Getter commands are only sent to the active sub effect. |
| 256 | uint32_t hostReplySize = replySize != NULL ? *replySize : 0; |
| 257 | bool hostReplied = false; |
| 258 | int hostStatus = 0; |
| 259 | uint32_t offloadReplySize = replySize != NULL ? *replySize : 0; |
| 260 | bool offloadReplied = false; |
| 261 | int offloadStatus = 0; |
| 262 | |
| 263 | if (pContext->eHandle[SUB_FX_HOST] && (!isGetterCmd(cmdCode) || index == SUB_FX_HOST)) { |
| 264 | hostStatus = (*pContext->eHandle[SUB_FX_HOST])->command( |
jpadmana | 60c60df | 2013-06-04 16:03:29 +0530 | [diff] [blame] | 265 | pContext->eHandle[SUB_FX_HOST], cmdCode, cmdSize, |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame^] | 266 | pCmdData, replySize != NULL ? &hostReplySize : NULL, pReplyData); |
| 267 | hostReplied = true; |
| 268 | } |
| 269 | if (pContext->eHandle[SUB_FX_OFFLOAD] && (!isGetterCmd(cmdCode) || index == SUB_FX_OFFLOAD)) { |
jpadmana | 60c60df | 2013-06-04 16:03:29 +0530 | [diff] [blame] | 270 | // In case of SET CMD, when the offload stream is unavailable, |
| 271 | // we will store the effect param values in the DSP effect wrapper. |
| 272 | // When the offload effects get enabled, we send these values to the |
| 273 | // DSP during Effect_config. |
| 274 | // So,we send the params to DSP wrapper also |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame^] | 275 | offloadStatus = (*pContext->eHandle[SUB_FX_OFFLOAD])->command( |
jpadmana | 60c60df | 2013-06-04 16:03:29 +0530 | [diff] [blame] | 276 | pContext->eHandle[SUB_FX_OFFLOAD], cmdCode, cmdSize, |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame^] | 277 | pCmdData, replySize != NULL ? &offloadReplySize : NULL, pReplyData); |
| 278 | offloadReplied = true; |
jpadmana | 60c60df | 2013-06-04 16:03:29 +0530 | [diff] [blame] | 279 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame^] | 280 | // By convention the offloaded implementation reply is returned if command is processed by both |
| 281 | // host and offloaded sub effects |
| 282 | if (offloadReplied){ |
| 283 | status = offloadStatus; |
| 284 | if (replySize) { |
| 285 | *replySize = offloadReplySize; |
| 286 | } |
| 287 | } else if (hostReplied) { |
| 288 | status = hostStatus; |
| 289 | if (replySize) { |
| 290 | *replySize = hostReplySize; |
| 291 | } |
| 292 | } |
| 293 | return status; |
jpadmana | 60c60df | 2013-06-04 16:03:29 +0530 | [diff] [blame] | 294 | } /* end Effect_command */ |
| 295 | |
| 296 | |
| 297 | /* Effect Control Interface Implementation: get_descriptor */ |
| 298 | int Effect_getDescriptor(effect_handle_t self, |
| 299 | effect_descriptor_t *pDescriptor) { |
| 300 | |
| 301 | EffectContext * pContext = (EffectContext *) self; |
| 302 | const effect_descriptor_t *desc; |
| 303 | |
| 304 | ALOGV("Effect_getDescriptor"); |
| 305 | if (pContext == NULL || pDescriptor == NULL) { |
| 306 | ALOGV("Effect_getDescriptor() invalid param"); |
| 307 | return -EINVAL; |
| 308 | } |
| 309 | if (pContext->desc == NULL) { |
| 310 | ALOGV("Effect_getDescriptor() could not get descriptor"); |
| 311 | return -EINVAL; |
| 312 | } |
| 313 | desc = &pContext->desc[SUB_FX_HOST]; |
| 314 | *pDescriptor = *desc; |
| 315 | pDescriptor->uuid = pContext->uuid; // Replace the uuid with the Proxy UUID |
| 316 | // Also set/clear the EFFECT_FLAG_OFFLOAD_SUPPORTED flag based on the sub effects availability |
| 317 | if (pContext->eHandle[SUB_FX_OFFLOAD] != NULL) |
| 318 | pDescriptor->flags |= EFFECT_FLAG_OFFLOAD_SUPPORTED; |
| 319 | else |
| 320 | pDescriptor->flags &= ~EFFECT_FLAG_OFFLOAD_SUPPORTED; |
| 321 | return 0; |
| 322 | } /* end Effect_getDescriptor */ |
| 323 | |
| 324 | } // namespace android |
| 325 | |
| 326 | __attribute__ ((visibility ("default"))) |
| 327 | audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = { |
| 328 | tag : AUDIO_EFFECT_LIBRARY_TAG, |
| 329 | version : EFFECT_LIBRARY_API_VERSION, |
| 330 | name : "Effect Proxy", |
| 331 | implementor : "AOSP", |
| 332 | create_effect : android::EffectProxyCreate, |
| 333 | release_effect : android::EffectProxyRelease, |
| 334 | get_descriptor : android::EffectProxyGetDescriptor, |
| 335 | }; |