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