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