blob: 716755b5732832994a229c6121eb4d885ec1eb94 [file] [log] [blame]
Eric Laurentc4aef752013-09-12 17:45:53 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "offload_visualizer"
18/*#define LOG_NDEBUG 0*/
19#include <assert.h>
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
Ben Romberger22d41232016-11-16 14:55:25 -0800182#ifdef PLATFORM_MSM8998
Garmond Leung6406e9d2016-08-23 16:31:03 -0700183#define CAPTURE_DEVICE 7
184#else
Eric Laurentc4aef752013-09-12 17:45:53 -0700185#define CAPTURE_DEVICE 8
Garmond Leung6406e9d2016-08-23 16:31:03 -0700186#endif
Eric Laurentc4aef752013-09-12 17:45:53 -0700187
188/* Proxy port supports only MMAP read and those fixed parameters*/
189#define AUDIO_CAPTURE_CHANNEL_COUNT 2
190#define AUDIO_CAPTURE_SMP_RATE 48000
191#define AUDIO_CAPTURE_PERIOD_SIZE (768)
192#define AUDIO_CAPTURE_PERIOD_COUNT 32
193
194struct pcm_config pcm_config_capture = {
195 .channels = AUDIO_CAPTURE_CHANNEL_COUNT,
196 .rate = AUDIO_CAPTURE_SMP_RATE,
197 .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
198 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
199 .format = PCM_FORMAT_S16_LE,
200 .start_threshold = AUDIO_CAPTURE_PERIOD_SIZE / 4,
201 .stop_threshold = INT_MAX,
202 .avail_min = AUDIO_CAPTURE_PERIOD_SIZE / 4,
203};
204
205
206/*
207 * Local functions
208 */
209
210static void init_once() {
211 list_init(&created_effects_list);
212 list_init(&active_outputs_list);
213
214 pthread_mutex_init(&lock, NULL);
215 pthread_mutex_init(&thread_lock, NULL);
216 pthread_cond_init(&cond, NULL);
217 exit_thread = false;
218 thread_status = -1;
219
220 init_status = 0;
221}
222
223int lib_init() {
224 pthread_once(&once, init_once);
225 return init_status;
226}
227
228bool effect_exists(effect_context_t *context) {
229 struct listnode *node;
230
231 list_for_each(node, &created_effects_list) {
232 effect_context_t *fx_ctxt = node_to_item(node,
233 effect_context_t,
234 effects_list_node);
235 if (fx_ctxt == context) {
236 return true;
237 }
238 }
239 return false;
240}
241
242output_context_t *get_output(audio_io_handle_t output) {
243 struct listnode *node;
244
245 list_for_each(node, &active_outputs_list) {
246 output_context_t *out_ctxt = node_to_item(node,
247 output_context_t,
248 outputs_list_node);
249 if (out_ctxt->handle == output) {
250 return out_ctxt;
251 }
252 }
253 return NULL;
254}
255
256void add_effect_to_output(output_context_t * output, effect_context_t *context) {
257 struct listnode *fx_node;
258
259 list_for_each(fx_node, &output->effects_list) {
260 effect_context_t *fx_ctxt = node_to_item(fx_node,
261 effect_context_t,
262 output_node);
263 if (fx_ctxt == context)
264 return;
265 }
266 list_add_tail(&output->effects_list, &context->output_node);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800267 if (context->ops.start)
268 context->ops.start(context, output);
Eric Laurentc4aef752013-09-12 17:45:53 -0700269}
270
271void remove_effect_from_output(output_context_t * output, effect_context_t *context) {
272 struct listnode *fx_node;
273
274 list_for_each(fx_node, &output->effects_list) {
275 effect_context_t *fx_ctxt = node_to_item(fx_node,
276 effect_context_t,
277 output_node);
278 if (fx_ctxt == context) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800279 if (context->ops.stop)
280 context->ops.stop(context, output);
Eric Laurentc4aef752013-09-12 17:45:53 -0700281 list_remove(&context->output_node);
282 return;
283 }
284 }
285}
286
287bool effects_enabled() {
288 struct listnode *out_node;
289
290 list_for_each(out_node, &active_outputs_list) {
291 struct listnode *fx_node;
292 output_context_t *out_ctxt = node_to_item(out_node,
293 output_context_t,
294 outputs_list_node);
295
296 list_for_each(fx_node, &out_ctxt->effects_list) {
297 effect_context_t *fx_ctxt = node_to_item(fx_node,
298 effect_context_t,
299 output_node);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800300 if (fx_ctxt->state == EFFECT_STATE_ACTIVE && fx_ctxt->ops.process != NULL)
Eric Laurentc4aef752013-09-12 17:45:53 -0700301 return true;
302 }
303 }
304 return false;
305}
306
vivek mehta39cfce62015-09-18 10:39:16 -0700307int set_control(const char* name, struct mixer *mixer, int value) {
Eric Laurentc4aef752013-09-12 17:45:53 -0700308 struct mixer_ctl *ctl;
309
vivek mehta39cfce62015-09-18 10:39:16 -0700310 ctl = mixer_get_ctl_by_name(mixer, name);
311 if (ctl == NULL) {
312 ALOGW("%s: could not get %s ctl", __func__, name);
313 return -EINVAL;
314 }
315 if (mixer_ctl_set_value(ctl, 0, value) != 0) {
316 ALOGW("%s: error setting value %d on %s ", __func__, value, name);
317 return -EINVAL;
318 }
319
320 return 0;
321}
322
323int configure_proxy_capture(struct mixer *mixer, int value) {
324 int retval = 0;
325
Ravi Kumar Alamanda518bcbb2014-11-14 16:51:10 -0800326 if (value && acdb_send_audio_cal)
327 acdb_send_audio_cal(AFE_PROXY_ACDB_ID, ACDB_DEV_TYPE_OUT);
328
vivek mehta39cfce62015-09-18 10:39:16 -0700329 retval = set_control("AFE_PCM_RX Audio Mixer MultiMedia4", mixer, value);
330
331 if (retval != 0)
332 return retval;
333
334 // Extending visualizer to capture for compress2 path as well.
335 // for extending it to multiple offload either this needs to be extended
336 // or need to find better solution to enable only active offload sessions
337
338 retval = set_control("AFE_PCM_RX Audio Mixer MultiMedia7", mixer, value);
339 if (retval != 0)
340 return retval;
Eric Laurentc4aef752013-09-12 17:45:53 -0700341
342 return 0;
343}
344
345
346void *capture_thread_loop(void *arg)
347{
348 int16_t data[AUDIO_CAPTURE_PERIOD_SIZE * AUDIO_CAPTURE_CHANNEL_COUNT * sizeof(int16_t)];
349 audio_buffer_t buf;
350 buf.frameCount = AUDIO_CAPTURE_PERIOD_SIZE;
351 buf.s16 = data;
352 bool capture_enabled = false;
353 struct mixer *mixer;
354 struct pcm *pcm = NULL;
355 int ret;
356 int retry_num = 0;
357
358 ALOGD("thread enter");
359
360 prctl(PR_SET_NAME, (unsigned long)"visualizer capture", 0, 0, 0);
361
362 pthread_mutex_lock(&lock);
363
364 mixer = mixer_open(MIXER_CARD);
365 while (mixer == NULL && retry_num < RETRY_NUMBER) {
366 usleep(RETRY_US);
367 mixer = mixer_open(MIXER_CARD);
368 retry_num++;
369 }
370 if (mixer == NULL) {
371 pthread_mutex_unlock(&lock);
372 return NULL;
373 }
374
375 for (;;) {
376 if (exit_thread) {
377 break;
378 }
379 if (effects_enabled()) {
380 if (!capture_enabled) {
381 ret = configure_proxy_capture(mixer, 1);
382 if (ret == 0) {
383 pcm = pcm_open(SOUND_CARD, CAPTURE_DEVICE,
384 PCM_IN|PCM_MMAP|PCM_NOIRQ, &pcm_config_capture);
385 if (pcm && !pcm_is_ready(pcm)) {
386 ALOGW("%s: %s", __func__, pcm_get_error(pcm));
387 pcm_close(pcm);
388 pcm = NULL;
389 configure_proxy_capture(mixer, 0);
390 } else {
391 capture_enabled = true;
392 ALOGD("%s: capture ENABLED", __func__);
393 }
394 }
395 }
396 } else {
397 if (capture_enabled) {
398 if (pcm != NULL)
399 pcm_close(pcm);
400 configure_proxy_capture(mixer, 0);
401 ALOGD("%s: capture DISABLED", __func__);
402 capture_enabled = false;
403 }
404 pthread_cond_wait(&cond, &lock);
405 }
406 if (!capture_enabled)
407 continue;
408
409 pthread_mutex_unlock(&lock);
410 ret = pcm_mmap_read(pcm, data, sizeof(data));
411 pthread_mutex_lock(&lock);
412
413 if (ret == 0) {
414 struct listnode *out_node;
415
416 list_for_each(out_node, &active_outputs_list) {
417 output_context_t *out_ctxt = node_to_item(out_node,
418 output_context_t,
419 outputs_list_node);
420 struct listnode *fx_node;
421
422 list_for_each(fx_node, &out_ctxt->effects_list) {
423 effect_context_t *fx_ctxt = node_to_item(fx_node,
424 effect_context_t,
425 output_node);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800426 if (fx_ctxt->ops.process != NULL)
427 fx_ctxt->ops.process(fx_ctxt, &buf, &buf);
Eric Laurentc4aef752013-09-12 17:45:53 -0700428 }
429 }
430 } else {
431 ALOGW("%s: read status %d %s", __func__, ret, pcm_get_error(pcm));
432 }
433 }
434
435 if (capture_enabled) {
436 if (pcm != NULL)
437 pcm_close(pcm);
438 configure_proxy_capture(mixer, 0);
439 }
440 mixer_close(mixer);
441 pthread_mutex_unlock(&lock);
442
443 ALOGD("thread exit");
444
445 return NULL;
446}
447
448/*
449 * Interface from audio HAL
450 */
451
452__attribute__ ((visibility ("default")))
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800453int visualizer_hal_start_output(audio_io_handle_t output, int pcm_id) {
Eric Laurentc4aef752013-09-12 17:45:53 -0700454 int ret;
455 struct listnode *node;
456
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800457 ALOGV("%s output %d pcm_id %d", __func__, output, pcm_id);
Eric Laurentc4aef752013-09-12 17:45:53 -0700458
459 if (lib_init() != 0)
460 return init_status;
461
462 pthread_mutex_lock(&thread_lock);
463 pthread_mutex_lock(&lock);
464 if (get_output(output) != NULL) {
465 ALOGW("%s output already started", __func__);
466 ret = -ENOSYS;
467 goto exit;
468 }
469
470 output_context_t *out_ctxt = (output_context_t *)malloc(sizeof(output_context_t));
wjiangebb69fa2014-05-15 19:38:26 +0800471 if (out_ctxt == NULL) {
472 ALOGE("%s fail to allocate memory", __func__);
473 ret = -ENOMEM;
474 goto exit;
475 }
Eric Laurentc4aef752013-09-12 17:45:53 -0700476 out_ctxt->handle = output;
477 list_init(&out_ctxt->effects_list);
478
479 list_for_each(node, &created_effects_list) {
480 effect_context_t *fx_ctxt = node_to_item(node,
481 effect_context_t,
482 effects_list_node);
483 if (fx_ctxt->out_handle == output) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800484 if (fx_ctxt->ops.start)
485 fx_ctxt->ops.start(fx_ctxt, out_ctxt);
Eric Laurentc4aef752013-09-12 17:45:53 -0700486 list_add_tail(&out_ctxt->effects_list, &fx_ctxt->output_node);
487 }
488 }
489 if (list_empty(&active_outputs_list)) {
490 exit_thread = false;
491 thread_status = pthread_create(&capture_thread, (const pthread_attr_t *) NULL,
492 capture_thread_loop, NULL);
493 }
494 list_add_tail(&active_outputs_list, &out_ctxt->outputs_list_node);
495 pthread_cond_signal(&cond);
496
497exit:
498 pthread_mutex_unlock(&lock);
499 pthread_mutex_unlock(&thread_lock);
500 return ret;
501}
502
503__attribute__ ((visibility ("default")))
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800504int visualizer_hal_stop_output(audio_io_handle_t output, int pcm_id) {
Eric Laurentc4aef752013-09-12 17:45:53 -0700505 int ret;
506 struct listnode *node;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800507 struct listnode *fx_node;
Eric Laurentc4aef752013-09-12 17:45:53 -0700508 output_context_t *out_ctxt;
509
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800510 ALOGV("%s output %d pcm_id %d", __func__, output, pcm_id);
Eric Laurentc4aef752013-09-12 17:45:53 -0700511
512 if (lib_init() != 0)
513 return init_status;
514
515 pthread_mutex_lock(&thread_lock);
516 pthread_mutex_lock(&lock);
517
518 out_ctxt = get_output(output);
519 if (out_ctxt == NULL) {
520 ALOGW("%s output not started", __func__);
521 ret = -ENOSYS;
522 goto exit;
523 }
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800524 list_for_each(fx_node, &out_ctxt->effects_list) {
525 effect_context_t *fx_ctxt = node_to_item(fx_node,
526 effect_context_t,
527 output_node);
528 if (fx_ctxt->ops.stop)
529 fx_ctxt->ops.stop(fx_ctxt, out_ctxt);
530 }
Eric Laurentc4aef752013-09-12 17:45:53 -0700531 list_remove(&out_ctxt->outputs_list_node);
532 pthread_cond_signal(&cond);
533
534 if (list_empty(&active_outputs_list)) {
535 if (thread_status == 0) {
536 exit_thread = true;
537 pthread_cond_signal(&cond);
538 pthread_mutex_unlock(&lock);
539 pthread_join(capture_thread, (void **) NULL);
540 pthread_mutex_lock(&lock);
541 thread_status = -1;
542 }
543 }
544
545 free(out_ctxt);
546
547exit:
548 pthread_mutex_unlock(&lock);
549 pthread_mutex_unlock(&thread_lock);
550 return ret;
551}
552
553
554/*
555 * Effect operations
556 */
557
558int set_config(effect_context_t *context, effect_config_t *config)
559{
560 if (config->inputCfg.samplingRate != config->outputCfg.samplingRate) return -EINVAL;
561 if (config->inputCfg.channels != config->outputCfg.channels) return -EINVAL;
562 if (config->inputCfg.format != config->outputCfg.format) return -EINVAL;
563 if (config->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL;
564 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};