blob: 9b11e20e57e320fa6d7fab09a1af8b8b459feec0 [file] [log] [blame]
Eric Laurentc4aef752013-09-12 17:45:53 -07001/*
2 * Copyright (C) 2013 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 "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>
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
33enum {
34 EFFECT_STATE_UNINITIALIZED,
35 EFFECT_STATE_INITIALIZED,
36 EFFECT_STATE_ACTIVE,
37};
38
39typedef struct effect_context_s effect_context_t;
Haynes Mathew George41f86652014-06-17 14:22:15 -070040typedef struct output_context_s output_context_t;
Eric Laurentc4aef752013-09-12 17:45:53 -070041
42/* effect specific operations. Only the init() and process() operations must be defined.
43 * Others are optional.
44 */
45typedef 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);
Haynes Mathew George41f86652014-06-17 14:22:15 -070051 int (*start)(effect_context_t *context, output_context_t *output);
52 int (*stop)(effect_context_t *context, output_context_t *output);
Eric Laurentc4aef752013-09-12 17:45:53 -070053 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
60struct effect_context_s {
61 const struct effect_interface_s *itfe;
62 struct listnode effects_list_node; /* node in created_effects_list */
63 struct listnode output_node; /* node in output_context_t.effects_list */
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
74typedef 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
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -070087#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
92typedef 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;
Eric Laurentc4aef752013-09-12 17:45:53 -070097
98typedef 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];
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700108 /* 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];
Eric Laurentc4aef752013-09-12 17:45:53 -0700114} visualizer_context_t;
115
116
117extern const struct effect_interface_s effect_interface;
118
119/* Offload visualizer UUID: 7a8044a0-1a71-11e3-a184-0002a5d5c51b */
120const effect_descriptor_t visualizer_descriptor = {
121 {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
122 {0x7a8044a0, 0x1a71, 0x11e3, 0xa184, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
123 EFFECT_CONTROL_API_VERSION,
124 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_HW_ACC_TUNNEL ),
125 0, /* TODO */
126 1,
127 "QCOM MSM offload visualizer",
128 "The Android Open Source Project",
129};
130
131const effect_descriptor_t *descriptors[] = {
132 &visualizer_descriptor,
133 NULL,
134};
135
136
137pthread_once_t once = PTHREAD_ONCE_INIT;
138int init_status;
139
140/* list of created effects. Updated by visualizer_hal_start_output()
141 * and visualizer_hal_stop_output() */
142struct listnode created_effects_list;
143/* list of active output streams. Updated by visualizer_hal_start_output()
144 * and visualizer_hal_stop_output() */
145struct 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 */
149pthread_t capture_thread;
150/* lock must be held when modifying or accessing created_effects_list or active_outputs_list */
151pthread_mutex_t lock;
152/* thread_lock must be held when starting or stopping the capture thread.
153 * Locking order: thread_lock -> lock */
154pthread_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. */
157pthread_cond_t cond;
158/* true when requesting the capture thread to exit */
159bool exit_thread;
160/* 0 if the capture thread was created successfully */
161int thread_status;
162
163
164#define DSP_OUTPUT_LATENCY_MS 0 /* Fudge factor for latency after capture point in audio DSP */
165
166/* Retry for delay for mixer open */
167#define RETRY_NUMBER 10
168#define RETRY_US 500000
169
170#define MIXER_CARD 0
171#define SOUND_CARD 0
172#define CAPTURE_DEVICE 8
173
174/* Proxy port supports only MMAP read and those fixed parameters*/
175#define AUDIO_CAPTURE_CHANNEL_COUNT 2
176#define AUDIO_CAPTURE_SMP_RATE 48000
177#define AUDIO_CAPTURE_PERIOD_SIZE (768)
178#define AUDIO_CAPTURE_PERIOD_COUNT 32
179
180struct pcm_config pcm_config_capture = {
181 .channels = AUDIO_CAPTURE_CHANNEL_COUNT,
182 .rate = AUDIO_CAPTURE_SMP_RATE,
183 .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
184 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
185 .format = PCM_FORMAT_S16_LE,
186 .start_threshold = AUDIO_CAPTURE_PERIOD_SIZE / 4,
187 .stop_threshold = INT_MAX,
188 .avail_min = AUDIO_CAPTURE_PERIOD_SIZE / 4,
189};
190
191
192/*
193 * Local functions
194 */
195
196static void init_once() {
197 list_init(&created_effects_list);
198 list_init(&active_outputs_list);
199
200 pthread_mutex_init(&lock, NULL);
201 pthread_mutex_init(&thread_lock, NULL);
202 pthread_cond_init(&cond, NULL);
203 exit_thread = false;
204 thread_status = -1;
205
206 init_status = 0;
207}
208
209int lib_init() {
210 pthread_once(&once, init_once);
211 return init_status;
212}
213
214bool effect_exists(effect_context_t *context) {
215 struct listnode *node;
216
217 list_for_each(node, &created_effects_list) {
218 effect_context_t *fx_ctxt = node_to_item(node,
219 effect_context_t,
220 effects_list_node);
221 if (fx_ctxt == context) {
222 return true;
223 }
224 }
225 return false;
226}
227
228output_context_t *get_output(audio_io_handle_t output) {
229 struct listnode *node;
230
231 list_for_each(node, &active_outputs_list) {
232 output_context_t *out_ctxt = node_to_item(node,
233 output_context_t,
234 outputs_list_node);
235 if (out_ctxt->handle == output) {
236 return out_ctxt;
237 }
238 }
239 return NULL;
240}
241
242void add_effect_to_output(output_context_t * output, effect_context_t *context) {
243 struct listnode *fx_node;
244
245 list_for_each(fx_node, &output->effects_list) {
246 effect_context_t *fx_ctxt = node_to_item(fx_node,
247 effect_context_t,
248 output_node);
249 if (fx_ctxt == context)
250 return;
251 }
252 list_add_tail(&output->effects_list, &context->output_node);
Haynes Mathew George41f86652014-06-17 14:22:15 -0700253 if (context->ops.start)
254 context->ops.start(context, output);
Eric Laurentc4aef752013-09-12 17:45:53 -0700255}
256
257void remove_effect_from_output(output_context_t * output, effect_context_t *context) {
258 struct listnode *fx_node;
259
260 list_for_each(fx_node, &output->effects_list) {
261 effect_context_t *fx_ctxt = node_to_item(fx_node,
262 effect_context_t,
263 output_node);
264 if (fx_ctxt == context) {
Haynes Mathew George41f86652014-06-17 14:22:15 -0700265 if (context->ops.stop)
266 context->ops.stop(context, output);
Eric Laurentc4aef752013-09-12 17:45:53 -0700267 list_remove(&context->output_node);
268 return;
269 }
270 }
271}
272
273bool effects_enabled() {
274 struct listnode *out_node;
275
276 list_for_each(out_node, &active_outputs_list) {
277 struct listnode *fx_node;
278 output_context_t *out_ctxt = node_to_item(out_node,
279 output_context_t,
280 outputs_list_node);
281
282 list_for_each(fx_node, &out_ctxt->effects_list) {
283 effect_context_t *fx_ctxt = node_to_item(fx_node,
284 effect_context_t,
285 output_node);
Haynes Mathew George41f86652014-06-17 14:22:15 -0700286 if (fx_ctxt->state == EFFECT_STATE_ACTIVE && fx_ctxt->ops.process != NULL)
Eric Laurentc4aef752013-09-12 17:45:53 -0700287 return true;
288 }
289 }
290 return false;
291}
292
293int configure_proxy_capture(struct mixer *mixer, int value) {
294 const char *proxy_ctl_name = "AFE_PCM_RX Audio Mixer MultiMedia4";
295 struct mixer_ctl *ctl;
296
297 ctl = mixer_get_ctl_by_name(mixer, proxy_ctl_name);
298 if (ctl == NULL) {
299 ALOGW("%s: could not get %s ctl", __func__, proxy_ctl_name);
300 return -EINVAL;
301 }
302 if (mixer_ctl_set_value(ctl, 0, value) != 0)
303 ALOGW("%s: error setting value %d on %s ", __func__, value, proxy_ctl_name);
304
305 return 0;
306}
307
308
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -0700309void *capture_thread_loop(void *arg __unused)
Eric Laurentc4aef752013-09-12 17:45:53 -0700310{
311 int16_t data[AUDIO_CAPTURE_PERIOD_SIZE * AUDIO_CAPTURE_CHANNEL_COUNT * sizeof(int16_t)];
312 audio_buffer_t buf;
313 buf.frameCount = AUDIO_CAPTURE_PERIOD_SIZE;
314 buf.s16 = data;
315 bool capture_enabled = false;
316 struct mixer *mixer;
317 struct pcm *pcm = NULL;
318 int ret;
319 int retry_num = 0;
320
321 ALOGD("thread enter");
322
323 prctl(PR_SET_NAME, (unsigned long)"visualizer capture", 0, 0, 0);
324
325 pthread_mutex_lock(&lock);
326
327 mixer = mixer_open(MIXER_CARD);
328 while (mixer == NULL && retry_num < RETRY_NUMBER) {
329 usleep(RETRY_US);
330 mixer = mixer_open(MIXER_CARD);
331 retry_num++;
332 }
333 if (mixer == NULL) {
334 pthread_mutex_unlock(&lock);
335 return NULL;
336 }
337
338 for (;;) {
339 if (exit_thread) {
340 break;
341 }
342 if (effects_enabled()) {
343 if (!capture_enabled) {
344 ret = configure_proxy_capture(mixer, 1);
345 if (ret == 0) {
346 pcm = pcm_open(SOUND_CARD, CAPTURE_DEVICE,
347 PCM_IN|PCM_MMAP|PCM_NOIRQ, &pcm_config_capture);
348 if (pcm && !pcm_is_ready(pcm)) {
349 ALOGW("%s: %s", __func__, pcm_get_error(pcm));
350 pcm_close(pcm);
351 pcm = NULL;
352 configure_proxy_capture(mixer, 0);
353 } else {
354 capture_enabled = true;
355 ALOGD("%s: capture ENABLED", __func__);
356 }
357 }
358 }
359 } else {
360 if (capture_enabled) {
361 if (pcm != NULL)
362 pcm_close(pcm);
363 configure_proxy_capture(mixer, 0);
364 ALOGD("%s: capture DISABLED", __func__);
365 capture_enabled = false;
366 }
367 pthread_cond_wait(&cond, &lock);
368 }
369 if (!capture_enabled)
370 continue;
371
372 pthread_mutex_unlock(&lock);
373 ret = pcm_mmap_read(pcm, data, sizeof(data));
374 pthread_mutex_lock(&lock);
375
376 if (ret == 0) {
377 struct listnode *out_node;
378
379 list_for_each(out_node, &active_outputs_list) {
380 output_context_t *out_ctxt = node_to_item(out_node,
381 output_context_t,
382 outputs_list_node);
383 struct listnode *fx_node;
384
385 list_for_each(fx_node, &out_ctxt->effects_list) {
386 effect_context_t *fx_ctxt = node_to_item(fx_node,
387 effect_context_t,
388 output_node);
Haynes Mathew George41f86652014-06-17 14:22:15 -0700389 if (fx_ctxt->ops.process != NULL)
390 fx_ctxt->ops.process(fx_ctxt, &buf, &buf);
Eric Laurentc4aef752013-09-12 17:45:53 -0700391 }
392 }
393 } else {
394 ALOGW("%s: read status %d %s", __func__, ret, pcm_get_error(pcm));
395 }
396 }
397
398 if (capture_enabled) {
399 if (pcm != NULL)
400 pcm_close(pcm);
401 configure_proxy_capture(mixer, 0);
402 }
403 mixer_close(mixer);
404 pthread_mutex_unlock(&lock);
405
406 ALOGD("thread exit");
407
408 return NULL;
409}
410
411/*
412 * Interface from audio HAL
413 */
414
415__attribute__ ((visibility ("default")))
Haynes Mathew George41f86652014-06-17 14:22:15 -0700416int visualizer_hal_start_output(audio_io_handle_t output, int pcm_id) {
Eric Laurentc4aef752013-09-12 17:45:53 -0700417 int ret;
418 struct listnode *node;
419
Haynes Mathew George41f86652014-06-17 14:22:15 -0700420 ALOGV("%s output %d pcm_id %d", __func__, output, pcm_id);
Eric Laurentc4aef752013-09-12 17:45:53 -0700421
422 if (lib_init() != 0)
423 return init_status;
424
425 pthread_mutex_lock(&thread_lock);
426 pthread_mutex_lock(&lock);
427 if (get_output(output) != NULL) {
428 ALOGW("%s output already started", __func__);
429 ret = -ENOSYS;
430 goto exit;
431 }
432
433 output_context_t *out_ctxt = (output_context_t *)malloc(sizeof(output_context_t));
434 out_ctxt->handle = output;
435 list_init(&out_ctxt->effects_list);
436
437 list_for_each(node, &created_effects_list) {
438 effect_context_t *fx_ctxt = node_to_item(node,
439 effect_context_t,
440 effects_list_node);
441 if (fx_ctxt->out_handle == output) {
Haynes Mathew George41f86652014-06-17 14:22:15 -0700442 if (fx_ctxt->ops.start)
443 fx_ctxt->ops.start(fx_ctxt, out_ctxt);
Eric Laurentc4aef752013-09-12 17:45:53 -0700444 list_add_tail(&out_ctxt->effects_list, &fx_ctxt->output_node);
445 }
446 }
447 if (list_empty(&active_outputs_list)) {
448 exit_thread = false;
449 thread_status = pthread_create(&capture_thread, (const pthread_attr_t *) NULL,
450 capture_thread_loop, NULL);
451 }
452 list_add_tail(&active_outputs_list, &out_ctxt->outputs_list_node);
453 pthread_cond_signal(&cond);
454
455exit:
456 pthread_mutex_unlock(&lock);
457 pthread_mutex_unlock(&thread_lock);
458 return ret;
459}
460
461__attribute__ ((visibility ("default")))
Haynes Mathew George41f86652014-06-17 14:22:15 -0700462int visualizer_hal_stop_output(audio_io_handle_t output, int pcm_id) {
Eric Laurentc4aef752013-09-12 17:45:53 -0700463 int ret;
464 struct listnode *node;
Haynes Mathew George41f86652014-06-17 14:22:15 -0700465 struct listnode *fx_node;
Eric Laurentc4aef752013-09-12 17:45:53 -0700466 output_context_t *out_ctxt;
467
Haynes Mathew George41f86652014-06-17 14:22:15 -0700468 ALOGV("%s output %d pcm_id %d", __func__, output, pcm_id);
Eric Laurentc4aef752013-09-12 17:45:53 -0700469
470 if (lib_init() != 0)
471 return init_status;
472
473 pthread_mutex_lock(&thread_lock);
474 pthread_mutex_lock(&lock);
475
476 out_ctxt = get_output(output);
477 if (out_ctxt == NULL) {
478 ALOGW("%s output not started", __func__);
479 ret = -ENOSYS;
480 goto exit;
481 }
Haynes Mathew George41f86652014-06-17 14:22:15 -0700482 list_for_each(fx_node, &out_ctxt->effects_list) {
483 effect_context_t *fx_ctxt = node_to_item(fx_node,
484 effect_context_t,
485 output_node);
486 if (fx_ctxt->ops.stop)
487 fx_ctxt->ops.stop(fx_ctxt, out_ctxt);
488 }
Eric Laurentc4aef752013-09-12 17:45:53 -0700489 list_remove(&out_ctxt->outputs_list_node);
490 pthread_cond_signal(&cond);
491
492 if (list_empty(&active_outputs_list)) {
493 if (thread_status == 0) {
494 exit_thread = true;
495 pthread_cond_signal(&cond);
496 pthread_mutex_unlock(&lock);
497 pthread_join(capture_thread, (void **) NULL);
498 pthread_mutex_lock(&lock);
499 thread_status = -1;
500 }
501 }
502
503 free(out_ctxt);
504
505exit:
506 pthread_mutex_unlock(&lock);
507 pthread_mutex_unlock(&thread_lock);
508 return ret;
509}
510
511
512/*
513 * Effect operations
514 */
515
516int set_config(effect_context_t *context, effect_config_t *config)
517{
518 if (config->inputCfg.samplingRate != config->outputCfg.samplingRate) return -EINVAL;
519 if (config->inputCfg.channels != config->outputCfg.channels) return -EINVAL;
520 if (config->inputCfg.format != config->outputCfg.format) return -EINVAL;
521 if (config->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL;
522 if (config->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
523 config->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
524 if (config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
525
526 context->config = *config;
527
528 if (context->ops.reset)
529 context->ops.reset(context);
530
531 return 0;
532}
533
534void get_config(effect_context_t *context, effect_config_t *config)
535{
536 *config = context->config;
537}
538
539
540/*
541 * Visualizer operations
542 */
543
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700544uint32_t visualizer_get_delta_time_ms_from_updated_time(visualizer_context_t* visu_ctxt) {
545 uint32_t delta_ms = 0;
546 if (visu_ctxt->buffer_update_time.tv_sec != 0) {
547 struct timespec ts;
548 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
549 time_t secs = ts.tv_sec - visu_ctxt->buffer_update_time.tv_sec;
550 long nsec = ts.tv_nsec - visu_ctxt->buffer_update_time.tv_nsec;
551 if (nsec < 0) {
552 --secs;
553 nsec += 1000000000;
554 }
555 delta_ms = secs * 1000 + nsec / 1000000;
556 }
557 }
558 return delta_ms;
559}
560
Eric Laurentc4aef752013-09-12 17:45:53 -0700561int visualizer_reset(effect_context_t *context)
562{
563 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
564
565 visu_ctxt->capture_idx = 0;
566 visu_ctxt->last_capture_idx = 0;
567 visu_ctxt->buffer_update_time.tv_sec = 0;
568 visu_ctxt->latency = DSP_OUTPUT_LATENCY_MS;
569 memset(visu_ctxt->capture_buf, 0x80, CAPTURE_BUF_SIZE);
570 return 0;
571}
572
573int visualizer_init(effect_context_t *context)
574{
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700575 int32_t i;
576
Eric Laurentc4aef752013-09-12 17:45:53 -0700577 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
578
579 context->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
580 context->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
581 context->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
582 context->config.inputCfg.samplingRate = 44100;
583 context->config.inputCfg.bufferProvider.getBuffer = NULL;
584 context->config.inputCfg.bufferProvider.releaseBuffer = NULL;
585 context->config.inputCfg.bufferProvider.cookie = NULL;
586 context->config.inputCfg.mask = EFFECT_CONFIG_ALL;
587 context->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
588 context->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
589 context->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
590 context->config.outputCfg.samplingRate = 44100;
591 context->config.outputCfg.bufferProvider.getBuffer = NULL;
592 context->config.outputCfg.bufferProvider.releaseBuffer = NULL;
593 context->config.outputCfg.bufferProvider.cookie = NULL;
594 context->config.outputCfg.mask = EFFECT_CONFIG_ALL;
595
596 visu_ctxt->capture_size = VISUALIZER_CAPTURE_SIZE_MAX;
597 visu_ctxt->scaling_mode = VISUALIZER_SCALING_MODE_NORMALIZED;
598
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700599 // measurement initialization
Eric Laurent0de8d1f2014-07-01 20:34:45 -0700600 visu_ctxt->channel_count = audio_channel_count_from_out_mask(context->config.inputCfg.channels);
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700601 visu_ctxt->meas_mode = MEASUREMENT_MODE_NONE;
602 visu_ctxt->meas_wndw_size_in_buffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
603 visu_ctxt->meas_buffer_idx = 0;
604 for (i=0 ; i<visu_ctxt->meas_wndw_size_in_buffers ; i++) {
605 visu_ctxt->past_meas[i].is_valid = false;
606 visu_ctxt->past_meas[i].peak_u16 = 0;
607 visu_ctxt->past_meas[i].rms_squared = 0;
608 }
609
Eric Laurentc4aef752013-09-12 17:45:53 -0700610 set_config(context, &context->config);
611
612 return 0;
613}
614
615int visualizer_get_parameter(effect_context_t *context, effect_param_t *p, uint32_t *size)
616{
617 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
618
619 p->status = 0;
620 *size = sizeof(effect_param_t) + sizeof(uint32_t);
621 if (p->psize != sizeof(uint32_t)) {
622 p->status = -EINVAL;
623 return 0;
624 }
625 switch (*(uint32_t *)p->data) {
626 case VISUALIZER_PARAM_CAPTURE_SIZE:
627 ALOGV("%s get capture_size = %d", __func__, visu_ctxt->capture_size);
628 *((uint32_t *)p->data + 1) = visu_ctxt->capture_size;
629 p->vsize = sizeof(uint32_t);
630 *size += sizeof(uint32_t);
631 break;
632 case VISUALIZER_PARAM_SCALING_MODE:
633 ALOGV("%s get scaling_mode = %d", __func__, visu_ctxt->scaling_mode);
634 *((uint32_t *)p->data + 1) = visu_ctxt->scaling_mode;
635 p->vsize = sizeof(uint32_t);
636 *size += sizeof(uint32_t);
637 break;
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700638 case VISUALIZER_PARAM_MEASUREMENT_MODE:
639 ALOGV("%s get meas_mode = %d", __func__, visu_ctxt->meas_mode);
640 *((uint32_t *)p->data + 1) = visu_ctxt->meas_mode;
641 p->vsize = sizeof(uint32_t);
642 *size += sizeof(uint32_t);
643 break;
Eric Laurentc4aef752013-09-12 17:45:53 -0700644 default:
645 p->status = -EINVAL;
646 }
647 return 0;
648}
649
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -0700650int visualizer_set_parameter(effect_context_t *context, effect_param_t *p, uint32_t size __unused)
Eric Laurentc4aef752013-09-12 17:45:53 -0700651{
652 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
653
654 if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t))
655 return -EINVAL;
656
657 switch (*(uint32_t *)p->data) {
658 case VISUALIZER_PARAM_CAPTURE_SIZE:
659 visu_ctxt->capture_size = *((uint32_t *)p->data + 1);
660 ALOGV("%s set capture_size = %d", __func__, visu_ctxt->capture_size);
661 break;
662 case VISUALIZER_PARAM_SCALING_MODE:
663 visu_ctxt->scaling_mode = *((uint32_t *)p->data + 1);
664 ALOGV("%s set scaling_mode = %d", __func__, visu_ctxt->scaling_mode);
665 break;
666 case VISUALIZER_PARAM_LATENCY:
667 /* Ignore latency as we capture at DSP output
668 * visu_ctxt->latency = *((uint32_t *)p->data + 1); */
669 ALOGV("%s set latency = %d", __func__, visu_ctxt->latency);
670 break;
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700671 case VISUALIZER_PARAM_MEASUREMENT_MODE:
672 visu_ctxt->meas_mode = *((uint32_t *)p->data + 1);
673 ALOGV("%s set meas_mode = %d", __func__, visu_ctxt->meas_mode);
674 break;
Eric Laurentc4aef752013-09-12 17:45:53 -0700675 default:
676 return -EINVAL;
677 }
678 return 0;
679}
680
681/* Real process function called from capture thread. Called with lock held */
682int visualizer_process(effect_context_t *context,
683 audio_buffer_t *inBuffer,
684 audio_buffer_t *outBuffer)
685{
686 visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
687
688 if (!effect_exists(context))
689 return -EINVAL;
690
691 if (inBuffer == NULL || inBuffer->raw == NULL ||
692 outBuffer == NULL || outBuffer->raw == NULL ||
693 inBuffer->frameCount != outBuffer->frameCount ||
694 inBuffer->frameCount == 0) {
695 return -EINVAL;
696 }
697
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700698 // perform measurements if needed
699 if (visu_ctxt->meas_mode & MEASUREMENT_MODE_PEAK_RMS) {
700 // find the peak and RMS squared for the new buffer
701 uint32_t inIdx;
702 int16_t max_sample = 0;
703 float rms_squared_acc = 0;
704 for (inIdx = 0 ; inIdx < inBuffer->frameCount * visu_ctxt->channel_count ; inIdx++) {
705 if (inBuffer->s16[inIdx] > max_sample) {
706 max_sample = inBuffer->s16[inIdx];
707 } else if (-inBuffer->s16[inIdx] > max_sample) {
708 max_sample = -inBuffer->s16[inIdx];
709 }
710 rms_squared_acc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]);
711 }
712 // store the measurement
713 visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].peak_u16 = (uint16_t)max_sample;
714 visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].rms_squared =
715 rms_squared_acc / (inBuffer->frameCount * visu_ctxt->channel_count);
716 visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].is_valid = true;
717 if (++visu_ctxt->meas_buffer_idx >= visu_ctxt->meas_wndw_size_in_buffers) {
718 visu_ctxt->meas_buffer_idx = 0;
719 }
720 }
721
Eric Laurentc4aef752013-09-12 17:45:53 -0700722 /* all code below assumes stereo 16 bit PCM output and input */
723 int32_t shift;
724
725 if (visu_ctxt->scaling_mode == VISUALIZER_SCALING_MODE_NORMALIZED) {
726 /* derive capture scaling factor from peak value in current buffer
727 * this gives more interesting captures for display. */
728 shift = 32;
729 int len = inBuffer->frameCount * 2;
730 int i;
731 for (i = 0; i < len; i++) {
732 int32_t smp = inBuffer->s16[i];
733 if (smp < 0) smp = -smp - 1; /* take care to keep the max negative in range */
734 int32_t clz = __builtin_clz(smp);
735 if (shift > clz) shift = clz;
736 }
737 /* A maximum amplitude signal will have 17 leading zeros, which we want to
738 * translate to a shift of 8 (for converting 16 bit to 8 bit) */
739 shift = 25 - shift;
740 /* Never scale by less than 8 to avoid returning unaltered PCM signal. */
741 if (shift < 3) {
742 shift = 3;
743 }
744 /* add one to combine the division by 2 needed after summing
745 * left and right channels below */
746 shift++;
747 } else {
748 assert(visu_ctxt->scaling_mode == VISUALIZER_SCALING_MODE_AS_PLAYED);
749 shift = 9;
750 }
751
752 uint32_t capt_idx;
753 uint32_t in_idx;
754 uint8_t *buf = visu_ctxt->capture_buf;
755 for (in_idx = 0, capt_idx = visu_ctxt->capture_idx;
756 in_idx < inBuffer->frameCount;
757 in_idx++, capt_idx++) {
758 if (capt_idx >= CAPTURE_BUF_SIZE) {
759 /* wrap around */
760 capt_idx = 0;
761 }
762 int32_t smp = inBuffer->s16[2 * in_idx] + inBuffer->s16[2 * in_idx + 1];
763 smp = smp >> shift;
764 buf[capt_idx] = ((uint8_t)smp)^0x80;
765 }
766
767 /* XXX the following two should really be atomic, though it probably doesn't
768 * matter much for visualization purposes */
769 visu_ctxt->capture_idx = capt_idx;
770 /* update last buffer update time stamp */
771 if (clock_gettime(CLOCK_MONOTONIC, &visu_ctxt->buffer_update_time) < 0) {
772 visu_ctxt->buffer_update_time.tv_sec = 0;
773 }
774
775 if (context->state != EFFECT_STATE_ACTIVE) {
776 ALOGV("%s DONE inactive", __func__);
777 return -ENODATA;
778 }
779
780 return 0;
781}
782
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -0700783int visualizer_command(effect_context_t * context, uint32_t cmdCode, uint32_t cmdSize __unused,
784 void *pCmdData __unused, uint32_t *replySize, void *pReplyData)
Eric Laurentc4aef752013-09-12 17:45:53 -0700785{
786 visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
787
788 switch (cmdCode) {
789 case VISUALIZER_CMD_CAPTURE:
790 if (pReplyData == NULL || *replySize != visu_ctxt->capture_size) {
791 ALOGV("%s VISUALIZER_CMD_CAPTURE error *replySize %d context->capture_size %d",
792 __func__, *replySize, visu_ctxt->capture_size);
793 return -EINVAL;
794 }
795
796 if (!context->offload_enabled)
797 break;
798
799 if (context->state == EFFECT_STATE_ACTIVE) {
800 int32_t latency_ms = visu_ctxt->latency;
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700801 const uint32_t delta_ms = visualizer_get_delta_time_ms_from_updated_time(visu_ctxt);
802 latency_ms -= delta_ms;
803 if (latency_ms < 0) {
804 latency_ms = 0;
Eric Laurentc4aef752013-09-12 17:45:53 -0700805 }
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700806 const uint32_t delta_smp = context->config.inputCfg.samplingRate * latency_ms / 1000;
Eric Laurentc4aef752013-09-12 17:45:53 -0700807
808 int32_t capture_point = visu_ctxt->capture_idx - visu_ctxt->capture_size - delta_smp;
809 int32_t capture_size = visu_ctxt->capture_size;
810 if (capture_point < 0) {
811 int32_t size = -capture_point;
812 if (size > capture_size)
813 size = capture_size;
814
815 memcpy(pReplyData,
816 visu_ctxt->capture_buf + CAPTURE_BUF_SIZE + capture_point,
817 size);
818 pReplyData = (void *)((size_t)pReplyData + size);
819 capture_size -= size;
820 capture_point = 0;
821 }
822 memcpy(pReplyData,
823 visu_ctxt->capture_buf + capture_point,
824 capture_size);
825
826
827 /* if audio framework has stopped playing audio although the effect is still
828 * active we must clear the capture buffer to return silence */
829 if ((visu_ctxt->last_capture_idx == visu_ctxt->capture_idx) &&
830 (visu_ctxt->buffer_update_time.tv_sec != 0)) {
831 if (delta_ms > MAX_STALL_TIME_MS) {
832 ALOGV("%s capture going to idle", __func__);
833 visu_ctxt->buffer_update_time.tv_sec = 0;
834 memset(pReplyData, 0x80, visu_ctxt->capture_size);
835 }
836 }
837 visu_ctxt->last_capture_idx = visu_ctxt->capture_idx;
838 } else {
839 memset(pReplyData, 0x80, visu_ctxt->capture_size);
840 }
841 break;
842
Jean-Michel Trivia6c11c12013-09-24 15:08:56 -0700843 case VISUALIZER_CMD_MEASURE: {
844 uint16_t peak_u16 = 0;
845 float sum_rms_squared = 0.0f;
846 uint8_t nb_valid_meas = 0;
847 /* reset measurements if last measurement was too long ago (which implies stored
848 * measurements aren't relevant anymore and shouldn't bias the new one) */
849 const int32_t delay_ms = visualizer_get_delta_time_ms_from_updated_time(visu_ctxt);
850 if (delay_ms > DISCARD_MEASUREMENTS_TIME_MS) {
851 uint32_t i;
852 ALOGV("Discarding measurements, last measurement is %dms old", delay_ms);
853 for (i=0 ; i<visu_ctxt->meas_wndw_size_in_buffers ; i++) {
854 visu_ctxt->past_meas[i].is_valid = false;
855 visu_ctxt->past_meas[i].peak_u16 = 0;
856 visu_ctxt->past_meas[i].rms_squared = 0;
857 }
858 visu_ctxt->meas_buffer_idx = 0;
859 } else {
860 /* only use actual measurements, otherwise the first RMS measure happening before
861 * MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
862 * low */
863 uint32_t i;
864 for (i=0 ; i < visu_ctxt->meas_wndw_size_in_buffers ; i++) {
865 if (visu_ctxt->past_meas[i].is_valid) {
866 if (visu_ctxt->past_meas[i].peak_u16 > peak_u16) {
867 peak_u16 = visu_ctxt->past_meas[i].peak_u16;
868 }
869 sum_rms_squared += visu_ctxt->past_meas[i].rms_squared;
870 nb_valid_meas++;
871 }
872 }
873 }
874 float rms = nb_valid_meas == 0 ? 0.0f : sqrtf(sum_rms_squared / nb_valid_meas);
875 int32_t* p_int_reply_data = (int32_t*)pReplyData;
876 /* convert from I16 sample values to mB and write results */
877 if (rms < 0.000016f) {
878 p_int_reply_data[MEASUREMENT_IDX_RMS] = -9600; //-96dB
879 } else {
880 p_int_reply_data[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
881 }
882 if (peak_u16 == 0) {
883 p_int_reply_data[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
884 } else {
885 p_int_reply_data[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peak_u16 / 32767.0f));
886 }
887 ALOGV("VISUALIZER_CMD_MEASURE peak=%d (%dmB), rms=%.1f (%dmB)",
888 peak_u16, p_int_reply_data[MEASUREMENT_IDX_PEAK],
889 rms, p_int_reply_data[MEASUREMENT_IDX_RMS]);
890 }
891 break;
892
Eric Laurentc4aef752013-09-12 17:45:53 -0700893 default:
894 ALOGW("%s invalid command %d", __func__, cmdCode);
895 return -EINVAL;
896 }
897 return 0;
898}
899
900
901/*
902 * Effect Library Interface Implementation
903 */
904
905int effect_lib_create(const effect_uuid_t *uuid,
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -0700906 int32_t sessionId __unused,
Eric Laurentc4aef752013-09-12 17:45:53 -0700907 int32_t ioId,
908 effect_handle_t *pHandle) {
909 int ret;
910 int i;
911
912 if (lib_init() != 0)
913 return init_status;
914
915 if (pHandle == NULL || uuid == NULL)
916 return -EINVAL;
917
918 for (i = 0; descriptors[i] != NULL; i++) {
919 if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0)
920 break;
921 }
922
923 if (descriptors[i] == NULL)
924 return -EINVAL;
925
926 effect_context_t *context;
927 if (memcmp(uuid, &visualizer_descriptor.uuid, sizeof(effect_uuid_t)) == 0) {
928 visualizer_context_t *visu_ctxt = (visualizer_context_t *)calloc(1,
929 sizeof(visualizer_context_t));
930 context = (effect_context_t *)visu_ctxt;
931 context->ops.init = visualizer_init;
932 context->ops.reset = visualizer_reset;
933 context->ops.process = visualizer_process;
934 context->ops.set_parameter = visualizer_set_parameter;
935 context->ops.get_parameter = visualizer_get_parameter;
936 context->ops.command = visualizer_command;
Haynes Mathew George41f86652014-06-17 14:22:15 -0700937 context->desc = &visualizer_descriptor;
Eric Laurentc4aef752013-09-12 17:45:53 -0700938 } else {
939 return -EINVAL;
940 }
941
942 context->itfe = &effect_interface;
943 context->state = EFFECT_STATE_UNINITIALIZED;
944 context->out_handle = (audio_io_handle_t)ioId;
Eric Laurentc4aef752013-09-12 17:45:53 -0700945
946 ret = context->ops.init(context);
947 if (ret < 0) {
948 ALOGW("%s init failed", __func__);
949 free(context);
950 return ret;
951 }
952
953 context->state = EFFECT_STATE_INITIALIZED;
954
955 pthread_mutex_lock(&lock);
956 list_add_tail(&created_effects_list, &context->effects_list_node);
957 output_context_t *out_ctxt = get_output(ioId);
958 if (out_ctxt != NULL)
959 add_effect_to_output(out_ctxt, context);
960 pthread_mutex_unlock(&lock);
961
962 *pHandle = (effect_handle_t)context;
963
964 ALOGV("%s created context %p", __func__, context);
965
966 return 0;
967
968}
969
970int effect_lib_release(effect_handle_t handle) {
971 effect_context_t *context = (effect_context_t *)handle;
972 int status;
973
974 if (lib_init() != 0)
975 return init_status;
976
977 ALOGV("%s context %p", __func__, handle);
978 pthread_mutex_lock(&lock);
979 status = -EINVAL;
980 if (effect_exists(context)) {
981 output_context_t *out_ctxt = get_output(context->out_handle);
982 if (out_ctxt != NULL)
983 remove_effect_from_output(out_ctxt, context);
984 list_remove(&context->effects_list_node);
985 if (context->ops.release)
986 context->ops.release(context);
987 free(context);
988 status = 0;
989 }
990 pthread_mutex_unlock(&lock);
991
992 return status;
993}
994
995int effect_lib_get_descriptor(const effect_uuid_t *uuid,
996 effect_descriptor_t *descriptor) {
997 int i;
998
999 if (lib_init() != 0)
1000 return init_status;
1001
1002 if (descriptor == NULL || uuid == NULL) {
1003 ALOGV("%s called with NULL pointer", __func__);
1004 return -EINVAL;
1005 }
1006
1007 for (i = 0; descriptors[i] != NULL; i++) {
1008 if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
1009 *descriptor = *descriptors[i];
1010 return 0;
1011 }
1012 }
1013
1014 return -EINVAL;
1015}
1016
1017/*
1018 * Effect Control Interface Implementation
1019 */
1020
1021 /* Stub function for effect interface: never called for offloaded effects */
1022int effect_process(effect_handle_t self,
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001023 audio_buffer_t *inBuffer __unused,
1024 audio_buffer_t *outBuffer __unused)
Eric Laurentc4aef752013-09-12 17:45:53 -07001025{
1026 effect_context_t * context = (effect_context_t *)self;
1027 int status = 0;
1028
1029 ALOGW("%s Called ?????", __func__);
1030
1031 pthread_mutex_lock(&lock);
1032 if (!effect_exists(context)) {
1033 status = -EINVAL;
1034 goto exit;
1035 }
1036
1037 if (context->state != EFFECT_STATE_ACTIVE) {
1038 status = -EINVAL;
1039 goto exit;
1040 }
1041
1042exit:
1043 pthread_mutex_unlock(&lock);
1044 return status;
1045}
1046
1047int effect_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
1048 void *pCmdData, uint32_t *replySize, void *pReplyData)
1049{
1050
1051 effect_context_t * context = (effect_context_t *)self;
1052 int retsize;
1053 int status = 0;
1054
1055 pthread_mutex_lock(&lock);
1056
1057 if (!effect_exists(context)) {
1058 status = -EINVAL;
1059 goto exit;
1060 }
1061
1062 if (context == NULL || context->state == EFFECT_STATE_UNINITIALIZED) {
1063 status = -EINVAL;
1064 goto exit;
1065 }
1066
1067// ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,
1068// "%s command %d cmdSize %d", __func__, cmdCode, cmdSize);
1069
1070 switch (cmdCode) {
1071 case EFFECT_CMD_INIT:
1072 if (pReplyData == NULL || *replySize != sizeof(int)) {
1073 status = -EINVAL;
1074 goto exit;
1075 }
1076 if (context->ops.init)
1077 *(int *) pReplyData = context->ops.init(context);
1078 else
1079 *(int *) pReplyData = 0;
1080 break;
1081 case EFFECT_CMD_SET_CONFIG:
1082 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
1083 || pReplyData == NULL || *replySize != sizeof(int)) {
1084 status = -EINVAL;
1085 goto exit;
1086 }
1087 *(int *) pReplyData = set_config(context, (effect_config_t *) pCmdData);
1088 break;
1089 case EFFECT_CMD_GET_CONFIG:
1090 if (pReplyData == NULL ||
1091 *replySize != sizeof(effect_config_t)) {
1092 status = -EINVAL;
1093 goto exit;
1094 }
1095 if (!context->offload_enabled) {
1096 status = -EINVAL;
1097 goto exit;
1098 }
1099
1100 get_config(context, (effect_config_t *)pReplyData);
1101 break;
1102 case EFFECT_CMD_RESET:
1103 if (context->ops.reset)
1104 context->ops.reset(context);
1105 break;
1106 case EFFECT_CMD_ENABLE:
1107 if (pReplyData == NULL || *replySize != sizeof(int)) {
1108 status = -EINVAL;
1109 goto exit;
1110 }
1111 if (context->state != EFFECT_STATE_INITIALIZED) {
1112 status = -ENOSYS;
1113 goto exit;
1114 }
1115 context->state = EFFECT_STATE_ACTIVE;
1116 if (context->ops.enable)
1117 context->ops.enable(context);
1118 pthread_cond_signal(&cond);
1119 ALOGV("%s EFFECT_CMD_ENABLE", __func__);
1120 *(int *)pReplyData = 0;
1121 break;
1122 case EFFECT_CMD_DISABLE:
1123 if (pReplyData == NULL || *replySize != sizeof(int)) {
1124 status = -EINVAL;
1125 goto exit;
1126 }
1127 if (context->state != EFFECT_STATE_ACTIVE) {
1128 status = -ENOSYS;
1129 goto exit;
1130 }
1131 context->state = EFFECT_STATE_INITIALIZED;
1132 if (context->ops.disable)
1133 context->ops.disable(context);
1134 pthread_cond_signal(&cond);
1135 ALOGV("%s EFFECT_CMD_DISABLE", __func__);
1136 *(int *)pReplyData = 0;
1137 break;
1138 case EFFECT_CMD_GET_PARAM: {
1139 if (pCmdData == NULL ||
1140 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
1141 pReplyData == NULL ||
1142 *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
1143 status = -EINVAL;
1144 goto exit;
1145 }
1146 if (!context->offload_enabled) {
1147 status = -EINVAL;
1148 goto exit;
1149 }
1150 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
1151 effect_param_t *p = (effect_param_t *)pReplyData;
1152 if (context->ops.get_parameter)
1153 context->ops.get_parameter(context, p, replySize);
1154 } break;
1155 case EFFECT_CMD_SET_PARAM: {
1156 if (pCmdData == NULL ||
1157 cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
1158 pReplyData == NULL || *replySize != sizeof(int32_t)) {
1159 status = -EINVAL;
1160 goto exit;
1161 }
1162 *(int32_t *)pReplyData = 0;
1163 effect_param_t *p = (effect_param_t *)pCmdData;
1164 if (context->ops.set_parameter)
1165 *(int32_t *)pReplyData = context->ops.set_parameter(context, p, *replySize);
1166
1167 } break;
1168 case EFFECT_CMD_SET_DEVICE:
1169 case EFFECT_CMD_SET_VOLUME:
1170 case EFFECT_CMD_SET_AUDIO_MODE:
1171 break;
1172
1173 case EFFECT_CMD_OFFLOAD: {
1174 output_context_t *out_ctxt;
1175
1176 if (cmdSize != sizeof(effect_offload_param_t) || pCmdData == NULL
1177 || pReplyData == NULL || *replySize != sizeof(int)) {
1178 ALOGV("%s EFFECT_CMD_OFFLOAD bad format", __func__);
1179 status = -EINVAL;
1180 break;
1181 }
1182
1183 effect_offload_param_t* offload_param = (effect_offload_param_t*)pCmdData;
1184
1185 ALOGV("%s EFFECT_CMD_OFFLOAD offload %d output %d",
1186 __func__, offload_param->isOffload, offload_param->ioHandle);
1187
1188 *(int *)pReplyData = 0;
1189
1190 context->offload_enabled = offload_param->isOffload;
1191 if (context->out_handle == offload_param->ioHandle)
1192 break;
1193
1194 out_ctxt = get_output(context->out_handle);
1195 if (out_ctxt != NULL)
1196 remove_effect_from_output(out_ctxt, context);
Haynes Mathew George41f86652014-06-17 14:22:15 -07001197
1198 context->out_handle = offload_param->ioHandle;
Eric Laurentc4aef752013-09-12 17:45:53 -07001199 out_ctxt = get_output(offload_param->ioHandle);
1200 if (out_ctxt != NULL)
1201 add_effect_to_output(out_ctxt, context);
1202
Eric Laurentc4aef752013-09-12 17:45:53 -07001203 } break;
1204
1205
1206 default:
1207 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY && context->ops.command)
1208 status = context->ops.command(context, cmdCode, cmdSize,
1209 pCmdData, replySize, pReplyData);
1210 else {
1211 ALOGW("%s invalid command %d", __func__, cmdCode);
1212 status = -EINVAL;
1213 }
1214 break;
1215 }
1216
1217exit:
1218 pthread_mutex_unlock(&lock);
1219
1220// ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,"%s DONE", __func__);
1221 return status;
1222}
1223
1224/* Effect Control Interface Implementation: get_descriptor */
1225int effect_get_descriptor(effect_handle_t self,
1226 effect_descriptor_t *descriptor)
1227{
1228 effect_context_t *context = (effect_context_t *)self;
1229
1230 if (!effect_exists(context))
1231 return -EINVAL;
1232
1233 if (descriptor == NULL)
1234 return -EINVAL;
1235
1236 *descriptor = *context->desc;
1237
1238 return 0;
1239}
1240
1241/* effect_handle_t interface implementation for visualizer effect */
1242const struct effect_interface_s effect_interface = {
1243 effect_process,
1244 effect_command,
1245 effect_get_descriptor,
1246 NULL,
1247};
1248
1249__attribute__ ((visibility ("default")))
1250audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
synergy dev19af91c2013-12-17 17:36:30 -08001251 .tag = AUDIO_EFFECT_LIBRARY_TAG,
1252 .version = EFFECT_LIBRARY_API_VERSION,
1253 .name = "Visualizer Library",
1254 .implementor = "The Android Open Source Project",
1255 .create_effect = effect_lib_create,
1256 .release_effect = effect_lib_release,
1257 .get_descriptor = effect_lib_get_descriptor,
Eric Laurentc4aef752013-09-12 17:45:53 -07001258};