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 "offload_visualizer"
18/*#define LOG_NDEBUG 0*/
19#include <assert.h>
20#include <math.h>
21#include <stdlib.h>
22#include <string.h>
23#include <time.h>
24#include <sys/prctl.h>
25
26#include <cutils/list.h>
27#include <cutils/log.h>
28#include <system/thread_defs.h>
29#include <tinyalsa/asoundlib.h>
30#include <audio_effects/effect_visualizer.h>
31
32
33enum {
34    EFFECT_STATE_UNINITIALIZED,
35    EFFECT_STATE_INITIALIZED,
36    EFFECT_STATE_ACTIVE,
37};
38
39typedef struct effect_context_s effect_context_t;
40typedef struct output_context_s output_context_t;
41
42/* effect specific operations. Only the init() and process() operations must be defined.
43 * Others are optional.
44 */
45typedef struct effect_ops_s {
46    int (*init)(effect_context_t *context);
47    int (*release)(effect_context_t *context);
48    int (*reset)(effect_context_t *context);
49    int (*enable)(effect_context_t *context);
50    int (*disable)(effect_context_t *context);
51    int (*start)(effect_context_t *context, output_context_t *output);
52    int (*stop)(effect_context_t *context, output_context_t *output);
53    int (*process)(effect_context_t *context, audio_buffer_t *in, audio_buffer_t *out);
54    int (*set_parameter)(effect_context_t *context, effect_param_t *param, uint32_t size);
55    int (*get_parameter)(effect_context_t *context, effect_param_t *param, uint32_t *size);
56    int (*command)(effect_context_t *context, uint32_t cmdCode, uint32_t cmdSize,
57            void *pCmdData, uint32_t *replySize, void *pReplyData);
58} effect_ops_t;
59
60struct effect_context_s {
61    const struct effect_interface_s *itfe;
62    struct listnode effects_list_node;  /* node in created_effects_list */
63    struct listnode output_node;  /* node in output_context_t.effects_list */
64    effect_config_t config;
65    const effect_descriptor_t *desc;
66    audio_io_handle_t out_handle;  /* io handle of the output the effect is attached to */
67    uint32_t state;
68    bool offload_enabled;  /* when offload is enabled we process VISUALIZER_CMD_CAPTURE command.
69                              Otherwise non offloaded visualizer has already processed the command
70                              and we must not overwrite the reply. */
71    effect_ops_t ops;
72};
73
74typedef struct output_context_s {
75    struct listnode outputs_list_node;  /* node in active_outputs_list */
76    audio_io_handle_t handle; /* io handle */
77    struct listnode effects_list; /* list of effects attached to this output */
78} output_context_t;
79
80
81/* maximum time since last capture buffer update before resetting capture buffer. This means
82  that the framework has stopped playing audio and we must start returning silence */
83#define MAX_STALL_TIME_MS 1000
84
85#define CAPTURE_BUF_SIZE 65536 /* "64k should be enough for everyone" */
86
87#define DISCARD_MEASUREMENTS_TIME_MS 2000 /* discard measurements older than this number of ms */
88
89/* maximum number of buffers for which we keep track of the measurements */
90#define MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS 25 /* note: buffer index is stored in uint8_t */
91
92typedef struct buffer_stats_s {
93    bool is_valid;
94    uint16_t peak_u16; /* the positive peak of the absolute value of the samples in a buffer */
95    float rms_squared; /* the average square of the samples in a buffer */
96} buffer_stats_t;
97
98typedef struct visualizer_context_s {
99    effect_context_t common;
100
101    uint32_t capture_idx;
102    uint32_t capture_size;
103    uint32_t scaling_mode;
104    uint32_t last_capture_idx;
105    uint32_t latency;
106    struct timespec buffer_update_time;
107    uint8_t capture_buf[CAPTURE_BUF_SIZE];
108    /* for measurements */
109    uint8_t channel_count; /* to avoid recomputing it every time a buffer is processed */
110    uint32_t meas_mode;
111    uint8_t meas_wndw_size_in_buffers;
112    uint8_t meas_buffer_idx;
113    buffer_stats_t past_meas[MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS];
114} visualizer_context_t;
115
116
117extern const struct effect_interface_s effect_interface;
118
119/* Offload visualizer UUID: 7a8044a0-1a71-11e3-a184-0002a5d5c51b */
120const effect_descriptor_t visualizer_descriptor = {
121        {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
122        {0x7a8044a0, 0x1a71, 0x11e3, 0xa184, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
123        EFFECT_CONTROL_API_VERSION,
124        (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_HW_ACC_TUNNEL ),
125        0, /* TODO */
126        1,
127        "QCOM MSM offload visualizer",
128        "The Android Open Source Project",
129};
130
131const effect_descriptor_t *descriptors[] = {
132        &visualizer_descriptor,
133        NULL,
134};
135
136
137pthread_once_t once = PTHREAD_ONCE_INIT;
138int init_status;
139
140/* list of created effects. Updated by visualizer_hal_start_output()
141 * and visualizer_hal_stop_output() */
142struct listnode created_effects_list;
143/* list of active output streams. Updated by visualizer_hal_start_output()
144 * and visualizer_hal_stop_output() */
145struct listnode active_outputs_list;
146
147/* thread capturing PCM from Proxy port and calling the process function on each enabled effect
148 * attached to an active output stream */
149pthread_t capture_thread;
150/* lock must be held when modifying or accessing created_effects_list or active_outputs_list */
151pthread_mutex_t lock;
152/* thread_lock must be held when starting or stopping the capture thread.
153 * Locking order: thread_lock -> lock */
154pthread_mutex_t thread_lock;
155/* cond is signaled when an output is started or stopped or an effect is enabled or disable: the
156 * capture thread will reevaluate the capture and effect rocess conditions. */
157pthread_cond_t cond;
158/* true when requesting the capture thread to exit */
159bool exit_thread;
160/* 0 if the capture thread was created successfully */
161int thread_status;
162
163
164#define DSP_OUTPUT_LATENCY_MS 0 /* Fudge factor for latency after capture point in audio DSP */
165
166/* Retry for delay for mixer open */
167#define RETRY_NUMBER 10
168#define RETRY_US 500000
169
170#define MIXER_CARD 0
171#define SOUND_CARD 0
172#define CAPTURE_DEVICE 8
173
174/* Proxy port supports only MMAP read and those fixed parameters*/
175#define AUDIO_CAPTURE_CHANNEL_COUNT 2
176#define AUDIO_CAPTURE_SMP_RATE 48000
177#define AUDIO_CAPTURE_PERIOD_SIZE (768)
178#define AUDIO_CAPTURE_PERIOD_COUNT 32
179
180struct pcm_config pcm_config_capture = {
181    .channels = AUDIO_CAPTURE_CHANNEL_COUNT,
182    .rate = AUDIO_CAPTURE_SMP_RATE,
183    .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
184    .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
185    .format = PCM_FORMAT_S16_LE,
186    .start_threshold = AUDIO_CAPTURE_PERIOD_SIZE / 4,
187    .stop_threshold = INT_MAX,
188    .avail_min = AUDIO_CAPTURE_PERIOD_SIZE / 4,
189};
190
191
192/*
193 *  Local functions
194 */
195
196static void init_once() {
197    list_init(&created_effects_list);
198    list_init(&active_outputs_list);
199
200    pthread_mutex_init(&lock, NULL);
201    pthread_mutex_init(&thread_lock, NULL);
202    pthread_cond_init(&cond, NULL);
203    exit_thread = false;
204    thread_status = -1;
205
206    init_status = 0;
207}
208
209int lib_init() {
210    pthread_once(&once, init_once);
211    return init_status;
212}
213
214bool effect_exists(effect_context_t *context) {
215    struct listnode *node;
216
217    list_for_each(node, &created_effects_list) {
218        effect_context_t *fx_ctxt = node_to_item(node,
219                                                     effect_context_t,
220                                                     effects_list_node);
221        if (fx_ctxt == context) {
222            return true;
223        }
224    }
225    return false;
226}
227
228output_context_t *get_output(audio_io_handle_t output) {
229    struct listnode *node;
230
231    list_for_each(node, &active_outputs_list) {
232        output_context_t *out_ctxt = node_to_item(node,
233                                                  output_context_t,
234                                                  outputs_list_node);
235        if (out_ctxt->handle == output) {
236            return out_ctxt;
237        }
238    }
239    return NULL;
240}
241
242void add_effect_to_output(output_context_t * output, effect_context_t *context) {
243    struct listnode *fx_node;
244
245    list_for_each(fx_node, &output->effects_list) {
246        effect_context_t *fx_ctxt = node_to_item(fx_node,
247                                                     effect_context_t,
248                                                     output_node);
249        if (fx_ctxt == context)
250            return;
251    }
252    list_add_tail(&output->effects_list, &context->output_node);
253    if (context->ops.start)
254        context->ops.start(context, output);
255}
256
257void remove_effect_from_output(output_context_t * output, effect_context_t *context) {
258    struct listnode *fx_node;
259
260    list_for_each(fx_node, &output->effects_list) {
261        effect_context_t *fx_ctxt = node_to_item(fx_node,
262                                                     effect_context_t,
263                                                     output_node);
264        if (fx_ctxt == context) {
265            if (context->ops.stop)
266                context->ops.stop(context, output);
267            list_remove(&context->output_node);
268            return;
269        }
270    }
271}
272
273bool effects_enabled() {
274    struct listnode *out_node;
275
276    list_for_each(out_node, &active_outputs_list) {
277        struct listnode *fx_node;
278        output_context_t *out_ctxt = node_to_item(out_node,
279                                                  output_context_t,
280                                                  outputs_list_node);
281
282        list_for_each(fx_node, &out_ctxt->effects_list) {
283            effect_context_t *fx_ctxt = node_to_item(fx_node,
284                                                         effect_context_t,
285                                                         output_node);
286            if (fx_ctxt->state == EFFECT_STATE_ACTIVE && fx_ctxt->ops.process != NULL)
287                return true;
288        }
289    }
290    return false;
291}
292
293int configure_proxy_capture(struct mixer *mixer, int value) {
294    const char *proxy_ctl_name = "AFE_PCM_RX Audio Mixer MultiMedia4";
295    struct mixer_ctl *ctl;
296
297    ctl = mixer_get_ctl_by_name(mixer, proxy_ctl_name);
298    if (ctl == NULL) {
299        ALOGW("%s: could not get %s ctl", __func__, proxy_ctl_name);
300        return -EINVAL;
301    }
302    if (mixer_ctl_set_value(ctl, 0, value) != 0)
303        ALOGW("%s: error setting value %d on %s ", __func__, value, proxy_ctl_name);
304
305    return 0;
306}
307
308
309void *capture_thread_loop(void *arg)
310{
311    int16_t data[AUDIO_CAPTURE_PERIOD_SIZE * AUDIO_CAPTURE_CHANNEL_COUNT * sizeof(int16_t)];
312    audio_buffer_t buf;
313    buf.frameCount = AUDIO_CAPTURE_PERIOD_SIZE;
314    buf.s16 = data;
315    bool capture_enabled = false;
316    struct mixer *mixer;
317    struct pcm *pcm = NULL;
318    int ret;
319    int retry_num = 0;
320
321    ALOGD("thread enter");
322
323    prctl(PR_SET_NAME, (unsigned long)"visualizer capture", 0, 0, 0);
324
325    pthread_mutex_lock(&lock);
326
327    mixer = mixer_open(MIXER_CARD);
328    while (mixer == NULL && retry_num < RETRY_NUMBER) {
329        usleep(RETRY_US);
330        mixer = mixer_open(MIXER_CARD);
331        retry_num++;
332    }
333    if (mixer == NULL) {
334        pthread_mutex_unlock(&lock);
335        return NULL;
336    }
337
338    for (;;) {
339        if (exit_thread) {
340            break;
341        }
342        if (effects_enabled()) {
343            if (!capture_enabled) {
344                ret = configure_proxy_capture(mixer, 1);
345                if (ret == 0) {
346                    pcm = pcm_open(SOUND_CARD, CAPTURE_DEVICE,
347                                   PCM_IN|PCM_MMAP|PCM_NOIRQ, &pcm_config_capture);
348                    if (pcm && !pcm_is_ready(pcm)) {
349                        ALOGW("%s: %s", __func__, pcm_get_error(pcm));
350                        pcm_close(pcm);
351                        pcm = NULL;
352                        configure_proxy_capture(mixer, 0);
353                    } else {
354                        capture_enabled = true;
355                        ALOGD("%s: capture ENABLED", __func__);
356                    }
357                }
358            }
359        } else {
360            if (capture_enabled) {
361                if (pcm != NULL)
362                    pcm_close(pcm);
363                configure_proxy_capture(mixer, 0);
364                ALOGD("%s: capture DISABLED", __func__);
365                capture_enabled = false;
366            }
367            pthread_cond_wait(&cond, &lock);
368        }
369        if (!capture_enabled)
370            continue;
371
372        pthread_mutex_unlock(&lock);
373        ret = pcm_mmap_read(pcm, data, sizeof(data));
374        pthread_mutex_lock(&lock);
375
376        if (ret == 0) {
377            struct listnode *out_node;
378
379            list_for_each(out_node, &active_outputs_list) {
380                output_context_t *out_ctxt = node_to_item(out_node,
381                                                          output_context_t,
382                                                          outputs_list_node);
383                struct listnode *fx_node;
384
385                list_for_each(fx_node, &out_ctxt->effects_list) {
386                    effect_context_t *fx_ctxt = node_to_item(fx_node,
387                                                                effect_context_t,
388                                                                output_node);
389                    if (fx_ctxt->ops.process != NULL)
390                        fx_ctxt->ops.process(fx_ctxt, &buf, &buf);
391                }
392            }
393        } else {
394            ALOGW("%s: read status %d %s", __func__, ret, pcm_get_error(pcm));
395        }
396    }
397
398    if (capture_enabled) {
399        if (pcm != NULL)
400            pcm_close(pcm);
401        configure_proxy_capture(mixer, 0);
402    }
403    mixer_close(mixer);
404    pthread_mutex_unlock(&lock);
405
406    ALOGD("thread exit");
407
408    return NULL;
409}
410
411/*
412 * Interface from audio HAL
413 */
414
415__attribute__ ((visibility ("default")))
416int visualizer_hal_start_output(audio_io_handle_t output, int pcm_id) {
417    int ret;
418    struct listnode *node;
419
420    ALOGV("%s output %d pcm_id %d", __func__, output, pcm_id);
421
422    if (lib_init() != 0)
423        return init_status;
424
425    pthread_mutex_lock(&thread_lock);
426    pthread_mutex_lock(&lock);
427    if (get_output(output) != NULL) {
428        ALOGW("%s output already started", __func__);
429        ret = -ENOSYS;
430        goto exit;
431    }
432
433    output_context_t *out_ctxt = (output_context_t *)malloc(sizeof(output_context_t));
434    if (out_ctxt == NULL) {
435        ALOGE("%s fail to allocate memory", __func__);
436        ret = -ENOMEM;
437        goto exit;
438    }
439    out_ctxt->handle = output;
440    list_init(&out_ctxt->effects_list);
441
442    list_for_each(node, &created_effects_list) {
443        effect_context_t *fx_ctxt = node_to_item(node,
444                                                     effect_context_t,
445                                                     effects_list_node);
446        if (fx_ctxt->out_handle == output) {
447            if (fx_ctxt->ops.start)
448                fx_ctxt->ops.start(fx_ctxt, out_ctxt);
449            list_add_tail(&out_ctxt->effects_list, &fx_ctxt->output_node);
450        }
451    }
452    if (list_empty(&active_outputs_list)) {
453        exit_thread = false;
454        thread_status = pthread_create(&capture_thread, (const pthread_attr_t *) NULL,
455                        capture_thread_loop, NULL);
456    }
457    list_add_tail(&active_outputs_list, &out_ctxt->outputs_list_node);
458    pthread_cond_signal(&cond);
459
460exit:
461    pthread_mutex_unlock(&lock);
462    pthread_mutex_unlock(&thread_lock);
463    return ret;
464}
465
466__attribute__ ((visibility ("default")))
467int visualizer_hal_stop_output(audio_io_handle_t output, int pcm_id) {
468    int ret;
469    struct listnode *node;
470    struct listnode *fx_node;
471    output_context_t *out_ctxt;
472
473    ALOGV("%s output %d pcm_id %d", __func__, output, pcm_id);
474
475    if (lib_init() != 0)
476        return init_status;
477
478    pthread_mutex_lock(&thread_lock);
479    pthread_mutex_lock(&lock);
480
481    out_ctxt = get_output(output);
482    if (out_ctxt == NULL) {
483        ALOGW("%s output not started", __func__);
484        ret = -ENOSYS;
485        goto exit;
486    }
487    list_for_each(fx_node, &out_ctxt->effects_list) {
488        effect_context_t *fx_ctxt = node_to_item(fx_node,
489                                                 effect_context_t,
490                                                 output_node);
491        if (fx_ctxt->ops.stop)
492            fx_ctxt->ops.stop(fx_ctxt, out_ctxt);
493    }
494    list_remove(&out_ctxt->outputs_list_node);
495    pthread_cond_signal(&cond);
496
497    if (list_empty(&active_outputs_list)) {
498        if (thread_status == 0) {
499            exit_thread = true;
500            pthread_cond_signal(&cond);
501            pthread_mutex_unlock(&lock);
502            pthread_join(capture_thread, (void **) NULL);
503            pthread_mutex_lock(&lock);
504            thread_status = -1;
505        }
506    }
507
508    free(out_ctxt);
509
510exit:
511    pthread_mutex_unlock(&lock);
512    pthread_mutex_unlock(&thread_lock);
513    return ret;
514}
515
516
517/*
518 * Effect operations
519 */
520
521int set_config(effect_context_t *context, effect_config_t *config)
522{
523    if (config->inputCfg.samplingRate != config->outputCfg.samplingRate) return -EINVAL;
524    if (config->inputCfg.channels != config->outputCfg.channels) return -EINVAL;
525    if (config->inputCfg.format != config->outputCfg.format) return -EINVAL;
526    if (config->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL;
527    if (config->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
528            config->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
529    if (config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
530
531    context->config = *config;
532
533    if (context->ops.reset)
534        context->ops.reset(context);
535
536    return 0;
537}
538
539void get_config(effect_context_t *context, effect_config_t *config)
540{
541    *config = context->config;
542}
543
544
545/*
546 * Visualizer operations
547 */
548
549uint32_t visualizer_get_delta_time_ms_from_updated_time(visualizer_context_t* visu_ctxt) {
550    uint32_t delta_ms = 0;
551    if (visu_ctxt->buffer_update_time.tv_sec != 0) {
552        struct timespec ts;
553        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
554            time_t secs = ts.tv_sec - visu_ctxt->buffer_update_time.tv_sec;
555            long nsec = ts.tv_nsec - visu_ctxt->buffer_update_time.tv_nsec;
556            if (nsec < 0) {
557                --secs;
558                nsec += 1000000000;
559            }
560            delta_ms = secs * 1000 + nsec / 1000000;
561        }
562    }
563    return delta_ms;
564}
565
566int visualizer_reset(effect_context_t *context)
567{
568    visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
569
570    visu_ctxt->capture_idx = 0;
571    visu_ctxt->last_capture_idx = 0;
572    visu_ctxt->buffer_update_time.tv_sec = 0;
573    visu_ctxt->latency = DSP_OUTPUT_LATENCY_MS;
574    memset(visu_ctxt->capture_buf, 0x80, CAPTURE_BUF_SIZE);
575    return 0;
576}
577
578int visualizer_init(effect_context_t *context)
579{
580    int32_t i;
581
582    visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
583
584    context->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
585    context->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
586    context->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
587    context->config.inputCfg.samplingRate = 44100;
588    context->config.inputCfg.bufferProvider.getBuffer = NULL;
589    context->config.inputCfg.bufferProvider.releaseBuffer = NULL;
590    context->config.inputCfg.bufferProvider.cookie = NULL;
591    context->config.inputCfg.mask = EFFECT_CONFIG_ALL;
592    context->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
593    context->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
594    context->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
595    context->config.outputCfg.samplingRate = 44100;
596    context->config.outputCfg.bufferProvider.getBuffer = NULL;
597    context->config.outputCfg.bufferProvider.releaseBuffer = NULL;
598    context->config.outputCfg.bufferProvider.cookie = NULL;
599    context->config.outputCfg.mask = EFFECT_CONFIG_ALL;
600
601    visu_ctxt->capture_size = VISUALIZER_CAPTURE_SIZE_MAX;
602    visu_ctxt->scaling_mode = VISUALIZER_SCALING_MODE_NORMALIZED;
603
604    // measurement initialization
605    visu_ctxt->channel_count = popcount(context->config.inputCfg.channels);
606    visu_ctxt->meas_mode = MEASUREMENT_MODE_NONE;
607    visu_ctxt->meas_wndw_size_in_buffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
608    visu_ctxt->meas_buffer_idx = 0;
609    for (i=0 ; i<visu_ctxt->meas_wndw_size_in_buffers ; i++) {
610        visu_ctxt->past_meas[i].is_valid = false;
611        visu_ctxt->past_meas[i].peak_u16 = 0;
612        visu_ctxt->past_meas[i].rms_squared = 0;
613    }
614
615    set_config(context, &context->config);
616
617    return 0;
618}
619
620int visualizer_get_parameter(effect_context_t *context, effect_param_t *p, uint32_t *size)
621{
622    visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
623
624    p->status = 0;
625    *size = sizeof(effect_param_t) + sizeof(uint32_t);
626    if (p->psize != sizeof(uint32_t)) {
627        p->status = -EINVAL;
628        return 0;
629    }
630    switch (*(uint32_t *)p->data) {
631    case VISUALIZER_PARAM_CAPTURE_SIZE:
632        ALOGV("%s get capture_size = %d", __func__, visu_ctxt->capture_size);
633        *((uint32_t *)p->data + 1) = visu_ctxt->capture_size;
634        p->vsize = sizeof(uint32_t);
635        *size += sizeof(uint32_t);
636        break;
637    case VISUALIZER_PARAM_SCALING_MODE:
638        ALOGV("%s get scaling_mode = %d", __func__, visu_ctxt->scaling_mode);
639        *((uint32_t *)p->data + 1) = visu_ctxt->scaling_mode;
640        p->vsize = sizeof(uint32_t);
641        *size += sizeof(uint32_t);
642        break;
643    case VISUALIZER_PARAM_MEASUREMENT_MODE:
644        ALOGV("%s get meas_mode = %d", __func__, visu_ctxt->meas_mode);
645        *((uint32_t *)p->data + 1) = visu_ctxt->meas_mode;
646        p->vsize = sizeof(uint32_t);
647        *size += sizeof(uint32_t);
648        break;
649    default:
650        p->status = -EINVAL;
651    }
652    return 0;
653}
654
655int visualizer_set_parameter(effect_context_t *context, effect_param_t *p, uint32_t size)
656{
657    visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
658
659    if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t))
660        return -EINVAL;
661
662    switch (*(uint32_t *)p->data) {
663    case VISUALIZER_PARAM_CAPTURE_SIZE:
664        visu_ctxt->capture_size = *((uint32_t *)p->data + 1);
665        ALOGV("%s set capture_size = %d", __func__, visu_ctxt->capture_size);
666        break;
667    case VISUALIZER_PARAM_SCALING_MODE:
668        visu_ctxt->scaling_mode = *((uint32_t *)p->data + 1);
669        ALOGV("%s set scaling_mode = %d", __func__, visu_ctxt->scaling_mode);
670        break;
671    case VISUALIZER_PARAM_LATENCY:
672        /* Ignore latency as we capture at DSP output
673         * visu_ctxt->latency = *((uint32_t *)p->data + 1); */
674        ALOGV("%s set latency = %d", __func__, visu_ctxt->latency);
675        break;
676    case VISUALIZER_PARAM_MEASUREMENT_MODE:
677        visu_ctxt->meas_mode = *((uint32_t *)p->data + 1);
678        ALOGV("%s set meas_mode = %d", __func__, visu_ctxt->meas_mode);
679        break;
680    default:
681        return -EINVAL;
682    }
683    return 0;
684}
685
686/* Real process function called from capture thread. Called with lock held */
687int visualizer_process(effect_context_t *context,
688                       audio_buffer_t *inBuffer,
689                       audio_buffer_t *outBuffer)
690{
691    visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
692
693    if (!effect_exists(context))
694        return -EINVAL;
695
696    if (inBuffer == NULL || inBuffer->raw == NULL ||
697        outBuffer == NULL || outBuffer->raw == NULL ||
698        inBuffer->frameCount != outBuffer->frameCount ||
699        inBuffer->frameCount == 0) {
700        return -EINVAL;
701    }
702
703    // perform measurements if needed
704    if (visu_ctxt->meas_mode & MEASUREMENT_MODE_PEAK_RMS) {
705        // find the peak and RMS squared for the new buffer
706        uint32_t inIdx;
707        int16_t max_sample = 0;
708        float rms_squared_acc = 0;
709        for (inIdx = 0 ; inIdx < inBuffer->frameCount * visu_ctxt->channel_count ; inIdx++) {
710            if (inBuffer->s16[inIdx] > max_sample) {
711                max_sample = inBuffer->s16[inIdx];
712            } else if (-inBuffer->s16[inIdx] > max_sample) {
713                max_sample = -inBuffer->s16[inIdx];
714            }
715            rms_squared_acc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]);
716        }
717        // store the measurement
718        visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].peak_u16 = (uint16_t)max_sample;
719        visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].rms_squared =
720                rms_squared_acc / (inBuffer->frameCount * visu_ctxt->channel_count);
721        visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].is_valid = true;
722        if (++visu_ctxt->meas_buffer_idx >= visu_ctxt->meas_wndw_size_in_buffers) {
723            visu_ctxt->meas_buffer_idx = 0;
724        }
725    }
726
727    /* all code below assumes stereo 16 bit PCM output and input */
728    int32_t shift;
729
730    if (visu_ctxt->scaling_mode == VISUALIZER_SCALING_MODE_NORMALIZED) {
731        /* derive capture scaling factor from peak value in current buffer
732         * this gives more interesting captures for display. */
733        shift = 32;
734        int len = inBuffer->frameCount * 2;
735        int i;
736        for (i = 0; i < len; i++) {
737            int32_t smp = inBuffer->s16[i];
738            if (smp < 0) smp = -smp - 1; /* take care to keep the max negative in range */
739            int32_t clz = __builtin_clz(smp);
740            if (shift > clz) shift = clz;
741        }
742        /* A maximum amplitude signal will have 17 leading zeros, which we want to
743         * translate to a shift of 8 (for converting 16 bit to 8 bit) */
744        shift = 25 - shift;
745        /* Never scale by less than 8 to avoid returning unaltered PCM signal. */
746        if (shift < 3) {
747            shift = 3;
748        }
749        /* add one to combine the division by 2 needed after summing
750         * left and right channels below */
751        shift++;
752    } else {
753        assert(visu_ctxt->scaling_mode == VISUALIZER_SCALING_MODE_AS_PLAYED);
754        shift = 9;
755    }
756
757    uint32_t capt_idx;
758    uint32_t in_idx;
759    uint8_t *buf = visu_ctxt->capture_buf;
760    for (in_idx = 0, capt_idx = visu_ctxt->capture_idx;
761         in_idx < inBuffer->frameCount;
762         in_idx++, capt_idx++) {
763        if (capt_idx >= CAPTURE_BUF_SIZE) {
764            /* wrap around */
765            capt_idx = 0;
766        }
767        int32_t smp = inBuffer->s16[2 * in_idx] + inBuffer->s16[2 * in_idx + 1];
768        smp = smp >> shift;
769        buf[capt_idx] = ((uint8_t)smp)^0x80;
770    }
771
772    /* XXX the following two should really be atomic, though it probably doesn't
773     * matter much for visualization purposes */
774    visu_ctxt->capture_idx = capt_idx;
775    /* update last buffer update time stamp */
776    if (clock_gettime(CLOCK_MONOTONIC, &visu_ctxt->buffer_update_time) < 0) {
777        visu_ctxt->buffer_update_time.tv_sec = 0;
778    }
779
780    if (context->state != EFFECT_STATE_ACTIVE) {
781        ALOGV("%s DONE inactive", __func__);
782        return -ENODATA;
783    }
784
785    return 0;
786}
787
788int visualizer_command(effect_context_t * context, uint32_t cmdCode, uint32_t cmdSize,
789        void *pCmdData, uint32_t *replySize, void *pReplyData)
790{
791    visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
792
793    switch (cmdCode) {
794    case VISUALIZER_CMD_CAPTURE:
795        if (pReplyData == NULL || *replySize != visu_ctxt->capture_size) {
796            ALOGV("%s VISUALIZER_CMD_CAPTURE error *replySize %d context->capture_size %d",
797                  __func__, *replySize, visu_ctxt->capture_size);
798            return -EINVAL;
799        }
800
801        if (!context->offload_enabled)
802            break;
803
804        if (context->state == EFFECT_STATE_ACTIVE) {
805            int32_t latency_ms = visu_ctxt->latency;
806            const uint32_t delta_ms = visualizer_get_delta_time_ms_from_updated_time(visu_ctxt);
807            latency_ms -= delta_ms;
808            if (latency_ms < 0) {
809                latency_ms = 0;
810            }
811            const uint32_t delta_smp = context->config.inputCfg.samplingRate * latency_ms / 1000;
812
813            int32_t capture_point = visu_ctxt->capture_idx - visu_ctxt->capture_size - delta_smp;
814            int32_t capture_size = visu_ctxt->capture_size;
815            if (capture_point < 0) {
816                int32_t size = -capture_point;
817                if (size > capture_size)
818                    size = capture_size;
819
820                memcpy(pReplyData,
821                       visu_ctxt->capture_buf + CAPTURE_BUF_SIZE + capture_point,
822                       size);
823                pReplyData = (void *)((size_t)pReplyData + size);
824                capture_size -= size;
825                capture_point = 0;
826            }
827            memcpy(pReplyData,
828                   visu_ctxt->capture_buf + capture_point,
829                   capture_size);
830
831
832            /* if audio framework has stopped playing audio although the effect is still
833             * active we must clear the capture buffer to return silence */
834            if ((visu_ctxt->last_capture_idx == visu_ctxt->capture_idx) &&
835                    (visu_ctxt->buffer_update_time.tv_sec != 0)) {
836                if (delta_ms > MAX_STALL_TIME_MS) {
837                    ALOGV("%s capture going to idle", __func__);
838                    visu_ctxt->buffer_update_time.tv_sec = 0;
839                    memset(pReplyData, 0x80, visu_ctxt->capture_size);
840                }
841            }
842            visu_ctxt->last_capture_idx = visu_ctxt->capture_idx;
843        } else {
844            memset(pReplyData, 0x80, visu_ctxt->capture_size);
845        }
846        break;
847
848    case VISUALIZER_CMD_MEASURE: {
849        uint16_t peak_u16 = 0;
850        float sum_rms_squared = 0.0f;
851        uint8_t nb_valid_meas = 0;
852        /* reset measurements if last measurement was too long ago (which implies stored
853         * measurements aren't relevant anymore and shouldn't bias the new one) */
854        const int32_t delay_ms = visualizer_get_delta_time_ms_from_updated_time(visu_ctxt);
855        if (delay_ms > DISCARD_MEASUREMENTS_TIME_MS) {
856            uint32_t i;
857            ALOGV("Discarding measurements, last measurement is %dms old", delay_ms);
858            for (i=0 ; i<visu_ctxt->meas_wndw_size_in_buffers ; i++) {
859                visu_ctxt->past_meas[i].is_valid = false;
860                visu_ctxt->past_meas[i].peak_u16 = 0;
861                visu_ctxt->past_meas[i].rms_squared = 0;
862            }
863            visu_ctxt->meas_buffer_idx = 0;
864        } else {
865            /* only use actual measurements, otherwise the first RMS measure happening before
866             * MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
867             * low */
868            uint32_t i;
869            for (i=0 ; i < visu_ctxt->meas_wndw_size_in_buffers ; i++) {
870                if (visu_ctxt->past_meas[i].is_valid) {
871                    if (visu_ctxt->past_meas[i].peak_u16 > peak_u16) {
872                        peak_u16 = visu_ctxt->past_meas[i].peak_u16;
873                    }
874                    sum_rms_squared += visu_ctxt->past_meas[i].rms_squared;
875                    nb_valid_meas++;
876                }
877            }
878        }
879        float rms = nb_valid_meas == 0 ? 0.0f : sqrtf(sum_rms_squared / nb_valid_meas);
880        int32_t* p_int_reply_data = (int32_t*)pReplyData;
881        /* convert from I16 sample values to mB and write results */
882        if (rms < 0.000016f) {
883            p_int_reply_data[MEASUREMENT_IDX_RMS] = -9600; //-96dB
884        } else {
885            p_int_reply_data[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
886        }
887        if (peak_u16 == 0) {
888            p_int_reply_data[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
889        } else {
890            p_int_reply_data[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peak_u16 / 32767.0f));
891        }
892        ALOGV("VISUALIZER_CMD_MEASURE peak=%d (%dmB), rms=%.1f (%dmB)",
893                peak_u16, p_int_reply_data[MEASUREMENT_IDX_PEAK],
894                rms, p_int_reply_data[MEASUREMENT_IDX_RMS]);
895        }
896        break;
897
898    default:
899        ALOGW("%s invalid command %d", __func__, cmdCode);
900        return -EINVAL;
901    }
902    return 0;
903}
904
905
906/*
907 * Effect Library Interface Implementation
908 */
909
910int effect_lib_create(const effect_uuid_t *uuid,
911                         int32_t sessionId,
912                         int32_t ioId,
913                         effect_handle_t *pHandle) {
914    int ret;
915    int i;
916
917    if (lib_init() != 0)
918        return init_status;
919
920    if (pHandle == NULL || uuid == NULL)
921        return -EINVAL;
922
923    for (i = 0; descriptors[i] != NULL; i++) {
924        if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0)
925            break;
926    }
927
928    if (descriptors[i] == NULL)
929        return -EINVAL;
930
931    effect_context_t *context;
932    if (memcmp(uuid, &visualizer_descriptor.uuid, sizeof(effect_uuid_t)) == 0) {
933        visualizer_context_t *visu_ctxt = (visualizer_context_t *)calloc(1,
934                                                                     sizeof(visualizer_context_t));
935        if (visu_ctxt == NULL) {
936            ALOGE("%s fail to allocate memory", __func__);
937            return -ENOMEM;
938        }
939        context = (effect_context_t *)visu_ctxt;
940        context->ops.init = visualizer_init;
941        context->ops.reset = visualizer_reset;
942        context->ops.process = visualizer_process;
943        context->ops.set_parameter = visualizer_set_parameter;
944        context->ops.get_parameter = visualizer_get_parameter;
945        context->ops.command = visualizer_command;
946        context->desc = &visualizer_descriptor;
947    } else {
948        return -EINVAL;
949    }
950
951    context->itfe = &effect_interface;
952    context->state = EFFECT_STATE_UNINITIALIZED;
953    context->out_handle = (audio_io_handle_t)ioId;
954
955    ret = context->ops.init(context);
956    if (ret < 0) {
957        ALOGW("%s init failed", __func__);
958        free(context);
959        return ret;
960    }
961
962    context->state = EFFECT_STATE_INITIALIZED;
963
964    pthread_mutex_lock(&lock);
965    list_add_tail(&created_effects_list, &context->effects_list_node);
966    output_context_t *out_ctxt = get_output(ioId);
967    if (out_ctxt != NULL)
968        add_effect_to_output(out_ctxt, context);
969    pthread_mutex_unlock(&lock);
970
971    *pHandle = (effect_handle_t)context;
972
973    ALOGV("%s created context %p", __func__, context);
974
975    return 0;
976
977}
978
979int effect_lib_release(effect_handle_t handle) {
980    effect_context_t *context = (effect_context_t *)handle;
981    int status;
982
983    if (lib_init() != 0)
984        return init_status;
985
986    ALOGV("%s context %p", __func__, handle);
987    pthread_mutex_lock(&lock);
988    status = -EINVAL;
989    if (effect_exists(context)) {
990        output_context_t *out_ctxt = get_output(context->out_handle);
991        if (out_ctxt != NULL)
992            remove_effect_from_output(out_ctxt, context);
993        list_remove(&context->effects_list_node);
994        if (context->ops.release)
995            context->ops.release(context);
996        free(context);
997        status = 0;
998    }
999    pthread_mutex_unlock(&lock);
1000
1001    return status;
1002}
1003
1004int effect_lib_get_descriptor(const effect_uuid_t *uuid,
1005                                effect_descriptor_t *descriptor) {
1006    int i;
1007
1008    if (lib_init() != 0)
1009        return init_status;
1010
1011    if (descriptor == NULL || uuid == NULL) {
1012        ALOGV("%s called with NULL pointer", __func__);
1013        return -EINVAL;
1014    }
1015
1016    for (i = 0; descriptors[i] != NULL; i++) {
1017        if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
1018            *descriptor = *descriptors[i];
1019            return 0;
1020        }
1021    }
1022
1023    return  -EINVAL;
1024}
1025
1026/*
1027 * Effect Control Interface Implementation
1028 */
1029
1030 /* Stub function for effect interface: never called for offloaded effects */
1031int effect_process(effect_handle_t self,
1032                       audio_buffer_t *inBuffer,
1033                       audio_buffer_t *outBuffer)
1034{
1035    effect_context_t * context = (effect_context_t *)self;
1036    int status = 0;
1037
1038    ALOGW("%s Called ?????", __func__);
1039
1040    pthread_mutex_lock(&lock);
1041    if (!effect_exists(context)) {
1042        status = -EINVAL;
1043        goto exit;
1044    }
1045
1046    if (context->state != EFFECT_STATE_ACTIVE) {
1047        status = -EINVAL;
1048        goto exit;
1049    }
1050
1051exit:
1052    pthread_mutex_unlock(&lock);
1053    return status;
1054}
1055
1056int effect_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
1057        void *pCmdData, uint32_t *replySize, void *pReplyData)
1058{
1059
1060    effect_context_t * context = (effect_context_t *)self;
1061    int retsize;
1062    int status = 0;
1063
1064    pthread_mutex_lock(&lock);
1065
1066    if (!effect_exists(context)) {
1067        status = -EINVAL;
1068        goto exit;
1069    }
1070
1071    if (context == NULL || context->state == EFFECT_STATE_UNINITIALIZED) {
1072        status = -EINVAL;
1073        goto exit;
1074    }
1075
1076//    ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,
1077//             "%s command %d cmdSize %d", __func__, cmdCode, cmdSize);
1078
1079    switch (cmdCode) {
1080    case EFFECT_CMD_INIT:
1081        if (pReplyData == NULL || *replySize != sizeof(int)) {
1082            status = -EINVAL;
1083            goto exit;
1084        }
1085        if (context->ops.init)
1086            *(int *) pReplyData = context->ops.init(context);
1087        else
1088            *(int *) pReplyData = 0;
1089        break;
1090    case EFFECT_CMD_SET_CONFIG:
1091        if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
1092                || pReplyData == NULL || *replySize != sizeof(int)) {
1093            status = -EINVAL;
1094            goto exit;
1095        }
1096        *(int *) pReplyData = set_config(context, (effect_config_t *) pCmdData);
1097        break;
1098    case EFFECT_CMD_GET_CONFIG:
1099        if (pReplyData == NULL ||
1100            *replySize != sizeof(effect_config_t)) {
1101            status = -EINVAL;
1102            goto exit;
1103        }
1104        if (!context->offload_enabled) {
1105            status = -EINVAL;
1106            goto exit;
1107        }
1108
1109        get_config(context, (effect_config_t *)pReplyData);
1110        break;
1111    case EFFECT_CMD_RESET:
1112        if (context->ops.reset)
1113            context->ops.reset(context);
1114        break;
1115    case EFFECT_CMD_ENABLE:
1116        if (pReplyData == NULL || *replySize != sizeof(int)) {
1117            status = -EINVAL;
1118            goto exit;
1119        }
1120        if (context->state != EFFECT_STATE_INITIALIZED) {
1121            status = -ENOSYS;
1122            goto exit;
1123        }
1124        context->state = EFFECT_STATE_ACTIVE;
1125        if (context->ops.enable)
1126            context->ops.enable(context);
1127        pthread_cond_signal(&cond);
1128        ALOGV("%s EFFECT_CMD_ENABLE", __func__);
1129        *(int *)pReplyData = 0;
1130        break;
1131    case EFFECT_CMD_DISABLE:
1132        if (pReplyData == NULL || *replySize != sizeof(int)) {
1133            status = -EINVAL;
1134            goto exit;
1135        }
1136        if (context->state != EFFECT_STATE_ACTIVE) {
1137            status = -ENOSYS;
1138            goto exit;
1139        }
1140        context->state = EFFECT_STATE_INITIALIZED;
1141        if (context->ops.disable)
1142            context->ops.disable(context);
1143        pthread_cond_signal(&cond);
1144        ALOGV("%s EFFECT_CMD_DISABLE", __func__);
1145        *(int *)pReplyData = 0;
1146        break;
1147    case EFFECT_CMD_GET_PARAM: {
1148        if (pCmdData == NULL ||
1149            cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
1150            pReplyData == NULL ||
1151            *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
1152            status = -EINVAL;
1153            goto exit;
1154        }
1155        if (!context->offload_enabled) {
1156            status = -EINVAL;
1157            goto exit;
1158        }
1159        memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
1160        effect_param_t *p = (effect_param_t *)pReplyData;
1161        if (context->ops.get_parameter)
1162            context->ops.get_parameter(context, p, replySize);
1163        } break;
1164    case EFFECT_CMD_SET_PARAM: {
1165        if (pCmdData == NULL ||
1166            cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
1167            pReplyData == NULL || *replySize != sizeof(int32_t)) {
1168            status = -EINVAL;
1169            goto exit;
1170        }
1171        *(int32_t *)pReplyData = 0;
1172        effect_param_t *p = (effect_param_t *)pCmdData;
1173        if (context->ops.set_parameter)
1174            *(int32_t *)pReplyData = context->ops.set_parameter(context, p, *replySize);
1175
1176        } break;
1177    case EFFECT_CMD_SET_DEVICE:
1178    case EFFECT_CMD_SET_VOLUME:
1179    case EFFECT_CMD_SET_AUDIO_MODE:
1180        break;
1181
1182    case EFFECT_CMD_OFFLOAD: {
1183        output_context_t *out_ctxt;
1184
1185        if (cmdSize != sizeof(effect_offload_param_t) || pCmdData == NULL
1186                || pReplyData == NULL || *replySize != sizeof(int)) {
1187            ALOGV("%s EFFECT_CMD_OFFLOAD bad format", __func__);
1188            status = -EINVAL;
1189            break;
1190        }
1191
1192        effect_offload_param_t* offload_param = (effect_offload_param_t*)pCmdData;
1193
1194        ALOGV("%s EFFECT_CMD_OFFLOAD offload %d output %d",
1195              __func__, offload_param->isOffload, offload_param->ioHandle);
1196
1197        *(int *)pReplyData = 0;
1198
1199        context->offload_enabled = offload_param->isOffload;
1200        if (context->out_handle == offload_param->ioHandle)
1201            break;
1202
1203        out_ctxt = get_output(context->out_handle);
1204        if (out_ctxt != NULL)
1205            remove_effect_from_output(out_ctxt, context);
1206
1207        context->out_handle = offload_param->ioHandle;
1208        out_ctxt = get_output(offload_param->ioHandle);
1209        if (out_ctxt != NULL)
1210            add_effect_to_output(out_ctxt, context);
1211
1212        } break;
1213
1214
1215    default:
1216        if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY && context->ops.command)
1217            status = context->ops.command(context, cmdCode, cmdSize,
1218                                          pCmdData, replySize, pReplyData);
1219        else {
1220            ALOGW("%s invalid command %d", __func__, cmdCode);
1221            status = -EINVAL;
1222        }
1223        break;
1224    }
1225
1226exit:
1227    pthread_mutex_unlock(&lock);
1228
1229//    ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,"%s DONE", __func__);
1230    return status;
1231}
1232
1233/* Effect Control Interface Implementation: get_descriptor */
1234int effect_get_descriptor(effect_handle_t   self,
1235                                    effect_descriptor_t *descriptor)
1236{
1237    effect_context_t *context = (effect_context_t *)self;
1238
1239    if (!effect_exists(context))
1240        return -EINVAL;
1241
1242    if (descriptor == NULL)
1243        return -EINVAL;
1244
1245    *descriptor = *context->desc;
1246
1247    return 0;
1248}
1249
1250/* effect_handle_t interface implementation for visualizer effect */
1251const struct effect_interface_s effect_interface = {
1252        effect_process,
1253        effect_command,
1254        effect_get_descriptor,
1255        NULL,
1256};
1257
1258__attribute__ ((visibility ("default")))
1259audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
1260    tag : AUDIO_EFFECT_LIBRARY_TAG,
1261    version : EFFECT_LIBRARY_API_VERSION,
1262    name : "Visualizer Library",
1263    implementor : "The Android Open Source Project",
1264    create_effect : effect_lib_create,
1265    release_effect : effect_lib_release,
1266    get_descriptor : effect_lib_get_descriptor,
1267};
1268