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