Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 1 | /* |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 2 | * Copyright (c) 2013-2015, 2017-2019, The Linux Foundation. All rights reserved. |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 3 | * Not a Contribution. |
| 4 | * |
| 5 | * Copyright (C) 2013 The Android Open Source Project |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #define LOG_TAG "offload_effect_virtualizer" |
Subhash Chandra Bose Naripeddy | e40a7cd | 2014-06-03 19:42:41 -0700 | [diff] [blame] | 21 | //#define LOG_NDEBUG 0 |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 22 | |
| 23 | #include <cutils/list.h> |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 24 | #include <log/log.h> |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 25 | #include <tinyalsa/asoundlib.h> |
Subhash Chandra Bose Naripeddy | 090a2aa | 2014-01-30 14:03:12 -0800 | [diff] [blame] | 26 | #include <sound/audio_effects.h> |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 27 | #include <audio_effects/effect_virtualizer.h> |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 28 | #include <audio_feature_manager.h> |
| 29 | #include <dlfcn.h> |
| 30 | #include <unistd.h> |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 31 | |
| 32 | #include "effect_api.h" |
| 33 | #include "virtualizer.h" |
| 34 | |
Trinath Thammishetty | 765efd2 | 2018-08-20 13:23:24 +0530 | [diff] [blame] | 35 | #define VIRUALIZER_MAX_LATENCY 30 |
| 36 | |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 37 | #define PRIMARY_HAL_PATH XSTR(LIB_AUDIO_HAL) |
| 38 | #define XSTR(x) STR(x) |
| 39 | #define STR(x) #x |
| 40 | |
| 41 | static bool (*is_feature_enabled)(audio_ext_feature); |
| 42 | |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 43 | #ifdef AUDIO_FEATURE_ENABLED_GCOV |
| 44 | extern void __gcov_flush(); |
| 45 | static void enable_gcov() |
| 46 | { |
| 47 | __gcov_flush(); |
| 48 | } |
| 49 | #else |
| 50 | static void enable_gcov() |
| 51 | { |
| 52 | } |
| 53 | #endif |
| 54 | |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 55 | /* Offload Virtualizer UUID: 509a4498-561a-4bea-b3b1-0002a5d5c51b */ |
| 56 | const effect_descriptor_t virtualizer_descriptor = { |
| 57 | {0x37cc2c00, 0xdddd, 0x11db, 0x8577, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, |
| 58 | {0x509a4498, 0x561a, 0x4bea, 0xb3b1, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid |
| 59 | EFFECT_CONTROL_API_VERSION, |
Weiyin Jiang | 90ac1ea | 2017-04-13 14:18:23 +0800 | [diff] [blame] | 60 | (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_DEVICE_IND | EFFECT_FLAG_HW_ACC_TUNNEL | |
| 61 | EFFECT_FLAG_VOLUME_CTRL), |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 62 | 0, /* TODO */ |
| 63 | 1, |
| 64 | "MSM offload virtualizer", |
| 65 | "The Android Open Source Project", |
| 66 | }; |
| 67 | |
| 68 | /* |
| 69 | * Virtualizer operations |
| 70 | */ |
| 71 | |
| 72 | int virtualizer_get_strength(virtualizer_context_t *context) |
| 73 | { |
Dhananjay Kumar | 574f392 | 2014-03-25 17:41:44 +0530 | [diff] [blame] | 74 | ALOGV("%s: ctxt %p, strength: %d", __func__, context, context->strength); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 75 | return context->strength; |
| 76 | } |
| 77 | |
| 78 | int virtualizer_set_strength(virtualizer_context_t *context, uint32_t strength) |
| 79 | { |
Dhananjay Kumar | 574f392 | 2014-03-25 17:41:44 +0530 | [diff] [blame] | 80 | ALOGV("%s: ctxt %p, strength: %d", __func__, context, strength); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 81 | context->strength = strength; |
| 82 | |
Weiyin Jiang | c8082d9 | 2015-07-21 11:26:02 +0800 | [diff] [blame] | 83 | /* |
| 84 | * Zero strength is not equivalent to disable state as down mix |
| 85 | * is still happening for multichannel inputs. |
| 86 | * For better user experience, explicitly disable virtualizer module |
| 87 | * when strength is 0. |
| 88 | */ |
Dhananjay Kumar | f7026ef | 2015-12-15 10:51:10 +0530 | [diff] [blame] | 89 | if (context->enabled_by_client) |
| 90 | offload_virtualizer_set_enable_flag(&(context->offload_virt), |
Weiyin Jiang | c8082d9 | 2015-07-21 11:26:02 +0800 | [diff] [blame] | 91 | ((strength > 0) && !(context->temp_disabled)) ? |
| 92 | true : false); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 93 | offload_virtualizer_set_strength(&(context->offload_virt), strength); |
| 94 | if (context->ctl) |
Subhash Chandra Bose Naripeddy | e40a7cd | 2014-06-03 19:42:41 -0700 | [diff] [blame] | 95 | offload_virtualizer_send_params(context->ctl, &context->offload_virt, |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 96 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG | |
| 97 | OFFLOAD_SEND_VIRTUALIZER_STRENGTH); |
Subhash Chandra Bose Naripeddy | e40a7cd | 2014-06-03 19:42:41 -0700 | [diff] [blame] | 98 | if (context->hw_acc_fd > 0) |
| 99 | hw_acc_virtualizer_send_params(context->hw_acc_fd, |
| 100 | &context->offload_virt, |
| 101 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG | |
| 102 | OFFLOAD_SEND_VIRTUALIZER_STRENGTH); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
Weiyin Jiang | b64b7dd | 2015-03-23 00:54:14 +0800 | [diff] [blame] | 106 | /* |
| 107 | * Check if an audio device is supported by this implementation |
| 108 | * |
| 109 | * [in] |
| 110 | * device device that is intented for processing (e.g. for binaural vs transaural) |
| 111 | * [out] |
| 112 | * false device is not applicable for effect |
| 113 | * true device is applicable for effect |
| 114 | */ |
| 115 | bool virtualizer_is_device_supported(audio_devices_t device) { |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 116 | if (is_feature_enabled != NULL && |
| 117 | is_feature_enabled(AFE_PROXY)) { |
| 118 | if (device == AUDIO_DEVICE_OUT_PROXY) |
| 119 | return false; |
| 120 | } |
| 121 | |
Weiyin Jiang | b64b7dd | 2015-03-23 00:54:14 +0800 | [diff] [blame] | 122 | switch (device) { |
| 123 | case AUDIO_DEVICE_OUT_SPEAKER: |
| 124 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT: |
| 125 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER: |
Weiyin Jiang | b64b7dd | 2015-03-23 00:54:14 +0800 | [diff] [blame] | 126 | case AUDIO_DEVICE_OUT_AUX_DIGITAL: |
| 127 | case AUDIO_DEVICE_OUT_USB_ACCESSORY: |
| 128 | case AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET: |
| 129 | return false; |
| 130 | default : |
| 131 | return true; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Check if a channel mask + audio device is supported by this implementation |
| 137 | * |
| 138 | * [in] |
| 139 | * channel_mask channel mask of input buffer |
| 140 | * device device that is intented for processing (e.g. for binaural vs transaural) |
| 141 | * [out] |
| 142 | * false if the configuration is not supported or it is unknown |
| 143 | * true if the configuration is supported |
| 144 | */ |
| 145 | bool virtualizer_is_configuration_supported(audio_channel_mask_t channel_mask, |
| 146 | audio_devices_t device) { |
| 147 | uint32_t channelCount = audio_channel_count_from_out_mask(channel_mask); |
| 148 | if ((channelCount == 0) || (channelCount > 2)) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | return virtualizer_is_device_supported(device); |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Force the virtualization mode to that of the given audio device |
| 157 | * |
| 158 | * [in] |
| 159 | * context effect engine context |
| 160 | * forced_device device whose virtualization mode we'll always use |
| 161 | * [out] |
| 162 | * -EINVAL if the device is not supported or is unknown |
| 163 | * 0 if the device is supported and the virtualization mode forced |
| 164 | */ |
| 165 | int virtualizer_force_virtualization_mode(virtualizer_context_t *context, |
| 166 | audio_devices_t forced_device) { |
| 167 | virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; |
| 168 | int status = 0; |
| 169 | bool use_virt = false; |
Dhananjay Kumar | f7026ef | 2015-12-15 10:51:10 +0530 | [diff] [blame] | 170 | int is_virt_enabled = virt_ctxt->enabled_by_client; |
Weiyin Jiang | b64b7dd | 2015-03-23 00:54:14 +0800 | [diff] [blame] | 171 | |
| 172 | ALOGV("%s: ctxt %p, forcedDev=0x%x enabled=%d tmpDisabled=%d", __func__, virt_ctxt, |
| 173 | forced_device, is_virt_enabled, virt_ctxt->temp_disabled); |
| 174 | |
| 175 | if (virtualizer_is_device_supported(forced_device) == false) { |
| 176 | if (forced_device != AUDIO_DEVICE_NONE) { |
| 177 | //forced device is not supported, make it behave as a reset of forced mode |
| 178 | forced_device = AUDIO_DEVICE_NONE; |
| 179 | // but return an error |
| 180 | status = -EINVAL; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (forced_device == AUDIO_DEVICE_NONE) { |
| 185 | // disabling forced virtualization mode: |
| 186 | // verify whether the virtualization should be enabled or disabled |
| 187 | if (virtualizer_is_device_supported(virt_ctxt->device)) { |
| 188 | use_virt = (is_virt_enabled == true); |
| 189 | } |
| 190 | virt_ctxt->forced_device = AUDIO_DEVICE_NONE; |
| 191 | } else { |
| 192 | // forcing virtualization mode: |
| 193 | // TODO: we assume device is supported, so hard coded a fixed one. |
| 194 | virt_ctxt->forced_device = AUDIO_DEVICE_OUT_WIRED_HEADPHONE; |
| 195 | // TODO: only enable for a supported mode, when the effect is enabled |
| 196 | use_virt = (is_virt_enabled == true); |
| 197 | } |
| 198 | |
| 199 | if (use_virt) { |
| 200 | if (virt_ctxt->temp_disabled == true) { |
| 201 | if (effect_is_active(&virt_ctxt->common)) { |
| 202 | offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true); |
| 203 | if (virt_ctxt->ctl) |
| 204 | offload_virtualizer_send_params(virt_ctxt->ctl, |
| 205 | &virt_ctxt->offload_virt, |
| 206 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); |
| 207 | if (virt_ctxt->hw_acc_fd > 0) |
| 208 | hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd, |
| 209 | &virt_ctxt->offload_virt, |
| 210 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); |
| 211 | } |
| 212 | ALOGV("%s: re-enable VIRTUALIZER", __func__); |
| 213 | virt_ctxt->temp_disabled = false; |
| 214 | } else { |
| 215 | ALOGV("%s: leaving VIRTUALIZER enabled", __func__); |
| 216 | } |
| 217 | } else { |
| 218 | if (virt_ctxt->temp_disabled == false) { |
| 219 | if (effect_is_active(&virt_ctxt->common)) { |
| 220 | offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false); |
| 221 | if (virt_ctxt->ctl) |
| 222 | offload_virtualizer_send_params(virt_ctxt->ctl, |
| 223 | &virt_ctxt->offload_virt, |
| 224 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); |
| 225 | if (virt_ctxt->hw_acc_fd > 0) |
| 226 | hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd, |
| 227 | &virt_ctxt->offload_virt, |
| 228 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); |
| 229 | } |
| 230 | ALOGV("%s: disable VIRTUALIZER", __func__); |
| 231 | virt_ctxt->temp_disabled = true; |
| 232 | } else { |
| 233 | ALOGV("%s: leaving VIRTUALIZER disabled", __func__); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | ALOGV("after %s: ctxt %p, enabled=%d tmpDisabled=%d", __func__, virt_ctxt, |
| 238 | is_virt_enabled, virt_ctxt->temp_disabled); |
| 239 | |
| 240 | return status; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Get the virtual speaker angles for a channel mask + audio device configuration |
| 245 | * which is guaranteed to be supported by this implementation |
| 246 | * |
| 247 | * [in] |
| 248 | * channel_mask the channel mask of the input to virtualize |
| 249 | * device the type of device that affects the processing (e.g. for binaural vs transaural) |
| 250 | * [in/out] |
| 251 | * speaker_angles the array of integer where each speaker angle is written as a triplet in the |
| 252 | * following format: |
| 253 | * int32_t a bit mask with a single value selected for each speaker, following |
| 254 | * the convention of the audio_channel_mask_t type |
| 255 | * int32_t a value in degrees expressing the speaker azimuth, where 0 is in front |
| 256 | * of the user, 180 behind, -90 to the left, 90 to the right of the user |
| 257 | * int32_t a value in degrees expressing the speaker elevation, where 0 is the |
| 258 | * horizontal plane, +90 is directly above the user, -90 below |
| 259 | * |
| 260 | */ |
| 261 | void virtualizer_get_speaker_angles(audio_channel_mask_t channel_mask __unused, |
| 262 | audio_devices_t device __unused, int32_t *speaker_angles) { |
| 263 | // the channel count is guaranteed to be 1 or 2 |
| 264 | // the device is guaranteed to be of type headphone |
| 265 | // this virtualizer is always 2in with speakers at -90 and 90deg of azimuth, 0deg of elevation |
| 266 | *speaker_angles++ = (int32_t) AUDIO_CHANNEL_OUT_FRONT_LEFT; |
| 267 | *speaker_angles++ = -90; // azimuth |
| 268 | *speaker_angles++ = 0; // elevation |
| 269 | *speaker_angles++ = (int32_t) AUDIO_CHANNEL_OUT_FRONT_RIGHT; |
| 270 | *speaker_angles++ = 90; // azimuth |
| 271 | *speaker_angles = 0; // elevation |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Retrieve the current device whose processing mode is used by this effect |
| 276 | * |
| 277 | * [out] |
| 278 | * AUDIO_DEVICE_NONE if the effect is not virtualizing |
| 279 | * or the device type if the effect is virtualizing |
| 280 | */ |
| 281 | audio_devices_t virtualizer_get_virtualization_mode(virtualizer_context_t *context) { |
| 282 | virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; |
| 283 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 284 | |
| 285 | if ((offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))) |
| 286 | && (virt_ctxt->temp_disabled == false)) { |
| 287 | if (virt_ctxt->forced_device != AUDIO_DEVICE_NONE) { |
| 288 | // virtualization mode is forced, return that device |
| 289 | device = virt_ctxt->forced_device; |
| 290 | } else { |
| 291 | // no forced mode, return the current device |
| 292 | device = virt_ctxt->device; |
| 293 | } |
| 294 | } |
| 295 | ALOGV("%s: returning 0x%x", __func__, device); |
| 296 | return device; |
| 297 | } |
| 298 | |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 299 | int virtualizer_get_parameter(effect_context_t *context, effect_param_t *p, |
| 300 | uint32_t *size) |
| 301 | { |
| 302 | virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; |
| 303 | int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t); |
| 304 | int32_t *param_tmp = (int32_t *)p->data; |
| 305 | int32_t param = *param_tmp++; |
| 306 | void *value = p->data + voffset; |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 307 | |
Dhananjay Kumar | 574f392 | 2014-03-25 17:41:44 +0530 | [diff] [blame] | 308 | ALOGV("%s: ctxt %p, param %d", __func__, virt_ctxt, param); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 309 | |
| 310 | p->status = 0; |
| 311 | |
| 312 | switch (param) { |
| 313 | case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED: |
| 314 | if (p->vsize < sizeof(uint32_t)) |
| 315 | p->status = -EINVAL; |
| 316 | p->vsize = sizeof(uint32_t); |
| 317 | break; |
| 318 | case VIRTUALIZER_PARAM_STRENGTH: |
| 319 | if (p->vsize < sizeof(int16_t)) |
| 320 | p->status = -EINVAL; |
| 321 | p->vsize = sizeof(int16_t); |
| 322 | break; |
Weiyin Jiang | b64b7dd | 2015-03-23 00:54:14 +0800 | [diff] [blame] | 323 | case VIRTUALIZER_PARAM_VIRTUAL_SPEAKER_ANGLES: |
| 324 | // return value size can only be interpreted as relative to input value, |
| 325 | // deferring validity check to below |
| 326 | break; |
| 327 | case VIRTUALIZER_PARAM_VIRTUALIZATION_MODE: |
| 328 | if (p->vsize != sizeof(uint32_t)) |
| 329 | p->status = -EINVAL; |
| 330 | p->vsize = sizeof(uint32_t); |
| 331 | break; |
Trinath Thammishetty | 765efd2 | 2018-08-20 13:23:24 +0530 | [diff] [blame] | 332 | case VIRTUALIZER_PARAM_LATENCY: |
| 333 | if (p->vsize < sizeof(uint32_t)) |
| 334 | p->status = -EINVAL; |
| 335 | p->vsize = sizeof(uint32_t); |
| 336 | break; |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 337 | default: |
| 338 | p->status = -EINVAL; |
| 339 | } |
| 340 | |
| 341 | *size = sizeof(effect_param_t) + voffset + p->vsize; |
| 342 | |
| 343 | if (p->status != 0) |
| 344 | return 0; |
| 345 | |
| 346 | switch (param) { |
| 347 | case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED: |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 348 | *(uint32_t *)value = 1; |
| 349 | break; |
| 350 | |
| 351 | case VIRTUALIZER_PARAM_STRENGTH: |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 352 | *(int16_t *)value = virtualizer_get_strength(virt_ctxt); |
| 353 | break; |
| 354 | |
Weiyin Jiang | b64b7dd | 2015-03-23 00:54:14 +0800 | [diff] [blame] | 355 | case VIRTUALIZER_PARAM_VIRTUAL_SPEAKER_ANGLES: |
| 356 | { |
| 357 | const audio_channel_mask_t channel_mask = (audio_channel_mask_t) *param_tmp++; |
| 358 | const audio_devices_t device = (audio_devices_t) *param_tmp; |
| 359 | uint32_t channel_cnt = audio_channel_count_from_out_mask(channel_mask); |
| 360 | |
| 361 | if (p->vsize < 3 * channel_cnt * sizeof(int32_t)){ |
| 362 | p->status = -EINVAL; |
| 363 | break; |
| 364 | } |
| 365 | // verify the configuration is supported |
| 366 | if(virtualizer_is_configuration_supported(channel_mask, device)) { |
| 367 | // configuration is supported, get the angles |
| 368 | virtualizer_get_speaker_angles(channel_mask, device, (int32_t *)value); |
| 369 | } else { |
| 370 | p->status = -EINVAL; |
| 371 | } |
| 372 | |
| 373 | break; |
| 374 | } |
| 375 | |
| 376 | case VIRTUALIZER_PARAM_VIRTUALIZATION_MODE: |
| 377 | *(uint32_t *)value = (uint32_t) virtualizer_get_virtualization_mode(virt_ctxt); |
| 378 | break; |
| 379 | |
Trinath Thammishetty | 765efd2 | 2018-08-20 13:23:24 +0530 | [diff] [blame] | 380 | case VIRTUALIZER_PARAM_LATENCY: |
| 381 | *(uint32_t *)value = VIRUALIZER_MAX_LATENCY; |
| 382 | break; |
| 383 | |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 384 | default: |
| 385 | p->status = -EINVAL; |
| 386 | break; |
| 387 | } |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | int virtualizer_set_parameter(effect_context_t *context, effect_param_t *p, |
Weiyin Jiang | b64b7dd | 2015-03-23 00:54:14 +0800 | [diff] [blame] | 393 | uint32_t size __unused) |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 394 | { |
| 395 | virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; |
| 396 | int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t); |
| 397 | void *value = p->data + voffset; |
| 398 | int32_t *param_tmp = (int32_t *)p->data; |
| 399 | int32_t param = *param_tmp++; |
| 400 | uint32_t strength; |
| 401 | |
Dhananjay Kumar | 574f392 | 2014-03-25 17:41:44 +0530 | [diff] [blame] | 402 | ALOGV("%s: ctxt %p, param %d", __func__, virt_ctxt, param); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 403 | |
| 404 | p->status = 0; |
| 405 | |
| 406 | switch (param) { |
| 407 | case VIRTUALIZER_PARAM_STRENGTH: |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 408 | strength = (uint32_t)(*(int16_t *)value); |
| 409 | virtualizer_set_strength(virt_ctxt, strength); |
| 410 | break; |
Weiyin Jiang | b64b7dd | 2015-03-23 00:54:14 +0800 | [diff] [blame] | 411 | case VIRTUALIZER_PARAM_FORCE_VIRTUALIZATION_MODE: |
| 412 | { |
| 413 | const audio_devices_t device = *(audio_devices_t *)value; |
| 414 | if (0 != virtualizer_force_virtualization_mode(virt_ctxt, device)) { |
| 415 | p->status = -EINVAL; |
| 416 | } |
| 417 | break; |
| 418 | } |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 419 | default: |
| 420 | p->status = -EINVAL; |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | int virtualizer_set_device(effect_context_t *context, uint32_t device) |
| 428 | { |
| 429 | virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; |
| 430 | |
Dhananjay Kumar | 574f392 | 2014-03-25 17:41:44 +0530 | [diff] [blame] | 431 | ALOGV("%s: ctxt %p, device: 0x%x", __func__, virt_ctxt, device); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 432 | virt_ctxt->device = device; |
Weiyin Jiang | b64b7dd | 2015-03-23 00:54:14 +0800 | [diff] [blame] | 433 | |
| 434 | if (virt_ctxt->forced_device == AUDIO_DEVICE_NONE) { |
| 435 | // default case unless configuration is forced |
| 436 | if (virtualizer_is_device_supported(device) == false) { |
| 437 | if (!virt_ctxt->temp_disabled) { |
Dhananjay Kumar | f7026ef | 2015-12-15 10:51:10 +0530 | [diff] [blame] | 438 | if (effect_is_active(&virt_ctxt->common) && virt_ctxt->enabled_by_client) { |
Weiyin Jiang | b64b7dd | 2015-03-23 00:54:14 +0800 | [diff] [blame] | 439 | offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false); |
| 440 | if (virt_ctxt->ctl) |
| 441 | offload_virtualizer_send_params(virt_ctxt->ctl, |
| 442 | &virt_ctxt->offload_virt, |
| 443 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); |
| 444 | if (virt_ctxt->hw_acc_fd > 0) |
| 445 | hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd, |
| 446 | &virt_ctxt->offload_virt, |
| 447 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); |
| 448 | } |
| 449 | virt_ctxt->temp_disabled = true; |
| 450 | ALOGI("%s: ctxt %p, disabled based on device", __func__, virt_ctxt); |
wjiang | 50b81f4 | 2014-08-06 08:03:14 +0800 | [diff] [blame] | 451 | } |
Weiyin Jiang | b64b7dd | 2015-03-23 00:54:14 +0800 | [diff] [blame] | 452 | } else { |
| 453 | if (virt_ctxt->temp_disabled) { |
Dhananjay Kumar | f7026ef | 2015-12-15 10:51:10 +0530 | [diff] [blame] | 454 | if (effect_is_active(&virt_ctxt->common) && virt_ctxt->enabled_by_client) { |
Weiyin Jiang | b64b7dd | 2015-03-23 00:54:14 +0800 | [diff] [blame] | 455 | offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true); |
| 456 | if (virt_ctxt->ctl) |
| 457 | offload_virtualizer_send_params(virt_ctxt->ctl, |
| 458 | &virt_ctxt->offload_virt, |
| 459 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); |
| 460 | if (virt_ctxt->hw_acc_fd > 0) |
| 461 | hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd, |
| 462 | &virt_ctxt->offload_virt, |
| 463 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); |
| 464 | } |
| 465 | virt_ctxt->temp_disabled = false; |
wjiang | 50b81f4 | 2014-08-06 08:03:14 +0800 | [diff] [blame] | 466 | } |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 467 | } |
| 468 | } |
Weiyin Jiang | b64b7dd | 2015-03-23 00:54:14 +0800 | [diff] [blame] | 469 | // else virtualization mode is forced to a certain device, nothing to do |
| 470 | |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 471 | offload_virtualizer_set_device(&(virt_ctxt->offload_virt), device); |
| 472 | return 0; |
| 473 | } |
| 474 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 475 | int virtualizer_reset(effect_context_t *context __unused) |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 476 | { |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | int virtualizer_init(effect_context_t *context) |
| 481 | { |
Dhananjay Kumar | 574f392 | 2014-03-25 17:41:44 +0530 | [diff] [blame] | 482 | ALOGV("%s: ctxt %p", __func__, context); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 483 | virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; |
| 484 | |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 485 | if (access(PRIMARY_HAL_PATH, R_OK) == 0) { |
| 486 | void *hal_lib_pointer = dlopen(PRIMARY_HAL_PATH, RTLD_NOW); |
| 487 | if (hal_lib_pointer == NULL) |
| 488 | ALOGE("%s: DLOPEN failed for %s", __func__, PRIMARY_HAL_PATH); |
| 489 | else { |
| 490 | is_feature_enabled = |
| 491 | (bool (*)(audio_ext_feature))dlsym(hal_lib_pointer, |
Aalique Grahame | 6e76371 | 2019-01-31 16:18:17 -0800 | [diff] [blame] | 492 | "audio_feature_manager_is_feature_enabled"); |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 493 | if (is_feature_enabled == NULL) |
| 494 | ALOGE("%s: dlsym failed", __func__); |
| 495 | } |
| 496 | } else |
| 497 | ALOGE("%s: not able to acces lib %s ", __func__, PRIMARY_HAL_PATH); |
| 498 | |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 499 | context->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
| 500 | context->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 501 | context->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 502 | context->config.inputCfg.samplingRate = 44100; |
| 503 | context->config.inputCfg.bufferProvider.getBuffer = NULL; |
| 504 | context->config.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 505 | context->config.inputCfg.bufferProvider.cookie = NULL; |
| 506 | context->config.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 507 | context->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
| 508 | context->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 509 | context->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 510 | context->config.outputCfg.samplingRate = 44100; |
| 511 | context->config.outputCfg.bufferProvider.getBuffer = NULL; |
| 512 | context->config.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 513 | context->config.outputCfg.bufferProvider.cookie = NULL; |
| 514 | context->config.outputCfg.mask = EFFECT_CONFIG_ALL; |
| 515 | |
| 516 | set_config(context, &context->config); |
| 517 | |
Dhananjay Kumar | f7026ef | 2015-12-15 10:51:10 +0530 | [diff] [blame] | 518 | virt_ctxt->enabled_by_client = false; |
wjiang | 50b81f4 | 2014-08-06 08:03:14 +0800 | [diff] [blame] | 519 | virt_ctxt->temp_disabled = false; |
Subhash Chandra Bose Naripeddy | e40a7cd | 2014-06-03 19:42:41 -0700 | [diff] [blame] | 520 | virt_ctxt->hw_acc_fd = -1; |
Weiyin Jiang | b64b7dd | 2015-03-23 00:54:14 +0800 | [diff] [blame] | 521 | virt_ctxt->forced_device = AUDIO_DEVICE_NONE; |
| 522 | virt_ctxt->device = AUDIO_DEVICE_NONE; |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 523 | memset(&(virt_ctxt->offload_virt), 0, sizeof(struct virtualizer_params)); |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 524 | enable_gcov(); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | int virtualizer_enable(effect_context_t *context) |
| 529 | { |
| 530 | virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; |
| 531 | |
Dhananjay Kumar | 574f392 | 2014-03-25 17:41:44 +0530 | [diff] [blame] | 532 | ALOGV("%s: ctxt %p, strength %d", __func__, virt_ctxt, virt_ctxt->strength); |
wjiang | 95d74c2 | 2014-03-28 12:29:58 +0800 | [diff] [blame] | 533 | |
Dhananjay Kumar | f7026ef | 2015-12-15 10:51:10 +0530 | [diff] [blame] | 534 | virt_ctxt->enabled_by_client = true; |
wjiang | 95d74c2 | 2014-03-28 12:29:58 +0800 | [diff] [blame] | 535 | if (!offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)) && |
| 536 | !(virt_ctxt->temp_disabled)) { |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 537 | offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true); |
wjiang | d45948e | 2014-02-24 22:19:43 +0800 | [diff] [blame] | 538 | if (virt_ctxt->ctl && virt_ctxt->strength) |
| 539 | offload_virtualizer_send_params(virt_ctxt->ctl, |
Subhash Chandra Bose Naripeddy | e40a7cd | 2014-06-03 19:42:41 -0700 | [diff] [blame] | 540 | &virt_ctxt->offload_virt, |
wjiang | d45948e | 2014-02-24 22:19:43 +0800 | [diff] [blame] | 541 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG | |
Subhash Chandra Bose Naripeddy | e40a7cd | 2014-06-03 19:42:41 -0700 | [diff] [blame] | 542 | OFFLOAD_SEND_VIRTUALIZER_STRENGTH); |
| 543 | if ((virt_ctxt->hw_acc_fd > 0) && virt_ctxt->strength) |
| 544 | hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd, |
| 545 | &virt_ctxt->offload_virt, |
| 546 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG | |
| 547 | OFFLOAD_SEND_VIRTUALIZER_STRENGTH); |
wjiang | d45948e | 2014-02-24 22:19:43 +0800 | [diff] [blame] | 548 | } |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 549 | enable_gcov(); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | int virtualizer_disable(effect_context_t *context) |
| 554 | { |
| 555 | virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; |
| 556 | |
Dhananjay Kumar | 574f392 | 2014-03-25 17:41:44 +0530 | [diff] [blame] | 557 | ALOGV("%s: ctxt %p", __func__, virt_ctxt); |
Dhananjay Kumar | f7026ef | 2015-12-15 10:51:10 +0530 | [diff] [blame] | 558 | |
| 559 | virt_ctxt->enabled_by_client = false; |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 560 | if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))) { |
| 561 | offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false); |
| 562 | if (virt_ctxt->ctl) |
| 563 | offload_virtualizer_send_params(virt_ctxt->ctl, |
Subhash Chandra Bose Naripeddy | e40a7cd | 2014-06-03 19:42:41 -0700 | [diff] [blame] | 564 | &virt_ctxt->offload_virt, |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 565 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); |
Subhash Chandra Bose Naripeddy | e40a7cd | 2014-06-03 19:42:41 -0700 | [diff] [blame] | 566 | if (virt_ctxt->hw_acc_fd > 0) |
| 567 | hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd, |
| 568 | &virt_ctxt->offload_virt, |
| 569 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 570 | } |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 571 | enable_gcov(); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | int virtualizer_start(effect_context_t *context, output_context_t *output) |
| 576 | { |
| 577 | virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; |
| 578 | |
Dhananjay Kumar | 574f392 | 2014-03-25 17:41:44 +0530 | [diff] [blame] | 579 | ALOGV("%s: ctxt %p, ctl %p", __func__, virt_ctxt, output->ctl); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 580 | virt_ctxt->ctl = output->ctl; |
Subhash Chandra Bose Naripeddy | e40a7cd | 2014-06-03 19:42:41 -0700 | [diff] [blame] | 581 | if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))) { |
Amit Shekhar | d02f2cd | 2014-01-16 16:51:43 -0800 | [diff] [blame] | 582 | if (virt_ctxt->ctl) |
Subhash Chandra Bose Naripeddy | e40a7cd | 2014-06-03 19:42:41 -0700 | [diff] [blame] | 583 | offload_virtualizer_send_params(virt_ctxt->ctl, &virt_ctxt->offload_virt, |
Amit Shekhar | d02f2cd | 2014-01-16 16:51:43 -0800 | [diff] [blame] | 584 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG | |
| 585 | OFFLOAD_SEND_VIRTUALIZER_STRENGTH); |
Subhash Chandra Bose Naripeddy | e40a7cd | 2014-06-03 19:42:41 -0700 | [diff] [blame] | 586 | if (virt_ctxt->hw_acc_fd > 0) |
| 587 | hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd, |
| 588 | &virt_ctxt->offload_virt, |
| 589 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG | |
| 590 | OFFLOAD_SEND_VIRTUALIZER_STRENGTH); |
| 591 | } |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 592 | enable_gcov(); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | |
Subhash Chandra Bose Naripeddy | e40a7cd | 2014-06-03 19:42:41 -0700 | [diff] [blame] | 596 | int virtualizer_stop(effect_context_t *context, output_context_t *output __unused) |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 597 | { |
| 598 | virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; |
| 599 | |
Dhananjay Kumar | 574f392 | 2014-03-25 17:41:44 +0530 | [diff] [blame] | 600 | ALOGV("%s: ctxt %p", __func__, virt_ctxt); |
Subhash Chandra Bose Naripeddy | e40a7cd | 2014-06-03 19:42:41 -0700 | [diff] [blame] | 601 | if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)) && |
| 602 | virt_ctxt->ctl) { |
| 603 | struct virtualizer_params virt; |
| 604 | virt.enable_flag = false; |
| 605 | offload_virtualizer_send_params(virt_ctxt->ctl, &virt, |
| 606 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG); |
| 607 | } |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 608 | virt_ctxt->ctl = NULL; |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 609 | enable_gcov(); |
Subhash Chandra Bose Naripeddy | 3eedc00 | 2013-11-12 20:45:15 -0800 | [diff] [blame] | 610 | return 0; |
| 611 | } |
Subhash Chandra Bose Naripeddy | e40a7cd | 2014-06-03 19:42:41 -0700 | [diff] [blame] | 612 | |
| 613 | int virtualizer_set_mode(effect_context_t *context, int32_t hw_acc_fd) |
| 614 | { |
| 615 | virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context; |
| 616 | |
| 617 | ALOGV("%s: ctxt %p", __func__, virt_ctxt); |
| 618 | virt_ctxt->hw_acc_fd = hw_acc_fd; |
| 619 | if ((virt_ctxt->hw_acc_fd > 0) && |
| 620 | (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)))) |
| 621 | hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd, |
| 622 | &virt_ctxt->offload_virt, |
| 623 | OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG | |
| 624 | OFFLOAD_SEND_VIRTUALIZER_STRENGTH); |
| 625 | return 0; |
| 626 | } |