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