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