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