blob: 7b35a778ac2da0ebb0135dcffcd2415a84635e05 [file] [log] [blame]
Aalique Grahame22e49102018-12-18 14:23:57 -08001/* Copyright (c) 2015, 2017, 2019 The Linux Foundation. All rights reserved.
Dhananjay Kumar1c978df2015-09-04 13:44:59 +05302 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29#define LOG_TAG "audio_pp_asphere"
30/*#define LOG_NDEBUG 0*/
31
32#include <errno.h>
33#include <fcntl.h>
34#include <stdlib.h>
35#include <unistd.h>
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080036#include <dlfcn.h>
Dhananjay Kumar1c978df2015-09-04 13:44:59 +053037#include <stdbool.h>
38#include <sys/stat.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080039#include <log/log.h>
Dhananjay Kumar1c978df2015-09-04 13:44:59 +053040#include <cutils/list.h>
41#include <cutils/str_parms.h>
42#include <cutils/properties.h>
43#include <hardware/audio_effect.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053044#include <pthread.h>
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080045#include <audio_feature_manager.h>
Dhananjay Kumar1c978df2015-09-04 13:44:59 +053046#include "bundle.h"
47#include "equalizer.h"
48#include "bass_boost.h"
49#include "virtualizer.h"
50#include "reverb.h"
51#include "asphere.h"
52
53#define ASPHERE_MIXER_NAME "MSM ASphere Set Param"
54
55#define AUDIO_PARAMETER_KEY_ASPHERE_STATUS "asphere_status"
56#define AUDIO_PARAMETER_KEY_ASPHERE_ENABLE "asphere_enable"
57#define AUDIO_PARAMETER_KEY_ASPHERE_STRENGTH "asphere_strength"
58
59#define AUDIO_ASPHERE_EVENT_NODE "/data/misc/audio_pp/event_node"
60
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080061#define PRIMARY_HAL_PATH XSTR(LIB_AUDIO_HAL)
62#define XSTR(x) STR(x)
63#define STR(x) #x
64
Dhananjay Kumar1c978df2015-09-04 13:44:59 +053065enum {
66 ASPHERE_ACTIVE = 0,
67 ASPHERE_SUSPENDED,
68 ASPHERE_ERROR
69};
70
Vatsal Buchac09ae062018-11-14 13:25:08 +053071#ifdef AUDIO_FEATURE_ENABLED_GCOV
72extern void __gcov_flush();
73static void enable_gcov()
74{
75 __gcov_flush();
76}
77#else
78static void enable_gcov()
79{
80}
81#endif
82
Dhananjay Kumar1c978df2015-09-04 13:44:59 +053083struct asphere_module {
84 bool enabled;
85 int status;
86 int strength;
87 pthread_mutex_t lock;
88 int init_status;
89};
90
91static struct asphere_module asphere;
92pthread_once_t asphere_once = PTHREAD_ONCE_INIT;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080093static bool (*is_feature_enabled)(audio_ext_feature);
Dhananjay Kumar1c978df2015-09-04 13:44:59 +053094
95static int asphere_create_app_notification_node(void)
96{
97 int fd;
98 if ((fd = open(AUDIO_ASPHERE_EVENT_NODE, O_CREAT|O_TRUNC|O_WRONLY,
99 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
100 ALOGE("creating notification node failed %d", errno);
101 return -EINVAL;
102 }
103 chmod(AUDIO_ASPHERE_EVENT_NODE, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH);
104 close(fd);
105 ALOGD("%s: successfully created notification node %s",
106 __func__, AUDIO_ASPHERE_EVENT_NODE);
107 return 0;
108}
109
110static int asphere_notify_app(void)
111{
112 int fd;
113 if ((fd = open(AUDIO_ASPHERE_EVENT_NODE, O_TRUNC|O_WRONLY)) < 0) {
114 ALOGE("opening notification node failed %d", errno);
115 return -EINVAL;
116 }
117 close(fd);
118 ALOGD("%s: successfully opened notification node", __func__);
119 return 0;
120}
121
122static int asphere_get_values_from_mixer(void)
123{
Manish Dewangan338c50a2017-09-12 15:22:03 +0530124 int ret = 0;
125 long val[2] = {-1, -1};
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530126 struct mixer_ctl *ctl = NULL;
127 struct mixer *mixer = mixer_open(MIXER_CARD);
128 if (mixer)
129 ctl = mixer_get_ctl_by_name(mixer, ASPHERE_MIXER_NAME);
130 if (!ctl) {
131 ALOGE("%s: could not get ctl for mixer cmd - %s",
132 __func__, ASPHERE_MIXER_NAME);
133 return -EINVAL;
134 }
135 ret = mixer_ctl_get_array(ctl, val, sizeof(val)/sizeof(val[0]));
136 if (!ret) {
137 asphere.enabled = (val[0] == 0) ? false : true;
138 asphere.strength = val[1];
139 }
Aalique Grahame22e49102018-12-18 14:23:57 -0800140 ALOGD("%s: returned %d, enabled:%ld, strength:%ld",
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530141 __func__, ret, val[0], val[1]);
142
143 return ret;
144}
145
146static int asphere_set_values_to_mixer(void)
147{
Manish Dewangan338c50a2017-09-12 15:22:03 +0530148 int ret = 0;
149 long val[2] = {-1, -1};
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530150 struct mixer_ctl *ctl = NULL;
151 struct mixer *mixer = mixer_open(MIXER_CARD);
152 if (mixer)
153 ctl = mixer_get_ctl_by_name(mixer, ASPHERE_MIXER_NAME);
154 if (!ctl) {
155 ALOGE("%s: could not get ctl for mixer cmd - %s",
156 __func__, ASPHERE_MIXER_NAME);
157 return -EINVAL;
158 }
159 val[0] = ((asphere.status == ASPHERE_ACTIVE) && asphere.enabled) ? 1 : 0;
160 val[1] = asphere.strength;
161
162 ret = mixer_ctl_set_array(ctl, val, sizeof(val)/sizeof(val[0]));
Aalique Grahame22e49102018-12-18 14:23:57 -0800163 ALOGD("%s: returned %d, enabled:%ld, strength:%ld",
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530164 __func__, ret, val[0], val[1]);
165
166 return ret;
167}
168
169static void asphere_init_once() {
170 ALOGD("%s", __func__);
171 pthread_mutex_init(&asphere.lock, NULL);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800172
173 if (access(PRIMARY_HAL_PATH, R_OK) == 0) {
174 void *hal_lib_pointer = dlopen(PRIMARY_HAL_PATH, RTLD_NOW);
175 if (hal_lib_pointer == NULL)
176 ALOGE("%s: DLOPEN failed for %s", __func__, PRIMARY_HAL_PATH);
177 else if ((is_feature_enabled = (bool (*)(audio_ext_feature))dlsym(hal_lib_pointer,
Aalique Grahame6e763712019-01-31 16:18:17 -0800178 "audio_feature_manager_is_feature_enabled")) != NULL) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800179 if (is_feature_enabled(AUDIOSPHERE)) {
180 asphere.init_status = 1;
181 asphere_get_values_from_mixer();
182 asphere_create_app_notification_node();
183 return;
184 } else
185 ALOGW("%s: asphere feature not enabled", __func__);
186 } else
187 ALOGE("%s: dlsym failed", __func__);
188 } else
189 ALOGE("%s: not able to acces lib %s ", __func__, PRIMARY_HAL_PATH);
190
191 asphere.init_status = 0;
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530192}
193
194static int asphere_init() {
195 pthread_once(&asphere_once, asphere_init_once);
Vatsal Buchac09ae062018-11-14 13:25:08 +0530196 enable_gcov();
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530197 return asphere.init_status;
198}
199
Weiyin Jiang785f1532019-04-12 14:38:09 +0800200static bool asphere_parms_allowed(struct str_parms *parms)
201{
202 if (str_parms_has_key(parms, AUDIO_PARAMETER_KEY_ASPHERE_ENABLE))
203 return true;
204 if (str_parms_has_key(parms, AUDIO_PARAMETER_KEY_ASPHERE_STRENGTH))
205 return true;
206 if (str_parms_has_key(parms, AUDIO_PARAMETER_KEY_ASPHERE_STATUS))
207 return true;
208
209 return false;
210}
211
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530212void asphere_set_parameters(struct str_parms *parms)
213{
214 int ret = 0;
215 bool enable = false;
216 int strength = -1;
217 char value[32] = {0};
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530218 bool set_enable = false, set_strength = false;
219
Weiyin Jiang785f1532019-04-12 14:38:09 +0800220 if (!asphere_parms_allowed(parms)) {
221 return;
222 }
223
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530224 if (asphere_init() != 1) {
225 ALOGW("%s: init check failed!!!", __func__);
226 return;
227 }
228
229 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_ASPHERE_ENABLE,
230 value, sizeof(value));
231 if (ret > 0) {
232 enable = (atoi(value) == 1) ? true : false;
233 set_enable = true;
234 }
235
236 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_ASPHERE_STRENGTH,
237 value, sizeof(value));
238 if (ret > 0) {
239 strength = atoi(value);
240 if (strength >= 0 && strength <= 1000)
241 set_strength = true;
242 }
243
244 if (set_enable || set_strength) {
245 pthread_mutex_lock(&asphere.lock);
246 asphere.enabled = set_enable ? enable : asphere.enabled;
247 asphere.strength = set_strength ? strength : asphere.strength;
248 ret = asphere_set_values_to_mixer();
249 pthread_mutex_unlock(&asphere.lock);
250 ALOGV("%s: exit ret %d", __func__, ret);
251 }
252}
253
254void asphere_get_parameters(struct str_parms *query,
255 struct str_parms *reply)
256{
257 char value[32] = {0};
Aalique Grahame22e49102018-12-18 14:23:57 -0800258 int ret;
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530259
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530260 if (asphere_init() != 1) {
261 ALOGW("%s: init check failed!!!", __func__);
262 return;
263 }
264
265 ret = str_parms_get_str(query, AUDIO_PARAMETER_KEY_ASPHERE_STATUS,
266 value, sizeof(value));
267 if (ret >= 0) {
268 str_parms_add_int(reply, AUDIO_PARAMETER_KEY_ASPHERE_STATUS,
269 asphere.status);
270 }
271
272 ret = str_parms_get_str(query, AUDIO_PARAMETER_KEY_ASPHERE_ENABLE,
273 value, sizeof(value));
274 if (ret >= 0) {
275 str_parms_add_int(reply, AUDIO_PARAMETER_KEY_ASPHERE_ENABLE,
276 asphere.enabled ? 1 : 0);
277 }
278
279 ret = str_parms_get_str(query, AUDIO_PARAMETER_KEY_ASPHERE_STRENGTH,
280 value, sizeof(value));
281 if (ret >= 0) {
282 str_parms_add_int(reply, AUDIO_PARAMETER_KEY_ASPHERE_STRENGTH,
283 asphere.strength);
284 }
285}
286
287static bool effect_needs_asphere_concurrency_handling(effect_context_t *context)
288{
289 if (memcmp(&context->desc->type,
290 &equalizer_descriptor.type, sizeof(effect_uuid_t)) == 0 ||
291 memcmp(&context->desc->type,
292 &bassboost_descriptor.type, sizeof(effect_uuid_t)) == 0 ||
293 memcmp(&context->desc->type,
294 &virtualizer_descriptor.type, sizeof(effect_uuid_t)) == 0 ||
295 memcmp(&context->desc->type,
296 &ins_preset_reverb_descriptor.type, sizeof(effect_uuid_t)) == 0 ||
297 memcmp(&context->desc->type,
298 &ins_env_reverb_descriptor.type, sizeof(effect_uuid_t)) == 0)
299 return true;
300
301 return false;
302}
303
304void handle_asphere_on_effect_enabled(bool enable,
305 effect_context_t *context,
306 struct listnode *created_effects)
307{
308 struct listnode *node;
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530309
310 ALOGV("%s: effect %0x", __func__, context->desc->type.timeLow);
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530311 if (asphere_init() != 1) {
312 ALOGW("%s: init check failed!!!", __func__);
313 return;
314 }
315
316 if (!effect_needs_asphere_concurrency_handling(context)) {
317 ALOGV("%s: effect %0x, do not need concurrency handling",
318 __func__, context->desc->type.timeLow);
319 return;
320 }
321
322 list_for_each(node, created_effects) {
323 effect_context_t *fx_ctxt = node_to_item(node,
324 effect_context_t,
325 effects_list_node);
326 if (fx_ctxt != NULL &&
327 effect_needs_asphere_concurrency_handling(fx_ctxt) == true &&
328 fx_ctxt != context && effect_is_active(fx_ctxt) == true) {
329 ALOGV("%s: found another effect %0x, skip processing %0x", __func__,
330 fx_ctxt->desc->type.timeLow, context->desc->type.timeLow);
331 return;
332 }
333 }
334 pthread_mutex_lock(&asphere.lock);
335 asphere.status = enable ? ASPHERE_SUSPENDED : ASPHERE_ACTIVE;
336 asphere_set_values_to_mixer();
337 asphere_notify_app();
338 pthread_mutex_unlock(&asphere.lock);
339}