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