summaryrefslogtreecommitdiffstats
path: root/voice_processing/voice_processing.c
blob: 90034115350a2f5919ce6ee73014b6998cc5139b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "voice_processing"
/*#define LOG_NDEBUG 0*/
#include <stdlib.h>
#include <dlfcn.h>
#include <cutils/log.h>
#include <cutils/list.h>
#include <hardware/audio_effect.h>
#include <audio_effects/effect_aec.h>
#include <audio_effects/effect_agc.h>
#include <audio_effects/effect_ns.h>


//------------------------------------------------------------------------------
// local definitions
//------------------------------------------------------------------------------

#define EFFECTS_DESCRIPTOR_LIBRARY_PATH "/system/lib/soundfx/libqcomvoiceprocessingdescriptors.so"

// types of pre processing modules
enum effect_id
{
    AEC_ID,        // Acoustic Echo Canceler
    NS_ID,         // Noise Suppressor
//ENABLE_AGC    AGC_ID,        // Automatic Gain Control
    NUM_ID
};

// Session state
enum session_state {
    SESSION_STATE_INIT,        // initialized
    SESSION_STATE_CONFIG       // configuration received
};

// Effect/Preprocessor state
enum effect_state {
    EFFECT_STATE_INIT,         // initialized
    EFFECT_STATE_CREATED,      // webRTC engine created
    EFFECT_STATE_CONFIG,       // configuration received/disabled
    EFFECT_STATE_ACTIVE        // active/enabled
};

// Effect context
struct effect_s {
    const struct effect_interface_s *itfe;
    uint32_t id;                // type of pre processor (enum effect_id)
    uint32_t state;             // current state (enum effect_state)
    struct session_s *session;  // session the effect is on
};

// Session context
struct session_s {
    struct listnode node;
    effect_config_t config;
    struct effect_s effects[NUM_ID]; // effects in this session
    uint32_t state;                  // current state (enum session_state)
    int id;                          // audio session ID
    int io;                          // handle of input stream this session is on
    uint32_t created_msk;            // bit field containing IDs of crested pre processors
    uint32_t enabled_msk;            // bit field containing IDs of enabled pre processors
    uint32_t processed_msk;          // bit field containing IDs of pre processors already
};


//------------------------------------------------------------------------------
// Default Effect descriptors. Device specific descriptors should be defined in
// libqcomvoiceprocessing.<product_name>.so if needed.
//------------------------------------------------------------------------------

// UUIDs for effect types have been generated from http://www.itu.int/ITU-T/asn1/uuid.html
// as the pre processing effects are not defined by OpenSL ES

