blob: 442ef3e0a92a97ff243ed23a66252675e930daab [file] [log] [blame]
Eric Laurentc4aef752013-09-12 17:45:53 -07001/*
Aalique Grahame6de37c02019-02-07 11:49:39 -08002 * Copyright (C) 2013 The Android Open Source Project
Eric Laurentc4aef752013-09-12 17:45:53 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "offload_visualizer"
18/*#define LOG_NDEBUG 0*/
19#include <assert.h>
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -070020#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070021#include <stdlib.h>
22#include <string.h>
23#include <time.h>
24#include <sys/prctl.h>
Ravi Kumar Alamanda518bcbb2014-11-14 16:51:10 -080025#include <dlfcn.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053026#include <pthread.h>
27#include <unistd.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070028
29#include <cutils/list.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080030#include <log/log.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070031#include <system/thread_defs.h>
32#include <tinyalsa/asoundlib.h>
33#include <audio_effects/effect_visualizer.h>
34
Ravi Kumar Alamanda518bcbb2014-11-14 16:51:10 -080035#define LIB_ACDB_LOADER "libacdbloader.so"
36#define ACDB_DEV_TYPE_OUT 1
37#define AFE_PROXY_ACDB_ID 45
38
39static void* acdb_handle;
40
41typedef void (*acdb_send_audio_cal_t)(int, int);
42
Vatsal Buchac09ae062018-11-14 13:25:08 +053043#ifdef AUDIO_FEATURE_ENABLED_GCOV
44extern void __gcov_flush();
45static void enable_gcov()
46{
47 __gcov_flush();
48}
49#else
50static void enable_gcov()
51{
52}
53#endif
54
Ravi Kumar Alamanda518bcbb2014-11-14 16:51:10 -080055acdb_send_audio_cal_t acdb_send_audio_cal;
Eric Laurentc4aef752013-09-12 17:45:53 -070056
57enum {
58 EFFECT_STATE_UNINITIALIZED,
59 EFFECT_STATE_INITIALIZED,
60 EFFECT_STATE_ACTIVE,
61};
62
Aalique Grahame49e6b682019-04-05 10:17:12 -070063enum pcm_device_param {
64 SND_CARD_NUM,
65 DEVICE_ID
66};
67
Eric Laurentc4aef752013-09-12 17:45:53 -070068typedef struct effect_context_s effect_context_t;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080069typedef struct output_context_s output_context_t;
Eric Laurentc4aef752013-09-12 17:45:53 -070070
71/* effect specific operations. Only the init() and process() operations must be defined.
72 * Others are optional.
73 */
74typedef struct effect_ops_s {
75 int (*init)(effect_context_t *context);
76 int (*release)(effect_context_t *context);
77 int (*reset)(effect_context_t *context);
78 int (*enable)(effect_context_t *context);
79 int (*disable)(effect_context_t *context);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080080 int (*start)(effect_context_t *context, output_context_t *output);
81 int (*stop)(effect_context_t *context, output_context_t *output);
Eric Laurentc4aef752013-09-12 17:45:53 -070082 int (*process)(effect_context_t *context, audio_buffer_t *in, audio_buffer_t *out);
83 int (*set_parameter)(effect_context_t *context, effect_param_t *param, uint32_t size);
84 int (*get_parameter)(effect_context_t *context, effect_param_t *param, uint32_t *size);
85 int (*command)(effect_context_t *context, uint32_t cmdCode, uint32_t cmdSize,
86 void *pCmdData, uint32_t *replySize, void *pReplyData);
87} effect_ops_t;
88
89struct effect_context_s {
90 const struct effect_interface_s *itfe;
91 struct listnode effects_list_node; /* node in created_effects_list */
92 struct listnode output_node; /* node in output_context_t.effects_list */
93 effect_config_t config;
94 const effect_descriptor_t *desc;
95 audio_io_handle_t out_handle; /* io handle of the output the effect is attached to */
96 uint32_t state;
97 bool offload_enabled; /* when offload is enabled we process VISUALIZER_CMD_CAPTURE command.
98 Otherwise non offloaded visualizer has already processed the command
99 and we must not overwrite the reply. */
100 effect_ops_t ops;
101};
102
103typedef struct output_context_s {
104 struct listnode outputs_list_node; /* node in active_outputs_list */
105 audio_io_handle_t handle; /* io handle */
106 struct listnode effects_list; /* list of effects attached to this output */
107} output_context_t;
108
109
110/* maximum time since last capture buffer update before resetting capture buffer. This means
111 that the framework has stopped playing audio and we must start returning silence */
112#define MAX_STALL_TIME_MS 1000
113
114#define CAPTURE_BUF_SIZE 65536 /* "64k should be enough for everyone" */
115
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700116#define DISCARD_MEASUREMENTS_TIME_MS 2000 /* discard measurements older than this number of ms */
117
118/* maximum number of buffers for which we keep track of the measurements */
119#define MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS 25 /* note: buffer index is stored in uint8_t */
120
121typedef struct buffer_stats_s {
122 bool is_valid;
123 uint16_t peak_u16; /* the positive peak of the absolute value of the samples in a buffer */
124 float rms_squared; /* the average square of the samples in a buffer */
125} buffer_stats_t;
Eric Laurentc4aef752013-09-12 17:45:53 -0700126
127typedef struct visualizer_context_s {
128 effect_context_t common;
129
130 uint32_t capture_idx;
131 uint32_t capture_size;
132 uint32_t scaling_mode;
133 uint32_t last_capture_idx;
134 uint32_t latency;
135 struct timespec buffer_update_time;
136 uint8_t capture_buf[CAPTURE_BUF_SIZE];
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700137 /* for measurements */
138 uint8_t channel_count; /* to avoid recomputing it every time a buffer is processed */
139 uint32_t meas_mode;
140 uint8_t meas_wndw_size_in_buffers;
141 uint8_t meas_buffer_idx;
142 buffer_stats_t past_meas[MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS];
Eric Laurentc4aef752013-09-12 17:45:53 -0700143} visualizer_context_t;
144
145
146extern const struct effect_interface_s effect_interface;
147
148/* Offload visualizer UUID: 7a8044a0-1a71-11e3-a184-0002a5d5c51b */
149const effect_descriptor_t visualizer_descriptor = {
150 {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
151 {0x7a8044a0, 0x1a71, 0x11e3, 0xa184, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
152 EFFECT_CONTROL_API_VERSION,
153 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_HW_ACC_TUNNEL ),
154 0, /* TODO */
155 1,
156 "QCOM MSM offload visualizer",
157 "The Android Open Source Project",
158};
159
160const effect_descriptor_t *descriptors[] = {
161 &visualizer_descriptor,
162 NULL,
163};
164
165
166pthread_once_t once = PTHREAD_ONCE_INIT;
167int init_status;
168
169/* list of created effects. Updated by visualizer_hal_start_output()
170 * and visualizer_hal_stop_output() */
171struct listnode created_effects_list;
172/* list of active output streams. Updated by visualizer_hal_start_output()
173 * and visualizer_hal_stop_output() */
174struct listnode active_outputs_list;
175
176/* thread capturing PCM from Proxy port and calling the process function on each enabled effect
177 * attached to an active output stream */
178pthread_t capture_thread;
179/* lock must be held when modifying or accessing created_effects_list or active_outputs_list */
180pthread_mutex_t lock;
181/* thread_lock must be held when starting or stopping the capture thread.
182 * Locking order: thread_lock -> lock */
183pthread_mutex_t thread_lock;
184/* cond is signaled when an output is started or stopped or an effect is enabled or disable: the
185 * capture thread will reevaluate the capture and effect rocess conditions. */
186pthread_cond_t cond;
187/* true when requesting the capture thread to exit */
188bool exit_thread;
189/* 0 if the capture thread was created successfully */
190int thread_status;
191
192
193#define DSP_OUTPUT_LATENCY_MS 0 /* Fudge factor for latency after capture point in audio DSP */
194
195/* Retry for delay for mixer open */
196#define RETRY_NUMBER 10
197#define RETRY_US 500000
198
199#define MIXER_CARD 0
200#define SOUND_CARD 0
Weiyin Jiang2d955482017-04-12 19:06:32 +0800201
202#ifndef CAPTURE_DEVICE
Aalique Grahame49e6b682019-04-05 10:17:12 -0700203#define CAPTURE_DEVICE 7
Garmond Leung6406e9d2016-08-23 16:31:03 -0700204#endif
Eric Laurentc4aef752013-09-12 17:45:53 -0700205
206/* Proxy port supports only MMAP read and those fixed parameters*/
207#define AUDIO_CAPTURE_CHANNEL_COUNT 2
208#define AUDIO_CAPTURE_SMP_RATE 48000
209#define AUDIO_CAPTURE_PERIOD_SIZE (768)
210#define AUDIO_CAPTURE_PERIOD_COUNT 32
211
212struct pcm_config pcm_config_capture = {
213 .channels = AUDIO_CAPTURE_CHANNEL_COUNT,
214 .rate = AUDIO_CAPTURE_SMP_RATE,
215 .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
216 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
217 .format = PCM_FORMAT_S16_LE,
218 .start_threshold = AUDIO_CAPTURE_PERIOD_SIZE / 4,
219 .stop_threshold = INT_MAX,
220 .avail_min = AUDIO_CAPTURE_PERIOD_SIZE / 4,
221};
222
223
224/*
225 * Local functions
226 */
227
228static void init_once() {
229 list_init(&created_effects_list);
230 list_init(&active_outputs_list);
231
232 pthread_mutex_init(&lock, NULL);
233 pthread_mutex_init(&thread_lock, NULL);
234 pthread_cond_init(&cond, NULL);
235 exit_thread = false;
236 thread_status = -1;
237
238 init_status = 0;
239}
240
241int lib_init() {
242 pthread_once(&once, init_once);
Vatsal Buchac09ae062018-11-14 13:25:08 +0530243 enable_gcov();
Eric Laurentc4aef752013-09-12 17:45:53 -0700244 return init_status;
245}
246
247bool effect_exists(effect_context_t *context) {
248 struct listnode *node;
249
250 list_for_each(node, &created_effects_list) {
251 effect_context_t *fx_ctxt = node_to_item(node,
252 effect_context_t,
253 effects_list_node);
254 if (fx_ctxt == context) {
255 return true;
256 }
257 }
258 return false;
259}
260
261output_context_t *get_output(audio_io_handle_t output) {
262 struct listnode *node;
263
264 list_for_each(node, &active_outputs_list) {
265 output_context_t *out_ctxt = node_to_item(node,
266 output_context_t,
267 outputs_list_node);
268 if (out_ctxt->handle == output) {
269 return out_ctxt;
270 }
271 }
272 return NULL;
273}
274
275void add_effect_to_output(output_context_t * output, effect_context_t *context) {
276 struct listnode *fx_node;
277
278 list_for_each(fx_node, &output->effects_list) {
279 effect_context_t *fx_ctxt = node_to_item(fx_node,
280 effect_context_t,
281 output_node);
282 if (fx_ctxt == context)
283 return;
284 }
285 list_add_tail(&output->effects_list, &context->output_node);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800286 if (context->ops.start)
287 context->ops.start(context, output);
Eric Laurentc4aef752013-09-12 17:45:53 -0700288}
289
290void remove_effect_from_output(output_context_t * output, effect_context_t *context) {
291 struct listnode *fx_node;
292
293 list_for_each(fx_node, &output->effects_list) {
294 effect_context_t *fx_ctxt = node_to_item(fx_node,
295 effect_context_t,
296 output_node);
297 if (fx_ctxt == context) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800298 if (context->ops.stop)
299 context->ops.stop(context, output);
Eric Laurentc4aef752013-09-12 17:45:53 -0700300 list_remove(&context->output_node);
301 return;
302 }
303 }
304}
305
306bool effects_enabled() {
307 struct listnode *out_node;
308
309 list_for_each(out_node, &active_outputs_list) {
310 struct listnode *fx_node;
311 output_context_t *out_ctxt = node_to_item(out_node,
312 output_context_t,
313 outputs_list_node);
314
315 list_for_each(fx_node, &out_ctxt->effects_list) {
316 effect_context_t *fx_ctxt = node_to_item(fx_node,
317 effect_context_t,
318 output_node);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800319 if (fx_ctxt->state == EFFECT_STATE_ACTIVE && fx_ctxt->ops.process != NULL)
Eric Laurentc4aef752013-09-12 17:45:53 -0700320 return true;
321 }
322 }
323 return false;
324}
325
vivek mehta39cfce62015-09-18 10:39:16 -0700326int set_control(const char* name, struct mixer *mixer, int value) {
Eric Laurentc4aef752013-09-12 17:45:53 -0700327 struct mixer_ctl *ctl;
328
vivek mehta39cfce62015-09-18 10:39:16 -0700329 ctl = mixer_get_ctl_by_name(mixer, name);
330 if (ctl == NULL) {
331 ALOGW("%s: could not get %s ctl", __func__, name);
332 return -EINVAL;
333 }
334 if (mixer_ctl_set_value(ctl, 0, value) != 0) {
335 ALOGW("%s: error setting value %d on %s ", __func__, value, name);
336 return -EINVAL;
337 }
338
339 return 0;
340}
341
342int configure_proxy_capture(struct mixer *mixer, int value) {
343 int retval = 0;
344
Ravi Kumar Alamanda518bcbb2014-11-14 16:51:10 -0800345 if (value && acdb_send_audio_cal)
346 acdb_send_audio_cal(AFE_PROXY_ACDB_ID, ACDB_DEV_TYPE_OUT);
347
vivek mehta39cfce62015-09-18 10:39:16 -0700348 retval = set_control("AFE_PCM_RX Audio Mixer MultiMedia4", mixer, value);
349
350 if (retval != 0)
351 return retval;
352
353 // Extending visualizer to capture for compress2 path as well.
354 // for extending it to multiple offload either this needs to be extended
355 // or need to find better solution to enable only active offload sessions
356
357 retval = set_control("AFE_PCM_RX Audio Mixer MultiMedia7", mixer, value);
358 if (retval != 0)
359 return retval;
Eric Laurentc4aef752013-09-12 17:45:53 -0700360
361 return 0;
362}
363
Aalique Grahame49e6b682019-04-05 10:17:12 -0700364// Get sound card number from pcm device
365int get_snd_card_num(char *device_info)
366{
Aalique Grahame09bb37c2019-05-10 14:19:43 -0700367 char *token = NULL, *saveptr = NULL;;
Aalique Grahame49e6b682019-04-05 10:17:12 -0700368 int num = -1;
369
Aalique Grahame09bb37c2019-05-10 14:19:43 -0700370 token = strtok_r(device_info, ": ", &saveptr);
371 token = strtok_r(token, "-", &saveptr);
Aalique Grahame49e6b682019-04-05 10:17:12 -0700372 if (token)
373 num = atoi(token);
374
375 return num;
376}
377
378// Get device id from pcm device
379int get_device_id(char *device_info)
380{
381 char *token = NULL, *saveptr = NULL;
382 int id = -1;
383
Aalique Grahame09bb37c2019-05-10 14:19:43 -0700384 token = strtok_r(device_info, ": ", &saveptr);
Aalique Grahame49e6b682019-04-05 10:17:12 -0700385 token = strtok_r(token, "-", &saveptr);
386 while (token != NULL) {
387 token = strtok_r(NULL, "-", &saveptr);
388 if (token) {
389 id = atoi(token);
390 break;
391 }
392 }
393
394 return id;
395}
396
397int parse_device_info(int param, char *device_info)
398{
399 switch (param) {
400 case SND_CARD_NUM:
401 return get_snd_card_num(device_info);
402 case DEVICE_ID:
403 return get_device_id(device_info);
404 default:
405 ALOGE("%s: invalid pcm device param", __func__);
406 return -1;
407 }
408}
409
410/*
411* Parse a pcm device from procfs
412* Entries in pcm file will have one of two formats:
413* <snd_card_num>-<device_id>: <descriptor> : : <playback> : <capture>
414* <snd_card_num>-<device_id>: <descriptor> : : <playback or capture>
415*/
416int parse_pcm_device(char *descriptor, int param)
417{
418 const char *pcm_devices_path = "/proc/asound/pcm";
419 char *device_info = NULL;
420 size_t len = 0;
421 ssize_t bytes_read = -1;
422 FILE *fp = NULL;
423 int ret = -1;
424
425 if (descriptor == NULL) {
426 ALOGE("%s: pcm device descriptor is NULL", __func__);
427 return ret;
428 }
429
430 if ((fp = fopen(pcm_devices_path, "r")) == NULL) {
431 ALOGE("Cannot open %s file to get list of pcm devices",
432 pcm_devices_path);
433 return ret;
434 }
435
436 while ((bytes_read = getline(&device_info, &len, fp) != -1)) {
437 if (strstr(device_info, descriptor)) {
438 ret = parse_device_info(param, device_info);
439 break;
440 }
441 }
442
443 if (device_info) {
444 free(device_info);
445 device_info = NULL;
446 }
447
448 fclose(fp);
449 fp = NULL;
450
451 return ret;
452}
Eric Laurentc4aef752013-09-12 17:45:53 -0700453
454void *capture_thread_loop(void *arg)
455{
456 int16_t data[AUDIO_CAPTURE_PERIOD_SIZE * AUDIO_CAPTURE_CHANNEL_COUNT * sizeof(int16_t)];
457 audio_buffer_t buf;
458 buf.frameCount = AUDIO_CAPTURE_PERIOD_SIZE;
459 buf.s16 = data;
460 bool capture_enabled = false;
461 struct mixer *mixer;
462 struct pcm *pcm = NULL;
463 int ret;
464 int retry_num = 0;
Aalique Grahame49e6b682019-04-05 10:17:12 -0700465 int sound_card = SOUND_CARD;
466 int capture_device = CAPTURE_DEVICE;
Eric Laurentc4aef752013-09-12 17:45:53 -0700467
468 ALOGD("thread enter");
469
470 prctl(PR_SET_NAME, (unsigned long)"visualizer capture", 0, 0, 0);
471
472 pthread_mutex_lock(&lock);
473
474 mixer = mixer_open(MIXER_CARD);
475 while (mixer == NULL && retry_num < RETRY_NUMBER) {
476 usleep(RETRY_US);
477 mixer = mixer_open(MIXER_CARD);
478 retry_num++;
479 }
480 if (mixer == NULL) {
481 pthread_mutex_unlock(&lock);
482 return NULL;
483 }
484
485 for (;;) {
486 if (exit_thread) {
487 break;
488 }
489 if (effects_enabled()) {
490 if (!capture_enabled) {
491 ret = configure_proxy_capture(mixer, 1);
492 if (ret == 0) {
Aalique Grahame49e6b682019-04-05 10:17:12 -0700493 sound_card =
494 parse_pcm_device("AFE-PROXY TX", SND_CARD_NUM);
495 sound_card =
496 (sound_card == -1)? SOUND_CARD : sound_card;
497 capture_device =
498 parse_pcm_device("AFE-PROXY TX", DEVICE_ID);
499 capture_device =
500 (capture_device == -1)? CAPTURE_DEVICE : capture_device;
501 pcm = pcm_open(sound_card, capture_device,
Eric Laurentc4aef752013-09-12 17:45:53 -0700502 PCM_IN|PCM_MMAP|PCM_NOIRQ, &pcm_config_capture);
503 if (pcm && !pcm_is_ready(pcm)) {
504 ALOGW("%s: %s", __func__, pcm_get_error(pcm));
505 pcm_close(pcm);
506 pcm = NULL;
507 configure_proxy_capture(mixer, 0);
Sujin Panickeree5c40c2019-03-29 13:04:37 +0530508 pthread_cond_wait(&cond, &lock);
Eric Laurentc4aef752013-09-12 17:45:53 -0700509 } else {
510 capture_enabled = true;
511 ALOGD("%s: capture ENABLED", __func__);
512 }
513 }
514 }
515 } else {
516 if (capture_enabled) {
517 if (pcm != NULL)
518 pcm_close(pcm);
519 configure_proxy_capture(mixer, 0);
520 ALOGD("%s: capture DISABLED", __func__);
521 capture_enabled = false;
522 }
523 pthread_cond_wait(&cond, &lock);
524 }
525 if (!capture_enabled)
526 continue;
527
528 pthread_mutex_unlock(&lock);
529 ret = pcm_mmap_read(pcm, data, sizeof(data));
530 pthread_mutex_lock(&lock);
531
532 if (ret == 0) {
533 struct listnode *out_node;
534
535 list_for_each(out_node, &active_outputs_list) {
536 output_context_t *out_ctxt = node_to_item(out_node,
537 output_context_t,
538 outputs_list_node);
539 struct listnode *fx_node;
540
541 list_for_each(fx_node, &out_ctxt->effects_list) {
542 effect_context_t *fx_ctxt = node_to_item(fx_node,
543 effect_context_t,
544 output_node);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800545 if (fx_ctxt->ops.process != NULL)
546 fx_ctxt->ops.process(fx_ctxt, &buf, &buf);
Eric Laurentc4aef752013-09-12 17:45:53 -0700547 }
548 }
549 } else {
550 ALOGW("%s: read status %d %s", __func__, ret, pcm_get_error(pcm));
551 }
552 }
553
554 if (capture_enabled) {
555 if (pcm != NULL)
556 pcm_close(pcm);
557 configure_proxy_capture(mixer, 0);
558 }
559 mixer_close(mixer);
560 pthread_mutex_unlock(&lock);
561
562 ALOGD("thread exit");
563
564 return NULL;
565}
566
567/*
568 * Interface from audio HAL
569 */
570
571__attribute__ ((visibility ("default")))
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800572int visualizer_hal_start_output(audio_io_handle_t output, int pcm_id) {
Dhananjay Kumar5f90a5a2017-04-25 20:55:27 +0530573 int ret = 0;
Eric Laurentc4aef752013-09-12 17:45:53 -0700574 struct listnode *node;
575
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800576 ALOGV("%s output %d pcm_id %d", __func__, output, pcm_id);
Eric Laurentc4aef752013-09-12 17:45:53 -0700577
578 if (lib_init() != 0)
579 return init_status;
580
581 pthread_mutex_lock(&thread_lock);
582 pthread_mutex_lock(&lock);
583 if (get_output(output) != NULL) {
584 ALOGW("%s output already started", __func__);
585 ret = -ENOSYS;
586 goto exit;
587 }
588
589 output_context_t *out_ctxt = (output_context_t *)malloc(sizeof(output_context_t));
wjiangebb69fa2014-05-15 19:38:26 +0800590 if (out_ctxt == NULL) {
591 ALOGE("%s fail to allocate memory", __func__);
592 ret = -ENOMEM;
593 goto exit;
594 }
Eric Laurentc4aef752013-09-12 17:45:53 -0700595 out_ctxt->handle = output;
596 list_init(&out_ctxt->effects_list);
597
598 list_for_each(node, &created_effects_list) {
599 effect_context_t *fx_ctxt = node_to_item(node,
600 effect_context_t,
601 effects_list_node);
602 if (fx_ctxt->out_handle == output) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800603 if (fx_ctxt->ops.start)
604 fx_ctxt->ops.start(fx_ctxt, out_ctxt);
Eric Laurentc4aef752013-09-12 17:45:53 -0700605 list_add_tail(&out_ctxt->effects_list, &fx_ctxt->output_node);
606 }
607 }
608 if (list_empty(&active_outputs_list)) {
609 exit_thread = false;
610 thread_status = pthread_create(&capture_thread, (const pthread_attr_t *) NULL,
611 capture_thread_loop, NULL);
612 }
613 list_add_tail(&active_outputs_list, &out_ctxt->outputs_list_node);
614 pthread_cond_signal(&cond);
615
616exit:
617 pthread_mutex_unlock(&lock);
618 pthread_mutex_unlock(&thread_lock);
619 return ret;
620}
621
622__attribute__ ((visibility ("default")))
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800623int visualizer_hal_stop_output(audio_io_handle_t output, int pcm_id) {
Dhananjay Kumar5f90a5a2017-04-25 20:55:27 +0530624 int ret = 0;
Eric Laurentc4aef752013-09-12 17:45:53 -0700625 struct listnode *node;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800626 struct listnode *fx_node;
Eric Laurentc4aef752013-09-12 17:45:53 -0700627 output_context_t *out_ctxt;
628
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800629 ALOGV("%s output %d pcm_id %d", __func__, output, pcm_id);
Eric Laurentc4aef752013-09-12 17:45:53 -0700630
631 if (lib_init() != 0)
632 return init_status;
633
634 pthread_mutex_lock(&thread_lock);
635 pthread_mutex_lock(&lock);
636
637 out_ctxt = get_output(output);
638 if (out_ctxt == NULL) {
639 ALOGW("%s output not started", __func__);
640 ret = -ENOSYS;
641 goto exit;
642 }
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800643 list_for_each(fx_node, &out_ctxt->effects_list) {
644 effect_context_t *fx_ctxt = node_to_item(fx_node,
645 effect_context_t,
646 output_node);
647 if (fx_ctxt->ops.stop)
648 fx_ctxt->ops.stop(fx_ctxt, out_ctxt);
649 }
Eric Laurentc4aef752013-09-12 17:45:53 -0700650 list_remove(&out_ctxt->outputs_list_node);
651 pthread_cond_signal(&cond);
652
653 if (list_empty(&active_outputs_list)) {
654 if (thread_status == 0) {
655 exit_thread = true;
656 pthread_cond_signal(&cond);
657 pthread_mutex_unlock(&lock);
658 pthread_join(capture_thread, (void **) NULL);
659 pthread_mutex_lock(&lock);
660 thread_status = -1;
661 }
662 }
663
664 free(out_ctxt);
665
666exit:
667 pthread_mutex_unlock(&lock);
668 pthread_mutex_unlock(&thread_lock);
669 return ret;
670}
671
672
673/*
674 * Effect operations
675 */
676
677int set_config(effect_context_t *context, effect_config_t *config)
678{
679 if (config->inputCfg.samplingRate != config->outputCfg.samplingRate) return -EINVAL;
680 if (config->inputCfg.channels != config->outputCfg.channels) return -EINVAL;
681 if (config->inputCfg.format != config->outputCfg.format) return -EINVAL;
Eric Laurentc4aef752013-09-12 17:45:53 -0700682 if (config->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
683 config->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
684 if (config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
685
686 context->config = *config;
687
688 if (context->ops.reset)
689 context->ops.reset(context);
690
691 return 0;
692}
693
694void get_config(effect_context_t *context, effect_config_t *config)
695{
696 *config = context->config;
697}
698
699
700/*
701 * Visualizer operations
702 */
703
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700704uint32_t visualizer_get_delta_time_ms_from_updated_time(visualizer_context_t* visu_ctxt) {
705 uint32_t delta_ms = 0;
706 if (visu_ctxt->buffer_update_time.tv_sec != 0) {
707 struct timespec ts;
708 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
709 time_t secs = ts.tv_sec - visu_ctxt->buffer_update_time.tv_sec;
710 long nsec = ts.tv_nsec - visu_ctxt->buffer_update_time.tv_nsec;
711 if (nsec < 0) {
712 --secs;
713 nsec += 1000000000;
714 }
715 delta_ms = secs * 1000 + nsec / 1000000;
716 }
717 }
718 return delta_ms;
719}
720
Eric Laurentc4aef752013-09-12 17:45:53 -0700721int visualizer_reset(effect_context_t *context)
722{
723 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
724
725 visu_ctxt->capture_idx = 0;
726 visu_ctxt->last_capture_idx = 0;
727 visu_ctxt->buffer_update_time.tv_sec = 0;
728 visu_ctxt->latency = DSP_OUTPUT_LATENCY_MS;
729 memset(visu_ctxt->capture_buf, 0x80, CAPTURE_BUF_SIZE);
730 return 0;
731}
732
733int visualizer_init(effect_context_t *context)
734{
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700735 int32_t i;
736
Eric Laurentc4aef752013-09-12 17:45:53 -0700737 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
738
739 context->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
740 context->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
741 context->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
742 context->config.inputCfg.samplingRate = 44100;
743 context->config.inputCfg.bufferProvider.getBuffer = NULL;
744 context->config.inputCfg.bufferProvider.releaseBuffer = NULL;
745 context->config.inputCfg.bufferProvider.cookie = NULL;
746 context->config.inputCfg.mask = EFFECT_CONFIG_ALL;
747 context->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
748 context->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
749 context->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
750 context->config.outputCfg.samplingRate = 44100;
751 context->config.outputCfg.bufferProvider.getBuffer = NULL;
752 context->config.outputCfg.bufferProvider.releaseBuffer = NULL;
753 context->config.outputCfg.bufferProvider.cookie = NULL;
754 context->config.outputCfg.mask = EFFECT_CONFIG_ALL;
755
756 visu_ctxt->capture_size = VISUALIZER_CAPTURE_SIZE_MAX;
757 visu_ctxt->scaling_mode = VISUALIZER_SCALING_MODE_NORMALIZED;
758
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700759 // measurement initialization
760 visu_ctxt->channel_count = popcount(context->config.inputCfg.channels);
761 visu_ctxt->meas_mode = MEASUREMENT_MODE_NONE;
762 visu_ctxt->meas_wndw_size_in_buffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
763 visu_ctxt->meas_buffer_idx = 0;
764 for (i=0 ; i<visu_ctxt->meas_wndw_size_in_buffers ; i++) {
765 visu_ctxt->past_meas[i].is_valid = false;
766 visu_ctxt->past_meas[i].peak_u16 = 0;
767 visu_ctxt->past_meas[i].rms_squared = 0;
768 }
769
Eric Laurentc4aef752013-09-12 17:45:53 -0700770 set_config(context, &context->config);
771
Ravi Kumar Alamanda518bcbb2014-11-14 16:51:10 -0800772 if (acdb_handle == NULL) {
773 acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
774 if (acdb_handle == NULL) {
775 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
776 } else {
777 acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(acdb_handle,
778 "acdb_loader_send_audio_cal");
779 if (!acdb_send_audio_cal)
780 ALOGE("%s: Could not find the symbol acdb_send_audio_cal from %s",
781 __func__, LIB_ACDB_LOADER);
782 }
783 }
784
Eric Laurentc4aef752013-09-12 17:45:53 -0700785 return 0;
786}
787
788int visualizer_get_parameter(effect_context_t *context, effect_param_t *p, uint32_t *size)
789{
790 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
791
792 p->status = 0;
793 *size = sizeof(effect_param_t) + sizeof(uint32_t);
794 if (p->psize != sizeof(uint32_t)) {
795 p->status = -EINVAL;
796 return 0;
797 }
798 switch (*(uint32_t *)p->data) {
799 case VISUALIZER_PARAM_CAPTURE_SIZE:
800 ALOGV("%s get capture_size = %d", __func__, visu_ctxt->capture_size);
801 *((uint32_t *)p->data + 1) = visu_ctxt->capture_size;
802 p->vsize = sizeof(uint32_t);
803 *size += sizeof(uint32_t);
804 break;
805 case VISUALIZER_PARAM_SCALING_MODE:
806 ALOGV("%s get scaling_mode = %d", __func__, visu_ctxt->scaling_mode);
807 *((uint32_t *)p->data + 1) = visu_ctxt->scaling_mode;
808 p->vsize = sizeof(uint32_t);
809 *size += sizeof(uint32_t);
810 break;
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700811 case VISUALIZER_PARAM_MEASUREMENT_MODE:
812 ALOGV("%s get meas_mode = %d", __func__, visu_ctxt->meas_mode);
813 *((uint32_t *)p->data + 1) = visu_ctxt->meas_mode;
814 p->vsize = sizeof(uint32_t);
815 *size += sizeof(uint32_t);
816 break;
Eric Laurentc4aef752013-09-12 17:45:53 -0700817 default:
818 p->status = -EINVAL;
819 }
820 return 0;
821}
822
823int visualizer_set_parameter(effect_context_t *context, effect_param_t *p, uint32_t size)
824{
825 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
826
827 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t))
828 return -EINVAL;
829
830 switch (*(uint32_t *)p->data) {
831 case VISUALIZER_PARAM_CAPTURE_SIZE:
832 visu_ctxt->capture_size = *((uint32_t *)p->data + 1);
833 ALOGV("%s set capture_size = %d", __func__, visu_ctxt->capture_size);
834 break;
835 case VISUALIZER_PARAM_SCALING_MODE:
836 visu_ctxt->scaling_mode = *((uint32_t *)p->data + 1);
837 ALOGV("%s set scaling_mode = %d", __func__, visu_ctxt->scaling_mode);
838 break;
839 case VISUALIZER_PARAM_LATENCY:
840 /* Ignore latency as we capture at DSP output
841 * visu_ctxt->latency = *((uint32_t *)p->data + 1); */
842 ALOGV("%s set latency = %d", __func__, visu_ctxt->latency);
843 break;
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700844 case VISUALIZER_PARAM_MEASUREMENT_MODE:
845 visu_ctxt->meas_mode = *((uint32_t *)p->data + 1);
846 ALOGV("%s set meas_mode = %d", __func__, visu_ctxt->meas_mode);
847 break;
Eric Laurentc4aef752013-09-12 17:45:53 -0700848 default:
849 return -EINVAL;
850 }
851 return 0;
852}
853
854/* Real process function called from capture thread. Called with lock held */
855int visualizer_process(effect_context_t *context,
856 audio_buffer_t *inBuffer,
857 audio_buffer_t *outBuffer)
858{
859 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
860
861 if (!effect_exists(context))
862 return -EINVAL;
863
864 if (inBuffer == NULL || inBuffer->raw == NULL ||
865 outBuffer == NULL || outBuffer->raw == NULL ||
866 inBuffer->frameCount != outBuffer->frameCount ||
867 inBuffer->frameCount == 0) {
868 return -EINVAL;
869 }
870
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700871 // perform measurements if needed
872 if (visu_ctxt->meas_mode & MEASUREMENT_MODE_PEAK_RMS) {
873 // find the peak and RMS squared for the new buffer
874 uint32_t inIdx;
875 int16_t max_sample = 0;
876 float rms_squared_acc = 0;
877 for (inIdx = 0 ; inIdx < inBuffer->frameCount * visu_ctxt->channel_count ; inIdx++) {
878 if (inBuffer->s16[inIdx] > max_sample) {
879 max_sample = inBuffer->s16[inIdx];
880 } else if (-inBuffer->s16[inIdx] > max_sample) {
881 max_sample = -inBuffer->s16[inIdx];
882 }
883 rms_squared_acc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]);
884 }
885 // store the measurement
886 visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].peak_u16 = (uint16_t)max_sample;
887 visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].rms_squared =
888 rms_squared_acc / (inBuffer->frameCount * visu_ctxt->channel_count);
889 visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].is_valid = true;
890 if (++visu_ctxt->meas_buffer_idx >= visu_ctxt->meas_wndw_size_in_buffers) {
891 visu_ctxt->meas_buffer_idx = 0;
892 }
893 }
894
Eric Laurentc4aef752013-09-12 17:45:53 -0700895 /* all code below assumes stereo 16 bit PCM output and input */
896 int32_t shift;
897
898 if (visu_ctxt->scaling_mode == VISUALIZER_SCALING_MODE_NORMALIZED) {
899 /* derive capture scaling factor from peak value in current buffer
900 * this gives more interesting captures for display. */
901 shift = 32;
902 int len = inBuffer->frameCount * 2;
903 int i;
904 for (i = 0; i < len; i++) {
905 int32_t smp = inBuffer->s16[i];
906 if (smp < 0) smp = -smp - 1; /* take care to keep the max negative in range */
907 int32_t clz = __builtin_clz(smp);
908 if (shift > clz) shift = clz;
909 }
910 /* A maximum amplitude signal will have 17 leading zeros, which we want to
911 * translate to a shift of 8 (for converting 16 bit to 8 bit) */
912 shift = 25 - shift;
913 /* Never scale by less than 8 to avoid returning unaltered PCM signal. */
914 if (shift < 3) {
915 shift = 3;
916 }
917 /* add one to combine the division by 2 needed after summing
918 * left and right channels below */
919 shift++;
920 } else {
921 assert(visu_ctxt->scaling_mode == VISUALIZER_SCALING_MODE_AS_PLAYED);
922 shift = 9;
923 }
924
925 uint32_t capt_idx;
926 uint32_t in_idx;
927 uint8_t *buf = visu_ctxt->capture_buf;
928 for (in_idx = 0, capt_idx = visu_ctxt->capture_idx;
929 in_idx < inBuffer->frameCount;
930 in_idx++, capt_idx++) {
931 if (capt_idx >= CAPTURE_BUF_SIZE) {
932 /* wrap around */
933 capt_idx = 0;
934 }
935 int32_t smp = inBuffer->s16[2 * in_idx] + inBuffer->s16[2 * in_idx + 1];
936 smp = smp >> shift;
937 buf[capt_idx] = ((uint8_t)smp)^0x80;
938 }
939
940 /* XXX the following two should really be atomic, though it probably doesn't
941 * matter much for visualization purposes */
942 visu_ctxt->capture_idx = capt_idx;
943 /* update last buffer update time stamp */
944 if (clock_gettime(CLOCK_MONOTONIC, &visu_ctxt->buffer_update_time) < 0) {
945 visu_ctxt->buffer_update_time.tv_sec = 0;
946 }
947
948 if (context->state != EFFECT_STATE_ACTIVE) {
949 ALOGV("%s DONE inactive", __func__);
950 return -ENODATA;
951 }
952
953 return 0;
954}
955
956int visualizer_command(effect_context_t * context, uint32_t cmdCode, uint32_t cmdSize,
957 void *pCmdData, uint32_t *replySize, void *pReplyData)
958{
959 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
960
961 switch (cmdCode) {
962 case VISUALIZER_CMD_CAPTURE:
963 if (pReplyData == NULL || *replySize != visu_ctxt->capture_size) {
964 ALOGV("%s VISUALIZER_CMD_CAPTURE error *replySize %d context->capture_size %d",
965 __func__, *replySize, visu_ctxt->capture_size);
966 return -EINVAL;
967 }
968
969 if (!context->offload_enabled)
970 break;
971
972 if (context->state == EFFECT_STATE_ACTIVE) {
973 int32_t latency_ms = visu_ctxt->latency;
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700974 const uint32_t delta_ms = visualizer_get_delta_time_ms_from_updated_time(visu_ctxt);
975 latency_ms -= delta_ms;
976 if (latency_ms < 0) {
977 latency_ms = 0;
Eric Laurentc4aef752013-09-12 17:45:53 -0700978 }
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700979 const uint32_t delta_smp = context->config.inputCfg.samplingRate * latency_ms / 1000;
Eric Laurentc4aef752013-09-12 17:45:53 -0700980
981 int32_t capture_point = visu_ctxt->capture_idx - visu_ctxt->capture_size - delta_smp;
982 int32_t capture_size = visu_ctxt->capture_size;
983 if (capture_point < 0) {
984 int32_t size = -capture_point;
985 if (size > capture_size)
986 size = capture_size;
987
988 memcpy(pReplyData,
989 visu_ctxt->capture_buf + CAPTURE_BUF_SIZE + capture_point,
990 size);
991 pReplyData = (void *)((size_t)pReplyData + size);
992 capture_size -= size;
993 capture_point = 0;
994 }
995 memcpy(pReplyData,
996 visu_ctxt->capture_buf + capture_point,
997 capture_size);
998
999
1000 /* if audio framework has stopped playing audio although the effect is still
1001 * active we must clear the capture buffer to return silence */
1002 if ((visu_ctxt->last_capture_idx == visu_ctxt->capture_idx) &&
1003 (visu_ctxt->buffer_update_time.tv_sec != 0)) {
1004 if (delta_ms > MAX_STALL_TIME_MS) {
1005 ALOGV("%s capture going to idle", __func__);
1006 visu_ctxt->buffer_update_time.tv_sec = 0;
1007 memset(pReplyData, 0x80, visu_ctxt->capture_size);
1008 }
1009 }
1010 visu_ctxt->last_capture_idx = visu_ctxt->capture_idx;
1011 } else {
1012 memset(pReplyData, 0x80, visu_ctxt->capture_size);
1013 }
1014 break;
1015
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -07001016 case VISUALIZER_CMD_MEASURE: {
ragod8c80c22016-08-22 17:59:38 -07001017 if (pReplyData == NULL || replySize == NULL ||
1018 *replySize < (sizeof(int32_t) * MEASUREMENT_COUNT)) {
rago95b51a52016-10-07 18:13:29 -07001019 if (replySize == NULL) {
1020 ALOGV("%s VISUALIZER_CMD_MEASURE error replySize NULL", __func__);
1021 } else {
1022 ALOGV("%s VISUALIZER_CMD_MEASURE error *replySize %u <"
1023 "(sizeof(int32_t) * MEASUREMENT_COUNT) %zu",
1024 __func__, *replySize, sizeof(int32_t) * MEASUREMENT_COUNT);
1025 }
ragod8c80c22016-08-22 17:59:38 -07001026 android_errorWriteLog(0x534e4554, "30229821");
1027 return -EINVAL;
1028 }
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -07001029 uint16_t peak_u16 = 0;
1030 float sum_rms_squared = 0.0f;
1031 uint8_t nb_valid_meas = 0;
1032 /* reset measurements if last measurement was too long ago (which implies stored
1033 * measurements aren't relevant anymore and shouldn't bias the new one) */
1034 const int32_t delay_ms = visualizer_get_delta_time_ms_from_updated_time(visu_ctxt);
1035 if (delay_ms > DISCARD_MEASUREMENTS_TIME_MS) {
1036 uint32_t i;
1037 ALOGV("Discarding measurements, last measurement is %dms old", delay_ms);
1038 for (i=0 ; i<visu_ctxt->meas_wndw_size_in_buffers ; i++) {
1039 visu_ctxt->past_meas[i].is_valid = false;
1040 visu_ctxt->past_meas[i].peak_u16 = 0;
1041 visu_ctxt->past_meas[i].rms_squared = 0;
1042 }
1043 visu_ctxt->meas_buffer_idx = 0;
1044 } else {
1045 /* only use actual measurements, otherwise the first RMS measure happening before
1046 * MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
1047 * low */
1048 uint32_t i;
1049 for (i=0 ; i < visu_ctxt->meas_wndw_size_in_buffers ; i++) {
1050 if (visu_ctxt->past_meas[i].is_valid) {
1051 if (visu_ctxt->past_meas[i].peak_u16 > peak_u16) {
1052 peak_u16 = visu_ctxt->past_meas[i].peak_u16;
1053 }
1054 sum_rms_squared += visu_ctxt->past_meas[i].rms_squared;
1055 nb_valid_meas++;
1056 }
1057 }
1058 }
1059 float rms = nb_valid_meas == 0 ? 0.0f : sqrtf(sum_rms_squared / nb_valid_meas);
1060 int32_t* p_int_reply_data = (int32_t*)pReplyData;
1061 /* convert from I16 sample values to mB and write results */
1062 if (rms < 0.000016f) {
1063 p_int_reply_data[MEASUREMENT_IDX_RMS] = -9600; //-96dB
1064 } else {
1065 p_int_reply_data[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
1066 }
1067 if (peak_u16 == 0) {
1068 p_int_reply_data[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
1069 } else {
1070 p_int_reply_data[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peak_u16 / 32767.0f));
1071 }
1072 ALOGV("VISUALIZER_CMD_MEASURE peak=%d (%dmB), rms=%.1f (%dmB)",
1073 peak_u16, p_int_reply_data[MEASUREMENT_IDX_PEAK],
1074 rms, p_int_reply_data[MEASUREMENT_IDX_RMS]);
1075 }
1076 break;
1077
Eric Laurentc4aef752013-09-12 17:45:53 -07001078 default:
1079 ALOGW("%s invalid command %d", __func__, cmdCode);
1080 return -EINVAL;
1081 }
1082 return 0;
1083}
1084
1085
1086/*
1087 * Effect Library Interface Implementation
1088 */
1089
1090int effect_lib_create(const effect_uuid_t *uuid,
1091 int32_t sessionId,
1092 int32_t ioId,
1093 effect_handle_t *pHandle) {
1094 int ret;
1095 int i;
1096
1097 if (lib_init() != 0)
1098 return init_status;
1099
1100 if (pHandle == NULL || uuid == NULL)
1101 return -EINVAL;
1102
1103 for (i = 0; descriptors[i] != NULL; i++) {
1104 if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0)
1105 break;
1106 }
1107
1108 if (descriptors[i] == NULL)
1109 return -EINVAL;
1110
1111 effect_context_t *context;
1112 if (memcmp(uuid, &visualizer_descriptor.uuid, sizeof(effect_uuid_t)) == 0) {
1113 visualizer_context_t *visu_ctxt = (visualizer_context_t *)calloc(1,
1114 sizeof(visualizer_context_t));
wjiangebb69fa2014-05-15 19:38:26 +08001115 if (visu_ctxt == NULL) {
1116 ALOGE("%s fail to allocate memory", __func__);
1117 return -ENOMEM;
1118 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001119 context = (effect_context_t *)visu_ctxt;
1120 context->ops.init = visualizer_init;
1121 context->ops.reset = visualizer_reset;
1122 context->ops.process = visualizer_process;
1123 context->ops.set_parameter = visualizer_set_parameter;
1124 context->ops.get_parameter = visualizer_get_parameter;
1125 context->ops.command = visualizer_command;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001126 context->desc = &visualizer_descriptor;
Eric Laurentc4aef752013-09-12 17:45:53 -07001127 } else {
1128 return -EINVAL;
1129 }
1130
1131 context->itfe = &effect_interface;
1132 context->state = EFFECT_STATE_UNINITIALIZED;
1133 context->out_handle = (audio_io_handle_t)ioId;
Eric Laurentc4aef752013-09-12 17:45:53 -07001134
1135 ret = context->ops.init(context);
1136 if (ret < 0) {
1137 ALOGW("%s init failed", __func__);
1138 free(context);
1139 return ret;
1140 }
1141
1142 context->state = EFFECT_STATE_INITIALIZED;
1143
1144 pthread_mutex_lock(&lock);
1145 list_add_tail(&created_effects_list, &context->effects_list_node);
1146 output_context_t *out_ctxt = get_output(ioId);
1147 if (out_ctxt != NULL)
1148 add_effect_to_output(out_ctxt, context);
1149 pthread_mutex_unlock(&lock);
1150
1151 *pHandle = (effect_handle_t)context;
1152
1153 ALOGV("%s created context %p", __func__, context);
1154
1155 return 0;
1156
1157}
1158
1159int effect_lib_release(effect_handle_t handle) {
1160 effect_context_t *context = (effect_context_t *)handle;
1161 int status;
1162
1163 if (lib_init() != 0)
1164 return init_status;
1165
1166 ALOGV("%s context %p", __func__, handle);
1167 pthread_mutex_lock(&lock);
1168 status = -EINVAL;
1169 if (effect_exists(context)) {
1170 output_context_t *out_ctxt = get_output(context->out_handle);
1171 if (out_ctxt != NULL)
1172 remove_effect_from_output(out_ctxt, context);
1173 list_remove(&context->effects_list_node);
1174 if (context->ops.release)
1175 context->ops.release(context);
1176 free(context);
1177 status = 0;
1178 }
1179 pthread_mutex_unlock(&lock);
1180
1181 return status;
1182}
1183
1184int effect_lib_get_descriptor(const effect_uuid_t *uuid,
1185 effect_descriptor_t *descriptor) {
1186 int i;
1187
1188 if (lib_init() != 0)
1189 return init_status;
1190
1191 if (descriptor == NULL || uuid == NULL) {
1192 ALOGV("%s called with NULL pointer", __func__);
1193 return -EINVAL;
1194 }
1195
1196 for (i = 0; descriptors[i] != NULL; i++) {
1197 if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
1198 *descriptor = *descriptors[i];
1199 return 0;
1200 }
1201 }
1202
1203 return -EINVAL;
1204}
1205
1206/*
1207 * Effect Control Interface Implementation
1208 */
1209
1210 /* Stub function for effect interface: never called for offloaded effects */
1211int effect_process(effect_handle_t self,
1212 audio_buffer_t *inBuffer,
1213 audio_buffer_t *outBuffer)
1214{
1215 effect_context_t * context = (effect_context_t *)self;
1216 int status = 0;
1217
1218 ALOGW("%s Called ?????", __func__);
1219
1220 pthread_mutex_lock(&lock);
1221 if (!effect_exists(context)) {
1222 status = -EINVAL;
1223 goto exit;
1224 }
1225
1226 if (context->state != EFFECT_STATE_ACTIVE) {
1227 status = -EINVAL;
1228 goto exit;
1229 }
1230
1231exit:
1232 pthread_mutex_unlock(&lock);
1233 return status;
1234}
1235
1236int effect_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
1237 void *pCmdData, uint32_t *replySize, void *pReplyData)
1238{
1239
1240 effect_context_t * context = (effect_context_t *)self;
1241 int retsize;
1242 int status = 0;
1243
1244 pthread_mutex_lock(&lock);
1245
1246 if (!effect_exists(context)) {
1247 status = -EINVAL;
1248 goto exit;
1249 }
1250
1251 if (context == NULL || context->state == EFFECT_STATE_UNINITIALIZED) {
1252 status = -EINVAL;
1253 goto exit;
1254 }
1255
1256// ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,
1257// "%s command %d cmdSize %d", __func__, cmdCode, cmdSize);
1258
1259 switch (cmdCode) {
1260 case EFFECT_CMD_INIT:
1261 if (pReplyData == NULL || *replySize != sizeof(int)) {
1262 status = -EINVAL;
1263 goto exit;
1264 }
1265 if (context->ops.init)
1266 *(int *) pReplyData = context->ops.init(context);
1267 else
1268 *(int *) pReplyData = 0;
1269 break;
1270 case EFFECT_CMD_SET_CONFIG:
1271 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
1272 || pReplyData == NULL || *replySize != sizeof(int)) {
1273 status = -EINVAL;
1274 goto exit;
1275 }
1276 *(int *) pReplyData = set_config(context, (effect_config_t *) pCmdData);
1277 break;
1278 case EFFECT_CMD_GET_CONFIG:
1279 if (pReplyData == NULL ||
1280 *replySize != sizeof(effect_config_t)) {
1281 status = -EINVAL;
1282 goto exit;
1283 }
1284 if (!context->offload_enabled) {
1285 status = -EINVAL;
1286 goto exit;
1287 }
1288
1289 get_config(context, (effect_config_t *)pReplyData);
1290 break;
1291 case EFFECT_CMD_RESET:
1292 if (context->ops.reset)
1293 context->ops.reset(context);
1294 break;
1295 case EFFECT_CMD_ENABLE:
1296 if (pReplyData == NULL || *replySize != sizeof(int)) {
1297 status = -EINVAL;
1298 goto exit;
1299 }
1300 if (context->state != EFFECT_STATE_INITIALIZED) {
1301 status = -ENOSYS;
1302 goto exit;
1303 }
1304 context->state = EFFECT_STATE_ACTIVE;
1305 if (context->ops.enable)
1306 context->ops.enable(context);
1307 pthread_cond_signal(&cond);
1308 ALOGV("%s EFFECT_CMD_ENABLE", __func__);
1309 *(int *)pReplyData = 0;
1310 break;
1311 case EFFECT_CMD_DISABLE:
1312 if (pReplyData == NULL || *replySize != sizeof(int)) {
1313 status = -EINVAL;
1314 goto exit;
1315 }
1316 if (context->state != EFFECT_STATE_ACTIVE) {
1317 status = -ENOSYS;
1318 goto exit;
1319 }
1320 context->state = EFFECT_STATE_INITIALIZED;
1321 if (context->ops.disable)
1322 context->ops.disable(context);
1323 pthread_cond_signal(&cond);
1324 ALOGV("%s EFFECT_CMD_DISABLE", __func__);
1325 *(int *)pReplyData = 0;
1326 break;
1327 case EFFECT_CMD_GET_PARAM: {
1328 if (pCmdData == NULL ||
1329 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
1330 pReplyData == NULL ||
1331 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
1332 status = -EINVAL;
1333 goto exit;
1334 }
1335 if (!context->offload_enabled) {
1336 status = -EINVAL;
1337 goto exit;
1338 }
1339 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
1340 effect_param_t *p = (effect_param_t *)pReplyData;
1341 if (context->ops.get_parameter)
1342 context->ops.get_parameter(context, p, replySize);
1343 } break;
1344 case EFFECT_CMD_SET_PARAM: {
1345 if (pCmdData == NULL ||
1346 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
1347 pReplyData == NULL || *replySize != sizeof(int32_t)) {
1348 status = -EINVAL;
1349 goto exit;
1350 }
1351 *(int32_t *)pReplyData = 0;
1352 effect_param_t *p = (effect_param_t *)pCmdData;
1353 if (context->ops.set_parameter)
1354 *(int32_t *)pReplyData = context->ops.set_parameter(context, p, *replySize);
1355
1356 } break;
1357 case EFFECT_CMD_SET_DEVICE:
1358 case EFFECT_CMD_SET_VOLUME:
1359 case EFFECT_CMD_SET_AUDIO_MODE:
1360 break;
1361
1362 case EFFECT_CMD_OFFLOAD: {
1363 output_context_t *out_ctxt;
1364
1365 if (cmdSize != sizeof(effect_offload_param_t) || pCmdData == NULL
1366 || pReplyData == NULL || *replySize != sizeof(int)) {
1367 ALOGV("%s EFFECT_CMD_OFFLOAD bad format", __func__);
1368 status = -EINVAL;
1369 break;
1370 }
1371
1372 effect_offload_param_t* offload_param = (effect_offload_param_t*)pCmdData;
1373
1374 ALOGV("%s EFFECT_CMD_OFFLOAD offload %d output %d",
1375 __func__, offload_param->isOffload, offload_param->ioHandle);
1376
1377 *(int *)pReplyData = 0;
1378
1379 context->offload_enabled = offload_param->isOffload;
1380 if (context->out_handle == offload_param->ioHandle)
1381 break;
1382
1383 out_ctxt = get_output(context->out_handle);
1384 if (out_ctxt != NULL)
1385 remove_effect_from_output(out_ctxt, context);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001386
1387 context->out_handle = offload_param->ioHandle;
Eric Laurentc4aef752013-09-12 17:45:53 -07001388 out_ctxt = get_output(offload_param->ioHandle);
1389 if (out_ctxt != NULL)
1390 add_effect_to_output(out_ctxt, context);
1391
Eric Laurentc4aef752013-09-12 17:45:53 -07001392 } break;
1393
1394
1395 default:
1396 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY && context->ops.command)
1397 status = context->ops.command(context, cmdCode, cmdSize,
1398 pCmdData, replySize, pReplyData);
1399 else {
1400 ALOGW("%s invalid command %d", __func__, cmdCode);
1401 status = -EINVAL;
1402 }
1403 break;
1404 }
1405
1406exit:
1407 pthread_mutex_unlock(&lock);
1408
1409// ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,"%s DONE", __func__);
1410 return status;
1411}
1412
1413/* Effect Control Interface Implementation: get_descriptor */
1414int effect_get_descriptor(effect_handle_t self,
1415 effect_descriptor_t *descriptor)
1416{
1417 effect_context_t *context = (effect_context_t *)self;
1418
1419 if (!effect_exists(context))
1420 return -EINVAL;
1421
1422 if (descriptor == NULL)
1423 return -EINVAL;
1424
1425 *descriptor = *context->desc;
1426
1427 return 0;
1428}
1429
1430/* effect_handle_t interface implementation for visualizer effect */
1431const struct effect_interface_s effect_interface = {
1432 effect_process,
1433 effect_command,
1434 effect_get_descriptor,
1435 NULL,
1436};
1437
1438__attribute__ ((visibility ("default")))
1439audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
1440 tag : AUDIO_EFFECT_LIBRARY_TAG,
1441 version : EFFECT_LIBRARY_API_VERSION,
1442 name : "Visualizer Library",
1443 implementor : "The Android Open Source Project",
1444 create_effect : effect_lib_create,
1445 release_effect : effect_lib_release,
1446 get_descriptor : effect_lib_get_descriptor,
1447};