blob: d363b7783954528e2c0b6f3302d81ba18e7ae26e [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
182#define CAPTURE_DEVICE 8
183
184/* Proxy port supports only MMAP read and those fixed parameters*/
185#define AUDIO_CAPTURE_CHANNEL_COUNT 2
186#define AUDIO_CAPTURE_SMP_RATE 48000
187#define AUDIO_CAPTURE_PERIOD_SIZE (768)
188#define AUDIO_CAPTURE_PERIOD_COUNT 32
189
190struct pcm_config pcm_config_capture = {
191 .channels = AUDIO_CAPTURE_CHANNEL_COUNT,
192 .rate = AUDIO_CAPTURE_SMP_RATE,
193 .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
194 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
195 .format = PCM_FORMAT_S16_LE,
196 .start_threshold = AUDIO_CAPTURE_PERIOD_SIZE / 4,
197 .stop_threshold = INT_MAX,
198 .avail_min = AUDIO_CAPTURE_PERIOD_SIZE / 4,
199};
200
201
202/*
203 * Local functions
204 */
205
206static void init_once() {
207 list_init(&created_effects_list);
208 list_init(&active_outputs_list);
209
210 pthread_mutex_init(&lock, NULL);
211 pthread_mutex_init(&thread_lock, NULL);
212 pthread_cond_init(&cond, NULL);
213 exit_thread = false;
214 thread_status = -1;
215
216 init_status = 0;
217}
218
219int lib_init() {
220 pthread_once(&once, init_once);
221 return init_status;
222}
223
224bool effect_exists(effect_context_t *context) {
225 struct listnode *node;
226
227 list_for_each(node, &created_effects_list) {
228 effect_context_t *fx_ctxt = node_to_item(node,
229 effect_context_t,
230 effects_list_node);
231 if (fx_ctxt == context) {
232 return true;
233 }
234 }
235 return false;
236}
237
238output_context_t *get_output(audio_io_handle_t output) {
239 struct listnode *node;
240
241 list_for_each(node, &active_outputs_list) {
242 output_context_t *out_ctxt = node_to_item(node,
243 output_context_t,
244 outputs_list_node);
245 if (out_ctxt->handle == output) {
246 return out_ctxt;
247 }
248 }
249 return NULL;
250}
251
252void add_effect_to_output(output_context_t * output, effect_context_t *context) {
253 struct listnode *fx_node;
254
255 list_for_each(fx_node, &output->effects_list) {
256 effect_context_t *fx_ctxt = node_to_item(fx_node,
257 effect_context_t,
258 output_node);
259 if (fx_ctxt == context)
260 return;
261 }
262 list_add_tail(&output->effects_list, &context->output_node);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800263 if (context->ops.start)
264 context->ops.start(context, output);
Eric Laurentc4aef752013-09-12 17:45:53 -0700265}
266
267void remove_effect_from_output(output_context_t * output, effect_context_t *context) {
268 struct listnode *fx_node;
269
270 list_for_each(fx_node, &output->effects_list) {
271 effect_context_t *fx_ctxt = node_to_item(fx_node,
272 effect_context_t,
273 output_node);
274 if (fx_ctxt == context) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800275 if (context->ops.stop)
276 context->ops.stop(context, output);
Eric Laurentc4aef752013-09-12 17:45:53 -0700277 list_remove(&context->output_node);
278 return;
279 }
280 }
281}
282
283bool effects_enabled() {
284 struct listnode *out_node;
285
286 list_for_each(out_node, &active_outputs_list) {
287 struct listnode *fx_node;
288 output_context_t *out_ctxt = node_to_item(out_node,
289 output_context_t,
290 outputs_list_node);
291
292 list_for_each(fx_node, &out_ctxt->effects_list) {
293 effect_context_t *fx_ctxt = node_to_item(fx_node,
294 effect_context_t,
295 output_node);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800296 if (fx_ctxt->state == EFFECT_STATE_ACTIVE && fx_ctxt->ops.process != NULL)
Eric Laurentc4aef752013-09-12 17:45:53 -0700297 return true;
298 }
299 }
300 return false;
301}
302
303int configure_proxy_capture(struct mixer *mixer, int value) {
304 const char *proxy_ctl_name = "AFE_PCM_RX Audio Mixer MultiMedia4";
305 struct mixer_ctl *ctl;
306
Ravi Kumar Alamanda518bcbb2014-11-14 16:51:10 -0800307 if (value && acdb_send_audio_cal)
308 acdb_send_audio_cal(AFE_PROXY_ACDB_ID, ACDB_DEV_TYPE_OUT);
309
Eric Laurentc4aef752013-09-12 17:45:53 -0700310 ctl = mixer_get_ctl_by_name(mixer, proxy_ctl_name);
311 if (ctl == NULL) {
312 ALOGW("%s: could not get %s ctl", __func__, proxy_ctl_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, proxy_ctl_name);
317
318 return 0;
319}
320
321
322void *capture_thread_loop(void *arg)
323{
324 int16_t data[AUDIO_CAPTURE_PERIOD_SIZE * AUDIO_CAPTURE_CHANNEL_COUNT * sizeof(int16_t)];
325 audio_buffer_t buf;
326 buf.frameCount = AUDIO_CAPTURE_PERIOD_SIZE;
327 buf.s16 = data;
328 bool capture_enabled = false;
329 struct mixer *mixer;
330 struct pcm *pcm = NULL;
331 int ret;
332 int retry_num = 0;
333
334 ALOGD("thread enter");
335
336 prctl(PR_SET_NAME, (unsigned long)"visualizer capture", 0, 0, 0);
337
338 pthread_mutex_lock(&lock);
339
340 mixer = mixer_open(MIXER_CARD);
341 while (mixer == NULL && retry_num < RETRY_NUMBER) {
342 usleep(RETRY_US);
343 mixer = mixer_open(MIXER_CARD);
344 retry_num++;
345 }
346 if (mixer == NULL) {
347 pthread_mutex_unlock(&lock);
348 return NULL;
349 }
350
351 for (;;) {
352 if (exit_thread) {
353 break;
354 }
355 if (effects_enabled()) {
356 if (!capture_enabled) {
357 ret = configure_proxy_capture(mixer, 1);
358 if (ret == 0) {
359 pcm = pcm_open(SOUND_CARD, CAPTURE_DEVICE,
360 PCM_IN|PCM_MMAP|PCM_NOIRQ, &pcm_config_capture);
361 if (pcm && !pcm_is_ready(pcm)) {
362 ALOGW("%s: %s", __func__, pcm_get_error(pcm));
363 pcm_close(pcm);
364 pcm = NULL;
365 configure_proxy_capture(mixer, 0);
366 } else {
367 capture_enabled = true;
368 ALOGD("%s: capture ENABLED", __func__);
369 }
370 }
371 }
372 } else {
373 if (capture_enabled) {
374 if (pcm != NULL)
375 pcm_close(pcm);
376 configure_proxy_capture(mixer, 0);
377 ALOGD("%s: capture DISABLED", __func__);
378 capture_enabled = false;
379 }
380 pthread_cond_wait(&cond, &lock);
381 }
382 if (!capture_enabled)
383 continue;
384
385 pthread_mutex_unlock(&lock);
386 ret = pcm_mmap_read(pcm, data, sizeof(data));
387 pthread_mutex_lock(&lock);
388
389 if (ret == 0) {
390 struct listnode *out_node;
391
392 list_for_each(out_node, &active_outputs_list) {
393 output_context_t *out_ctxt = node_to_item(out_node,
394 output_context_t,
395 outputs_list_node);
396 struct listnode *fx_node;
397
398 list_for_each(fx_node, &out_ctxt->effects_list) {
399 effect_context_t *fx_ctxt = node_to_item(fx_node,
400 effect_context_t,
401 output_node);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800402 if (fx_ctxt->ops.process != NULL)
403 fx_ctxt->ops.process(fx_ctxt, &buf, &buf);
Eric Laurentc4aef752013-09-12 17:45:53 -0700404 }
405 }
406 } else {
407 ALOGW("%s: read status %d %s", __func__, ret, pcm_get_error(pcm));
408 }
409 }
410
411 if (capture_enabled) {
412 if (pcm != NULL)
413 pcm_close(pcm);
414 configure_proxy_capture(mixer, 0);
415 }
416 mixer_close(mixer);
417 pthread_mutex_unlock(&lock);
418
419 ALOGD("thread exit");
420
421 return NULL;
422}
423
424/*
425 * Interface from audio HAL
426 */
427
428__attribute__ ((visibility ("default")))
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800429int visualizer_hal_start_output(audio_io_handle_t output, int pcm_id) {
Eric Laurentc4aef752013-09-12 17:45:53 -0700430 int ret;
431 struct listnode *node;
432
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800433 ALOGV("%s output %d pcm_id %d", __func__, output, pcm_id);
Eric Laurentc4aef752013-09-12 17:45:53 -0700434
435 if (lib_init() != 0)
436 return init_status;
437
438 pthread_mutex_lock(&thread_lock);
439 pthread_mutex_lock(&lock);
440 if (get_output(output) != NULL) {
441 ALOGW("%s output already started", __func__);
442 ret = -ENOSYS;
443 goto exit;
444 }
445
446 output_context_t *out_ctxt = (output_context_t *)malloc(sizeof(output_context_t));
wjiangebb69fa2014-05-15 19:38:26 +0800447 if (out_ctxt == NULL) {
448 ALOGE("%s fail to allocate memory", __func__);
449 ret = -ENOMEM;
450 goto exit;
451 }
Eric Laurentc4aef752013-09-12 17:45:53 -0700452 out_ctxt->handle = output;
453 list_init(&out_ctxt->effects_list);
454
455 list_for_each(node, &created_effects_list) {
456 effect_context_t *fx_ctxt = node_to_item(node,
457 effect_context_t,
458 effects_list_node);
459 if (fx_ctxt->out_handle == output) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800460 if (fx_ctxt->ops.start)
461 fx_ctxt->ops.start(fx_ctxt, out_ctxt);
Eric Laurentc4aef752013-09-12 17:45:53 -0700462 list_add_tail(&out_ctxt->effects_list, &fx_ctxt->output_node);
463 }
464 }
465 if (list_empty(&active_outputs_list)) {
466 exit_thread = false;
467 thread_status = pthread_create(&capture_thread, (const pthread_attr_t *) NULL,
468 capture_thread_loop, NULL);
469 }
470 list_add_tail(&active_outputs_list, &out_ctxt->outputs_list_node);
471 pthread_cond_signal(&cond);
472
473exit:
474 pthread_mutex_unlock(&lock);
475 pthread_mutex_unlock(&thread_lock);
476 return ret;
477}
478
479__attribute__ ((visibility ("default")))
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800480int visualizer_hal_stop_output(audio_io_handle_t output, int pcm_id) {
Eric Laurentc4aef752013-09-12 17:45:53 -0700481 int ret;
482 struct listnode *node;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800483 struct listnode *fx_node;
Eric Laurentc4aef752013-09-12 17:45:53 -0700484 output_context_t *out_ctxt;
485
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800486 ALOGV("%s output %d pcm_id %d", __func__, output, pcm_id);
Eric Laurentc4aef752013-09-12 17:45:53 -0700487
488 if (lib_init() != 0)
489 return init_status;
490
491 pthread_mutex_lock(&thread_lock);
492 pthread_mutex_lock(&lock);
493
494 out_ctxt = get_output(output);
495 if (out_ctxt == NULL) {
496 ALOGW("%s output not started", __func__);
497 ret = -ENOSYS;
498 goto exit;
499 }
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800500 list_for_each(fx_node, &out_ctxt->effects_list) {
501 effect_context_t *fx_ctxt = node_to_item(fx_node,
502 effect_context_t,
503 output_node);
504 if (fx_ctxt->ops.stop)
505 fx_ctxt->ops.stop(fx_ctxt, out_ctxt);
506 }
Eric Laurentc4aef752013-09-12 17:45:53 -0700507 list_remove(&out_ctxt->outputs_list_node);
508 pthread_cond_signal(&cond);
509
510 if (list_empty(&active_outputs_list)) {
511 if (thread_status == 0) {
512 exit_thread = true;
513 pthread_cond_signal(&cond);
514 pthread_mutex_unlock(&lock);
515 pthread_join(capture_thread, (void **) NULL);
516 pthread_mutex_lock(&lock);
517 thread_status = -1;
518 }
519 }
520
521 free(out_ctxt);
522
523exit:
524 pthread_mutex_unlock(&lock);
525 pthread_mutex_unlock(&thread_lock);
526 return ret;
527}
528
529
530/*
531 * Effect operations
532 */
533
534int set_config(effect_context_t *context, effect_config_t *config)
535{
536 if (config->inputCfg.samplingRate != config->outputCfg.samplingRate) return -EINVAL;
537 if (config->inputCfg.channels != config->outputCfg.channels) return -EINVAL;
538 if (config->inputCfg.format != config->outputCfg.format) return -EINVAL;
539 if (config->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL;
540 if (config->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
541 config->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
542 if (config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
543
544 context->config = *config;
545
546 if (context->ops.reset)
547 context->ops.reset(context);
548
549 return 0;
550}
551
552void get_config(effect_context_t *context, effect_config_t *config)
553{
554 *config = context->config;
555}
556
557
558/*
559 * Visualizer operations
560 */
561
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700562uint32_t visualizer_get_delta_time_ms_from_updated_time(visualizer_context_t* visu_ctxt) {
563 uint32_t delta_ms = 0;
564 if (visu_ctxt->buffer_update_time.tv_sec != 0) {
565 struct timespec ts;
566 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
567 time_t secs = ts.tv_sec - visu_ctxt->buffer_update_time.tv_sec;
568 long nsec = ts.tv_nsec - visu_ctxt->buffer_update_time.tv_nsec;
569 if (nsec < 0) {
570 --secs;
571 nsec += 1000000000;
572 }
573 delta_ms = secs * 1000 + nsec / 1000000;
574 }
575 }
576 return delta_ms;
577}
578
Eric Laurentc4aef752013-09-12 17:45:53 -0700579int visualizer_reset(effect_context_t *context)
580{
581 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
582
583 visu_ctxt->capture_idx = 0;
584 visu_ctxt->last_capture_idx = 0;
585 visu_ctxt->buffer_update_time.tv_sec = 0;
586 visu_ctxt->latency = DSP_OUTPUT_LATENCY_MS;
587 memset(visu_ctxt->capture_buf, 0x80, CAPTURE_BUF_SIZE);
588 return 0;
589}
590
591int visualizer_init(effect_context_t *context)
592{
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700593 int32_t i;
594
Eric Laurentc4aef752013-09-12 17:45:53 -0700595 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
596
597 context->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
598 context->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
599 context->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
600 context->config.inputCfg.samplingRate = 44100;
601 context->config.inputCfg.bufferProvider.getBuffer = NULL;
602 context->config.inputCfg.bufferProvider.releaseBuffer = NULL;
603 context->config.inputCfg.bufferProvider.cookie = NULL;
604 context->config.inputCfg.mask = EFFECT_CONFIG_ALL;
605 context->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
606 context->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
607 context->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
608 context->config.outputCfg.samplingRate = 44100;
609 context->config.outputCfg.bufferProvider.getBuffer = NULL;
610 context->config.outputCfg.bufferProvider.releaseBuffer = NULL;
611 context->config.outputCfg.bufferProvider.cookie = NULL;
612 context->config.outputCfg.mask = EFFECT_CONFIG_ALL;
613
614 visu_ctxt->capture_size = VISUALIZER_CAPTURE_SIZE_MAX;
615 visu_ctxt->scaling_mode = VISUALIZER_SCALING_MODE_NORMALIZED;
616
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700617 // measurement initialization
618 visu_ctxt->channel_count = popcount(context->config.inputCfg.channels);
619 visu_ctxt->meas_mode = MEASUREMENT_MODE_NONE;
620 visu_ctxt->meas_wndw_size_in_buffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
621 visu_ctxt->meas_buffer_idx = 0;
622 for (i=0 ; i<visu_ctxt->meas_wndw_size_in_buffers ; i++) {
623 visu_ctxt->past_meas[i].is_valid = false;
624 visu_ctxt->past_meas[i].peak_u16 = 0;
625 visu_ctxt->past_meas[i].rms_squared = 0;
626 }
627
Eric Laurentc4aef752013-09-12 17:45:53 -0700628 set_config(context, &context->config);
629
Ravi Kumar Alamanda518bcbb2014-11-14 16:51:10 -0800630 if (acdb_handle == NULL) {
631 acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
632 if (acdb_handle == NULL) {
633 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
634 } else {
635 acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(acdb_handle,
636 "acdb_loader_send_audio_cal");
637 if (!acdb_send_audio_cal)
638 ALOGE("%s: Could not find the symbol acdb_send_audio_cal from %s",
639 __func__, LIB_ACDB_LOADER);
640 }
641 }
642
Eric Laurentc4aef752013-09-12 17:45:53 -0700643 return 0;
644}
645
646int visualizer_get_parameter(effect_context_t *context, effect_param_t *p, uint32_t *size)
647{
648 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
649
650 p->status = 0;
651 *size = sizeof(effect_param_t) + sizeof(uint32_t);
652 if (p->psize != sizeof(uint32_t)) {
653 p->status = -EINVAL;
654 return 0;
655 }
656 switch (*(uint32_t *)p->data) {
657 case VISUALIZER_PARAM_CAPTURE_SIZE:
658 ALOGV("%s get capture_size = %d", __func__, visu_ctxt->capture_size);
659 *((uint32_t *)p->data + 1) = visu_ctxt->capture_size;
660 p->vsize = sizeof(uint32_t);
661 *size += sizeof(uint32_t);
662 break;
663 case VISUALIZER_PARAM_SCALING_MODE:
664 ALOGV("%s get scaling_mode = %d", __func__, visu_ctxt->scaling_mode);
665 *((uint32_t *)p->data + 1) = visu_ctxt->scaling_mode;
666 p->vsize = sizeof(uint32_t);
667 *size += sizeof(uint32_t);
668 break;
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700669 case VISUALIZER_PARAM_MEASUREMENT_MODE:
670 ALOGV("%s get meas_mode = %d", __func__, visu_ctxt->meas_mode);
671 *((uint32_t *)p->data + 1) = visu_ctxt->meas_mode;
672 p->vsize = sizeof(uint32_t);
673 *size += sizeof(uint32_t);
674 break;
Eric Laurentc4aef752013-09-12 17:45:53 -0700675 default:
676 p->status = -EINVAL;
677 }
678 return 0;
679}
680
681int visualizer_set_parameter(effect_context_t *context, effect_param_t *p, uint32_t size)
682{
683 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
684
685 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t))
686 return -EINVAL;
687
688 switch (*(uint32_t *)p->data) {
689 case VISUALIZER_PARAM_CAPTURE_SIZE:
690 visu_ctxt->capture_size = *((uint32_t *)p->data + 1);
691 ALOGV("%s set capture_size = %d", __func__, visu_ctxt->capture_size);
692 break;
693 case VISUALIZER_PARAM_SCALING_MODE:
694 visu_ctxt->scaling_mode = *((uint32_t *)p->data + 1);
695 ALOGV("%s set scaling_mode = %d", __func__, visu_ctxt->scaling_mode);
696 break;
697 case VISUALIZER_PARAM_LATENCY:
698 /* Ignore latency as we capture at DSP output
699 * visu_ctxt->latency = *((uint32_t *)p->data + 1); */
700 ALOGV("%s set latency = %d", __func__, visu_ctxt->latency);
701 break;
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700702 case VISUALIZER_PARAM_MEASUREMENT_MODE:
703 visu_ctxt->meas_mode = *((uint32_t *)p->data + 1);
704 ALOGV("%s set meas_mode = %d", __func__, visu_ctxt->meas_mode);
705 break;
Eric Laurentc4aef752013-09-12 17:45:53 -0700706 default:
707 return -EINVAL;
708 }
709 return 0;
710}
711
712/* Real process function called from capture thread. Called with lock held */
713int visualizer_process(effect_context_t *context,
714 audio_buffer_t *inBuffer,
715 audio_buffer_t *outBuffer)
716{
717 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
718
719 if (!effect_exists(context))
720 return -EINVAL;
721
722 if (inBuffer == NULL || inBuffer->raw == NULL ||
723 outBuffer == NULL || outBuffer->raw == NULL ||
724 inBuffer->frameCount != outBuffer->frameCount ||
725 inBuffer->frameCount == 0) {
726 return -EINVAL;
727 }
728
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700729 // perform measurements if needed
730 if (visu_ctxt->meas_mode & MEASUREMENT_MODE_PEAK_RMS) {
731 // find the peak and RMS squared for the new buffer
732 uint32_t inIdx;
733 int16_t max_sample = 0;
734 float rms_squared_acc = 0;
735 for (inIdx = 0 ; inIdx < inBuffer->frameCount * visu_ctxt->channel_count ; inIdx++) {
736 if (inBuffer->s16[inIdx] > max_sample) {
737 max_sample = inBuffer->s16[inIdx];
738 } else if (-inBuffer->s16[inIdx] > max_sample) {
739 max_sample = -inBuffer->s16[inIdx];
740 }
741 rms_squared_acc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]);
742 }
743 // store the measurement
744 visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].peak_u16 = (uint16_t)max_sample;
745 visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].rms_squared =
746 rms_squared_acc / (inBuffer->frameCount * visu_ctxt->channel_count);
747 visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].is_valid = true;
748 if (++visu_ctxt->meas_buffer_idx >= visu_ctxt->meas_wndw_size_in_buffers) {
749 visu_ctxt->meas_buffer_idx = 0;
750 }
751 }
752
Eric Laurentc4aef752013-09-12 17:45:53 -0700753 /* all code below assumes stereo 16 bit PCM output and input */
754 int32_t shift;
755
756 if (visu_ctxt->scaling_mode == VISUALIZER_SCALING_MODE_NORMALIZED) {
757 /* derive capture scaling factor from peak value in current buffer
758 * this gives more interesting captures for display. */
759 shift = 32;
760 int len = inBuffer->frameCount * 2;
761 int i;
762 for (i = 0; i < len; i++) {
763 int32_t smp = inBuffer->s16[i];
764 if (smp < 0) smp = -smp - 1; /* take care to keep the max negative in range */
765 int32_t clz = __builtin_clz(smp);
766 if (shift > clz) shift = clz;
767 }
768 /* A maximum amplitude signal will have 17 leading zeros, which we want to
769 * translate to a shift of 8 (for converting 16 bit to 8 bit) */
770 shift = 25 - shift;
771 /* Never scale by less than 8 to avoid returning unaltered PCM signal. */
772 if (shift < 3) {
773 shift = 3;
774 }
775 /* add one to combine the division by 2 needed after summing
776 * left and right channels below */
777 shift++;
778 } else {
779 assert(visu_ctxt->scaling_mode == VISUALIZER_SCALING_MODE_AS_PLAYED);
780 shift = 9;
781 }
782
783 uint32_t capt_idx;
784 uint32_t in_idx;
785 uint8_t *buf = visu_ctxt->capture_buf;
786 for (in_idx = 0, capt_idx = visu_ctxt->capture_idx;
787 in_idx < inBuffer->frameCount;
788 in_idx++, capt_idx++) {
789 if (capt_idx >= CAPTURE_BUF_SIZE) {
790 /* wrap around */
791 capt_idx = 0;
792 }
793 int32_t smp = inBuffer->s16[2 * in_idx] + inBuffer->s16[2 * in_idx + 1];
794 smp = smp >> shift;
795 buf[capt_idx] = ((uint8_t)smp)^0x80;
796 }
797
798 /* XXX the following two should really be atomic, though it probably doesn't
799 * matter much for visualization purposes */
800 visu_ctxt->capture_idx = capt_idx;
801 /* update last buffer update time stamp */
802 if (clock_gettime(CLOCK_MONOTONIC, &visu_ctxt->buffer_update_time) < 0) {
803 visu_ctxt->buffer_update_time.tv_sec = 0;
804 }
805
806 if (context->state != EFFECT_STATE_ACTIVE) {
807 ALOGV("%s DONE inactive", __func__);
808 return -ENODATA;
809 }
810
811 return 0;
812}
813
814int visualizer_command(effect_context_t * context, uint32_t cmdCode, uint32_t cmdSize,
815 void *pCmdData, uint32_t *replySize, void *pReplyData)
816{
817 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
818
819 switch (cmdCode) {
820 case VISUALIZER_CMD_CAPTURE:
821 if (pReplyData == NULL || *replySize != visu_ctxt->capture_size) {
822 ALOGV("%s VISUALIZER_CMD_CAPTURE error *replySize %d context->capture_size %d",
823 __func__, *replySize, visu_ctxt->capture_size);
824 return -EINVAL;
825 }
826
827 if (!context->offload_enabled)
828 break;
829
830 if (context->state == EFFECT_STATE_ACTIVE) {
831 int32_t latency_ms = visu_ctxt->latency;
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700832 const uint32_t delta_ms = visualizer_get_delta_time_ms_from_updated_time(visu_ctxt);
833 latency_ms -= delta_ms;
834 if (latency_ms < 0) {
835 latency_ms = 0;
Eric Laurentc4aef752013-09-12 17:45:53 -0700836 }
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700837 const uint32_t delta_smp = context->config.inputCfg.samplingRate * latency_ms / 1000;
Eric Laurentc4aef752013-09-12 17:45:53 -0700838
839 int32_t capture_point = visu_ctxt->capture_idx - visu_ctxt->capture_size - delta_smp;
840 int32_t capture_size = visu_ctxt->capture_size;
841 if (capture_point < 0) {
842 int32_t size = -capture_point;
843 if (size > capture_size)
844 size = capture_size;
845
846 memcpy(pReplyData,
847 visu_ctxt->capture_buf + CAPTURE_BUF_SIZE + capture_point,
848 size);
849 pReplyData = (void *)((size_t)pReplyData + size);
850 capture_size -= size;
851 capture_point = 0;
852 }
853 memcpy(pReplyData,
854 visu_ctxt->capture_buf + capture_point,
855 capture_size);
856
857
858 /* if audio framework has stopped playing audio although the effect is still
859 * active we must clear the capture buffer to return silence */
860 if ((visu_ctxt->last_capture_idx == visu_ctxt->capture_idx) &&
861 (visu_ctxt->buffer_update_time.tv_sec != 0)) {
862 if (delta_ms > MAX_STALL_TIME_MS) {
863 ALOGV("%s capture going to idle", __func__);
864 visu_ctxt->buffer_update_time.tv_sec = 0;
865 memset(pReplyData, 0x80, visu_ctxt->capture_size);
866 }
867 }
868 visu_ctxt->last_capture_idx = visu_ctxt->capture_idx;
869 } else {
870 memset(pReplyData, 0x80, visu_ctxt->capture_size);
871 }
872 break;
873
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700874 case VISUALIZER_CMD_MEASURE: {
875 uint16_t peak_u16 = 0;
876 float sum_rms_squared = 0.0f;
877 uint8_t nb_valid_meas = 0;
878 /* reset measurements if last measurement was too long ago (which implies stored
879 * measurements aren't relevant anymore and shouldn't bias the new one) */
880 const int32_t delay_ms = visualizer_get_delta_time_ms_from_updated_time(visu_ctxt);
881 if (delay_ms > DISCARD_MEASUREMENTS_TIME_MS) {
882 uint32_t i;
883 ALOGV("Discarding measurements, last measurement is %dms old", delay_ms);
884 for (i=0 ; i<visu_ctxt->meas_wndw_size_in_buffers ; i++) {
885 visu_ctxt->past_meas[i].is_valid = false;
886 visu_ctxt->past_meas[i].peak_u16 = 0;
887 visu_ctxt->past_meas[i].rms_squared = 0;
888 }
889 visu_ctxt->meas_buffer_idx = 0;
890 } else {
891 /* only use actual measurements, otherwise the first RMS measure happening before
892 * MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
893 * low */
894 uint32_t i;
895 for (i=0 ; i < visu_ctxt->meas_wndw_size_in_buffers ; i++) {
896 if (visu_ctxt->past_meas[i].is_valid) {
897 if (visu_ctxt->past_meas[i].peak_u16 > peak_u16) {
898 peak_u16 = visu_ctxt->past_meas[i].peak_u16;
899 }
900 sum_rms_squared += visu_ctxt->past_meas[i].rms_squared;
901 nb_valid_meas++;
902 }
903 }
904 }
905 float rms = nb_valid_meas == 0 ? 0.0f : sqrtf(sum_rms_squared / nb_valid_meas);
906 int32_t* p_int_reply_data = (int32_t*)pReplyData;
907 /* convert from I16 sample values to mB and write results */
908 if (rms < 0.000016f) {
909 p_int_reply_data[MEASUREMENT_IDX_RMS] = -9600; //-96dB
910 } else {
911 p_int_reply_data[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
912 }
913 if (peak_u16 == 0) {
914 p_int_reply_data[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
915 } else {
916 p_int_reply_data[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peak_u16 / 32767.0f));
917 }
918 ALOGV("VISUALIZER_CMD_MEASURE peak=%d (%dmB), rms=%.1f (%dmB)",
919 peak_u16, p_int_reply_data[MEASUREMENT_IDX_PEAK],
920 rms, p_int_reply_data[MEASUREMENT_IDX_RMS]);
921 }
922 break;
923
Eric Laurentc4aef752013-09-12 17:45:53 -0700924 default:
925 ALOGW("%s invalid command %d", __func__, cmdCode);
926 return -EINVAL;
927 }
928 return 0;
929}
930
931
932/*
933 * Effect Library Interface Implementation
934 */
935
936int effect_lib_create(const effect_uuid_t *uuid,
937 int32_t sessionId,
938 int32_t ioId,
939 effect_handle_t *pHandle) {
940 int ret;
941 int i;
942
943 if (lib_init() != 0)
944 return init_status;
945
946 if (pHandle == NULL || uuid == NULL)
947 return -EINVAL;
948
949 for (i = 0; descriptors[i] != NULL; i++) {
950 if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0)
951 break;
952 }
953
954 if (descriptors[i] == NULL)
955 return -EINVAL;
956
957 effect_context_t *context;
958 if (memcmp(uuid, &visualizer_descriptor.uuid, sizeof(effect_uuid_t)) == 0) {
959 visualizer_context_t *visu_ctxt = (visualizer_context_t *)calloc(1,
960 sizeof(visualizer_context_t));
wjiangebb69fa2014-05-15 19:38:26 +0800961 if (visu_ctxt == NULL) {
962 ALOGE("%s fail to allocate memory", __func__);
963 return -ENOMEM;
964 }
Eric Laurentc4aef752013-09-12 17:45:53 -0700965 context = (effect_context_t *)visu_ctxt;
966 context->ops.init = visualizer_init;
967 context->ops.reset = visualizer_reset;
968 context->ops.process = visualizer_process;
969 context->ops.set_parameter = visualizer_set_parameter;
970 context->ops.get_parameter = visualizer_get_parameter;
971 context->ops.command = visualizer_command;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800972 context->desc = &visualizer_descriptor;
Eric Laurentc4aef752013-09-12 17:45:53 -0700973 } else {
974 return -EINVAL;
975 }
976
977 context->itfe = &effect_interface;
978 context->state = EFFECT_STATE_UNINITIALIZED;
979 context->out_handle = (audio_io_handle_t)ioId;
Eric Laurentc4aef752013-09-12 17:45:53 -0700980
981 ret = context->ops.init(context);
982 if (ret < 0) {
983 ALOGW("%s init failed", __func__);
984 free(context);
985 return ret;
986 }
987
988 context->state = EFFECT_STATE_INITIALIZED;
989
990 pthread_mutex_lock(&lock);
991 list_add_tail(&created_effects_list, &context->effects_list_node);
992 output_context_t *out_ctxt = get_output(ioId);
993 if (out_ctxt != NULL)
994 add_effect_to_output(out_ctxt, context);
995 pthread_mutex_unlock(&lock);
996
997 *pHandle = (effect_handle_t)context;
998
999 ALOGV("%s created context %p", __func__, context);
1000
1001 return 0;
1002
1003}
1004
1005int effect_lib_release(effect_handle_t handle) {
1006 effect_context_t *context = (effect_context_t *)handle;
1007 int status;
1008
1009 if (lib_init() != 0)
1010 return init_status;
1011
1012 ALOGV("%s context %p", __func__, handle);
1013 pthread_mutex_lock(&lock);
1014 status = -EINVAL;
1015 if (effect_exists(context)) {
1016 output_context_t *out_ctxt = get_output(context->out_handle);
1017 if (out_ctxt != NULL)
1018 remove_effect_from_output(out_ctxt, context);
1019 list_remove(&context->effects_list_node);
1020 if (context->ops.release)
1021 context->ops.release(context);
1022 free(context);
1023 status = 0;
1024 }
1025 pthread_mutex_unlock(&lock);
1026
1027 return status;
1028}
1029
1030int effect_lib_get_descriptor(const effect_uuid_t *uuid,
1031 effect_descriptor_t *descriptor) {
1032 int i;
1033
1034 if (lib_init() != 0)
1035 return init_status;
1036
1037 if (descriptor == NULL || uuid == NULL) {
1038 ALOGV("%s called with NULL pointer", __func__);
1039 return -EINVAL;
1040 }
1041
1042 for (i = 0; descriptors[i] != NULL; i++) {
1043 if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
1044 *descriptor = *descriptors[i];
1045 return 0;
1046 }
1047 }
1048
1049 return -EINVAL;
1050}
1051
1052/*
1053 * Effect Control Interface Implementation
1054 */
1055
1056 /* Stub function for effect interface: never called for offloaded effects */
1057int effect_process(effect_handle_t self,
1058 audio_buffer_t *inBuffer,
1059 audio_buffer_t *outBuffer)
1060{
1061 effect_context_t * context = (effect_context_t *)self;
1062 int status = 0;
1063
1064 ALOGW("%s Called ?????", __func__);
1065
1066 pthread_mutex_lock(&lock);
1067 if (!effect_exists(context)) {
1068 status = -EINVAL;
1069 goto exit;
1070 }
1071
1072 if (context->state != EFFECT_STATE_ACTIVE) {
1073 status = -EINVAL;
1074 goto exit;
1075 }
1076
1077exit:
1078 pthread_mutex_unlock(&lock);
1079 return status;
1080}
1081
1082int effect_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
1083 void *pCmdData, uint32_t *replySize, void *pReplyData)
1084{
1085
1086 effect_context_t * context = (effect_context_t *)self;
1087 int retsize;
1088 int status = 0;
1089
1090 pthread_mutex_lock(&lock);
1091
1092 if (!effect_exists(context)) {
1093 status = -EINVAL;
1094 goto exit;
1095 }
1096
1097 if (context == NULL || context->state == EFFECT_STATE_UNINITIALIZED) {
1098 status = -EINVAL;
1099 goto exit;
1100 }
1101
1102// ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,
1103// "%s command %d cmdSize %d", __func__, cmdCode, cmdSize);
1104
1105 switch (cmdCode) {
1106 case EFFECT_CMD_INIT:
1107 if (pReplyData == NULL || *replySize != sizeof(int)) {
1108 status = -EINVAL;
1109 goto exit;
1110 }
1111 if (context->ops.init)
1112 *(int *) pReplyData = context->ops.init(context);
1113 else
1114 *(int *) pReplyData = 0;
1115 break;
1116 case EFFECT_CMD_SET_CONFIG:
1117 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
1118 || pReplyData == NULL || *replySize != sizeof(int)) {
1119 status = -EINVAL;
1120 goto exit;
1121 }
1122 *(int *) pReplyData = set_config(context, (effect_config_t *) pCmdData);
1123 break;
1124 case EFFECT_CMD_GET_CONFIG:
1125 if (pReplyData == NULL ||
1126 *replySize != sizeof(effect_config_t)) {
1127 status = -EINVAL;
1128 goto exit;
1129 }
1130 if (!context->offload_enabled) {
1131 status = -EINVAL;
1132 goto exit;
1133 }
1134
1135 get_config(context, (effect_config_t *)pReplyData);
1136 break;
1137 case EFFECT_CMD_RESET:
1138 if (context->ops.reset)
1139 context->ops.reset(context);
1140 break;
1141 case EFFECT_CMD_ENABLE:
1142 if (pReplyData == NULL || *replySize != sizeof(int)) {
1143 status = -EINVAL;
1144 goto exit;
1145 }
1146 if (context->state != EFFECT_STATE_INITIALIZED) {
1147 status = -ENOSYS;
1148 goto exit;
1149 }
1150 context->state = EFFECT_STATE_ACTIVE;
1151 if (context->ops.enable)
1152 context->ops.enable(context);
1153 pthread_cond_signal(&cond);
1154 ALOGV("%s EFFECT_CMD_ENABLE", __func__);
1155 *(int *)pReplyData = 0;
1156 break;
1157 case EFFECT_CMD_DISABLE:
1158 if (pReplyData == NULL || *replySize != sizeof(int)) {
1159 status = -EINVAL;
1160 goto exit;
1161 }
1162 if (context->state != EFFECT_STATE_ACTIVE) {
1163 status = -ENOSYS;
1164 goto exit;
1165 }
1166 context->state = EFFECT_STATE_INITIALIZED;
1167 if (context->ops.disable)
1168 context->ops.disable(context);
1169 pthread_cond_signal(&cond);
1170 ALOGV("%s EFFECT_CMD_DISABLE", __func__);
1171 *(int *)pReplyData = 0;
1172 break;
1173 case EFFECT_CMD_GET_PARAM: {
1174 if (pCmdData == NULL ||
1175 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
1176 pReplyData == NULL ||
1177 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
1178 status = -EINVAL;
1179 goto exit;
1180 }
1181 if (!context->offload_enabled) {
1182 status = -EINVAL;
1183 goto exit;
1184 }
1185 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
1186 effect_param_t *p = (effect_param_t *)pReplyData;
1187 if (context->ops.get_parameter)
1188 context->ops.get_parameter(context, p, replySize);
1189 } break;
1190 case EFFECT_CMD_SET_PARAM: {
1191 if (pCmdData == NULL ||
1192 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
1193 pReplyData == NULL || *replySize != sizeof(int32_t)) {
1194 status = -EINVAL;
1195 goto exit;
1196 }
1197 *(int32_t *)pReplyData = 0;
1198 effect_param_t *p = (effect_param_t *)pCmdData;
1199 if (context->ops.set_parameter)
1200 *(int32_t *)pReplyData = context->ops.set_parameter(context, p, *replySize);
1201
1202 } break;
1203 case EFFECT_CMD_SET_DEVICE:
1204 case EFFECT_CMD_SET_VOLUME:
1205 case EFFECT_CMD_SET_AUDIO_MODE:
1206 break;
1207
1208 case EFFECT_CMD_OFFLOAD: {
1209 output_context_t *out_ctxt;
1210
1211 if (cmdSize != sizeof(effect_offload_param_t) || pCmdData == NULL
1212 || pReplyData == NULL || *replySize != sizeof(int)) {
1213 ALOGV("%s EFFECT_CMD_OFFLOAD bad format", __func__);
1214 status = -EINVAL;
1215 break;
1216 }
1217
1218 effect_offload_param_t* offload_param = (effect_offload_param_t*)pCmdData;
1219
1220 ALOGV("%s EFFECT_CMD_OFFLOAD offload %d output %d",
1221 __func__, offload_param->isOffload, offload_param->ioHandle);
1222
1223 *(int *)pReplyData = 0;
1224
1225 context->offload_enabled = offload_param->isOffload;
1226 if (context->out_handle == offload_param->ioHandle)
1227 break;
1228
1229 out_ctxt = get_output(context->out_handle);
1230 if (out_ctxt != NULL)
1231 remove_effect_from_output(out_ctxt, context);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001232
1233 context->out_handle = offload_param->ioHandle;
Eric Laurentc4aef752013-09-12 17:45:53 -07001234 out_ctxt = get_output(offload_param->ioHandle);
1235 if (out_ctxt != NULL)
1236 add_effect_to_output(out_ctxt, context);
1237
Eric Laurentc4aef752013-09-12 17:45:53 -07001238 } break;
1239
1240
1241 default:
1242 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY && context->ops.command)
1243 status = context->ops.command(context, cmdCode, cmdSize,
1244 pCmdData, replySize, pReplyData);
1245 else {
1246 ALOGW("%s invalid command %d", __func__, cmdCode);
1247 status = -EINVAL;
1248 }
1249 break;
1250 }
1251
1252exit:
1253 pthread_mutex_unlock(&lock);
1254
1255// ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,"%s DONE", __func__);
1256 return status;
1257}
1258
1259/* Effect Control Interface Implementation: get_descriptor */
1260int effect_get_descriptor(effect_handle_t self,
1261 effect_descriptor_t *descriptor)
1262{
1263 effect_context_t *context = (effect_context_t *)self;
1264
1265 if (!effect_exists(context))
1266 return -EINVAL;
1267
1268 if (descriptor == NULL)
1269 return -EINVAL;
1270
1271 *descriptor = *context->desc;
1272
1273 return 0;
1274}
1275
1276/* effect_handle_t interface implementation for visualizer effect */
1277const struct effect_interface_s effect_interface = {
1278 effect_process,
1279 effect_command,
1280 effect_get_descriptor,
1281 NULL,
1282};
1283
1284__attribute__ ((visibility ("default")))
1285audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
1286 tag : AUDIO_EFFECT_LIBRARY_TAG,
1287 version : EFFECT_LIBRARY_API_VERSION,
1288 name : "Visualizer Library",
1289 implementor : "The Android Open Source Project",
1290 create_effect : effect_lib_create,
1291 release_effect : effect_lib_release,
1292 get_descriptor : effect_lib_get_descriptor,
1293};