Eric Laurent | 73fb11d | 2013-04-09 11:24:13 -0700 | [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 "voice_processing" |
| 18 | /*#define LOG_NDEBUG 0*/ |
| 19 | #include <cutils/log.h> |
| 20 | #include <cutils/list.h> |
| 21 | #include <hardware/audio_effect.h> |
| 22 | #include <audio_effects/effect_aec.h> |
| 23 | #include <audio_effects/effect_agc.h> |
| 24 | #include <audio_effects/effect_ns.h> |
| 25 | |
| 26 | |
| 27 | //------------------------------------------------------------------------------ |
| 28 | // local definitions |
| 29 | //------------------------------------------------------------------------------ |
| 30 | |
| 31 | // types of pre processing modules |
| 32 | enum effect_id |
| 33 | { |
| 34 | AEC_ID, // Acoustic Echo Canceler |
| 35 | NS_ID, // Noise Suppressor |
| 36 | //ENABLE_AGC AGC_ID, // Automatic Gain Control |
| 37 | NUM_ID |
| 38 | }; |
| 39 | |
| 40 | // Session state |
| 41 | enum session_state { |
| 42 | SESSION_STATE_INIT, // initialized |
| 43 | SESSION_STATE_CONFIG // configuration received |
| 44 | }; |
| 45 | |
| 46 | // Effect/Preprocessor state |
| 47 | enum effect_state { |
| 48 | EFFECT_STATE_INIT, // initialized |
| 49 | EFFECT_STATE_CREATED, // webRTC engine created |
| 50 | EFFECT_STATE_CONFIG, // configuration received/disabled |
| 51 | EFFECT_STATE_ACTIVE // active/enabled |
| 52 | }; |
| 53 | |
| 54 | // Effect context |
| 55 | struct effect_s { |
| 56 | const struct effect_interface_s *itfe; |
| 57 | uint32_t id; // type of pre processor (enum effect_id) |
| 58 | uint32_t state; // current state (enum effect_state) |
| 59 | struct session_s *session; // session the effect is on |
| 60 | }; |
| 61 | |
| 62 | // Session context |
| 63 | struct session_s { |
| 64 | struct listnode node; |
| 65 | effect_config_t config; |
| 66 | struct effect_s effects[NUM_ID]; // effects in this session |
| 67 | uint32_t state; // current state (enum session_state) |
| 68 | int id; // audio session ID |
| 69 | int io; // handle of input stream this session is on |
| 70 | uint32_t created_msk; // bit field containing IDs of crested pre processors |
| 71 | uint32_t enabled_msk; // bit field containing IDs of enabled pre processors |
| 72 | uint32_t processed_msk; // bit field containing IDs of pre processors already |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | //------------------------------------------------------------------------------ |
| 77 | // Effect descriptors |
| 78 | //------------------------------------------------------------------------------ |
| 79 | |
| 80 | // UUIDs for effect types have been generated from http://www.itu.int/ITU-T/asn1/uuid.html |
| 81 | // as the pre processing effects are not defined by OpenSL ES |
| 82 | |
| 83 | // Acoustic Echo Cancellation |
| 84 | static const effect_descriptor_t aec_descriptor = { |
| 85 | { 0x7b491460, 0x8d4d, 0x11e0, 0xbd61, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // type |
| 86 | { 0x0f8d0d2a, 0x59e5, 0x45fe, 0xb6e4, { 0x24, 0x8c, 0x8a, 0x79, 0x91, 0x09 } }, // uuid |
| 87 | EFFECT_CONTROL_API_VERSION, |
| 88 | (EFFECT_FLAG_TYPE_PRE_PROC|EFFECT_FLAG_DEVICE_IND), |
| 89 | 0, |
| 90 | 0, |
| 91 | "Acoustic Echo Canceler", |
| 92 | "Qualcomm Fluence" |
| 93 | }; |
| 94 | |
| 95 | // Noise suppression |
| 96 | static const effect_descriptor_t ns_descriptor = { |
| 97 | { 0x58b4b260, 0x8e06, 0x11e0, 0xaa8e, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // type |
| 98 | { 0x1d97bb0b, 0x9e2f, 0x4403, 0x9ae3, { 0x58, 0xc2, 0x55, 0x43, 0x06, 0xf8 } }, // uuid |
| 99 | EFFECT_CONTROL_API_VERSION, |
| 100 | (EFFECT_FLAG_TYPE_PRE_PROC|EFFECT_FLAG_DEVICE_IND), |
| 101 | 0, |
| 102 | 0, |
| 103 | "Noise Suppression", |
| 104 | "Qualcomm Fluence" |
| 105 | }; |
| 106 | |
| 107 | //ENABLE_AGC |
| 108 | // Automatic Gain Control |
| 109 | //static const effect_descriptor_t agc_descriptor = { |
| 110 | // { 0x0a8abfe0, 0x654c, 0x11e0, 0xba26, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // type |
| 111 | // { 0x0dd49521, 0x8c59, 0x40b1, 0xb403, { 0xe0, 0x8d, 0x5f, 0x01, 0x87, 0x5e } }, // uuid |
| 112 | // EFFECT_CONTROL_API_VERSION, |
| 113 | // (EFFECT_FLAG_TYPE_PRE_PROC|EFFECT_FLAG_DEVICE_IND), |
| 114 | // 0, |
| 115 | // 0, |
| 116 | // "Automatic Gain Control", |
| 117 | // "Qualcomm Fluence" |
| 118 | //}; |
| 119 | |
| 120 | static const effect_descriptor_t *descriptors[NUM_ID] = { |
| 121 | &aec_descriptor, |
| 122 | &ns_descriptor, |
| 123 | //ENABLE_AGC &agc_descriptor, |
| 124 | }; |
| 125 | |
| 126 | |
| 127 | static int init_status = 1; |
| 128 | struct listnode session_list; |
| 129 | static const struct effect_interface_s effect_interface; |
| 130 | static const effect_uuid_t * uuid_to_id_table[NUM_ID]; |
| 131 | |
| 132 | //------------------------------------------------------------------------------ |
| 133 | // Helper functions |
| 134 | //------------------------------------------------------------------------------ |
| 135 | |
| 136 | static const effect_uuid_t * id_to_uuid(int id) |
| 137 | { |
| 138 | if (id >= NUM_ID) |
| 139 | return EFFECT_UUID_NULL; |
| 140 | |
| 141 | return uuid_to_id_table[id]; |
| 142 | } |
| 143 | |
| 144 | static uint32_t uuid_to_id(const effect_uuid_t * uuid) |
| 145 | { |
| 146 | size_t i; |
| 147 | for (i = 0; i < NUM_ID; i++) |
| 148 | if (memcmp(uuid, uuid_to_id_table[i], sizeof(*uuid)) == 0) |
| 149 | break; |
| 150 | |
| 151 | return i; |
| 152 | } |
| 153 | |
| 154 | //------------------------------------------------------------------------------ |
| 155 | // Effect functions |
| 156 | //------------------------------------------------------------------------------ |
| 157 | |
| 158 | static void session_set_fx_enabled(struct session_s *session, uint32_t id, bool enabled); |
| 159 | |
| 160 | #define BAD_STATE_ABORT(from, to) \ |
| 161 | LOG_ALWAYS_FATAL("Bad state transition from %d to %d", from, to); |
| 162 | |
| 163 | static int effect_set_state(struct effect_s *effect, uint32_t state) |
| 164 | { |
| 165 | int status = 0; |
| 166 | ALOGV("effect_set_state() id %d, new %d old %d", effect->id, state, effect->state); |
| 167 | switch(state) { |
| 168 | case EFFECT_STATE_INIT: |
| 169 | switch(effect->state) { |
| 170 | case EFFECT_STATE_ACTIVE: |
| 171 | session_set_fx_enabled(effect->session, effect->id, false); |
| 172 | case EFFECT_STATE_CONFIG: |
| 173 | case EFFECT_STATE_CREATED: |
| 174 | case EFFECT_STATE_INIT: |
| 175 | break; |
| 176 | default: |
| 177 | BAD_STATE_ABORT(effect->state, state); |
| 178 | } |
| 179 | break; |
| 180 | case EFFECT_STATE_CREATED: |
| 181 | switch(effect->state) { |
| 182 | case EFFECT_STATE_INIT: |
| 183 | break; |
| 184 | case EFFECT_STATE_CREATED: |
| 185 | case EFFECT_STATE_ACTIVE: |
| 186 | case EFFECT_STATE_CONFIG: |
| 187 | ALOGE("effect_set_state() invalid transition"); |
| 188 | status = -ENOSYS; |
| 189 | break; |
| 190 | default: |
| 191 | BAD_STATE_ABORT(effect->state, state); |
| 192 | } |
| 193 | break; |
| 194 | case EFFECT_STATE_CONFIG: |
| 195 | switch(effect->state) { |
| 196 | case EFFECT_STATE_INIT: |
| 197 | ALOGE("effect_set_state() invalid transition"); |
| 198 | status = -ENOSYS; |
| 199 | break; |
| 200 | case EFFECT_STATE_ACTIVE: |
| 201 | session_set_fx_enabled(effect->session, effect->id, false); |
| 202 | break; |
| 203 | case EFFECT_STATE_CREATED: |
| 204 | case EFFECT_STATE_CONFIG: |
| 205 | break; |
| 206 | default: |
| 207 | BAD_STATE_ABORT(effect->state, state); |
| 208 | } |
| 209 | break; |
| 210 | case EFFECT_STATE_ACTIVE: |
| 211 | switch(effect->state) { |
| 212 | case EFFECT_STATE_INIT: |
| 213 | case EFFECT_STATE_CREATED: |
| 214 | ALOGE("effect_set_state() invalid transition"); |
| 215 | status = -ENOSYS; |
| 216 | break; |
| 217 | case EFFECT_STATE_ACTIVE: |
| 218 | // enabling an already enabled effect is just ignored |
| 219 | break; |
| 220 | case EFFECT_STATE_CONFIG: |
| 221 | session_set_fx_enabled(effect->session, effect->id, true); |
| 222 | break; |
| 223 | default: |
| 224 | BAD_STATE_ABORT(effect->state, state); |
| 225 | } |
| 226 | break; |
| 227 | default: |
| 228 | BAD_STATE_ABORT(effect->state, state); |
| 229 | } |
| 230 | |
| 231 | if (status == 0) |
| 232 | effect->state = state; |
| 233 | |
| 234 | return status; |
| 235 | } |
| 236 | |
| 237 | static int effect_init(struct effect_s *effect, uint32_t id) |
| 238 | { |
| 239 | effect->itfe = &effect_interface; |
| 240 | effect->id = id; |
| 241 | effect->state = EFFECT_STATE_INIT; |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int effect_create(struct effect_s *effect, |
| 246 | struct session_s *session, |
| 247 | effect_handle_t *interface) |
| 248 | { |
| 249 | effect->session = session; |
| 250 | *interface = (effect_handle_t)&effect->itfe; |
| 251 | return effect_set_state(effect, EFFECT_STATE_CREATED); |
| 252 | } |
| 253 | |
| 254 | static int effect_release(struct effect_s *effect) |
| 255 | { |
| 256 | return effect_set_state(effect, EFFECT_STATE_INIT); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | //------------------------------------------------------------------------------ |
| 261 | // Session functions |
| 262 | //------------------------------------------------------------------------------ |
| 263 | |
| 264 | static int session_init(struct session_s *session) |
| 265 | { |
| 266 | size_t i; |
| 267 | int status = 0; |
| 268 | |
| 269 | session->state = SESSION_STATE_INIT; |
| 270 | session->id = 0; |
| 271 | session->io = 0; |
| 272 | session->created_msk = 0; |
| 273 | for (i = 0; i < NUM_ID && status == 0; i++) |
| 274 | status = effect_init(&session->effects[i], i); |
| 275 | |
| 276 | return status; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | static int session_create_effect(struct session_s *session, |
| 281 | int32_t id, |
| 282 | effect_handle_t *interface) |
| 283 | { |
| 284 | int status = -ENOMEM; |
| 285 | |
| 286 | ALOGV("session_create_effect() %s, created_msk %08x", |
| 287 | id == AEC_ID ? "AEC" : id == NS_ID ? "NS" : "?", session->created_msk); |
| 288 | |
| 289 | if (session->created_msk == 0) { |
| 290 | session->config.inputCfg.samplingRate = 16000; |
| 291 | session->config.inputCfg.channels = AUDIO_CHANNEL_IN_MONO; |
| 292 | session->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 293 | session->config.outputCfg.samplingRate = 16000; |
| 294 | session->config.outputCfg.channels = AUDIO_CHANNEL_IN_MONO; |
| 295 | session->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 296 | session->enabled_msk = 0; |
| 297 | session->processed_msk = 0; |
| 298 | } |
| 299 | status = effect_create(&session->effects[id], session, interface); |
| 300 | if (status < 0) |
| 301 | goto error; |
| 302 | |
| 303 | ALOGV("session_create_effect() OK"); |
| 304 | session->created_msk |= (1<<id); |
| 305 | return status; |
| 306 | |
| 307 | error: |
| 308 | return status; |
| 309 | } |
| 310 | |
| 311 | static int session_release_effect(struct session_s *session, |
| 312 | struct effect_s *fx) |
| 313 | { |
| 314 | ALOGW_IF(effect_release(fx) != 0, " session_release_effect() failed for id %d", fx->id); |
| 315 | |
| 316 | session->created_msk &= ~(1<<fx->id); |
| 317 | if (session->created_msk == 0) |
| 318 | { |
| 319 | ALOGV("session_release_effect() last effect: removing session"); |
| 320 | list_remove(&session->node); |
| 321 | free(session); |
| 322 | } |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | static int session_set_config(struct session_s *session, effect_config_t *config) |
| 329 | { |
| 330 | int status; |
| 331 | |
| 332 | if (config->inputCfg.samplingRate != config->outputCfg.samplingRate || |
| 333 | config->inputCfg.format != config->outputCfg.format || |
| 334 | config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) |
| 335 | return -EINVAL; |
| 336 | |
| 337 | ALOGV("session_set_config() sampling rate %d channels %08x", |
| 338 | config->inputCfg.samplingRate, config->inputCfg.channels); |
| 339 | |
| 340 | // if at least one process is enabled, do not accept configuration changes |
| 341 | if (session->enabled_msk) { |
| 342 | if (session->config.inputCfg.samplingRate != config->inputCfg.samplingRate || |
| 343 | session->config.inputCfg.channels != config->inputCfg.channels || |
| 344 | session->config.outputCfg.channels != config->outputCfg.channels) |
| 345 | return -ENOSYS; |
| 346 | else |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | memcpy(&session->config, config, sizeof(effect_config_t)); |
| 351 | |
| 352 | session->state = SESSION_STATE_CONFIG; |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static void session_get_config(struct session_s *session, effect_config_t *config) |
| 357 | { |
| 358 | memcpy(config, &session->config, sizeof(effect_config_t)); |
| 359 | |
| 360 | config->inputCfg.mask = config->outputCfg.mask = |
| 361 | (EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT); |
| 362 | } |
| 363 | |
| 364 | |
| 365 | static void session_set_fx_enabled(struct session_s *session, uint32_t id, bool enabled) |
| 366 | { |
| 367 | if (enabled) { |
| 368 | if(session->enabled_msk == 0) { |
| 369 | /* do first enable here */ |
| 370 | } |
| 371 | session->enabled_msk |= (1 << id); |
| 372 | } else { |
| 373 | session->enabled_msk &= ~(1 << id); |
| 374 | if(session->enabled_msk == 0) { |
| 375 | /* do last enable here */ |
| 376 | } |
| 377 | } |
| 378 | ALOGV("session_set_fx_enabled() id %d, enabled %d enabled_msk %08x", |
| 379 | id, enabled, session->enabled_msk); |
| 380 | session->processed_msk = 0; |
| 381 | } |
| 382 | |
| 383 | //------------------------------------------------------------------------------ |
| 384 | // Global functions |
| 385 | //------------------------------------------------------------------------------ |
| 386 | |
| 387 | static struct session_s *get_session(int32_t id, int32_t sessionId, int32_t ioId) |
| 388 | { |
| 389 | size_t i; |
| 390 | int free = -1; |
| 391 | struct listnode *node; |
| 392 | struct session_s *session; |
| 393 | |
| 394 | list_for_each(node, &session_list) { |
| 395 | session = node_to_item(node, struct session_s, node); |
| 396 | if (session->io == ioId) { |
| 397 | if (session->created_msk & (1 << id)) { |
| 398 | ALOGV("get_session() effect %d already created", id); |
| 399 | return NULL; |
| 400 | } |
| 401 | ALOGV("get_session() found session %p", session); |
| 402 | return session; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | session = (struct session_s *)calloc(1, sizeof(struct session_s)); |
| 407 | session_init(session); |
| 408 | session->id = sessionId; |
| 409 | session->io = ioId; |
| 410 | list_add_tail(&session_list, &session->node); |
| 411 | |
| 412 | ALOGV("get_session() created session %p", session); |
| 413 | |
| 414 | return session; |
| 415 | } |
| 416 | |
| 417 | static int init() { |
| 418 | size_t i; |
| 419 | int status = 0; |
| 420 | |
| 421 | if (init_status <= 0) |
| 422 | return init_status; |
| 423 | |
| 424 | uuid_to_id_table[AEC_ID] = FX_IID_AEC; |
| 425 | uuid_to_id_table[NS_ID] = FX_IID_NS; |
| 426 | |
| 427 | list_init(&session_list); |
| 428 | |
| 429 | init_status = 0; |
| 430 | return init_status; |
| 431 | } |
| 432 | |
| 433 | static const effect_descriptor_t *get_descriptor(const effect_uuid_t *uuid) |
| 434 | { |
| 435 | size_t i; |
| 436 | for (i = 0; i < NUM_ID; i++) |
| 437 | if (memcmp(&descriptors[i]->uuid, uuid, sizeof(effect_uuid_t)) == 0) |
| 438 | return descriptors[i]; |
| 439 | |
| 440 | return NULL; |
| 441 | } |
| 442 | |
| 443 | |
| 444 | //------------------------------------------------------------------------------ |
| 445 | // Effect Control Interface Implementation |
| 446 | //------------------------------------------------------------------------------ |
| 447 | |
| 448 | static int fx_process(effect_handle_t self, |
| 449 | audio_buffer_t *inBuffer, |
| 450 | audio_buffer_t *outBuffer) |
| 451 | { |
| 452 | struct effect_s *effect = (struct effect_s *)self; |
| 453 | struct session_s *session; |
| 454 | |
| 455 | if (effect == NULL) { |
| 456 | ALOGV("fx_process() ERROR effect == NULL"); |
| 457 | return -EINVAL; |
| 458 | } |
| 459 | |
| 460 | if (inBuffer == NULL || inBuffer->raw == NULL || |
| 461 | outBuffer == NULL || outBuffer->raw == NULL) { |
| 462 | ALOGW("fx_process() ERROR bad pointer"); |
| 463 | return -EINVAL; |
| 464 | } |
| 465 | |
| 466 | session = (struct session_s *)effect->session; |
| 467 | |
| 468 | session->processed_msk |= (1<<effect->id); |
| 469 | |
| 470 | if ((session->processed_msk & session->enabled_msk) == session->enabled_msk) { |
| 471 | effect->session->processed_msk = 0; |
| 472 | return 0; |
| 473 | } else |
| 474 | return -ENODATA; |
| 475 | } |
| 476 | |
| 477 | static int fx_command(effect_handle_t self, |
| 478 | uint32_t cmdCode, |
| 479 | uint32_t cmdSize, |
| 480 | void *pCmdData, |
| 481 | uint32_t *replySize, |
| 482 | void *pReplyData) |
| 483 | { |
| 484 | struct effect_s *effect = (struct effect_s *)self; |
| 485 | |
| 486 | if (effect == NULL) |
| 487 | return -EINVAL; |
| 488 | |
| 489 | //ALOGV("fx_command: command %d cmdSize %d",cmdCode, cmdSize); |
| 490 | |
| 491 | switch (cmdCode) { |
| 492 | case EFFECT_CMD_INIT: |
| 493 | if (pReplyData == NULL || *replySize != sizeof(int)) |
| 494 | return -EINVAL; |
| 495 | |
| 496 | *(int *)pReplyData = 0; |
| 497 | break; |
| 498 | |
| 499 | case EFFECT_CMD_SET_CONFIG: { |
| 500 | if (pCmdData == NULL|| |
| 501 | cmdSize != sizeof(effect_config_t)|| |
| 502 | pReplyData == NULL|| |
| 503 | *replySize != sizeof(int)) { |
| 504 | ALOGV("fx_command() EFFECT_CMD_SET_CONFIG invalid args"); |
| 505 | return -EINVAL; |
| 506 | } |
| 507 | *(int *)pReplyData = session_set_config(effect->session, (effect_config_t *)pCmdData); |
| 508 | if (*(int *)pReplyData != 0) |
| 509 | break; |
| 510 | |
| 511 | if (effect->state != EFFECT_STATE_ACTIVE) |
| 512 | *(int *)pReplyData = effect_set_state(effect, EFFECT_STATE_CONFIG); |
| 513 | |
| 514 | } break; |
| 515 | |
| 516 | case EFFECT_CMD_GET_CONFIG: |
| 517 | if (pReplyData == NULL || |
| 518 | *replySize != sizeof(effect_config_t)) { |
| 519 | ALOGV("fx_command() EFFECT_CMD_GET_CONFIG invalid args"); |
| 520 | return -EINVAL; |
| 521 | } |
| 522 | |
| 523 | session_get_config(effect->session, (effect_config_t *)pReplyData); |
| 524 | break; |
| 525 | |
| 526 | case EFFECT_CMD_RESET: |
| 527 | break; |
| 528 | |
| 529 | case EFFECT_CMD_GET_PARAM: { |
| 530 | if (pCmdData == NULL || |
| 531 | cmdSize < (int)sizeof(effect_param_t) || |
| 532 | pReplyData == NULL || |
| 533 | *replySize < (int)sizeof(effect_param_t)) { |
| 534 | ALOGV("fx_command() EFFECT_CMD_GET_PARAM invalid args"); |
| 535 | return -EINVAL; |
| 536 | } |
| 537 | effect_param_t *p = (effect_param_t *)pCmdData; |
| 538 | |
| 539 | memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + p->psize); |
| 540 | p = (effect_param_t *)pReplyData; |
| 541 | p->status = -ENOSYS; |
| 542 | |
| 543 | } break; |
| 544 | |
| 545 | case EFFECT_CMD_SET_PARAM: { |
| 546 | if (pCmdData == NULL|| |
| 547 | cmdSize < (int)sizeof(effect_param_t) || |
| 548 | pReplyData == NULL || |
| 549 | *replySize != sizeof(int32_t)) { |
| 550 | ALOGV("fx_command() EFFECT_CMD_SET_PARAM invalid args"); |
| 551 | return -EINVAL; |
| 552 | } |
| 553 | effect_param_t *p = (effect_param_t *) pCmdData; |
| 554 | |
| 555 | if (p->psize != sizeof(int32_t)) { |
| 556 | ALOGV("fx_command() EFFECT_CMD_SET_PARAM invalid param format"); |
| 557 | return -EINVAL; |
| 558 | } |
| 559 | *(int *)pReplyData = -ENOSYS; |
| 560 | } break; |
| 561 | |
| 562 | case EFFECT_CMD_ENABLE: |
| 563 | if (pReplyData == NULL || *replySize != sizeof(int)) { |
| 564 | ALOGV("fx_command() EFFECT_CMD_ENABLE invalid args"); |
| 565 | return -EINVAL; |
| 566 | } |
| 567 | *(int *)pReplyData = effect_set_state(effect, EFFECT_STATE_ACTIVE); |
| 568 | break; |
| 569 | |
| 570 | case EFFECT_CMD_DISABLE: |
| 571 | if (pReplyData == NULL || *replySize != sizeof(int)) { |
| 572 | ALOGV("fx_command() EFFECT_CMD_DISABLE invalid args"); |
| 573 | return -EINVAL; |
| 574 | } |
| 575 | *(int *)pReplyData = effect_set_state(effect, EFFECT_STATE_CONFIG); |
| 576 | break; |
| 577 | |
| 578 | case EFFECT_CMD_SET_DEVICE: |
| 579 | case EFFECT_CMD_SET_INPUT_DEVICE: |
| 580 | case EFFECT_CMD_SET_VOLUME: |
| 581 | case EFFECT_CMD_SET_AUDIO_MODE: |
| 582 | if (pCmdData == NULL || |
| 583 | cmdSize != sizeof(uint32_t)) { |
| 584 | ALOGV("fx_command() %s invalid args", |
| 585 | cmdCode == EFFECT_CMD_SET_DEVICE ? "EFFECT_CMD_SET_DEVICE" : |
| 586 | cmdCode == EFFECT_CMD_SET_INPUT_DEVICE ? "EFFECT_CMD_SET_INPUT_DEVICE" : |
| 587 | cmdCode == EFFECT_CMD_SET_VOLUME ? "EFFECT_CMD_SET_VOLUME" : |
| 588 | cmdCode == EFFECT_CMD_SET_AUDIO_MODE ? "EFFECT_CMD_SET_AUDIO_MODE" : |
| 589 | ""); |
| 590 | return -EINVAL; |
| 591 | } |
| 592 | ALOGV("fx_command() %s value %08x", |
| 593 | cmdCode == EFFECT_CMD_SET_DEVICE ? "EFFECT_CMD_SET_DEVICE" : |
| 594 | cmdCode == EFFECT_CMD_SET_INPUT_DEVICE ? "EFFECT_CMD_SET_INPUT_DEVICE" : |
| 595 | cmdCode == EFFECT_CMD_SET_VOLUME ? "EFFECT_CMD_SET_VOLUME" : |
| 596 | cmdCode == EFFECT_CMD_SET_AUDIO_MODE ? "EFFECT_CMD_SET_AUDIO_MODE": |
| 597 | "", |
| 598 | *(int *)pCmdData); |
| 599 | break; |
| 600 | |
| 601 | default: |
| 602 | return -EINVAL; |
| 603 | } |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | |
| 608 | static int fx_get_descriptor(effect_handle_t self, |
| 609 | effect_descriptor_t *pDescriptor) |
| 610 | { |
| 611 | struct effect_s *effect = (struct effect_s *)self; |
| 612 | |
| 613 | if (effect == NULL || pDescriptor == NULL) |
| 614 | return -EINVAL; |
| 615 | |
| 616 | *pDescriptor = *descriptors[effect->id]; |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | |
| 622 | // effect_handle_t interface implementation for effect |
| 623 | static const struct effect_interface_s effect_interface = { |
| 624 | fx_process, |
| 625 | fx_command, |
| 626 | fx_get_descriptor, |
| 627 | NULL |
| 628 | }; |
| 629 | |
| 630 | //------------------------------------------------------------------------------ |
| 631 | // Effect Library Interface Implementation |
| 632 | //------------------------------------------------------------------------------ |
| 633 | |
| 634 | static int lib_create(const effect_uuid_t *uuid, |
| 635 | int32_t sessionId, |
| 636 | int32_t ioId, |
| 637 | effect_handle_t *pInterface) |
| 638 | { |
| 639 | ALOGV("lib_create: uuid: %08x session %d IO: %d", uuid->timeLow, sessionId, ioId); |
| 640 | |
| 641 | int status; |
| 642 | const effect_descriptor_t *desc; |
| 643 | struct session_s *session; |
| 644 | uint32_t id; |
| 645 | |
| 646 | if (init() != 0) |
| 647 | return init_status; |
| 648 | |
| 649 | desc = get_descriptor(uuid); |
| 650 | |
| 651 | if (desc == NULL) { |
| 652 | ALOGW("lib_create: fx not found uuid: %08x", uuid->timeLow); |
| 653 | return -EINVAL; |
| 654 | } |
| 655 | id = uuid_to_id(&desc->type); |
| 656 | |
| 657 | session = get_session(id, sessionId, ioId); |
| 658 | |
| 659 | if (session == NULL) { |
| 660 | ALOGW("lib_create: no more session available"); |
| 661 | return -EINVAL; |
| 662 | } |
| 663 | |
| 664 | status = session_create_effect(session, id, pInterface); |
| 665 | |
| 666 | if (status < 0 && session->created_msk == 0) { |
| 667 | list_remove(&session->node); |
| 668 | free(session); |
| 669 | } |
| 670 | return status; |
| 671 | } |
| 672 | |
| 673 | static int lib_release(effect_handle_t interface) |
| 674 | { |
| 675 | struct listnode *node; |
| 676 | struct session_s *session; |
| 677 | |
| 678 | ALOGV("lib_release %p", interface); |
| 679 | if (init() != 0) |
| 680 | return init_status; |
| 681 | |
| 682 | struct effect_s *fx = (struct effect_s *)interface; |
| 683 | |
| 684 | list_for_each(node, &session_list) { |
| 685 | session = node_to_item(node, struct session_s, node); |
| 686 | if (session == fx->session) { |
| 687 | session_release_effect(fx->session, fx); |
| 688 | return 0; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | return -EINVAL; |
| 693 | } |
| 694 | |
| 695 | static int lib_get_descriptor(const effect_uuid_t *uuid, |
| 696 | effect_descriptor_t *pDescriptor) |
| 697 | { |
| 698 | const effect_descriptor_t *desc; |
| 699 | |
| 700 | if (pDescriptor == NULL || uuid == NULL) |
| 701 | return -EINVAL; |
| 702 | |
| 703 | desc = get_descriptor(uuid); |
| 704 | if (desc == NULL) { |
| 705 | ALOGV("lib_get_descriptor() not found"); |
| 706 | return -EINVAL; |
| 707 | } |
| 708 | |
| 709 | ALOGV("lib_get_descriptor() got fx %s", desc->name); |
| 710 | |
| 711 | *pDescriptor = *desc; |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | // This is the only symbol that needs to be exported |
| 716 | __attribute__ ((visibility ("default"))) |
| 717 | audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = { |
| 718 | tag : AUDIO_EFFECT_LIBRARY_TAG, |
| 719 | version : EFFECT_LIBRARY_API_VERSION, |
| 720 | name : "MSM8960 Audio Preprocessing Library", |
| 721 | implementor : "The Android Open Source Project", |
| 722 | create_effect : lib_create, |
| 723 | release_effect : lib_release, |
| 724 | get_descriptor : lib_get_descriptor |
| 725 | }; |