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