Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2015, 2017, 2019 The Linux Foundation. All rights reserved. |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 2 | * |
| 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 Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 36 | #include <dlfcn.h> |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 37 | #include <stdbool.h> |
| 38 | #include <sys/stat.h> |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 39 | #include <log/log.h> |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 40 | #include <cutils/list.h> |
| 41 | #include <cutils/str_parms.h> |
| 42 | #include <cutils/properties.h> |
| 43 | #include <hardware/audio_effect.h> |
Vinay Verma | addfa4a | 2018-04-29 14:03:38 +0530 | [diff] [blame] | 44 | #include <pthread.h> |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 45 | #include <audio_feature_manager.h> |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 46 | #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 Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 61 | #define PRIMARY_HAL_PATH XSTR(LIB_AUDIO_HAL) |
| 62 | #define XSTR(x) STR(x) |
| 63 | #define STR(x) #x |
| 64 | |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 65 | enum { |
| 66 | ASPHERE_ACTIVE = 0, |
| 67 | ASPHERE_SUSPENDED, |
| 68 | ASPHERE_ERROR |
| 69 | }; |
| 70 | |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 71 | #ifdef AUDIO_FEATURE_ENABLED_GCOV |
| 72 | extern void __gcov_flush(); |
| 73 | static void enable_gcov() |
| 74 | { |
| 75 | __gcov_flush(); |
| 76 | } |
| 77 | #else |
| 78 | static void enable_gcov() |
| 79 | { |
| 80 | } |
| 81 | #endif |
| 82 | |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 83 | struct asphere_module { |
| 84 | bool enabled; |
| 85 | int status; |
| 86 | int strength; |
| 87 | pthread_mutex_t lock; |
| 88 | int init_status; |
| 89 | }; |
| 90 | |
| 91 | static struct asphere_module asphere; |
| 92 | pthread_once_t asphere_once = PTHREAD_ONCE_INIT; |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 93 | static bool (*is_feature_enabled)(audio_ext_feature); |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 94 | |
| 95 | static 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 | |
| 110 | static 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 | |
| 122 | static int asphere_get_values_from_mixer(void) |
| 123 | { |
Manish Dewangan | 338c50a | 2017-09-12 15:22:03 +0530 | [diff] [blame] | 124 | int ret = 0; |
| 125 | long val[2] = {-1, -1}; |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 126 | 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 Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 140 | ALOGD("%s: returned %d, enabled:%ld, strength:%ld", |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 141 | __func__, ret, val[0], val[1]); |
| 142 | |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | static int asphere_set_values_to_mixer(void) |
| 147 | { |
Manish Dewangan | 338c50a | 2017-09-12 15:22:03 +0530 | [diff] [blame] | 148 | int ret = 0; |
| 149 | long val[2] = {-1, -1}; |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 150 | 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 Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 163 | ALOGD("%s: returned %d, enabled:%ld, strength:%ld", |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 164 | __func__, ret, val[0], val[1]); |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | static void asphere_init_once() { |
| 170 | ALOGD("%s", __func__); |
| 171 | pthread_mutex_init(&asphere.lock, NULL); |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 172 | |
| 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 Grahame | 6e76371 | 2019-01-31 16:18:17 -0800 | [diff] [blame] | 178 | "audio_feature_manager_is_feature_enabled")) != NULL) { |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 179 | 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 Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static int asphere_init() { |
| 195 | pthread_once(&asphere_once, asphere_init_once); |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 196 | enable_gcov(); |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 197 | return asphere.init_status; |
| 198 | } |
| 199 | |
| 200 | void asphere_set_parameters(struct str_parms *parms) |
| 201 | { |
| 202 | int ret = 0; |
| 203 | bool enable = false; |
| 204 | int strength = -1; |
| 205 | char value[32] = {0}; |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 206 | bool set_enable = false, set_strength = false; |
| 207 | |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 208 | 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 | |
| 238 | void asphere_get_parameters(struct str_parms *query, |
| 239 | struct str_parms *reply) |
| 240 | { |
| 241 | char value[32] = {0}; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 242 | int ret; |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 243 | |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 244 | 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 | |
| 271 | static 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 | |
| 288 | void handle_asphere_on_effect_enabled(bool enable, |
| 289 | effect_context_t *context, |
| 290 | struct listnode *created_effects) |
| 291 | { |
| 292 | struct listnode *node; |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 293 | |
| 294 | ALOGV("%s: effect %0x", __func__, context->desc->type.timeLow); |
Dhananjay Kumar | 1c978df | 2015-09-04 13:44:59 +0530 | [diff] [blame] | 295 | 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 | } |