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