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