// Acoustic Echo Cancellation
static const effect_descriptor_t qcom_default_aec_descriptor = {
        { 0x7b491460, 0x8d4d, 0x11e0, 0xbd61, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // type
        { 0x0f8d0d2a, 0x59e5, 0x45fe, 0xb6e4, { 0x24, 0x8c, 0x8a, 0x79, 0x91, 0x09 } }, // uuid
        EFFECT_CONTROL_API_VERSION,
        (EFFECT_FLAG_TYPE_PRE_PROC|EFFECT_FLAG_DEVICE_IND),
        0,
        0,
        "Acoustic Echo Canceler",
        "Qualcomm Fluence"
};

// Noise suppression
static const effect_descriptor_t qcom_default_ns_descriptor = {
        { 0x58b4b260, 0x8e06, 0x11e0, 0xaa8e, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // type
        { 0x1d97bb0b, 0x9e2f, 0x4403, 0x9ae3, { 0x58, 0xc2, 0x55, 0x43, 0x06, 0xf8 } }, // uuid
        EFFECT_CONTROL_API_VERSION,
        (EFFECT_FLAG_TYPE_PRE_PROC|EFFECT_FLAG_DEVICE_IND),
        0,
        0,
        "Noise Suppression",
        "Qualcomm Fluence"
};

//ENABLE_AGC
// Automatic Gain Control
//static const effect_descriptor_t qcom_default_agc_descriptor = {
//        { 0x0a8abfe0, 0x654c, 0x11e0, 0xba26, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // type
//        { 0x0dd49521, 0x8c59, 0x40b1, 0xb403, { 0xe0, 0x8d, 0x5f, 0x01, 0x87, 0x5e } }, // uuid
//        EFFECT_CONTROL_API_VERSION,
//        (EFFECT_FLAG_TYPE_PRE_PROC|EFFECT_FLAG_DEVICE_IND),
//        0,
//        0,
//        "Automatic Gain Control",
//        "Qualcomm Fluence"
//};

const effect_descriptor_t *descriptors[NUM_ID] = {
        &qcom_default_aec_descriptor,
        &qcom_default_ns_descriptor,
//ENABLE_AGC       &qcom_default_agc_descriptor,
};


static int init_status = 1;
struct listnode session_list;
static const struct effect_interface_s effect_interface;
static const effect_uuid_t * uuid_to_id_table[NUM_ID];

//------------------------------------------------------------------------------
// Helper functions
//------------------------------------------------------------------------------

static const effect_uuid_t * id_to_uuid(int id)
{
    if (id >= NUM_ID)
        return EFFECT_UUID_NULL;

    return uuid_to_id_table[id];
}

static uint32_t uuid_to_id(const effect_uuid_t * uuid)
{
    size_t i;
    for (i = 0; i < NUM_ID; i++)
        if (memcmp(uuid, uuid_to_id_table[i], sizeof(*uuid)) == 0)
            break;

    return i;
}

//------------------------------------------------------------------------------
// Effect functions
//------------------------------------------------------------------------------

static void session_set_fx_enabled(struct session_s *session, uint32_t id, bool enabled);

#define BAD_STATE_ABORT(from, to) \
        LOG_ALWAYS_FATAL("Bad state transition from %d to %d", from, to);

static int effect_set_state(struct effect_s *effect, uint32_t state)
{
    int status = 0;
    ALOGV("effect_set_state() id %d, new %d old %d", effect->id, state, effect->state);
    switch(state) {
    case EFFECT_STATE_INIT:
        switch(effect->state) {
        case EFFECT_STATE_ACTIVE:
            session_set_fx_enabled(effect->session, effect->id, false);
        case EFFECT_STATE_CONFIG:
        case EFFECT_STATE_CREATED:
        case EFFECT_STATE_INIT:
            break;
        default:
            BAD_STATE_ABORT(effect->state, state);
        }
        break;
    case EFFECT_STATE_CREATED:
        switch(effect->state) {
        case EFFECT_STATE_INIT:
            break;
        case EFFECT_STATE_CREATED:
        case EFFECT_STATE_ACTIVE:
        case EFFECT_STATE_CONFIG:
            ALOGE("effect_set_state() invalid transition");
            status = -ENOSYS;
            break;
        default:
            BAD_STATE_ABORT(effect->state, state);
        }
        break;
    case EFFECT_STATE_CONFIG:
        switch(effect->state) {
        case EFFECT_STATE_INIT:
            ALOGE("effect_set_state() invalid transition");
            status = -ENOSYS;
            break;
        case EFFECT_STATE_ACTIVE:
            session_set_fx_enabled(effect->session, effect->id, false);
            break;
        case EFFECT_STATE_CREATED:
        case EFFECT_STATE_CONFIG:
            break;
        default:
            BAD_STATE_ABORT(effect->state, state);
        }
        break;
    case EFFECT_STATE_ACTIVE:
        switch(effect->state) {
        case EFFECT_STATE_INIT:
        case EFFECT_STATE_CREATED:
            ALOGE("effect_set_state() invalid transition");
            status = -ENOSYS;
            break;
        case EFFECT_STATE_ACTIVE:
            // enabling an already enabled effect is just ignored
            break;
        case EFFECT_STATE_CONFIG:
            session_set_fx_enabled(effect->session, effect->id, true);
            break;
        default:
            BAD_STATE_ABORT(effect->state, state);
        }
        break;
    default:
        BAD_STATE_ABORT(effect->state, state);
    }

    if (status == 0)
        effect->state = state;

    return status;
}

static int effect_init(struct effect_s *effect, uint32_t id)
{
    effect->itfe = &effect_interface;
    effect->id = id;
    effect->state = EFFECT_STATE_INIT;
    return 0;
}

static int effect_create(struct effect_s *effect,
               struct session_s *session,
               effect_handle_t  *interface)
{
    effect->session = session;
    *interface = (effect_handle_t)&effect->itfe;
    return effect_set_state(effect, EFFECT_STATE_CREATED);
}

static int effect_release(struct effect_s *effect)
{
    return effect_set_state(effect, EFFECT_STATE_INIT);
}


//------------------------------------------------------------------------------
// Session functions
//------------------------------------------------------------------------------

static int session_init(struct session_s *session)
{
    size_t i;
    int status = 0;

    session->state = SESSION_STATE_INIT;
    session->id = 0;
    session->io = 0;
    session->created_msk = 0;
    for (i = 0; i < NUM_ID && status == 0; i++)
        status = effect_init(&session->effects[i], i);

    return status;
}


static int session_create_effect(struct session_s *session,
                                 int32_t id,
                                 effect_handle_t  *interface)
{
    int status = -ENOMEM;

    ALOGV("session_create_effect() %s, created_msk %08x",
          id == AEC_ID ? "AEC" : id == NS_ID ? "NS" : "?", session->created_msk);

    if (session->created_msk == 0) {
        session->config.inputCfg.samplingRate = 16000;
        session->config.inputCfg.channels = AUDIO_CHANNEL_IN_MONO;
        session->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
        session->config.outputCfg.samplingRate = 16000;
        session->config.outputCfg.channels = AUDIO_CHANNEL_IN_MONO;
        session->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
        session->enabled_msk = 0;
        session->processed_msk = 0;
    }
    status = effect_create(&session->effects[id], session, interface);
    if (status < 0)
        goto error;

    ALOGV("session_create_effect() OK");
    session->created_msk |= (1<<id);
    return status;

error:
    return status;
}

static int session_release_effect(struct session_s *session,
                                  struct effect_s *fx)
{
    ALOGW_IF(effect_release(fx) != 0, " session_release_effect() failed for id %d", fx->id);

    session->created_msk &= ~(1<<fx->id);
    if (session->created_msk == 0)
    {
        ALOGV("session_release_effect() last effect: removing session");
        list_remove(&session->node);
        free(session);
    }

    return 0;
}


static int session_set_config(struct session_s *session, effect_config_t *config)
{
    int status;

    if (config->inputCfg.samplingRate != config->outputCfg.samplingRate ||
            config->inputCfg.format != config->outputCfg.format ||
            config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT)
        return -EINVAL;

    ALOGV("session_set_config() sampling rate %d channels %08x",
         config->inputCfg.samplingRate, config->inputCfg.channels);

    // if at least one process is enabled, do not accept configuration changes
    if (session->enabled_msk) {
        if (session->config.inputCfg.samplingRate != config->inputCfg.samplingRate ||
                session->config.inputCfg.channels != config->inputCfg.channels ||
                session->config.outputCfg.channels != config->outputCfg.channels)
            return -ENOSYS;
        else
            return 0;
    }

    memcpy(&session->config, config, sizeof(effect_config_t));

    session->state = SESSION_STATE_CONFIG;
    return 0;
}

static void session_get_config(struct session_s *session, effect_config_t *config)
{
    memcpy(config, &session->config, sizeof(effect_config_t));

    config->inputCfg.mask = config->outputCfg.mask =
            (EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT);
}


static void session_set_fx_enabled(struct session_s *session, uint32_t id, bool enabled)
{
    if (enabled) {
        if(session->enabled_msk == 0) {
            /* do first enable here */
        }
        session->enabled_msk |= (1 << id);
    } else {
        session->enabled_msk &= ~(1 << id);
        if(session->enabled_msk == 0) {
            /* do last enable here */
        }
    }
    ALOGV("session_set_fx_enabled() id %d, enabled %d enabled_msk %08x",
         id, enabled, session->enabled_msk);
    session->processed_msk = 0;
}

//------------------------------------------------------------------------------
// Global functions
//------------------------------------------------------------------------------

static struct session_s *get_session(int32_t id, int32_t  sessionId, int32_t  ioId)
{
    size_t i;
    int free = -1;
    struct listnode *node;
    struct session_s *session;

    list_for_each(node, &session_list) {
        session = node_to_item(node, struct session_s, node);
        if (session->io == ioId) {
            if (session->created_msk & (1 << id)) {
                ALOGV("get_session() effect %d already created", id);
                return NULL;
            }
            ALOGV("get_session() found session %p", session);
            return session;
        }
    }

    session = (struct session_s *)calloc(1, sizeof(struct session_s));
    if (session == NULL) {
        ALOGE("get_session() fail to allocate memory");
        return NULL;
    }
    session_init(session);
    session->id = sessionId;
    session->io = ioId;
    list_add_tail(&session_list, &session->node);

    ALOGV("get_session() created session %p", session);

    return session;
}

static int init() {
    void *lib_handle;
    const effect_descriptor_t *desc;

    if (init_status <= 0)
        return init_status;

    if (access(EFFECTS_DESCRIPTOR_LIBRARY_PATH, R_OK) == 0) {
        lib_handle = dlopen(EFFECTS_DESCRIPTOR_LIBRARY_PATH, RTLD_NOW);
        if (lib_handle == NULL) {
            ALOGE("%s: DLOPEN failed for %s", __func__, EFFECTS_DESCRIPTOR_LIBRARY_PATH);
        } else {
            ALOGV("%s: DLOPEN successful for %s", __func__, EFFECTS_DESCRIPTOR_LIBRARY_PATH);
            desc = (const effect_descriptor_t *)dlsym(lib_handle,
                                                        "qcom_product_aec_descriptor");
            if (desc)
                descriptors[AEC_ID] = desc;

            desc = (const effect_descriptor_t *)dlsym(lib_handle,
                                                        "qcom_product_ns_descriptor");
            if (desc)
                descriptors[NS_ID] = desc;

//ENABLE_AGC
//            desc = (const effect_descriptor_t *)dlsym(lib_handle,
//                                                        "qcom_product_agc_descriptor");
//            if (desc)
//                descriptors[AGC_ID] = desc;
        }
    }

    uuid_to_id_table[AEC_ID] = FX_IID_AEC;
    uuid_to_id_table[NS_ID] = FX_IID_NS;
//ENABLE_AGC uuid_to_id_table[AGC_ID] = FX_IID_AGC;

    list_init(&session_list);

    init_status = 0;
    return init_status;
}

static const effect_descriptor_t *get_descriptor(const effect_uuid_t *uuid)
{
    size_t i;
    for (i = 0; i < NUM_ID; i++)
        if (memcmp(&descriptors[i]->uuid, uuid, sizeof(effect_uuid_t)) == 0)
            return descriptors[i];

    return NULL;
}


//------------------------------------------------------------------------------
// Effect Control Interface Implementation
//------------------------------------------------------------------------------

static int fx_process(effect_handle_t     self,
                            audio_buffer_t    *inBuffer,
                            audio_buffer_t    *outBuffer)
{
    struct effect_s *effect = (struct effect_s *)self;
    struct session_s *session;

    if (effect == NULL) {
        ALOGV("fx_process() ERROR effect == NULL");
        return -EINVAL;
    }

    if (inBuffer == NULL  || inBuffer->raw == NULL  ||
            outBuffer == NULL || outBuffer->raw == NULL) {
        ALOGW("fx_process() ERROR bad pointer");
        return -EINVAL;
    }

    session = (struct session_s *)effect->session;

    session->processed_msk |= (1<<effect->id);

    if ((session->processed_msk & session->enabled_msk) == session->enabled_msk) {
        effect->session->processed_msk = 0;
        return 0;
    } else
        return -ENODATA;
}

static int fx_command(effect_handle_t  self,
                            uint32_t            cmdCode,
                            uint32_t            cmdSize,
                            void                *pCmdData,
                            uint32_t            *replySize,
                            void                *pReplyData)
{
    struct effect_s *effect = (struct effect_s *)self;

    if (effect == NULL)
        return -EINVAL;

    //ALOGV("fx_command: command %d cmdSize %d",cmdCode, cmdSize);

    switch (cmdCode) {
        case EFFECT_CMD_INIT:
            if (pReplyData == NULL || *replySize != sizeof(int))
                return -EINVAL;

            *(int *)pReplyData = 0;
            break;

        case EFFECT_CMD_SET_CONFIG: {
            if (pCmdData    == NULL||
                    cmdSize     != sizeof(effect_config_t)||
                    pReplyData  == NULL||
                    *replySize  != sizeof(int)) {
                ALOGV("fx_command() EFFECT_CMD_SET_CONFIG invalid args");
                return -EINVAL;
            }
            *(int *)pReplyData = session_set_config(effect->session, (effect_config_t *)pCmdData);
            if (*(int *)pReplyData != 0)
                break;

            if (effect->state != EFFECT_STATE_ACTIVE)
                *(int *)pReplyData = effect_set_state(effect, EFFECT_STATE_CONFIG);

        } break;

        case EFFECT_CMD_GET_CONFIG:
            if (pReplyData == NULL ||
                    *replySize != sizeof(effect_config_t)) {
                ALOGV("fx_command() EFFECT_CMD_GET_CONFIG invalid args");
                return -EINVAL;
            }

            session_get_config(effect->session, (effect_config_t *)pReplyData);
            break;

        case EFFECT_CMD_RESET:
            break;

        case EFFECT_CMD_GET_PARAM: {
            if (pCmdData == NULL ||
                    cmdSize < (int)sizeof(effect_param_t) ||
                    pReplyData == NULL ||
                    *replySize < (int)sizeof(effect_param_t)) {
                ALOGV("fx_command() EFFECT_CMD_GET_PARAM invalid args");
                return -EINVAL;
            }
            effect_param_t *p = (effect_param_t *)pCmdData;

            memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + p->psize);
            p = (effect_param_t *)pReplyData;
            p->status = -ENOSYS;

        } break;

        case EFFECT_CMD_SET_PARAM: {
            if (pCmdData == NULL||
                    cmdSize < (int)sizeof(effect_param_t) ||
                    pReplyData == NULL ||
                    *replySize != sizeof(int32_t)) {
                ALOGV("fx_command() EFFECT_CMD_SET_PARAM invalid args");
                return -EINVAL;
            }
            effect_param_t *p = (effect_param_t *) pCmdData;

            if (p->psize != sizeof(int32_t)) {
                ALOGV("fx_command() EFFECT_CMD_SET_PARAM invalid param format");
                return -EINVAL;
            }
            *(int *)pReplyData = -ENOSYS;
        } break;

        case EFFECT_CMD_ENABLE:
            if (pReplyData == NULL || *replySize != sizeof(int)) {
                ALOGV("fx_command() EFFECT_CMD_ENABLE invalid args");
                return -EINVAL;
            }
            *(int *)pReplyData = effect_set_state(effect, EFFECT_STATE_ACTIVE);
            break;

        case EFFECT_CMD_DISABLE:
            if (pReplyData == NULL || *replySize != sizeof(int)) {
                ALOGV("fx_command() EFFECT_CMD_DISABLE invalid args");
                return -EINVAL;
            }
            *(int *)pReplyData  = effect_set_state(effect, EFFECT_STATE_CONFIG);
            break;

        case EFFECT_CMD_SET_DEVICE:
        case EFFECT_CMD_SET_INPUT_DEVICE:
        case EFFECT_CMD_SET_VOLUME:
        case EFFECT_CMD_SET_AUDIO_MODE:
            if (pCmdData == NULL ||
                    cmdSize != sizeof(uint32_t)) {
                ALOGV("fx_command() %s invalid args",
                      cmdCode == EFFECT_CMD_SET_DEVICE ? "EFFECT_CMD_SET_DEVICE" :
                      cmdCode == EFFECT_CMD_SET_INPUT_DEVICE ? "EFFECT_CMD_SET_INPUT_DEVICE" :
                      cmdCode == EFFECT_CMD_SET_VOLUME ? "EFFECT_CMD_SET_VOLUME" :
                      cmdCode == EFFECT_CMD_SET_AUDIO_MODE ? "EFFECT_CMD_SET_AUDIO_MODE" :
                       "");
                return -EINVAL;
            }
            ALOGV("fx_command() %s value %08x",
                  cmdCode == EFFECT_CMD_SET_DEVICE ? "EFFECT_CMD_SET_DEVICE" :
                  cmdCode == EFFECT_CMD_SET_INPUT_DEVICE ? "EFFECT_CMD_SET_INPUT_DEVICE" :
                  cmdCode == EFFECT_CMD_SET_VOLUME ? "EFFECT_CMD_SET_VOLUME" :
                  cmdCode == EFFECT_CMD_SET_AUDIO_MODE ? "EFFECT_CMD_SET_AUDIO_MODE":
                  "",
                  *(int *)pCmdData);
            break;

        default:
            return -EINVAL;
    }
    return 0;
}


static int fx_get_descriptor(effect_handle_t   self,
                                  effect_descriptor_t *pDescriptor)
{
    struct effect_s *effect = (struct effect_s *)self;

    if (effect == NULL || pDescriptor == NULL)
        return -EINVAL;

    *pDescriptor = *descriptors[effect->id];

    return 0;
}


// effect_handle_t interface implementation for effect
static const struct effect_interface_s effect_interface = {
    fx_process,
    fx_command,
    fx_get_descriptor,
    NULL
};

//------------------------------------------------------------------------------
// Effect Library Interface Implementation
//------------------------------------------------------------------------------

static int lib_create(const effect_uuid_t *uuid,
                            int32_t             sessionId,
                            int32_t             ioId,
                            effect_handle_t  *pInterface)
{
    ALOGV("lib_create: uuid: %08x session %d IO: %d", uuid->timeLow, sessionId, ioId);

    int status;
    const effect_descriptor_t *desc;
    struct session_s *session;
    uint32_t id;

    if (init() != 0)
        return init_status;

    desc =  get_descriptor(uuid);

    if (desc == NULL) {
        ALOGW("lib_create: fx not found uuid: %08x", uuid->timeLow);
        return -EINVAL;
    }
    id = uuid_to_id(&desc->type);
    if (id >= NUM_ID) {
        ALOGW("lib_create: fx not found type: %08x", desc->type.timeLow);
        return -EINVAL;
    }

    session = get_session(id, sessionId, ioId);

    if (session == NULL) {
        ALOGW("lib_create: no more session available");
        return -EINVAL;
    }

    status = session_create_effect(session, id, pInterface);

    if (status < 0 && session->created_msk == 0) {
        list_remove(&session->node);
        free(session);
    }
    return status;
}

static int lib_release(effect_handle_t interface)
{
    struct listnode *node;
    struct session_s *session;

    ALOGV("lib_release %p", interface);
    if (init() != 0)
        return init_status;

    struct effect_s *fx = (struct effect_s *)interface;

    list_for_each(node, &session_list) {
        session = node_to_item(node, struct session_s, node);
        if (session == fx->session) {
            session_release_effect(fx->session, fx);
            return 0;
        }
    }

    return -EINVAL;
}

static int lib_get_descriptor(const effect_uuid_t *uuid,
                                   effect_descriptor_t *pDescriptor)
{
    const effect_descriptor_t *desc;

    if (pDescriptor == NULL || uuid == NULL)
        return -EINVAL;

    if (init() != 0)
        return init_status;

    desc = get_descriptor(uuid);
    if (desc == NULL) {
        ALOGV("lib_get_descriptor() not found");
        return  -EINVAL;
    }

    ALOGV("lib_get_descriptor() got fx %s", desc->name);

    *pDescriptor = *desc;
    return 0;
}

// This is the only symbol that needs to be exported
__attribute__ ((visibility ("default")))
audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
    .tag = AUDIO_EFFECT_LIBRARY_TAG,
    .version = EFFECT_LIBRARY_API_VERSION,
    .name = "MSM8960 Audio Preprocessing Library",
    .implementor = "The Android Open Source Project",
    .create_effect = lib_create,
    .release_effect = lib_release,
    .get_descriptor = lib_get_descriptor
};