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