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