blob: 65b593837cc2510a5bc026300511e0aa5723fbc5 [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
Eric Laurentc4aef752013-09-12 17:45:53 -0700195#define SOUND_CARD 0
Weiyin Jiang2d955482017-04-12 19:06:32 +0800196
197#ifndef CAPTURE_DEVICE
Aalique Grahame49e6b682019-04-05 10:17:12 -0700198#define CAPTURE_DEVICE 7
Garmond Leung6406e9d2016-08-23 16:31:03 -0700199#endif
Eric Laurentc4aef752013-09-12 17:45:53 -0700200
201/* Proxy port supports only MMAP read and those fixed parameters*/
202#define AUDIO_CAPTURE_CHANNEL_COUNT 2
203#define AUDIO_CAPTURE_SMP_RATE 48000
204#define AUDIO_CAPTURE_PERIOD_SIZE (768)
205#define AUDIO_CAPTURE_PERIOD_COUNT 32
206
207struct pcm_config pcm_config_capture = {
208 .channels = AUDIO_CAPTURE_CHANNEL_COUNT,
209 .rate = AUDIO_CAPTURE_SMP_RATE,
210 .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
211 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
212 .format = PCM_FORMAT_S16_LE,
213 .start_threshold = AUDIO_CAPTURE_PERIOD_SIZE / 4,
214 .stop_threshold = INT_MAX,
215 .avail_min = AUDIO_CAPTURE_PERIOD_SIZE / 4,
216};
217
218
219/*
220 * Local functions
221 */
222
223static void init_once() {
224 list_init(&created_effects_list);
225 list_init(&active_outputs_list);
226
227 pthread_mutex_init(&lock, NULL);
228 pthread_mutex_init(&thread_lock, NULL);
229 pthread_cond_init(&cond, NULL);
230 exit_thread = false;
231 thread_status = -1;
232
233 init_status = 0;
234}
235
236int lib_init() {
237 pthread_once(&once, init_once);
Vatsal Buchac09ae062018-11-14 13:25:08 +0530238 enable_gcov();
Eric Laurentc4aef752013-09-12 17:45:53 -0700239 return init_status;
240}
241
242bool effect_exists(effect_context_t *context) {
243 struct listnode *node;
244
245 list_for_each(node, &created_effects_list) {
246 effect_context_t *fx_ctxt = node_to_item(node,
247 effect_context_t,
248 effects_list_node);
249 if (fx_ctxt == context) {
250 return true;
251 }
252 }
253 return false;
254}
255
256output_context_t *get_output(audio_io_handle_t output) {
257 struct listnode *node;
258
259 list_for_each(node, &active_outputs_list) {
260 output_context_t *out_ctxt = node_to_item(node,
261 output_context_t,
262 outputs_list_node);
263 if (out_ctxt->handle == output) {
264 return out_ctxt;
265 }
266 }
267 return NULL;
268}
269
270void add_effect_to_output(output_context_t * output, effect_context_t *context) {
271 struct listnode *fx_node;
272
273 list_for_each(fx_node, &output->effects_list) {
274 effect_context_t *fx_ctxt = node_to_item(fx_node,
275 effect_context_t,
276 output_node);
277 if (fx_ctxt == context)
278 return;
279 }
280 list_add_tail(&output->effects_list, &context->output_node);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800281 if (context->ops.start)
282 context->ops.start(context, output);
Eric Laurentc4aef752013-09-12 17:45:53 -0700283}
284
285void remove_effect_from_output(output_context_t * output, effect_context_t *context) {
286 struct listnode *fx_node;
287
288 list_for_each(fx_node, &output->effects_list) {
289 effect_context_t *fx_ctxt = node_to_item(fx_node,
290 effect_context_t,
291 output_node);
292 if (fx_ctxt == context) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800293 if (context->ops.stop)
294 context->ops.stop(context, output);
Eric Laurentc4aef752013-09-12 17:45:53 -0700295 list_remove(&context->output_node);
296 return;
297 }
298 }
299}
300
301bool effects_enabled() {
302 struct listnode *out_node;
303
304 list_for_each(out_node, &active_outputs_list) {
305 struct listnode *fx_node;
306 output_context_t *out_ctxt = node_to_item(out_node,
307 output_context_t,
308 outputs_list_node);
309
310 list_for_each(fx_node, &out_ctxt->effects_list) {
311 effect_context_t *fx_ctxt = node_to_item(fx_node,
312 effect_context_t,
313 output_node);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800314 if (fx_ctxt->state == EFFECT_STATE_ACTIVE && fx_ctxt->ops.process != NULL)
Eric Laurentc4aef752013-09-12 17:45:53 -0700315 return true;
316 }
317 }
318 return false;
319}
320
vivek mehta39cfce62015-09-18 10:39:16 -0700321int set_control(const char* name, struct mixer *mixer, int value) {
Eric Laurentc4aef752013-09-12 17:45:53 -0700322 struct mixer_ctl *ctl;
323
vivek mehta39cfce62015-09-18 10:39:16 -0700324 ctl = mixer_get_ctl_by_name(mixer, name);
325 if (ctl == NULL) {
326 ALOGW("%s: could not get %s ctl", __func__, name);
327 return -EINVAL;
328 }
329 if (mixer_ctl_set_value(ctl, 0, value) != 0) {
330 ALOGW("%s: error setting value %d on %s ", __func__, value, name);
331 return -EINVAL;
332 }
333
334 return 0;
335}
336
337int configure_proxy_capture(struct mixer *mixer, int value) {
338 int retval = 0;
339
Ravi Kumar Alamanda518bcbb2014-11-14 16:51:10 -0800340 if (value && acdb_send_audio_cal)
341 acdb_send_audio_cal(AFE_PROXY_ACDB_ID, ACDB_DEV_TYPE_OUT);
342
vivek mehta39cfce62015-09-18 10:39:16 -0700343 retval = set_control("AFE_PCM_RX Audio Mixer MultiMedia4", mixer, value);
344
345 if (retval != 0)
346 return retval;
347
348 // Extending visualizer to capture for compress2 path as well.
349 // for extending it to multiple offload either this needs to be extended
350 // or need to find better solution to enable only active offload sessions
351
352 retval = set_control("AFE_PCM_RX Audio Mixer MultiMedia7", mixer, value);
353 if (retval != 0)
354 return retval;
Eric Laurentc4aef752013-09-12 17:45:53 -0700355
356 return 0;
357}
358
Aalique Grahame49e6b682019-04-05 10:17:12 -0700359// Get sound card number from pcm device
360int get_snd_card_num(char *device_info)
361{
Aalique Grahame09bb37c2019-05-10 14:19:43 -0700362 char *token = NULL, *saveptr = NULL;;
Aalique Grahame49e6b682019-04-05 10:17:12 -0700363 int num = -1;
364
Aalique Grahame09bb37c2019-05-10 14:19:43 -0700365 token = strtok_r(device_info, ": ", &saveptr);
366 token = strtok_r(token, "-", &saveptr);
Aalique Grahame49e6b682019-04-05 10:17:12 -0700367 if (token)
368 num = atoi(token);
369
370 return num;
371}
372
373// Get device id from pcm device
374int get_device_id(char *device_info)
375{
376 char *token = NULL, *saveptr = NULL;
377 int id = -1;
378
Aalique Grahame09bb37c2019-05-10 14:19:43 -0700379 token = strtok_r(device_info, ": ", &saveptr);
Aalique Grahame49e6b682019-04-05 10:17:12 -0700380 token = strtok_r(token, "-", &saveptr);
381 while (token != NULL) {
382 token = strtok_r(NULL, "-", &saveptr);
383 if (token) {
384 id = atoi(token);
385 break;
386 }
387 }
388
389 return id;
390}
391
392int parse_device_info(int param, char *device_info)
393{
394 switch (param) {
395 case SND_CARD_NUM:
396 return get_snd_card_num(device_info);
397 case DEVICE_ID:
398 return get_device_id(device_info);
399 default:
400 ALOGE("%s: invalid pcm device param", __func__);
401 return -1;
402 }
403}
404
405/*
406* Parse a pcm device from procfs
407* Entries in pcm file will have one of two formats:
408* <snd_card_num>-<device_id>: <descriptor> : : <playback> : <capture>
409* <snd_card_num>-<device_id>: <descriptor> : : <playback or capture>
410*/
411int parse_pcm_device(char *descriptor, int param)
412{
413 const char *pcm_devices_path = "/proc/asound/pcm";
414 char *device_info = NULL;
415 size_t len = 0;
416 ssize_t bytes_read = -1;
417 FILE *fp = NULL;
418 int ret = -1;
419
420 if (descriptor == NULL) {
421 ALOGE("%s: pcm device descriptor is NULL", __func__);
422 return ret;
423 }
424
425 if ((fp = fopen(pcm_devices_path, "r")) == NULL) {
426 ALOGE("Cannot open %s file to get list of pcm devices",
427 pcm_devices_path);
428 return ret;
429 }
430
431 while ((bytes_read = getline(&device_info, &len, fp) != -1)) {
432 if (strstr(device_info, descriptor)) {
433 ret = parse_device_info(param, device_info);
434 break;
435 }
436 }
437
438 if (device_info) {
439 free(device_info);
440 device_info = NULL;
441 }
442
443 fclose(fp);
444 fp = NULL;
445
446 return ret;
447}
Eric Laurentc4aef752013-09-12 17:45:53 -0700448
449void *capture_thread_loop(void *arg)
450{
451 int16_t data[AUDIO_CAPTURE_PERIOD_SIZE * AUDIO_CAPTURE_CHANNEL_COUNT * sizeof(int16_t)];
452 audio_buffer_t buf;
453 buf.frameCount = AUDIO_CAPTURE_PERIOD_SIZE;
454 buf.s16 = data;
455 bool capture_enabled = false;
456 struct mixer *mixer;
457 struct pcm *pcm = NULL;
458 int ret;
459 int retry_num = 0;
Aalique Grahame49e6b682019-04-05 10:17:12 -0700460 int sound_card = SOUND_CARD;
461 int capture_device = CAPTURE_DEVICE;
Eric Laurentc4aef752013-09-12 17:45:53 -0700462
463 ALOGD("thread enter");
464
465 prctl(PR_SET_NAME, (unsigned long)"visualizer capture", 0, 0, 0);
466
467 pthread_mutex_lock(&lock);
468
Mingshu Pangdf4dc4a2020-02-12 20:26:54 +0800469 sound_card =
470 parse_pcm_device("AFE-PROXY TX", SND_CARD_NUM);
471 sound_card =
472 (sound_card == -1)? SOUND_CARD : sound_card;
473
474 mixer = mixer_open(sound_card);
Eric Laurentc4aef752013-09-12 17:45:53 -0700475 if (mixer == NULL) {
476 pthread_mutex_unlock(&lock);
477 return NULL;
478 }
479
480 for (;;) {
481 if (exit_thread) {
482 break;
483 }
484 if (effects_enabled()) {
485 if (!capture_enabled) {
486 ret = configure_proxy_capture(mixer, 1);
487 if (ret == 0) {
Aalique Grahame49e6b682019-04-05 10:17:12 -0700488 capture_device =
489 parse_pcm_device("AFE-PROXY TX", DEVICE_ID);
490 capture_device =
491 (capture_device == -1)? CAPTURE_DEVICE : capture_device;
492 pcm = pcm_open(sound_card, capture_device,
Eric Laurentc4aef752013-09-12 17:45:53 -0700493 PCM_IN|PCM_MMAP|PCM_NOIRQ, &pcm_config_capture);
494 if (pcm && !pcm_is_ready(pcm)) {
495 ALOGW("%s: %s", __func__, pcm_get_error(pcm));
496 pcm_close(pcm);
497 pcm = NULL;
498 configure_proxy_capture(mixer, 0);
Sujin Panickeree5c40c2019-03-29 13:04:37 +0530499 pthread_cond_wait(&cond, &lock);
Eric Laurentc4aef752013-09-12 17:45:53 -0700500 } else {
501 capture_enabled = true;
502 ALOGD("%s: capture ENABLED", __func__);
503 }
504 }
505 }
506 } else {
507 if (capture_enabled) {
508 if (pcm != NULL)
509 pcm_close(pcm);
510 configure_proxy_capture(mixer, 0);
511 ALOGD("%s: capture DISABLED", __func__);
512 capture_enabled = false;
513 }
514 pthread_cond_wait(&cond, &lock);
515 }
516 if (!capture_enabled)
517 continue;
518
519 pthread_mutex_unlock(&lock);
520 ret = pcm_mmap_read(pcm, data, sizeof(data));
521 pthread_mutex_lock(&lock);
522
523 if (ret == 0) {
524 struct listnode *out_node;
525
526 list_for_each(out_node, &active_outputs_list) {
527 output_context_t *out_ctxt = node_to_item(out_node,
528 output_context_t,
529 outputs_list_node);
530 struct listnode *fx_node;
531
532 list_for_each(fx_node, &out_ctxt->effects_list) {
533 effect_context_t *fx_ctxt = node_to_item(fx_node,
534 effect_context_t,
535 output_node);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800536 if (fx_ctxt->ops.process != NULL)
537 fx_ctxt->ops.process(fx_ctxt, &buf, &buf);
Eric Laurentc4aef752013-09-12 17:45:53 -0700538 }
539 }
540 } else {
541 ALOGW("%s: read status %d %s", __func__, ret, pcm_get_error(pcm));
542 }
543 }
544
545 if (capture_enabled) {
546 if (pcm != NULL)
547 pcm_close(pcm);
548 configure_proxy_capture(mixer, 0);
549 }
550 mixer_close(mixer);
551 pthread_mutex_unlock(&lock);
552
553 ALOGD("thread exit");
554
555 return NULL;
556}
557
558/*
559 * Interface from audio HAL
560 */
561
562__attribute__ ((visibility ("default")))
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800563int visualizer_hal_start_output(audio_io_handle_t output, int pcm_id) {
Dhananjay Kumar5f90a5a2017-04-25 20:55:27 +0530564 int ret = 0;
Eric Laurentc4aef752013-09-12 17:45:53 -0700565 struct listnode *node;
566
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800567 ALOGV("%s output %d pcm_id %d", __func__, output, pcm_id);
Eric Laurentc4aef752013-09-12 17:45:53 -0700568
569 if (lib_init() != 0)
570 return init_status;
571
572 pthread_mutex_lock(&thread_lock);
573 pthread_mutex_lock(&lock);
574 if (get_output(output) != NULL) {
575 ALOGW("%s output already started", __func__);
576 ret = -ENOSYS;
577 goto exit;
578 }
579
580 output_context_t *out_ctxt = (output_context_t *)malloc(sizeof(output_context_t));
wjiangebb69fa2014-05-15 19:38:26 +0800581 if (out_ctxt == NULL) {
582 ALOGE("%s fail to allocate memory", __func__);
583 ret = -ENOMEM;
584 goto exit;
585 }
Eric Laurentc4aef752013-09-12 17:45:53 -0700586 out_ctxt->handle = output;
587 list_init(&out_ctxt->effects_list);
588
589 list_for_each(node, &created_effects_list) {
590 effect_context_t *fx_ctxt = node_to_item(node,
591 effect_context_t,
592 effects_list_node);
593 if (fx_ctxt->out_handle == output) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800594 if (fx_ctxt->ops.start)
595 fx_ctxt->ops.start(fx_ctxt, out_ctxt);
Eric Laurentc4aef752013-09-12 17:45:53 -0700596 list_add_tail(&out_ctxt->effects_list, &fx_ctxt->output_node);
597 }
598 }
599 if (list_empty(&active_outputs_list)) {
600 exit_thread = false;
601 thread_status = pthread_create(&capture_thread, (const pthread_attr_t *) NULL,
602 capture_thread_loop, NULL);
603 }
604 list_add_tail(&active_outputs_list, &out_ctxt->outputs_list_node);
605 pthread_cond_signal(&cond);
606
607exit:
608 pthread_mutex_unlock(&lock);
609 pthread_mutex_unlock(&thread_lock);
610 return ret;
611}
612
613__attribute__ ((visibility ("default")))
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800614int visualizer_hal_stop_output(audio_io_handle_t output, int pcm_id) {
Dhananjay Kumar5f90a5a2017-04-25 20:55:27 +0530615 int ret = 0;
Eric Laurentc4aef752013-09-12 17:45:53 -0700616 struct listnode *node;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800617 struct listnode *fx_node;
Eric Laurentc4aef752013-09-12 17:45:53 -0700618 output_context_t *out_ctxt;
619
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800620 ALOGV("%s output %d pcm_id %d", __func__, output, pcm_id);
Eric Laurentc4aef752013-09-12 17:45:53 -0700621
622 if (lib_init() != 0)
623 return init_status;
624
625 pthread_mutex_lock(&thread_lock);
626 pthread_mutex_lock(&lock);
627
628 out_ctxt = get_output(output);
629 if (out_ctxt == NULL) {
630 ALOGW("%s output not started", __func__);
631 ret = -ENOSYS;
632 goto exit;
633 }
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -0800634 list_for_each(fx_node, &out_ctxt->effects_list) {
635 effect_context_t *fx_ctxt = node_to_item(fx_node,
636 effect_context_t,
637 output_node);
638 if (fx_ctxt->ops.stop)
639 fx_ctxt->ops.stop(fx_ctxt, out_ctxt);
640 }
Eric Laurentc4aef752013-09-12 17:45:53 -0700641 list_remove(&out_ctxt->outputs_list_node);
642 pthread_cond_signal(&cond);
643
644 if (list_empty(&active_outputs_list)) {
645 if (thread_status == 0) {
646 exit_thread = true;
647 pthread_cond_signal(&cond);
648 pthread_mutex_unlock(&lock);
649 pthread_join(capture_thread, (void **) NULL);
650 pthread_mutex_lock(&lock);
651 thread_status = -1;
652 }
653 }
654
655 free(out_ctxt);
656
657exit:
658 pthread_mutex_unlock(&lock);
659 pthread_mutex_unlock(&thread_lock);
660 return ret;
661}
662
663
664/*
665 * Effect operations
666 */
667
668int set_config(effect_context_t *context, effect_config_t *config)
669{
670 if (config->inputCfg.samplingRate != config->outputCfg.samplingRate) return -EINVAL;
671 if (config->inputCfg.channels != config->outputCfg.channels) return -EINVAL;
672 if (config->inputCfg.format != config->outputCfg.format) return -EINVAL;
Eric Laurentc4aef752013-09-12 17:45:53 -0700673 if (config->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
674 config->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
675 if (config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
676
677 context->config = *config;
678
679 if (context->ops.reset)
680 context->ops.reset(context);
681
682 return 0;
683}
684
685void get_config(effect_context_t *context, effect_config_t *config)
686{
687 *config = context->config;
688}
689
690
691/*
692 * Visualizer operations
693 */
694
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700695uint32_t visualizer_get_delta_time_ms_from_updated_time(visualizer_context_t* visu_ctxt) {
696 uint32_t delta_ms = 0;
697 if (visu_ctxt->buffer_update_time.tv_sec != 0) {
698 struct timespec ts;
699 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
700 time_t secs = ts.tv_sec - visu_ctxt->buffer_update_time.tv_sec;
701 long nsec = ts.tv_nsec - visu_ctxt->buffer_update_time.tv_nsec;
702 if (nsec < 0) {
703 --secs;
704 nsec += 1000000000;
705 }
706 delta_ms = secs * 1000 + nsec / 1000000;
707 }
708 }
709 return delta_ms;
710}
711
Eric Laurentc4aef752013-09-12 17:45:53 -0700712int visualizer_reset(effect_context_t *context)
713{
714 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
715
716 visu_ctxt->capture_idx = 0;
717 visu_ctxt->last_capture_idx = 0;
718 visu_ctxt->buffer_update_time.tv_sec = 0;
719 visu_ctxt->latency = DSP_OUTPUT_LATENCY_MS;
720 memset(visu_ctxt->capture_buf, 0x80, CAPTURE_BUF_SIZE);
721 return 0;
722}
723
724int visualizer_init(effect_context_t *context)
725{
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700726 int32_t i;
727
Eric Laurentc4aef752013-09-12 17:45:53 -0700728 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
729
730 context->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
731 context->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
732 context->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
733 context->config.inputCfg.samplingRate = 44100;
734 context->config.inputCfg.bufferProvider.getBuffer = NULL;
735 context->config.inputCfg.bufferProvider.releaseBuffer = NULL;
736 context->config.inputCfg.bufferProvider.cookie = NULL;
737 context->config.inputCfg.mask = EFFECT_CONFIG_ALL;
738 context->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
739 context->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
740 context->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
741 context->config.outputCfg.samplingRate = 44100;
742 context->config.outputCfg.bufferProvider.getBuffer = NULL;
743 context->config.outputCfg.bufferProvider.releaseBuffer = NULL;
744 context->config.outputCfg.bufferProvider.cookie = NULL;
745 context->config.outputCfg.mask = EFFECT_CONFIG_ALL;
746
747 visu_ctxt->capture_size = VISUALIZER_CAPTURE_SIZE_MAX;
748 visu_ctxt->scaling_mode = VISUALIZER_SCALING_MODE_NORMALIZED;
749
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700750 // measurement initialization
751 visu_ctxt->channel_count = popcount(context->config.inputCfg.channels);
752 visu_ctxt->meas_mode = MEASUREMENT_MODE_NONE;
753 visu_ctxt->meas_wndw_size_in_buffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
754 visu_ctxt->meas_buffer_idx = 0;
755 for (i=0 ; i<visu_ctxt->meas_wndw_size_in_buffers ; i++) {
756 visu_ctxt->past_meas[i].is_valid = false;
757 visu_ctxt->past_meas[i].peak_u16 = 0;
758 visu_ctxt->past_meas[i].rms_squared = 0;
759 }
760
Eric Laurentc4aef752013-09-12 17:45:53 -0700761 set_config(context, &context->config);
762
Ravi Kumar Alamanda518bcbb2014-11-14 16:51:10 -0800763 if (acdb_handle == NULL) {
764 acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
765 if (acdb_handle == NULL) {
766 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
767 } else {
768 acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(acdb_handle,
769 "acdb_loader_send_audio_cal");
770 if (!acdb_send_audio_cal)
771 ALOGE("%s: Could not find the symbol acdb_send_audio_cal from %s",
772 __func__, LIB_ACDB_LOADER);
773 }
774 }
775
Eric Laurentc4aef752013-09-12 17:45:53 -0700776 return 0;
777}
778
779int visualizer_get_parameter(effect_context_t *context, effect_param_t *p, uint32_t *size)
780{
781 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
782
783 p->status = 0;
784 *size = sizeof(effect_param_t) + sizeof(uint32_t);
785 if (p->psize != sizeof(uint32_t)) {
786 p->status = -EINVAL;
787 return 0;
788 }
789 switch (*(uint32_t *)p->data) {
790 case VISUALIZER_PARAM_CAPTURE_SIZE:
791 ALOGV("%s get capture_size = %d", __func__, visu_ctxt->capture_size);
792 *((uint32_t *)p->data + 1) = visu_ctxt->capture_size;
793 p->vsize = sizeof(uint32_t);
794 *size += sizeof(uint32_t);
795 break;
796 case VISUALIZER_PARAM_SCALING_MODE:
797 ALOGV("%s get scaling_mode = %d", __func__, visu_ctxt->scaling_mode);
798 *((uint32_t *)p->data + 1) = visu_ctxt->scaling_mode;
799 p->vsize = sizeof(uint32_t);
800 *size += sizeof(uint32_t);
801 break;
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700802 case VISUALIZER_PARAM_MEASUREMENT_MODE:
803 ALOGV("%s get meas_mode = %d", __func__, visu_ctxt->meas_mode);
804 *((uint32_t *)p->data + 1) = visu_ctxt->meas_mode;
805 p->vsize = sizeof(uint32_t);
806 *size += sizeof(uint32_t);
807 break;
Eric Laurentc4aef752013-09-12 17:45:53 -0700808 default:
809 p->status = -EINVAL;
810 }
811 return 0;
812}
813
814int visualizer_set_parameter(effect_context_t *context, effect_param_t *p, uint32_t size)
815{
816 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
817
818 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t))
819 return -EINVAL;
820
821 switch (*(uint32_t *)p->data) {
822 case VISUALIZER_PARAM_CAPTURE_SIZE:
823 visu_ctxt->capture_size = *((uint32_t *)p->data + 1);
824 ALOGV("%s set capture_size = %d", __func__, visu_ctxt->capture_size);
825 break;
826 case VISUALIZER_PARAM_SCALING_MODE:
827 visu_ctxt->scaling_mode = *((uint32_t *)p->data + 1);
828 ALOGV("%s set scaling_mode = %d", __func__, visu_ctxt->scaling_mode);
829 break;
830 case VISUALIZER_PARAM_LATENCY:
831 /* Ignore latency as we capture at DSP output
832 * visu_ctxt->latency = *((uint32_t *)p->data + 1); */
833 ALOGV("%s set latency = %d", __func__, visu_ctxt->latency);
834 break;
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700835 case VISUALIZER_PARAM_MEASUREMENT_MODE:
836 visu_ctxt->meas_mode = *((uint32_t *)p->data + 1);
837 ALOGV("%s set meas_mode = %d", __func__, visu_ctxt->meas_mode);
838 break;
Eric Laurentc4aef752013-09-12 17:45:53 -0700839 default:
840 return -EINVAL;
841 }
842 return 0;
843}
844
845/* Real process function called from capture thread. Called with lock held */
846int visualizer_process(effect_context_t *context,
847 audio_buffer_t *inBuffer,
848 audio_buffer_t *outBuffer)
849{
850 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
851
852 if (!effect_exists(context))
853 return -EINVAL;
854
855 if (inBuffer == NULL || inBuffer->raw == NULL ||
856 outBuffer == NULL || outBuffer->raw == NULL ||
857 inBuffer->frameCount != outBuffer->frameCount ||
858 inBuffer->frameCount == 0) {
859 return -EINVAL;
860 }
861
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700862 // perform measurements if needed
863 if (visu_ctxt->meas_mode & MEASUREMENT_MODE_PEAK_RMS) {
864 // find the peak and RMS squared for the new buffer
865 uint32_t inIdx;
866 int16_t max_sample = 0;
867 float rms_squared_acc = 0;
868 for (inIdx = 0 ; inIdx < inBuffer->frameCount * visu_ctxt->channel_count ; inIdx++) {
869 if (inBuffer->s16[inIdx] > max_sample) {
870 max_sample = inBuffer->s16[inIdx];
871 } else if (-inBuffer->s16[inIdx] > max_sample) {
872 max_sample = -inBuffer->s16[inIdx];
873 }
874 rms_squared_acc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]);
875 }
876 // store the measurement
877 visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].peak_u16 = (uint16_t)max_sample;
878 visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].rms_squared =
879 rms_squared_acc / (inBuffer->frameCount * visu_ctxt->channel_count);
880 visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].is_valid = true;
881 if (++visu_ctxt->meas_buffer_idx >= visu_ctxt->meas_wndw_size_in_buffers) {
882 visu_ctxt->meas_buffer_idx = 0;
883 }
884 }
885
Eric Laurentc4aef752013-09-12 17:45:53 -0700886 /* all code below assumes stereo 16 bit PCM output and input */
887 int32_t shift;
888
889 if (visu_ctxt->scaling_mode == VISUALIZER_SCALING_MODE_NORMALIZED) {
890 /* derive capture scaling factor from peak value in current buffer
891 * this gives more interesting captures for display. */
892 shift = 32;
893 int len = inBuffer->frameCount * 2;
894 int i;
895 for (i = 0; i < len; i++) {
896 int32_t smp = inBuffer->s16[i];
897 if (smp < 0) smp = -smp - 1; /* take care to keep the max negative in range */
898 int32_t clz = __builtin_clz(smp);
899 if (shift > clz) shift = clz;
900 }
901 /* A maximum amplitude signal will have 17 leading zeros, which we want to
902 * translate to a shift of 8 (for converting 16 bit to 8 bit) */
903 shift = 25 - shift;
904 /* Never scale by less than 8 to avoid returning unaltered PCM signal. */
905 if (shift < 3) {
906 shift = 3;
907 }
908 /* add one to combine the division by 2 needed after summing
909 * left and right channels below */
910 shift++;
911 } else {
912 assert(visu_ctxt->scaling_mode == VISUALIZER_SCALING_MODE_AS_PLAYED);
913 shift = 9;
914 }
915
916 uint32_t capt_idx;
917 uint32_t in_idx;
918 uint8_t *buf = visu_ctxt->capture_buf;
919 for (in_idx = 0, capt_idx = visu_ctxt->capture_idx;
920 in_idx < inBuffer->frameCount;
921 in_idx++, capt_idx++) {
922 if (capt_idx >= CAPTURE_BUF_SIZE) {
923 /* wrap around */
924 capt_idx = 0;
925 }
926 int32_t smp = inBuffer->s16[2 * in_idx] + inBuffer->s16[2 * in_idx + 1];
927 smp = smp >> shift;
928 buf[capt_idx] = ((uint8_t)smp)^0x80;
929 }
930
931 /* XXX the following two should really be atomic, though it probably doesn't
932 * matter much for visualization purposes */
933 visu_ctxt->capture_idx = capt_idx;
934 /* update last buffer update time stamp */
935 if (clock_gettime(CLOCK_MONOTONIC, &visu_ctxt->buffer_update_time) < 0) {
936 visu_ctxt->buffer_update_time.tv_sec = 0;
937 }
938
939 if (context->state != EFFECT_STATE_ACTIVE) {
940 ALOGV("%s DONE inactive", __func__);
941 return -ENODATA;
942 }
943
944 return 0;
945}
946
947int visualizer_command(effect_context_t * context, uint32_t cmdCode, uint32_t cmdSize,
948 void *pCmdData, uint32_t *replySize, void *pReplyData)
949{
950 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
951
952 switch (cmdCode) {
953 case VISUALIZER_CMD_CAPTURE:
954 if (pReplyData == NULL || *replySize != visu_ctxt->capture_size) {
955 ALOGV("%s VISUALIZER_CMD_CAPTURE error *replySize %d context->capture_size %d",
956 __func__, *replySize, visu_ctxt->capture_size);
957 return -EINVAL;
958 }
959
960 if (!context->offload_enabled)
961 break;
962
963 if (context->state == EFFECT_STATE_ACTIVE) {
964 int32_t latency_ms = visu_ctxt->latency;
Mingshu Pang3c694b42019-12-10 13:59:00 +0800965 const int32_t delta_ms = visualizer_get_delta_time_ms_from_updated_time(visu_ctxt);
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700966 latency_ms -= delta_ms;
967 if (latency_ms < 0) {
968 latency_ms = 0;
Eric Laurentc4aef752013-09-12 17:45:53 -0700969 }
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700970 const uint32_t delta_smp = context->config.inputCfg.samplingRate * latency_ms / 1000;
Eric Laurentc4aef752013-09-12 17:45:53 -0700971
Mingshu Pang3c694b42019-12-10 13:59:00 +0800972 int64_t capture_point = visu_ctxt->capture_idx;
973 capture_point -= visu_ctxt->capture_size;
974 capture_point -= delta_smp;
Weiyin Jiangacf12bb2019-11-21 16:54:14 +0800975 int64_t capture_size = visu_ctxt->capture_size;
Eric Laurentc4aef752013-09-12 17:45:53 -0700976 if (capture_point < 0) {
Weiyin Jiangacf12bb2019-11-21 16:54:14 +0800977 int64_t size = -capture_point;
Eric Laurentc4aef752013-09-12 17:45:53 -0700978 if (size > capture_size)
979 size = capture_size;
980
981 memcpy(pReplyData,
982 visu_ctxt->capture_buf + CAPTURE_BUF_SIZE + capture_point,
983 size);
984 pReplyData = (void *)((size_t)pReplyData + size);
985 capture_size -= size;
986 capture_point = 0;
987 }
988 memcpy(pReplyData,
989 visu_ctxt->capture_buf + capture_point,
990 capture_size);
991
992
993 /* if audio framework has stopped playing audio although the effect is still
994 * active we must clear the capture buffer to return silence */
995 if ((visu_ctxt->last_capture_idx == visu_ctxt->capture_idx) &&
996 (visu_ctxt->buffer_update_time.tv_sec != 0)) {
997 if (delta_ms > MAX_STALL_TIME_MS) {
998 ALOGV("%s capture going to idle", __func__);
999 visu_ctxt->buffer_update_time.tv_sec = 0;
1000 memset(pReplyData, 0x80, visu_ctxt->capture_size);
1001 }
1002 }
1003 visu_ctxt->last_capture_idx = visu_ctxt->capture_idx;
1004 } else {
1005 memset(pReplyData, 0x80, visu_ctxt->capture_size);
1006 }
1007 break;
1008
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -07001009 case VISUALIZER_CMD_MEASURE: {
ragod8c80c22016-08-22 17:59:38 -07001010 if (pReplyData == NULL || replySize == NULL ||
1011 *replySize < (sizeof(int32_t) * MEASUREMENT_COUNT)) {
rago95b51a52016-10-07 18:13:29 -07001012 if (replySize == NULL) {
1013 ALOGV("%s VISUALIZER_CMD_MEASURE error replySize NULL", __func__);
1014 } else {
1015 ALOGV("%s VISUALIZER_CMD_MEASURE error *replySize %u <"
1016 "(sizeof(int32_t) * MEASUREMENT_COUNT) %zu",
1017 __func__, *replySize, sizeof(int32_t) * MEASUREMENT_COUNT);
1018 }
ragod8c80c22016-08-22 17:59:38 -07001019 android_errorWriteLog(0x534e4554, "30229821");
1020 return -EINVAL;
1021 }
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -07001022 uint16_t peak_u16 = 0;
1023 float sum_rms_squared = 0.0f;
1024 uint8_t nb_valid_meas = 0;
1025 /* reset measurements if last measurement was too long ago (which implies stored
1026 * measurements aren't relevant anymore and shouldn't bias the new one) */
1027 const int32_t delay_ms = visualizer_get_delta_time_ms_from_updated_time(visu_ctxt);
1028 if (delay_ms > DISCARD_MEASUREMENTS_TIME_MS) {
1029 uint32_t i;
1030 ALOGV("Discarding measurements, last measurement is %dms old", delay_ms);
1031 for (i=0 ; i<visu_ctxt->meas_wndw_size_in_buffers ; i++) {
1032 visu_ctxt->past_meas[i].is_valid = false;
1033 visu_ctxt->past_meas[i].peak_u16 = 0;
1034 visu_ctxt->past_meas[i].rms_squared = 0;
1035 }
1036 visu_ctxt->meas_buffer_idx = 0;
1037 } else {
1038 /* only use actual measurements, otherwise the first RMS measure happening before
1039 * MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
1040 * low */
1041 uint32_t i;
1042 for (i=0 ; i < visu_ctxt->meas_wndw_size_in_buffers ; i++) {
1043 if (visu_ctxt->past_meas[i].is_valid) {
1044 if (visu_ctxt->past_meas[i].peak_u16 > peak_u16) {
1045 peak_u16 = visu_ctxt->past_meas[i].peak_u16;
1046 }
1047 sum_rms_squared += visu_ctxt->past_meas[i].rms_squared;
1048 nb_valid_meas++;
1049 }
1050 }
1051 }
1052 float rms = nb_valid_meas == 0 ? 0.0f : sqrtf(sum_rms_squared / nb_valid_meas);
1053 int32_t* p_int_reply_data = (int32_t*)pReplyData;
1054 /* convert from I16 sample values to mB and write results */
1055 if (rms < 0.000016f) {
1056 p_int_reply_data[MEASUREMENT_IDX_RMS] = -9600; //-96dB
1057 } else {
1058 p_int_reply_data[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
1059 }
1060 if (peak_u16 == 0) {
1061 p_int_reply_data[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
1062 } else {
1063 p_int_reply_data[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peak_u16 / 32767.0f));
1064 }
1065 ALOGV("VISUALIZER_CMD_MEASURE peak=%d (%dmB), rms=%.1f (%dmB)",
1066 peak_u16, p_int_reply_data[MEASUREMENT_IDX_PEAK],
1067 rms, p_int_reply_data[MEASUREMENT_IDX_RMS]);
1068 }
1069 break;
1070
Eric Laurentc4aef752013-09-12 17:45:53 -07001071 default:
1072 ALOGW("%s invalid command %d", __func__, cmdCode);
1073 return -EINVAL;
1074 }
1075 return 0;
1076}
1077
1078
1079/*
1080 * Effect Library Interface Implementation
1081 */
1082
1083int effect_lib_create(const effect_uuid_t *uuid,
1084 int32_t sessionId,
1085 int32_t ioId,
1086 effect_handle_t *pHandle) {
1087 int ret;
1088 int i;
1089
1090 if (lib_init() != 0)
1091 return init_status;
1092
1093 if (pHandle == NULL || uuid == NULL)
1094 return -EINVAL;
1095
1096 for (i = 0; descriptors[i] != NULL; i++) {
1097 if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0)
1098 break;
1099 }
1100
1101 if (descriptors[i] == NULL)
1102 return -EINVAL;
1103
1104 effect_context_t *context;
1105 if (memcmp(uuid, &visualizer_descriptor.uuid, sizeof(effect_uuid_t)) == 0) {
1106 visualizer_context_t *visu_ctxt = (visualizer_context_t *)calloc(1,
1107 sizeof(visualizer_context_t));
wjiangebb69fa2014-05-15 19:38:26 +08001108 if (visu_ctxt == NULL) {
1109 ALOGE("%s fail to allocate memory", __func__);
1110 return -ENOMEM;
1111 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001112 context = (effect_context_t *)visu_ctxt;
1113 context->ops.init = visualizer_init;
1114 context->ops.reset = visualizer_reset;
1115 context->ops.process = visualizer_process;
1116 context->ops.set_parameter = visualizer_set_parameter;
1117 context->ops.get_parameter = visualizer_get_parameter;
1118 context->ops.command = visualizer_command;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001119 context->desc = &visualizer_descriptor;
Eric Laurentc4aef752013-09-12 17:45:53 -07001120 } else {
1121 return -EINVAL;
1122 }
1123
1124 context->itfe = &effect_interface;
1125 context->state = EFFECT_STATE_UNINITIALIZED;
1126 context->out_handle = (audio_io_handle_t)ioId;
Eric Laurentc4aef752013-09-12 17:45:53 -07001127
1128 ret = context->ops.init(context);
1129 if (ret < 0) {
1130 ALOGW("%s init failed", __func__);
1131 free(context);
1132 return ret;
1133 }
1134
1135 context->state = EFFECT_STATE_INITIALIZED;
1136
1137 pthread_mutex_lock(&lock);
1138 list_add_tail(&created_effects_list, &context->effects_list_node);
1139 output_context_t *out_ctxt = get_output(ioId);
1140 if (out_ctxt != NULL)
1141 add_effect_to_output(out_ctxt, context);
1142 pthread_mutex_unlock(&lock);
1143
1144 *pHandle = (effect_handle_t)context;
1145
1146 ALOGV("%s created context %p", __func__, context);
1147
1148 return 0;
1149
1150}
1151
1152int effect_lib_release(effect_handle_t handle) {
1153 effect_context_t *context = (effect_context_t *)handle;
1154 int status;
1155
1156 if (lib_init() != 0)
1157 return init_status;
1158
1159 ALOGV("%s context %p", __func__, handle);
1160 pthread_mutex_lock(&lock);
1161 status = -EINVAL;
1162 if (effect_exists(context)) {
1163 output_context_t *out_ctxt = get_output(context->out_handle);
1164 if (out_ctxt != NULL)
1165 remove_effect_from_output(out_ctxt, context);
1166 list_remove(&context->effects_list_node);
1167 if (context->ops.release)
1168 context->ops.release(context);
1169 free(context);
1170 status = 0;
1171 }
1172 pthread_mutex_unlock(&lock);
1173
1174 return status;
1175}
1176
1177int effect_lib_get_descriptor(const effect_uuid_t *uuid,
1178 effect_descriptor_t *descriptor) {
1179 int i;
1180
1181 if (lib_init() != 0)
1182 return init_status;
1183
1184 if (descriptor == NULL || uuid == NULL) {
1185 ALOGV("%s called with NULL pointer", __func__);
1186 return -EINVAL;
1187 }
1188
1189 for (i = 0; descriptors[i] != NULL; i++) {
1190 if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
1191 *descriptor = *descriptors[i];
1192 return 0;
1193 }
1194 }
1195
1196 return -EINVAL;
1197}
1198
1199/*
1200 * Effect Control Interface Implementation
1201 */
1202
1203 /* Stub function for effect interface: never called for offloaded effects */
1204int effect_process(effect_handle_t self,
1205 audio_buffer_t *inBuffer,
1206 audio_buffer_t *outBuffer)
1207{
1208 effect_context_t * context = (effect_context_t *)self;
1209 int status = 0;
1210
1211 ALOGW("%s Called ?????", __func__);
1212
1213 pthread_mutex_lock(&lock);
1214 if (!effect_exists(context)) {
1215 status = -EINVAL;
1216 goto exit;
1217 }
1218
1219 if (context->state != EFFECT_STATE_ACTIVE) {
1220 status = -EINVAL;
1221 goto exit;
1222 }
1223
1224exit:
1225 pthread_mutex_unlock(&lock);
1226 return status;
1227}
1228
1229int effect_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
1230 void *pCmdData, uint32_t *replySize, void *pReplyData)
1231{
1232
1233 effect_context_t * context = (effect_context_t *)self;
1234 int retsize;
1235 int status = 0;
1236
1237 pthread_mutex_lock(&lock);
1238
1239 if (!effect_exists(context)) {
1240 status = -EINVAL;
1241 goto exit;
1242 }
1243
1244 if (context == NULL || context->state == EFFECT_STATE_UNINITIALIZED) {
1245 status = -EINVAL;
1246 goto exit;
1247 }
1248
1249// ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,
1250// "%s command %d cmdSize %d", __func__, cmdCode, cmdSize);
1251
1252 switch (cmdCode) {
1253 case EFFECT_CMD_INIT:
1254 if (pReplyData == NULL || *replySize != sizeof(int)) {
1255 status = -EINVAL;
1256 goto exit;
1257 }
1258 if (context->ops.init)
1259 *(int *) pReplyData = context->ops.init(context);
1260 else
1261 *(int *) pReplyData = 0;
1262 break;
1263 case EFFECT_CMD_SET_CONFIG:
1264 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
1265 || pReplyData == NULL || *replySize != sizeof(int)) {
1266 status = -EINVAL;
1267 goto exit;
1268 }
1269 *(int *) pReplyData = set_config(context, (effect_config_t *) pCmdData);
1270 break;
1271 case EFFECT_CMD_GET_CONFIG:
1272 if (pReplyData == NULL ||
1273 *replySize != sizeof(effect_config_t)) {
1274 status = -EINVAL;
1275 goto exit;
1276 }
1277 if (!context->offload_enabled) {
1278 status = -EINVAL;
1279 goto exit;
1280 }
1281
1282 get_config(context, (effect_config_t *)pReplyData);
1283 break;
1284 case EFFECT_CMD_RESET:
1285 if (context->ops.reset)
1286 context->ops.reset(context);
1287 break;
1288 case EFFECT_CMD_ENABLE:
1289 if (pReplyData == NULL || *replySize != sizeof(int)) {
1290 status = -EINVAL;
1291 goto exit;
1292 }
1293 if (context->state != EFFECT_STATE_INITIALIZED) {
1294 status = -ENOSYS;
1295 goto exit;
1296 }
1297 context->state = EFFECT_STATE_ACTIVE;
1298 if (context->ops.enable)
1299 context->ops.enable(context);
1300 pthread_cond_signal(&cond);
1301 ALOGV("%s EFFECT_CMD_ENABLE", __func__);
1302 *(int *)pReplyData = 0;
1303 break;
1304 case EFFECT_CMD_DISABLE:
1305 if (pReplyData == NULL || *replySize != sizeof(int)) {
1306 status = -EINVAL;
1307 goto exit;
1308 }
1309 if (context->state != EFFECT_STATE_ACTIVE) {
1310 status = -ENOSYS;
1311 goto exit;
1312 }
1313 context->state = EFFECT_STATE_INITIALIZED;
1314 if (context->ops.disable)
1315 context->ops.disable(context);
1316 pthread_cond_signal(&cond);
1317 ALOGV("%s EFFECT_CMD_DISABLE", __func__);
1318 *(int *)pReplyData = 0;
1319 break;
1320 case EFFECT_CMD_GET_PARAM: {
1321 if (pCmdData == NULL ||
1322 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
1323 pReplyData == NULL ||
1324 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
1325 status = -EINVAL;
1326 goto exit;
1327 }
1328 if (!context->offload_enabled) {
1329 status = -EINVAL;
1330 goto exit;
1331 }
1332 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
1333 effect_param_t *p = (effect_param_t *)pReplyData;
1334 if (context->ops.get_parameter)
1335 context->ops.get_parameter(context, p, replySize);
1336 } break;
1337 case EFFECT_CMD_SET_PARAM: {
1338 if (pCmdData == NULL ||
1339 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
1340 pReplyData == NULL || *replySize != sizeof(int32_t)) {
1341 status = -EINVAL;
1342 goto exit;
1343 }
1344 *(int32_t *)pReplyData = 0;
1345 effect_param_t *p = (effect_param_t *)pCmdData;
1346 if (context->ops.set_parameter)
1347 *(int32_t *)pReplyData = context->ops.set_parameter(context, p, *replySize);
1348
1349 } break;
1350 case EFFECT_CMD_SET_DEVICE:
1351 case EFFECT_CMD_SET_VOLUME:
1352 case EFFECT_CMD_SET_AUDIO_MODE:
1353 break;
1354
1355 case EFFECT_CMD_OFFLOAD: {
1356 output_context_t *out_ctxt;
1357
1358 if (cmdSize != sizeof(effect_offload_param_t) || pCmdData == NULL
1359 || pReplyData == NULL || *replySize != sizeof(int)) {
1360 ALOGV("%s EFFECT_CMD_OFFLOAD bad format", __func__);
1361 status = -EINVAL;
1362 break;
1363 }
1364
1365 effect_offload_param_t* offload_param = (effect_offload_param_t*)pCmdData;
1366
1367 ALOGV("%s EFFECT_CMD_OFFLOAD offload %d output %d",
1368 __func__, offload_param->isOffload, offload_param->ioHandle);
1369
1370 *(int *)pReplyData = 0;
1371
1372 context->offload_enabled = offload_param->isOffload;
1373 if (context->out_handle == offload_param->ioHandle)
1374 break;
1375
1376 out_ctxt = get_output(context->out_handle);
1377 if (out_ctxt != NULL)
1378 remove_effect_from_output(out_ctxt, context);
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001379
1380 context->out_handle = offload_param->ioHandle;
Eric Laurentc4aef752013-09-12 17:45:53 -07001381 out_ctxt = get_output(offload_param->ioHandle);
1382 if (out_ctxt != NULL)
1383 add_effect_to_output(out_ctxt, context);
1384
Eric Laurentc4aef752013-09-12 17:45:53 -07001385 } break;
1386
1387
1388 default:
1389 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY && context->ops.command)
1390 status = context->ops.command(context, cmdCode, cmdSize,
1391 pCmdData, replySize, pReplyData);
1392 else {
1393 ALOGW("%s invalid command %d", __func__, cmdCode);
1394 status = -EINVAL;
1395 }
1396 break;
1397 }
1398
1399exit:
1400 pthread_mutex_unlock(&lock);
1401
1402// ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,"%s DONE", __func__);
1403 return status;
1404}
1405
1406/* Effect Control Interface Implementation: get_descriptor */
1407int effect_get_descriptor(effect_handle_t self,
1408 effect_descriptor_t *descriptor)
1409{
1410 effect_context_t *context = (effect_context_t *)self;
1411
1412 if (!effect_exists(context))
1413 return -EINVAL;
1414
1415 if (descriptor == NULL)
1416 return -EINVAL;
1417
1418 *descriptor = *context->desc;
1419
1420 return 0;
1421}
1422
1423/* effect_handle_t interface implementation for visualizer effect */
1424const struct effect_interface_s effect_interface = {
1425 effect_process,
1426 effect_command,
1427 effect_get_descriptor,
1428 NULL,
1429};
1430
1431__attribute__ ((visibility ("default")))
1432audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
1433 tag : AUDIO_EFFECT_LIBRARY_TAG,
1434 version : EFFECT_LIBRARY_API_VERSION,
1435 name : "Visualizer Library",
1436 implementor : "The Android Open Source Project",
1437 create_effect : effect_lib_create,
1438 release_effect : effect_lib_release,
1439 get_descriptor : effect_lib_get_descriptor,
1440};