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