blob: e1a34a0558722b30f3a655c9a778ab9618467215 [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;
Mingshu Pang3c694b42019-12-10 13:59:00 +0800974 const int32_t delta_ms = visualizer_get_delta_time_ms_from_updated_time(visu_ctxt);
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700975 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
Mingshu Pang3c694b42019-12-10 13:59:00 +0800981 int64_t capture_point = visu_ctxt->capture_idx;
982 capture_point -= visu_ctxt->capture_size;
983 capture_point -= delta_smp;
Weiyin Jiangacf12bb2019-11-21 16:54:14 +0800984 int64_t capture_size = visu_ctxt->capture_size;
Eric Laurentc4aef752013-09-12 17:45:53 -0700985 if (capture_point < 0) {
Weiyin Jiangacf12bb2019-11-21 16:54:14 +0800986 int64_t size = -capture_point;
Eric Laurentc4aef752013-09-12 17:45:53 -0700987 if (size > capture_size)
988 size = capture_size;
989
990 memcpy(pReplyData,
991 visu_ctxt->capture_buf + CAPTURE_BUF_SIZE + capture_point,
992 size);
993 pReplyData = (void *)((size_t)pReplyData + size);
994 capture_size -= size;
995 capture_point = 0;
996 }
997 memcpy(pReplyData,
998 visu_ctxt->capture_buf + capture_point,
999 capture_size);
1000
1001
1002 /* if audio framework has stopped playing audio although the effect is still
1003 * active we must clear the capture buffer to return silence */
1004 if ((visu_ctxt->last_capture_idx == visu_ctxt->capture_idx) &&
1005 (visu_ctxt->buffer_update_time.tv_sec != 0)) {
1006 if (delta_ms > MAX_STALL_TIME_MS) {
1007 ALOGV("%s capture going to idle", __func__);
1008 visu_ctxt->buffer_update_time.tv_sec = 0;
1009 memset(pReplyData, 0x80, visu_ctxt->capture_size);
1010 }
1011 }
1012 visu_ctxt->last_capture_idx = visu_ctxt->capture_idx;
1013 } else {
1014 memset(pReplyData, 0x80, visu_ctxt->capture_size);
1015 }
1016 break;
1017
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -07001018 case VISUALIZER_CMD_MEASURE: {
ragod8c80c22016-08-22 17:59:38 -07001019 if (pReplyData == NULL || replySize == NULL ||
1020 *replySize < (sizeof(int32_t) * MEASUREMENT_COUNT)) {
rago95b51a52016-10-07 18:13:29 -07001021 if (replySize == NULL) {
1022 ALOGV("%s VISUALIZER_CMD_MEASURE error replySize NULL", __func__);
1023 } else {
1024 ALOGV("%s VISUALIZER_CMD_MEASURE error *replySize %u <"
1025 "(sizeof(int32_t) * MEASUREMENT_COUNT) %zu",
1026 __func__, *replySize, sizeof(int32_t) * MEASUREMENT_COUNT);
1027 }
ragod8c80c22016-08-22 17:59:38 -07001028 android_errorWriteLog(0x534e4554, "30229821");
1029 return -EINVAL;
1030 }
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -07001031 uint16_t peak_u16 = 0;
1032 float sum_rms_squared = 0.0f;
1033 uint8_t nb_valid_meas = 0;
1034 /* reset measurements if last measurement was too long ago (which implies stored
1035 * measurements aren't relevant anymore and shouldn't bias the new one) */
1036 const int32_t delay_ms = visualizer_get_delta_time_ms_from_updated_time(visu_ctxt);
1037 if (delay_ms > DISCARD_MEASUREMENTS_TIME_MS) {
1038 uint32_t i;
1039 ALOGV("Discarding measurements, last measurement is %dms old", delay_ms);
1040 for (i=0 ; i<visu_ctxt->meas_wndw_size_in_buffers ; i++) {
1041 visu_ctxt->past_meas[i].is_valid = false;
1042 visu_ctxt->past_meas[i].peak_u16 = 0;
1043 visu_ctxt->past_meas[i].rms_squared = 0;
1044 }
1045 visu_ctxt->meas_buffer_idx = 0;
1046 } else {
1047 /* only use actual measurements, otherwise the first RMS measure happening before
1048 * MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
1049 * low */
1050 uint32_t i;
1051 for (i=0 ; i < visu_ctxt->meas_wndw_size_in_buffers ; i++) {
1052 if (visu_ctxt->past_meas[i].is_valid) {
1053 if (visu_ctxt->past_meas[i].peak_u16 > peak_u16) {
1054 peak_u16 = visu_ctxt->past_meas[i].peak_u16;
1055 }
1056 sum_rms_squared += visu_ctxt->past_meas[i].rms_squared;
1057 nb_valid_meas++;
1058 }
1059 }
1060 }
1061 float rms = nb_valid_meas == 0 ? 0.0f : sqrtf(sum_rms_squared / nb_valid_meas);
1062 int32_t* p_int_reply_data = (int32_t*)pReplyData;
1063 /* convert from I16 sample values to mB and write results */
1064 if (rms < 0.000016f) {
1065 p_int_reply_data[MEASUREMENT_IDX_RMS] = -9600; //-96dB
1066 } else {
1067 p_int_reply_data[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
1068 }
1069 if (peak_u16 == 0) {
1070 p_int_reply_data[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
1071 } else {
1072 p_int_reply_data[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peak_u16 / 32767.0f));
1073 }
1074 ALOGV("VISUALIZER_CMD_MEASURE peak=%d (%dmB), rms=%.1f (%dmB)",
1075 peak_u16, p_int_reply_data[MEASUREMENT_IDX_PEAK],
1076 rms, p_int_reply_data[MEASUREMENT_IDX_RMS]);
1077 }
1078 break;
1079
Eric Laurentc4aef752013-09-12 17:45:53 -07001080 default:
1081 ALOGW("%s invalid command %d", __func__, cmdCode);
1082 return -EINVAL;
1083 }
1084 return 0;
1085}
1086
1087
1088/*
1089 * Effect Library Interface Implementation
1090 */
1091
1092int effect_lib_create(const effect_uuid_t *uuid,
1093 int32_t sessionId,
1094 int32_t ioId,
1095 effect_handle_t *pHandle) {
1096 int ret;
1097 int i;
1098
1099 if (lib_init() != 0)
1100 return init_status;
1101
1102 if (pHandle == NULL || uuid == NULL)
1103 return -EINVAL;
1104
1105 for (i = 0; descriptors[i] != NULL; i++) {
1106 if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0)
1107 break;
1108 }
1109
1110 if (descriptors[i] == NULL)
1111 return -EINVAL;
1112
1113 effect_context_t *context;
1114 if (memcmp(uuid, &visualizer_descriptor.uuid, sizeof(effect_uuid_t)) == 0) {
1115 visualizer_context_t *visu_ctxt = (visualizer_context_t *)calloc(1,
1116 sizeof(visualizer_context_t));
wjiangebb69fa2014-05-15 19:38:26 +08001117 if (visu_ctxt == NULL) {
1118 ALOGE("%s fail to allocate memory", __func__);
1119 return -ENOMEM;
1120 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001121 context = (effect_context_t *)visu_ctxt;
1122 context->ops.init = visualizer_init;
1123 context->ops.reset = visualizer_reset;
1124 context->ops.process = visualizer_process;
1125 context->ops.set_parameter = visualizer_set_parameter;
1126 context->ops.get_parameter = visualizer_get_parameter;
1127 context->ops.command = visualizer_command;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001128 context->desc = &visualizer_descriptor;
Eric Laurentc4aef752013-09-12 17:45:53 -07001129 } else {
1130 return -EINVAL;
1131 }
1132
1133 context->itfe = &effect_interface;
1134 context->state = EFFECT_STATE_UNINITIALIZED;
1135 context->out_handle = (audio_io_handle_t)ioId;
Eric Laurentc4aef752013-09-12 17:45:53 -07001136
1137 ret = context->ops.init(context);
1138 if (ret < 0) {
1139 ALOGW("%s init failed", __func__);
1140 free(context);
1141 return ret;
1142 }
1143
1144 context->state = EFFECT_STATE_INITIALIZED;
1145
1146 pthread_mutex_lock(&lock);
1147 list_add_tail(&created_effects_list, &context->effects_list_node);
1148 output_context_t *out_ctxt = get_output(ioId);
1149 if (out_ctxt != NULL)
1150 add_effect_to_output(out_ctxt, context);
1151 pthread_mutex_unlock(&lock);
1152
1153 *pHandle = (effect_handle_t)context;
1154
1155 ALOGV("%s created context %p", __func__, context);
1156
1157 return 0;
1158
1159}
1160
1161int effect_lib_release(effect_handle_t handle) {
1162 effect_context_t *context = (effect_context_t *)handle;
1163 int status;
1164
1165 if (lib_init() != 0)
1166 return init_status;
1167
1168 ALOGV("%s context %p", __func__, handle);
1169 pthread_mutex_lock(&lock);
1170 status = -EINVAL;
1171 if (effect_exists(context)) {
1172 output_context_t *out_ctxt = get_output(context->out_handle);
1173 if (out_ctxt != NULL)
1174 remove_effect_from_output(out_ctxt, context);
1175 list_remove(&context->effects_list_node);
1176 if (context->ops.release)
1177 context->ops.release(context);
1178 free(context);
1179 status = 0;
1180 }
1181 pthread_mutex_unlock(&lock);
1182
1183 return status;
1184}
1185
1186int effect_lib_get_descriptor(const effect_uuid_t *uuid,
1187 effect_descriptor_t *descriptor) {
1188 int i;
1189
1190 if (lib_init() != 0)
1191 return init_status;
1192
1193 if (descriptor == NULL || uuid == NULL) {
1194 ALOGV("%s called with NULL pointer", __func__);
1195 return -EINVAL;
1196 }
1197
1198 for (i = 0; descriptors[i] != NULL; i++) {
1199 if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
1200 *descriptor = *descriptors[i];
1201 return 0;
1202 }
1203 }
1204
1205 return -EINVAL;
1206}
1207
1208/*
1209 * Effect Control Interface Implementation
1210 */
1211
1212 /* Stub function for effect interface: never called for offloaded effects */
1213int effect_process(effect_handle_t self,
1214 audio_buffer_t *inBuffer,
1215 audio_buffer_t *outBuffer)
1216{
1217 effect_context_t * context = (effect_context_t *)self;
1218 int status = 0;
1219
1220 ALOGW("%s Called ?????", __func__);
1221
1222 pthread_mutex_lock(&lock);
1223 if (!effect_exists(context)) {
1224 status = -EINVAL;
1225 goto exit;
1226 }
1227
1228 if (context->state != EFFECT_STATE_ACTIVE) {
1229 status = -EINVAL;
1230 goto exit;
1231 }
1232
1233exit:
1234 pthread_mutex_unlock(&lock);
1235 return status;
1236}
1237
1238int effect_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
1239 void *pCmdData, uint32_t *replySize, void *pReplyData)
1240{
1241
1242 effect_context_t * context = (effect_context_t *)self;
1243 int retsize;
1244 int status = 0;
1245
1246 pthread_mutex_lock(&lock);
1247
1248 if (!effect_exists(context)) {
1249 status = -EINVAL;
1250 goto exit;
1251 }
1252
1253 if (context == NULL || context->state == EFFECT_STATE_UNINITIALIZED) {
1254 status = -EINVAL;
1255 goto exit;
1256 }
1257
1258// ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,
1259// "%s command %d cmdSize %d", __func__, cmdCode, cmdSize);
1260
1261 switch (cmdCode) {
1262 case EFFECT_CMD_INIT:
1263 if (pReplyData == NULL || *replySize != sizeof(int)) {
1264 status = -EINVAL;
1265 goto exit;
1266 }
1267 if (context->ops.init)
1268 *(int *) pReplyData = context->ops.init(context);
1269 else
1270 *(int *) pReplyData = 0;
1271 break;
1272 case EFFECT_CMD_SET_CONFIG:
1273 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
1274 || pReplyData == NULL || *replySize != sizeof(int)) {
1275 status = -EINVAL;
1276 goto exit;
1277 }
1278 *(int *) pReplyData = set_config(context, (effect_config_t *) pCmdData);
1279 break;
1280 case EFFECT_CMD_GET_CONFIG:
1281 if (pReplyData == NULL ||
1282 *replySize != sizeof(effect_config_t)) {
1283 status = -EINVAL;
1284 goto exit;
1285 }
1286 if (!context->offload_enabled) {
1287 status = -EINVAL;
1288 goto exit;
1289 }
1290
1291 get_config(context, (effect_config_t *)pReplyData);
1292 break;
1293 case EFFECT_CMD_RESET:
1294 if (context->ops.reset)
1295 context->ops.reset(context);
1296 break;
1297 case EFFECT_CMD_ENABLE:
1298 if (pReplyData == NULL || *replySize != sizeof(int)) {
1299 status = -EINVAL;
1300 goto exit;
1301 }
1302 if (context->state != EFFECT_STATE_INITIALIZED) {
1303 status = -ENOSYS;
1304 goto exit;
1305 }
1306 context->state = EFFECT_STATE_ACTIVE;
1307 if (context->ops.enable)
1308 context->ops.enable(context);
1309 pthread_cond_signal(&cond);
1310 ALOGV("%s EFFECT_CMD_ENABLE", __func__);
1311 *(int *)pReplyData = 0;
1312 break;
1313 case EFFECT_CMD_DISABLE:
1314 if (pReplyData == NULL || *replySize != sizeof(int)) {
1315 status = -EINVAL;
1316 goto exit;
1317 }
1318 if (context->state != EFFECT_STATE_ACTIVE) {
1319 status = -ENOSYS;
1320 goto exit;
1321 }
1322 context->state = EFFECT_STATE_INITIALIZED;
1323 if (context->ops.disable)
1324 context->ops.disable(context);
1325 pthread_cond_signal(&cond);
1326 ALOGV("%s EFFECT_CMD_DISABLE", __func__);
1327 *(int *)pReplyData = 0;
1328 break;
1329 case EFFECT_CMD_GET_PARAM: {
1330 if (pCmdData == NULL ||
1331 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
1332 pReplyData == NULL ||
1333 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
1334 status = -EINVAL;
1335 goto exit;
1336 }
1337 if (!context->offload_enabled) {
1338 status = -EINVAL;
1339 goto exit;
1340 }
1341 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
1342 effect_param_t *p = (effect_param_t *)pReplyData;
1343 if (context->ops.get_parameter)
1344 context->ops.get_parameter(context, p, replySize);
1345 } break;
1346 case EFFECT_CMD_SET_PARAM: {
1347 if (pCmdData == NULL ||
1348 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
1349 pReplyData == NULL || *replySize != sizeof(int32_t)) {
1350 status = -EINVAL;
1351 goto exit;
1352 }
1353 *(int32_t *)pReplyData = 0;
1354 effect_param_t *p = (effect_param_t *)pCmdData;
1355 if (context->ops.set_parameter)
1356 *(int32_t *)pReplyData = context->ops.set_parameter(context, p, *replySize);
1357
1358 } break;
1359 case EFFECT_CMD_SET_DEVICE:
1360 case EFFECT_CMD_SET_VOLUME:
1361 case EFFECT_CMD_SET_AUDIO_MODE:
1362 break;
1363
1364 case EFFECT_CMD_OFFLOAD: {
1365 output_context_t *out_ctxt;
1366
1367 if (cmdSize != sizeof(effect_offload_param_t) || pCmdData == NULL
1368 || pReplyData == NULL || *replySize != sizeof(int)) {
1369 ALOGV("%s EFFECT_CMD_OFFLOAD bad format", __func__);
1370 status = -EINVAL;
1371 break;
1372 }
1373
1374 effect_offload_param_t* offload_param = (effect_offload_param_t*)pCmdData;
1375
1376 ALOGV("%s EFFECT_CMD_OFFLOAD offload %d output %d",
1377 __func__, offload_param->isOffload, offload_param->ioHandle);
1378
1379 *(int *)pReplyData = 0;
1380
1381 context->offload_enabled = offload_param->isOffload;
1382 if (context->out_handle == offload_param->ioHandle)
1383 break;
1384
1385 out_ctxt = get_output(context->out_handle);
1386 if (out_ctxt != NULL)
1387 remove_effect_from_output(out_ctxt, context);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001388
1389 context->out_handle = offload_param->ioHandle;
Eric Laurentc4aef752013-09-12 17:45:53 -07001390 out_ctxt = get_output(offload_param->ioHandle);
1391 if (out_ctxt != NULL)
1392 add_effect_to_output(out_ctxt, context);
1393
Eric Laurentc4aef752013-09-12 17:45:53 -07001394 } break;
1395
1396
1397 default:
1398 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY && context->ops.command)
1399 status = context->ops.command(context, cmdCode, cmdSize,
1400 pCmdData, replySize, pReplyData);
1401 else {
1402 ALOGW("%s invalid command %d", __func__, cmdCode);
1403 status = -EINVAL;
1404 }
1405 break;
1406 }
1407
1408exit:
1409 pthread_mutex_unlock(&lock);
1410
1411// ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,"%s DONE", __func__);
1412 return status;
1413}
1414
1415/* Effect Control Interface Implementation: get_descriptor */
1416int effect_get_descriptor(effect_handle_t self,
1417 effect_descriptor_t *descriptor)
1418{
1419 effect_context_t *context = (effect_context_t *)self;
1420
1421 if (!effect_exists(context))
1422 return -EINVAL;
1423
1424 if (descriptor == NULL)
1425 return -EINVAL;
1426
1427 *descriptor = *context->desc;
1428
1429 return 0;
1430}
1431
1432/* effect_handle_t interface implementation for visualizer effect */
1433const struct effect_interface_s effect_interface = {
1434 effect_process,
1435 effect_command,
1436 effect_get_descriptor,
1437 NULL,
1438};
1439
1440__attribute__ ((visibility ("default")))
1441audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
1442 tag : AUDIO_EFFECT_LIBRARY_TAG,
1443 version : EFFECT_LIBRARY_API_VERSION,
1444 name : "Visualizer Library",
1445 implementor : "The Android Open Source Project",
1446 create_effect : effect_lib_create,
1447 release_effect : effect_lib_release,
1448 get_descriptor : effect_lib_get_descriptor,
1449};