blob: 75ce4a5cd6e275120682d5f142d5fff2e31dd00f [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>
20#include <stdlib.h>
21#include <string.h>
22#include <time.h>
23#include <sys/prctl.h>
24
25#include <cutils/list.h>
26#include <cutils/log.h>
27#include <system/thread_defs.h>
28#include <tinyalsa/asoundlib.h>
29#include <audio_effects/effect_visualizer.h>
30
31
32enum {
33 EFFECT_STATE_UNINITIALIZED,
34 EFFECT_STATE_INITIALIZED,
35 EFFECT_STATE_ACTIVE,
36};
37
38typedef struct effect_context_s effect_context_t;
39
40/* effect specific operations. Only the init() and process() operations must be defined.
41 * Others are optional.
42 */
43typedef struct effect_ops_s {
44 int (*init)(effect_context_t *context);
45 int (*release)(effect_context_t *context);
46 int (*reset)(effect_context_t *context);
47 int (*enable)(effect_context_t *context);
48 int (*disable)(effect_context_t *context);
49 int (*process)(effect_context_t *context, audio_buffer_t *in, audio_buffer_t *out);
50 int (*set_parameter)(effect_context_t *context, effect_param_t *param, uint32_t size);
51 int (*get_parameter)(effect_context_t *context, effect_param_t *param, uint32_t *size);
52 int (*command)(effect_context_t *context, uint32_t cmdCode, uint32_t cmdSize,
53 void *pCmdData, uint32_t *replySize, void *pReplyData);
54} effect_ops_t;
55
56struct effect_context_s {
57 const struct effect_interface_s *itfe;
58 struct listnode effects_list_node; /* node in created_effects_list */
59 struct listnode output_node; /* node in output_context_t.effects_list */
60 effect_config_t config;
61 const effect_descriptor_t *desc;
62 audio_io_handle_t out_handle; /* io handle of the output the effect is attached to */
63 uint32_t state;
64 bool offload_enabled; /* when offload is enabled we process VISUALIZER_CMD_CAPTURE command.
65 Otherwise non offloaded visualizer has already processed the command
66 and we must not overwrite the reply. */
67 effect_ops_t ops;
68};
69
70typedef struct output_context_s {
71 struct listnode outputs_list_node; /* node in active_outputs_list */
72 audio_io_handle_t handle; /* io handle */
73 struct listnode effects_list; /* list of effects attached to this output */
74} output_context_t;
75
76
77/* maximum time since last capture buffer update before resetting capture buffer. This means
78 that the framework has stopped playing audio and we must start returning silence */
79#define MAX_STALL_TIME_MS 1000
80
81#define CAPTURE_BUF_SIZE 65536 /* "64k should be enough for everyone" */
82
83
84typedef struct visualizer_context_s {
85 effect_context_t common;
86
87 uint32_t capture_idx;
88 uint32_t capture_size;
89 uint32_t scaling_mode;
90 uint32_t last_capture_idx;
91 uint32_t latency;
92 struct timespec buffer_update_time;
93 uint8_t capture_buf[CAPTURE_BUF_SIZE];
94} visualizer_context_t;
95
96
97extern const struct effect_interface_s effect_interface;
98
99/* Offload visualizer UUID: 7a8044a0-1a71-11e3-a184-0002a5d5c51b */
100const effect_descriptor_t visualizer_descriptor = {
101 {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
102 {0x7a8044a0, 0x1a71, 0x11e3, 0xa184, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
103 EFFECT_CONTROL_API_VERSION,
104 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_HW_ACC_TUNNEL ),
105 0, /* TODO */
106 1,
107 "QCOM MSM offload visualizer",
108 "The Android Open Source Project",
109};
110
111const effect_descriptor_t *descriptors[] = {
112 &visualizer_descriptor,
113 NULL,
114};
115
116
117pthread_once_t once = PTHREAD_ONCE_INIT;
118int init_status;
119
120/* list of created effects. Updated by visualizer_hal_start_output()
121 * and visualizer_hal_stop_output() */
122struct listnode created_effects_list;
123/* list of active output streams. Updated by visualizer_hal_start_output()
124 * and visualizer_hal_stop_output() */
125struct listnode active_outputs_list;
126
127/* thread capturing PCM from Proxy port and calling the process function on each enabled effect
128 * attached to an active output stream */
129pthread_t capture_thread;
130/* lock must be held when modifying or accessing created_effects_list or active_outputs_list */
131pthread_mutex_t lock;
132/* thread_lock must be held when starting or stopping the capture thread.
133 * Locking order: thread_lock -> lock */
134pthread_mutex_t thread_lock;
135/* cond is signaled when an output is started or stopped or an effect is enabled or disable: the
136 * capture thread will reevaluate the capture and effect rocess conditions. */
137pthread_cond_t cond;
138/* true when requesting the capture thread to exit */
139bool exit_thread;
140/* 0 if the capture thread was created successfully */
141int thread_status;
142
143
144#define DSP_OUTPUT_LATENCY_MS 0 /* Fudge factor for latency after capture point in audio DSP */
145
146/* Retry for delay for mixer open */
147#define RETRY_NUMBER 10
148#define RETRY_US 500000
149
150#define MIXER_CARD 0
151#define SOUND_CARD 0
152#define CAPTURE_DEVICE 8
153
154/* Proxy port supports only MMAP read and those fixed parameters*/
155#define AUDIO_CAPTURE_CHANNEL_COUNT 2
156#define AUDIO_CAPTURE_SMP_RATE 48000
157#define AUDIO_CAPTURE_PERIOD_SIZE (768)
158#define AUDIO_CAPTURE_PERIOD_COUNT 32
159
160struct pcm_config pcm_config_capture = {
161 .channels = AUDIO_CAPTURE_CHANNEL_COUNT,
162 .rate = AUDIO_CAPTURE_SMP_RATE,
163 .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
164 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
165 .format = PCM_FORMAT_S16_LE,
166 .start_threshold = AUDIO_CAPTURE_PERIOD_SIZE / 4,
167 .stop_threshold = INT_MAX,
168 .avail_min = AUDIO_CAPTURE_PERIOD_SIZE / 4,
169};
170
171
172/*
173 * Local functions
174 */
175
176static void init_once() {
177 list_init(&created_effects_list);
178 list_init(&active_outputs_list);
179
180 pthread_mutex_init(&lock, NULL);
181 pthread_mutex_init(&thread_lock, NULL);
182 pthread_cond_init(&cond, NULL);
183 exit_thread = false;
184 thread_status = -1;
185
186 init_status = 0;
187}
188
189int lib_init() {
190 pthread_once(&once, init_once);
191 return init_status;
192}
193
194bool effect_exists(effect_context_t *context) {
195 struct listnode *node;
196
197 list_for_each(node, &created_effects_list) {
198 effect_context_t *fx_ctxt = node_to_item(node,
199 effect_context_t,
200 effects_list_node);
201 if (fx_ctxt == context) {
202 return true;
203 }
204 }
205 return false;
206}
207
208output_context_t *get_output(audio_io_handle_t output) {
209 struct listnode *node;
210
211 list_for_each(node, &active_outputs_list) {
212 output_context_t *out_ctxt = node_to_item(node,
213 output_context_t,
214 outputs_list_node);
215 if (out_ctxt->handle == output) {
216 return out_ctxt;
217 }
218 }
219 return NULL;
220}
221
222void add_effect_to_output(output_context_t * output, effect_context_t *context) {
223 struct listnode *fx_node;
224
225 list_for_each(fx_node, &output->effects_list) {
226 effect_context_t *fx_ctxt = node_to_item(fx_node,
227 effect_context_t,
228 output_node);
229 if (fx_ctxt == context)
230 return;
231 }
232 list_add_tail(&output->effects_list, &context->output_node);
233}
234
235void remove_effect_from_output(output_context_t * output, effect_context_t *context) {
236 struct listnode *fx_node;
237
238 list_for_each(fx_node, &output->effects_list) {
239 effect_context_t *fx_ctxt = node_to_item(fx_node,
240 effect_context_t,
241 output_node);
242 if (fx_ctxt == context) {
243 list_remove(&context->output_node);
244 return;
245 }
246 }
247}
248
249bool effects_enabled() {
250 struct listnode *out_node;
251
252 list_for_each(out_node, &active_outputs_list) {
253 struct listnode *fx_node;
254 output_context_t *out_ctxt = node_to_item(out_node,
255 output_context_t,
256 outputs_list_node);
257
258 list_for_each(fx_node, &out_ctxt->effects_list) {
259 effect_context_t *fx_ctxt = node_to_item(fx_node,
260 effect_context_t,
261 output_node);
262 if (fx_ctxt->state == EFFECT_STATE_ACTIVE)
263 return true;
264 }
265 }
266 return false;
267}
268
269int configure_proxy_capture(struct mixer *mixer, int value) {
270 const char *proxy_ctl_name = "AFE_PCM_RX Audio Mixer MultiMedia4";
271 struct mixer_ctl *ctl;
272
273 ctl = mixer_get_ctl_by_name(mixer, proxy_ctl_name);
274 if (ctl == NULL) {
275 ALOGW("%s: could not get %s ctl", __func__, proxy_ctl_name);
276 return -EINVAL;
277 }
278 if (mixer_ctl_set_value(ctl, 0, value) != 0)
279 ALOGW("%s: error setting value %d on %s ", __func__, value, proxy_ctl_name);
280
281 return 0;
282}
283
284
285void *capture_thread_loop(void *arg)
286{
287 int16_t data[AUDIO_CAPTURE_PERIOD_SIZE * AUDIO_CAPTURE_CHANNEL_COUNT * sizeof(int16_t)];
288 audio_buffer_t buf;
289 buf.frameCount = AUDIO_CAPTURE_PERIOD_SIZE;
290 buf.s16 = data;
291 bool capture_enabled = false;
292 struct mixer *mixer;
293 struct pcm *pcm = NULL;
294 int ret;
295 int retry_num = 0;
296
297 ALOGD("thread enter");
298
299 prctl(PR_SET_NAME, (unsigned long)"visualizer capture", 0, 0, 0);
300
301 pthread_mutex_lock(&lock);
302
303 mixer = mixer_open(MIXER_CARD);
304 while (mixer == NULL && retry_num < RETRY_NUMBER) {
305 usleep(RETRY_US);
306 mixer = mixer_open(MIXER_CARD);
307 retry_num++;
308 }
309 if (mixer == NULL) {
310 pthread_mutex_unlock(&lock);
311 return NULL;
312 }
313
314 for (;;) {
315 if (exit_thread) {
316 break;
317 }
318 if (effects_enabled()) {
319 if (!capture_enabled) {
320 ret = configure_proxy_capture(mixer, 1);
321 if (ret == 0) {
322 pcm = pcm_open(SOUND_CARD, CAPTURE_DEVICE,
323 PCM_IN|PCM_MMAP|PCM_NOIRQ, &pcm_config_capture);
324 if (pcm && !pcm_is_ready(pcm)) {
325 ALOGW("%s: %s", __func__, pcm_get_error(pcm));
326 pcm_close(pcm);
327 pcm = NULL;
328 configure_proxy_capture(mixer, 0);
329 } else {
330 capture_enabled = true;
331 ALOGD("%s: capture ENABLED", __func__);
332 }
333 }
334 }
335 } else {
336 if (capture_enabled) {
337 if (pcm != NULL)
338 pcm_close(pcm);
339 configure_proxy_capture(mixer, 0);
340 ALOGD("%s: capture DISABLED", __func__);
341 capture_enabled = false;
342 }
343 pthread_cond_wait(&cond, &lock);
344 }
345 if (!capture_enabled)
346 continue;
347
348 pthread_mutex_unlock(&lock);
349 ret = pcm_mmap_read(pcm, data, sizeof(data));
350 pthread_mutex_lock(&lock);
351
352 if (ret == 0) {
353 struct listnode *out_node;
354
355 list_for_each(out_node, &active_outputs_list) {
356 output_context_t *out_ctxt = node_to_item(out_node,
357 output_context_t,
358 outputs_list_node);
359 struct listnode *fx_node;
360
361 list_for_each(fx_node, &out_ctxt->effects_list) {
362 effect_context_t *fx_ctxt = node_to_item(fx_node,
363 effect_context_t,
364 output_node);
365 fx_ctxt->ops.process(fx_ctxt, &buf, &buf);
366 }
367 }
368 } else {
369 ALOGW("%s: read status %d %s", __func__, ret, pcm_get_error(pcm));
370 }
371 }
372
373 if (capture_enabled) {
374 if (pcm != NULL)
375 pcm_close(pcm);
376 configure_proxy_capture(mixer, 0);
377 }
378 mixer_close(mixer);
379 pthread_mutex_unlock(&lock);
380
381 ALOGD("thread exit");
382
383 return NULL;
384}
385
386/*
387 * Interface from audio HAL
388 */
389
390__attribute__ ((visibility ("default")))
391int visualizer_hal_start_output(audio_io_handle_t output) {
392 int ret;
393 struct listnode *node;
394
395 ALOGV("%s", __func__);
396
397 if (lib_init() != 0)
398 return init_status;
399
400 pthread_mutex_lock(&thread_lock);
401 pthread_mutex_lock(&lock);
402 if (get_output(output) != NULL) {
403 ALOGW("%s output already started", __func__);
404 ret = -ENOSYS;
405 goto exit;
406 }
407
408 output_context_t *out_ctxt = (output_context_t *)malloc(sizeof(output_context_t));
409 out_ctxt->handle = output;
410 list_init(&out_ctxt->effects_list);
411
412 list_for_each(node, &created_effects_list) {
413 effect_context_t *fx_ctxt = node_to_item(node,
414 effect_context_t,
415 effects_list_node);
416 if (fx_ctxt->out_handle == output) {
417 list_add_tail(&out_ctxt->effects_list, &fx_ctxt->output_node);
418 }
419 }
420 if (list_empty(&active_outputs_list)) {
421 exit_thread = false;
422 thread_status = pthread_create(&capture_thread, (const pthread_attr_t *) NULL,
423 capture_thread_loop, NULL);
424 }
425 list_add_tail(&active_outputs_list, &out_ctxt->outputs_list_node);
426 pthread_cond_signal(&cond);
427
428exit:
429 pthread_mutex_unlock(&lock);
430 pthread_mutex_unlock(&thread_lock);
431 return ret;
432}
433
434__attribute__ ((visibility ("default")))
435int visualizer_hal_stop_output(audio_io_handle_t output) {
436 int ret;
437 struct listnode *node;
438 output_context_t *out_ctxt;
439
440 ALOGV("%s", __func__);
441
442 if (lib_init() != 0)
443 return init_status;
444
445 pthread_mutex_lock(&thread_lock);
446 pthread_mutex_lock(&lock);
447
448 out_ctxt = get_output(output);
449 if (out_ctxt == NULL) {
450 ALOGW("%s output not started", __func__);
451 ret = -ENOSYS;
452 goto exit;
453 }
454
455 list_remove(&out_ctxt->outputs_list_node);
456 pthread_cond_signal(&cond);
457
458 if (list_empty(&active_outputs_list)) {
459 if (thread_status == 0) {
460 exit_thread = true;
461 pthread_cond_signal(&cond);
462 pthread_mutex_unlock(&lock);
463 pthread_join(capture_thread, (void **) NULL);
464 pthread_mutex_lock(&lock);
465 thread_status = -1;
466 }
467 }
468
469 free(out_ctxt);
470
471exit:
472 pthread_mutex_unlock(&lock);
473 pthread_mutex_unlock(&thread_lock);
474 return ret;
475}
476
477
478/*
479 * Effect operations
480 */
481
482int set_config(effect_context_t *context, effect_config_t *config)
483{
484 if (config->inputCfg.samplingRate != config->outputCfg.samplingRate) return -EINVAL;
485 if (config->inputCfg.channels != config->outputCfg.channels) return -EINVAL;
486 if (config->inputCfg.format != config->outputCfg.format) return -EINVAL;
487 if (config->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL;
488 if (config->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
489 config->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
490 if (config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
491
492 context->config = *config;
493
494 if (context->ops.reset)
495 context->ops.reset(context);
496
497 return 0;
498}
499
500void get_config(effect_context_t *context, effect_config_t *config)
501{
502 *config = context->config;
503}
504
505
506/*
507 * Visualizer operations
508 */
509
510int visualizer_reset(effect_context_t *context)
511{
512 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
513
514 visu_ctxt->capture_idx = 0;
515 visu_ctxt->last_capture_idx = 0;
516 visu_ctxt->buffer_update_time.tv_sec = 0;
517 visu_ctxt->latency = DSP_OUTPUT_LATENCY_MS;
518 memset(visu_ctxt->capture_buf, 0x80, CAPTURE_BUF_SIZE);
519 return 0;
520}
521
522int visualizer_init(effect_context_t *context)
523{
524 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
525
526 context->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
527 context->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
528 context->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
529 context->config.inputCfg.samplingRate = 44100;
530 context->config.inputCfg.bufferProvider.getBuffer = NULL;
531 context->config.inputCfg.bufferProvider.releaseBuffer = NULL;
532 context->config.inputCfg.bufferProvider.cookie = NULL;
533 context->config.inputCfg.mask = EFFECT_CONFIG_ALL;
534 context->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
535 context->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
536 context->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
537 context->config.outputCfg.samplingRate = 44100;
538 context->config.outputCfg.bufferProvider.getBuffer = NULL;
539 context->config.outputCfg.bufferProvider.releaseBuffer = NULL;
540 context->config.outputCfg.bufferProvider.cookie = NULL;
541 context->config.outputCfg.mask = EFFECT_CONFIG_ALL;
542
543 visu_ctxt->capture_size = VISUALIZER_CAPTURE_SIZE_MAX;
544 visu_ctxt->scaling_mode = VISUALIZER_SCALING_MODE_NORMALIZED;
545
546 set_config(context, &context->config);
547
548 return 0;
549}
550
551int visualizer_get_parameter(effect_context_t *context, effect_param_t *p, uint32_t *size)
552{
553 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
554
555 p->status = 0;
556 *size = sizeof(effect_param_t) + sizeof(uint32_t);
557 if (p->psize != sizeof(uint32_t)) {
558 p->status = -EINVAL;
559 return 0;
560 }
561 switch (*(uint32_t *)p->data) {
562 case VISUALIZER_PARAM_CAPTURE_SIZE:
563 ALOGV("%s get capture_size = %d", __func__, visu_ctxt->capture_size);
564 *((uint32_t *)p->data + 1) = visu_ctxt->capture_size;
565 p->vsize = sizeof(uint32_t);
566 *size += sizeof(uint32_t);
567 break;
568 case VISUALIZER_PARAM_SCALING_MODE:
569 ALOGV("%s get scaling_mode = %d", __func__, visu_ctxt->scaling_mode);
570 *((uint32_t *)p->data + 1) = visu_ctxt->scaling_mode;
571 p->vsize = sizeof(uint32_t);
572 *size += sizeof(uint32_t);
573 break;
574 default:
575 p->status = -EINVAL;
576 }
577 return 0;
578}
579
580int visualizer_set_parameter(effect_context_t *context, effect_param_t *p, uint32_t size)
581{
582 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
583
584 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t))
585 return -EINVAL;
586
587 switch (*(uint32_t *)p->data) {
588 case VISUALIZER_PARAM_CAPTURE_SIZE:
589 visu_ctxt->capture_size = *((uint32_t *)p->data + 1);
590 ALOGV("%s set capture_size = %d", __func__, visu_ctxt->capture_size);
591 break;
592 case VISUALIZER_PARAM_SCALING_MODE:
593 visu_ctxt->scaling_mode = *((uint32_t *)p->data + 1);
594 ALOGV("%s set scaling_mode = %d", __func__, visu_ctxt->scaling_mode);
595 break;
596 case VISUALIZER_PARAM_LATENCY:
597 /* Ignore latency as we capture at DSP output
598 * visu_ctxt->latency = *((uint32_t *)p->data + 1); */
599 ALOGV("%s set latency = %d", __func__, visu_ctxt->latency);
600 break;
601 default:
602 return -EINVAL;
603 }
604 return 0;
605}
606
607/* Real process function called from capture thread. Called with lock held */
608int visualizer_process(effect_context_t *context,
609 audio_buffer_t *inBuffer,
610 audio_buffer_t *outBuffer)
611{
612 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
613
614 if (!effect_exists(context))
615 return -EINVAL;
616
617 if (inBuffer == NULL || inBuffer->raw == NULL ||
618 outBuffer == NULL || outBuffer->raw == NULL ||
619 inBuffer->frameCount != outBuffer->frameCount ||
620 inBuffer->frameCount == 0) {
621 return -EINVAL;
622 }
623
624 /* all code below assumes stereo 16 bit PCM output and input */
625 int32_t shift;
626
627 if (visu_ctxt->scaling_mode == VISUALIZER_SCALING_MODE_NORMALIZED) {
628 /* derive capture scaling factor from peak value in current buffer
629 * this gives more interesting captures for display. */
630 shift = 32;
631 int len = inBuffer->frameCount * 2;
632 int i;
633 for (i = 0; i < len; i++) {
634 int32_t smp = inBuffer->s16[i];
635 if (smp < 0) smp = -smp - 1; /* take care to keep the max negative in range */
636 int32_t clz = __builtin_clz(smp);
637 if (shift > clz) shift = clz;
638 }
639 /* A maximum amplitude signal will have 17 leading zeros, which we want to
640 * translate to a shift of 8 (for converting 16 bit to 8 bit) */
641 shift = 25 - shift;
642 /* Never scale by less than 8 to avoid returning unaltered PCM signal. */
643 if (shift < 3) {
644 shift = 3;
645 }
646 /* add one to combine the division by 2 needed after summing
647 * left and right channels below */
648 shift++;
649 } else {
650 assert(visu_ctxt->scaling_mode == VISUALIZER_SCALING_MODE_AS_PLAYED);
651 shift = 9;
652 }
653
654 uint32_t capt_idx;
655 uint32_t in_idx;
656 uint8_t *buf = visu_ctxt->capture_buf;
657 for (in_idx = 0, capt_idx = visu_ctxt->capture_idx;
658 in_idx < inBuffer->frameCount;
659 in_idx++, capt_idx++) {
660 if (capt_idx >= CAPTURE_BUF_SIZE) {
661 /* wrap around */
662 capt_idx = 0;
663 }
664 int32_t smp = inBuffer->s16[2 * in_idx] + inBuffer->s16[2 * in_idx + 1];
665 smp = smp >> shift;
666 buf[capt_idx] = ((uint8_t)smp)^0x80;
667 }
668
669 /* XXX the following two should really be atomic, though it probably doesn't
670 * matter much for visualization purposes */
671 visu_ctxt->capture_idx = capt_idx;
672 /* update last buffer update time stamp */
673 if (clock_gettime(CLOCK_MONOTONIC, &visu_ctxt->buffer_update_time) < 0) {
674 visu_ctxt->buffer_update_time.tv_sec = 0;
675 }
676
677 if (context->state != EFFECT_STATE_ACTIVE) {
678 ALOGV("%s DONE inactive", __func__);
679 return -ENODATA;
680 }
681
682 return 0;
683}
684
685int visualizer_command(effect_context_t * context, uint32_t cmdCode, uint32_t cmdSize,
686 void *pCmdData, uint32_t *replySize, void *pReplyData)
687{
688 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
689
690 switch (cmdCode) {
691 case VISUALIZER_CMD_CAPTURE:
692 if (pReplyData == NULL || *replySize != visu_ctxt->capture_size) {
693 ALOGV("%s VISUALIZER_CMD_CAPTURE error *replySize %d context->capture_size %d",
694 __func__, *replySize, visu_ctxt->capture_size);
695 return -EINVAL;
696 }
697
698 if (!context->offload_enabled)
699 break;
700
701 if (context->state == EFFECT_STATE_ACTIVE) {
702 int32_t latency_ms = visu_ctxt->latency;
703 uint32_t delta_ms = 0;
704 if (visu_ctxt->buffer_update_time.tv_sec != 0) {
705 struct timespec ts;
706 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
707 time_t secs = ts.tv_sec - visu_ctxt->buffer_update_time.tv_sec;
708 long nsec = ts.tv_nsec - visu_ctxt->buffer_update_time.tv_nsec;
709 if (nsec < 0) {
710 --secs;
711 nsec += 1000000000;
712 }
713 delta_ms = secs * 1000 + nsec / 1000000;
714 latency_ms -= delta_ms;
715 if (latency_ms < 0)
716 latency_ms = 0;
717 }
718 }
719 uint32_t delta_smp = context->config.inputCfg.samplingRate * latency_ms / 1000;
720
721 int32_t capture_point = visu_ctxt->capture_idx - visu_ctxt->capture_size - delta_smp;
722 int32_t capture_size = visu_ctxt->capture_size;
723 if (capture_point < 0) {
724 int32_t size = -capture_point;
725 if (size > capture_size)
726 size = capture_size;
727
728 memcpy(pReplyData,
729 visu_ctxt->capture_buf + CAPTURE_BUF_SIZE + capture_point,
730 size);
731 pReplyData = (void *)((size_t)pReplyData + size);
732 capture_size -= size;
733 capture_point = 0;
734 }
735 memcpy(pReplyData,
736 visu_ctxt->capture_buf + capture_point,
737 capture_size);
738
739
740 /* if audio framework has stopped playing audio although the effect is still
741 * active we must clear the capture buffer to return silence */
742 if ((visu_ctxt->last_capture_idx == visu_ctxt->capture_idx) &&
743 (visu_ctxt->buffer_update_time.tv_sec != 0)) {
744 if (delta_ms > MAX_STALL_TIME_MS) {
745 ALOGV("%s capture going to idle", __func__);
746 visu_ctxt->buffer_update_time.tv_sec = 0;
747 memset(pReplyData, 0x80, visu_ctxt->capture_size);
748 }
749 }
750 visu_ctxt->last_capture_idx = visu_ctxt->capture_idx;
751 } else {
752 memset(pReplyData, 0x80, visu_ctxt->capture_size);
753 }
754 break;
755
756 default:
757 ALOGW("%s invalid command %d", __func__, cmdCode);
758 return -EINVAL;
759 }
760 return 0;
761}
762
763
764/*
765 * Effect Library Interface Implementation
766 */
767
768int effect_lib_create(const effect_uuid_t *uuid,
769 int32_t sessionId,
770 int32_t ioId,
771 effect_handle_t *pHandle) {
772 int ret;
773 int i;
774
775 if (lib_init() != 0)
776 return init_status;
777
778 if (pHandle == NULL || uuid == NULL)
779 return -EINVAL;
780
781 for (i = 0; descriptors[i] != NULL; i++) {
782 if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0)
783 break;
784 }
785
786 if (descriptors[i] == NULL)
787 return -EINVAL;
788
789 effect_context_t *context;
790 if (memcmp(uuid, &visualizer_descriptor.uuid, sizeof(effect_uuid_t)) == 0) {
791 visualizer_context_t *visu_ctxt = (visualizer_context_t *)calloc(1,
792 sizeof(visualizer_context_t));
793 context = (effect_context_t *)visu_ctxt;
794 context->ops.init = visualizer_init;
795 context->ops.reset = visualizer_reset;
796 context->ops.process = visualizer_process;
797 context->ops.set_parameter = visualizer_set_parameter;
798 context->ops.get_parameter = visualizer_get_parameter;
799 context->ops.command = visualizer_command;
800 } else {
801 return -EINVAL;
802 }
803
804 context->itfe = &effect_interface;
805 context->state = EFFECT_STATE_UNINITIALIZED;
806 context->out_handle = (audio_io_handle_t)ioId;
807 context->desc = &visualizer_descriptor;
808
809 ret = context->ops.init(context);
810 if (ret < 0) {
811 ALOGW("%s init failed", __func__);
812 free(context);
813 return ret;
814 }
815
816 context->state = EFFECT_STATE_INITIALIZED;
817
818 pthread_mutex_lock(&lock);
819 list_add_tail(&created_effects_list, &context->effects_list_node);
820 output_context_t *out_ctxt = get_output(ioId);
821 if (out_ctxt != NULL)
822 add_effect_to_output(out_ctxt, context);
823 pthread_mutex_unlock(&lock);
824
825 *pHandle = (effect_handle_t)context;
826
827 ALOGV("%s created context %p", __func__, context);
828
829 return 0;
830
831}
832
833int effect_lib_release(effect_handle_t handle) {
834 effect_context_t *context = (effect_context_t *)handle;
835 int status;
836
837 if (lib_init() != 0)
838 return init_status;
839
840 ALOGV("%s context %p", __func__, handle);
841 pthread_mutex_lock(&lock);
842 status = -EINVAL;
843 if (effect_exists(context)) {
844 output_context_t *out_ctxt = get_output(context->out_handle);
845 if (out_ctxt != NULL)
846 remove_effect_from_output(out_ctxt, context);
847 list_remove(&context->effects_list_node);
848 if (context->ops.release)
849 context->ops.release(context);
850 free(context);
851 status = 0;
852 }
853 pthread_mutex_unlock(&lock);
854
855 return status;
856}
857
858int effect_lib_get_descriptor(const effect_uuid_t *uuid,
859 effect_descriptor_t *descriptor) {
860 int i;
861
862 if (lib_init() != 0)
863 return init_status;
864
865 if (descriptor == NULL || uuid == NULL) {
866 ALOGV("%s called with NULL pointer", __func__);
867 return -EINVAL;
868 }
869
870 for (i = 0; descriptors[i] != NULL; i++) {
871 if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
872 *descriptor = *descriptors[i];
873 return 0;
874 }
875 }
876
877 return -EINVAL;
878}
879
880/*
881 * Effect Control Interface Implementation
882 */
883
884 /* Stub function for effect interface: never called for offloaded effects */
885int effect_process(effect_handle_t self,
886 audio_buffer_t *inBuffer,
887 audio_buffer_t *outBuffer)
888{
889 effect_context_t * context = (effect_context_t *)self;
890 int status = 0;
891
892 ALOGW("%s Called ?????", __func__);
893
894 pthread_mutex_lock(&lock);
895 if (!effect_exists(context)) {
896 status = -EINVAL;
897 goto exit;
898 }
899
900 if (context->state != EFFECT_STATE_ACTIVE) {
901 status = -EINVAL;
902 goto exit;
903 }
904
905exit:
906 pthread_mutex_unlock(&lock);
907 return status;
908}
909
910int effect_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
911 void *pCmdData, uint32_t *replySize, void *pReplyData)
912{
913
914 effect_context_t * context = (effect_context_t *)self;
915 int retsize;
916 int status = 0;
917
918 pthread_mutex_lock(&lock);
919
920 if (!effect_exists(context)) {
921 status = -EINVAL;
922 goto exit;
923 }
924
925 if (context == NULL || context->state == EFFECT_STATE_UNINITIALIZED) {
926 status = -EINVAL;
927 goto exit;
928 }
929
930// ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,
931// "%s command %d cmdSize %d", __func__, cmdCode, cmdSize);
932
933 switch (cmdCode) {
934 case EFFECT_CMD_INIT:
935 if (pReplyData == NULL || *replySize != sizeof(int)) {
936 status = -EINVAL;
937 goto exit;
938 }
939 if (context->ops.init)
940 *(int *) pReplyData = context->ops.init(context);
941 else
942 *(int *) pReplyData = 0;
943 break;
944 case EFFECT_CMD_SET_CONFIG:
945 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
946 || pReplyData == NULL || *replySize != sizeof(int)) {
947 status = -EINVAL;
948 goto exit;
949 }
950 *(int *) pReplyData = set_config(context, (effect_config_t *) pCmdData);
951 break;
952 case EFFECT_CMD_GET_CONFIG:
953 if (pReplyData == NULL ||
954 *replySize != sizeof(effect_config_t)) {
955 status = -EINVAL;
956 goto exit;
957 }
958 if (!context->offload_enabled) {
959 status = -EINVAL;
960 goto exit;
961 }
962
963 get_config(context, (effect_config_t *)pReplyData);
964 break;
965 case EFFECT_CMD_RESET:
966 if (context->ops.reset)
967 context->ops.reset(context);
968 break;
969 case EFFECT_CMD_ENABLE:
970 if (pReplyData == NULL || *replySize != sizeof(int)) {
971 status = -EINVAL;
972 goto exit;
973 }
974 if (context->state != EFFECT_STATE_INITIALIZED) {
975 status = -ENOSYS;
976 goto exit;
977 }
978 context->state = EFFECT_STATE_ACTIVE;
979 if (context->ops.enable)
980 context->ops.enable(context);
981 pthread_cond_signal(&cond);
982 ALOGV("%s EFFECT_CMD_ENABLE", __func__);
983 *(int *)pReplyData = 0;
984 break;
985 case EFFECT_CMD_DISABLE:
986 if (pReplyData == NULL || *replySize != sizeof(int)) {
987 status = -EINVAL;
988 goto exit;
989 }
990 if (context->state != EFFECT_STATE_ACTIVE) {
991 status = -ENOSYS;
992 goto exit;
993 }
994 context->state = EFFECT_STATE_INITIALIZED;
995 if (context->ops.disable)
996 context->ops.disable(context);
997 pthread_cond_signal(&cond);
998 ALOGV("%s EFFECT_CMD_DISABLE", __func__);
999 *(int *)pReplyData = 0;
1000 break;
1001 case EFFECT_CMD_GET_PARAM: {
1002 if (pCmdData == NULL ||
1003 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
1004 pReplyData == NULL ||
1005 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
1006 status = -EINVAL;
1007 goto exit;
1008 }
1009 if (!context->offload_enabled) {
1010 status = -EINVAL;
1011 goto exit;
1012 }
1013 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
1014 effect_param_t *p = (effect_param_t *)pReplyData;
1015 if (context->ops.get_parameter)
1016 context->ops.get_parameter(context, p, replySize);
1017 } break;
1018 case EFFECT_CMD_SET_PARAM: {
1019 if (pCmdData == NULL ||
1020 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
1021 pReplyData == NULL || *replySize != sizeof(int32_t)) {
1022 status = -EINVAL;
1023 goto exit;
1024 }
1025 *(int32_t *)pReplyData = 0;
1026 effect_param_t *p = (effect_param_t *)pCmdData;
1027 if (context->ops.set_parameter)
1028 *(int32_t *)pReplyData = context->ops.set_parameter(context, p, *replySize);
1029
1030 } break;
1031 case EFFECT_CMD_SET_DEVICE:
1032 case EFFECT_CMD_SET_VOLUME:
1033 case EFFECT_CMD_SET_AUDIO_MODE:
1034 break;
1035
1036 case EFFECT_CMD_OFFLOAD: {
1037 output_context_t *out_ctxt;
1038
1039 if (cmdSize != sizeof(effect_offload_param_t) || pCmdData == NULL
1040 || pReplyData == NULL || *replySize != sizeof(int)) {
1041 ALOGV("%s EFFECT_CMD_OFFLOAD bad format", __func__);
1042 status = -EINVAL;
1043 break;
1044 }
1045
1046 effect_offload_param_t* offload_param = (effect_offload_param_t*)pCmdData;
1047
1048 ALOGV("%s EFFECT_CMD_OFFLOAD offload %d output %d",
1049 __func__, offload_param->isOffload, offload_param->ioHandle);
1050
1051 *(int *)pReplyData = 0;
1052
1053 context->offload_enabled = offload_param->isOffload;
1054 if (context->out_handle == offload_param->ioHandle)
1055 break;
1056
1057 out_ctxt = get_output(context->out_handle);
1058 if (out_ctxt != NULL)
1059 remove_effect_from_output(out_ctxt, context);
1060 out_ctxt = get_output(offload_param->ioHandle);
1061 if (out_ctxt != NULL)
1062 add_effect_to_output(out_ctxt, context);
1063
1064 context->out_handle = offload_param->ioHandle;
1065
1066 } break;
1067
1068
1069 default:
1070 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY && context->ops.command)
1071 status = context->ops.command(context, cmdCode, cmdSize,
1072 pCmdData, replySize, pReplyData);
1073 else {
1074 ALOGW("%s invalid command %d", __func__, cmdCode);
1075 status = -EINVAL;
1076 }
1077 break;
1078 }
1079
1080exit:
1081 pthread_mutex_unlock(&lock);
1082
1083// ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,"%s DONE", __func__);
1084 return status;
1085}
1086
1087/* Effect Control Interface Implementation: get_descriptor */
1088int effect_get_descriptor(effect_handle_t self,
1089 effect_descriptor_t *descriptor)
1090{
1091 effect_context_t *context = (effect_context_t *)self;
1092
1093 if (!effect_exists(context))
1094 return -EINVAL;
1095
1096 if (descriptor == NULL)
1097 return -EINVAL;
1098
1099 *descriptor = *context->desc;
1100
1101 return 0;
1102}
1103
1104/* effect_handle_t interface implementation for visualizer effect */
1105const struct effect_interface_s effect_interface = {
1106 effect_process,
1107 effect_command,
1108 effect_get_descriptor,
1109 NULL,
1110};
1111
1112__attribute__ ((visibility ("default")))
1113audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
1114 tag : AUDIO_EFFECT_LIBRARY_TAG,
1115 version : EFFECT_LIBRARY_API_VERSION,
1116 name : "Visualizer Library",
1117 implementor : "The Android Open Source Project",
1118 create_effect : effect_lib_create,
1119 release_effect : effect_lib_release,
1120 get_descriptor : effect_lib_get_descriptor,
1121};