blob: efe07c68f39ae271024ef9364252de0b76a32a12 [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>
36#include <stdbool.h>
37#include <sys/stat.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080038#include <log/log.h>
Dhananjay Kumar1c978df2015-09-04 13:44:59 +053039#include <cutils/list.h>
40#include <cutils/str_parms.h>
41#include <cutils/properties.h>
42#include <hardware/audio_effect.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053043#include <pthread.h>
Dhananjay Kumar1c978df2015-09-04 13:44:59 +053044#include "bundle.h"
45#include "equalizer.h"
46#include "bass_boost.h"
47#include "virtualizer.h"
48#include "reverb.h"
49#include "asphere.h"
50
51#define ASPHERE_MIXER_NAME "MSM ASphere Set Param"
52
53#define AUDIO_PARAMETER_KEY_ASPHERE_STATUS "asphere_status"
54#define AUDIO_PARAMETER_KEY_ASPHERE_ENABLE "asphere_enable"
55#define AUDIO_PARAMETER_KEY_ASPHERE_STRENGTH "asphere_strength"
56
57#define AUDIO_ASPHERE_EVENT_NODE "/data/misc/audio_pp/event_node"
58
59enum {
60 ASPHERE_ACTIVE = 0,
61 ASPHERE_SUSPENDED,
62 ASPHERE_ERROR
63};
64
Vatsal Buchac09ae062018-11-14 13:25:08 +053065#ifdef AUDIO_FEATURE_ENABLED_GCOV
66extern void __gcov_flush();
67static void enable_gcov()
68{
69 __gcov_flush();
70}
71#else
72static void enable_gcov()
73{
74}
75#endif
76
Dhananjay Kumar1c978df2015-09-04 13:44:59 +053077struct asphere_module {
78 bool enabled;
79 int status;
80 int strength;
81 pthread_mutex_t lock;
82 int init_status;
83};
84
85static struct asphere_module asphere;
86pthread_once_t asphere_once = PTHREAD_ONCE_INIT;
87
88static int asphere_create_app_notification_node(void)
89{
90 int fd;
91 if ((fd = open(AUDIO_ASPHERE_EVENT_NODE, O_CREAT|O_TRUNC|O_WRONLY,
92 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
93 ALOGE("creating notification node failed %d", errno);
94 return -EINVAL;
95 }
96 chmod(AUDIO_ASPHERE_EVENT_NODE, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH);
97 close(fd);
98 ALOGD("%s: successfully created notification node %s",
99 __func__, AUDIO_ASPHERE_EVENT_NODE);
100 return 0;
101}
102
103static int asphere_notify_app(void)
104{
105 int fd;
106 if ((fd = open(AUDIO_ASPHERE_EVENT_NODE, O_TRUNC|O_WRONLY)) < 0) {
107 ALOGE("opening notification node failed %d", errno);
108 return -EINVAL;
109 }
110 close(fd);
111 ALOGD("%s: successfully opened notification node", __func__);
112 return 0;
113}
114
115static int asphere_get_values_from_mixer(void)
116{
Manish Dewangan338c50a2017-09-12 15:22:03 +0530117 int ret = 0;
118 long val[2] = {-1, -1};
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530119 struct mixer_ctl *ctl = NULL;
120 struct mixer *mixer = mixer_open(MIXER_CARD);
121 if (mixer)
122 ctl = mixer_get_ctl_by_name(mixer, ASPHERE_MIXER_NAME);
123 if (!ctl) {
124 ALOGE("%s: could not get ctl for mixer cmd - %s",
125 __func__, ASPHERE_MIXER_NAME);
126 return -EINVAL;
127 }
128 ret = mixer_ctl_get_array(ctl, val, sizeof(val)/sizeof(val[0]));
129 if (!ret) {
130 asphere.enabled = (val[0] == 0) ? false : true;
131 asphere.strength = val[1];
132 }
Aalique Grahame22e49102018-12-18 14:23:57 -0800133 ALOGD("%s: returned %d, enabled:%ld, strength:%ld",
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530134 __func__, ret, val[0], val[1]);
135
136 return ret;
137}
138
139static int asphere_set_values_to_mixer(void)
140{
Manish Dewangan338c50a2017-09-12 15:22:03 +0530141 int ret = 0;
142 long val[2] = {-1, -1};
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530143 struct mixer_ctl *ctl = NULL;
144 struct mixer *mixer = mixer_open(MIXER_CARD);
145 if (mixer)
146 ctl = mixer_get_ctl_by_name(mixer, ASPHERE_MIXER_NAME);
147 if (!ctl) {
148 ALOGE("%s: could not get ctl for mixer cmd - %s",
149 __func__, ASPHERE_MIXER_NAME);
150 return -EINVAL;
151 }
152 val[0] = ((asphere.status == ASPHERE_ACTIVE) && asphere.enabled) ? 1 : 0;
153 val[1] = asphere.strength;
154
155 ret = mixer_ctl_set_array(ctl, val, sizeof(val)/sizeof(val[0]));
Aalique Grahame22e49102018-12-18 14:23:57 -0800156 ALOGD("%s: returned %d, enabled:%ld, strength:%ld",
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530157 __func__, ret, val[0], val[1]);
158
159 return ret;
160}
161
162static void asphere_init_once() {
163 ALOGD("%s", __func__);
164 pthread_mutex_init(&asphere.lock, NULL);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800165
vivek mehtaae1018c2019-05-09 12:19:57 -0700166 if (property_get_bool("vendor.audio.feature.audio_sphere.enable", false)) {
167 asphere.init_status = 1;
168 asphere_get_values_from_mixer();
169 asphere_create_app_notification_node();
170 return;
171 } else {
172 ALOGW("%s: asphere feature not enabled", __func__);
173 }
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800174
175 asphere.init_status = 0;
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530176}
177
178static int asphere_init() {
179 pthread_once(&asphere_once, asphere_init_once);
Vatsal Buchac09ae062018-11-14 13:25:08 +0530180 enable_gcov();
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530181 return asphere.init_status;
182}
183
Weiyin Jiang785f1532019-04-12 14:38:09 +0800184static bool asphere_parms_allowed(struct str_parms *parms)
185{
186 if (str_parms_has_key(parms, AUDIO_PARAMETER_KEY_ASPHERE_ENABLE))
187 return true;
188 if (str_parms_has_key(parms, AUDIO_PARAMETER_KEY_ASPHERE_STRENGTH))
189 return true;
190 if (str_parms_has_key(parms, AUDIO_PARAMETER_KEY_ASPHERE_STATUS))
191 return true;
192
193 return false;
194}
195
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530196void asphere_set_parameters(struct str_parms *parms)
197{
198 int ret = 0;
199 bool enable = false;
200 int strength = -1;
201 char value[32] = {0};
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530202 bool set_enable = false, set_strength = false;
203
Weiyin Jiang785f1532019-04-12 14:38:09 +0800204 if (!asphere_parms_allowed(parms)) {
205 return;
206 }
207
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530208 if (asphere_init() != 1) {
209 ALOGW("%s: init check failed!!!", __func__);
210 return;
211 }
212
213 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_ASPHERE_ENABLE,
214 value, sizeof(value));
215 if (ret > 0) {
216 enable = (atoi(value) == 1) ? true : false;
217 set_enable = true;
218 }
219
220 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_ASPHERE_STRENGTH,
221 value, sizeof(value));
222 if (ret > 0) {
223 strength = atoi(value);
224 if (strength >= 0 && strength <= 1000)
225 set_strength = true;
226 }
227
228 if (set_enable || set_strength) {
229 pthread_mutex_lock(&asphere.lock);
230 asphere.enabled = set_enable ? enable : asphere.enabled;
231 asphere.strength = set_strength ? strength : asphere.strength;
232 ret = asphere_set_values_to_mixer();
233 pthread_mutex_unlock(&asphere.lock);
234 ALOGV("%s: exit ret %d", __func__, ret);
235 }
236}
237
238void asphere_get_parameters(struct str_parms *query,
239 struct str_parms *reply)
240{
241 char value[32] = {0};
Aalique Grahame22e49102018-12-18 14:23:57 -0800242 int ret;
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530243
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530244 if (asphere_init() != 1) {
245 ALOGW("%s: init check failed!!!", __func__);
246 return;
247 }
248
249 ret = str_parms_get_str(query, AUDIO_PARAMETER_KEY_ASPHERE_STATUS,
250 value, sizeof(value));
251 if (ret >= 0) {
252 str_parms_add_int(reply, AUDIO_PARAMETER_KEY_ASPHERE_STATUS,
253 asphere.status);
254 }
255
256 ret = str_parms_get_str(query, AUDIO_PARAMETER_KEY_ASPHERE_ENABLE,
257 value, sizeof(value));
258 if (ret >= 0) {
259 str_parms_add_int(reply, AUDIO_PARAMETER_KEY_ASPHERE_ENABLE,
260 asphere.enabled ? 1 : 0);
261 }
262
263 ret = str_parms_get_str(query, AUDIO_PARAMETER_KEY_ASPHERE_STRENGTH,
264 value, sizeof(value));
265 if (ret >= 0) {
266 str_parms_add_int(reply, AUDIO_PARAMETER_KEY_ASPHERE_STRENGTH,
267 asphere.strength);
268 }
269}
270
271static bool effect_needs_asphere_concurrency_handling(effect_context_t *context)
272{
273 if (memcmp(&context->desc->type,
274 &equalizer_descriptor.type, sizeof(effect_uuid_t)) == 0 ||
275 memcmp(&context->desc->type,
276 &bassboost_descriptor.type, sizeof(effect_uuid_t)) == 0 ||
277 memcmp(&context->desc->type,
278 &virtualizer_descriptor.type, sizeof(effect_uuid_t)) == 0 ||
279 memcmp(&context->desc->type,
280 &ins_preset_reverb_descriptor.type, sizeof(effect_uuid_t)) == 0 ||
281 memcmp(&context->desc->type,
282 &ins_env_reverb_descriptor.type, sizeof(effect_uuid_t)) == 0)
283 return true;
284
285 return false;
286}
287
288void handle_asphere_on_effect_enabled(bool enable,
289 effect_context_t *context,
290 struct listnode *created_effects)
291{
292 struct listnode *node;
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530293
294 ALOGV("%s: effect %0x", __func__, context->desc->type.timeLow);
Dhananjay Kumar1c978df2015-09-04 13:44:59 +0530295 if (asphere_init() != 1) {
296 ALOGW("%s: init check failed!!!", __func__);
297 return;
298 }
299
300 if (!effect_needs_asphere_concurrency_handling(context)) {
301 ALOGV("%s: effect %0x, do not need concurrency handling",
302 __func__, context->desc->type.timeLow);
303 return;
304 }
305
306 list_for_each(node, created_effects) {
307 effect_context_t *fx_ctxt = node_to_item(node,
308 effect_context_t,
309 effects_list_node);
310 if (fx_ctxt != NULL &&
311 effect_needs_asphere_concurrency_handling(fx_ctxt) == true &&
312 fx_ctxt != context && effect_is_active(fx_ctxt) == true) {
313 ALOGV("%s: found another effect %0x, skip processing %0x", __func__,
314 fx_ctxt->desc->type.timeLow, context->desc->type.timeLow);
315 return;
316 }
317 }
318 pthread_mutex_lock(&asphere.lock);
319 asphere.status = enable ? ASPHERE_SUSPENDED : ASPHERE_ACTIVE;
320 asphere_set_values_to_mixer();
321 asphere_notify_app();
322 pthread_mutex_unlock(&asphere.lock);
323}