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