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