blob: 0199a511c415bf6b599b98e11c29b2b8f9ade6e3 [file] [log] [blame]
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -08001/*
Aalique Grahame22e49102018-12-18 14:23:57 -08002 * Copyright (c) 2013-2015, 2017-2019, The Linux Foundation. All rights reserved.
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -08003 * 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 Naripeddye40a7cd2014-06-03 19:42:41 -070021//#define LOG_NDEBUG 0
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080022
23#include <cutils/list.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080024#include <log/log.h>
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080025#include <tinyalsa/asoundlib.h>
Subhash Chandra Bose Naripeddy090a2aa2014-01-30 14:03:12 -080026#include <sound/audio_effects.h>
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080027#include <audio_effects/effect_virtualizer.h>
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080028#include <audio_feature_manager.h>
29#include <dlfcn.h>
30#include <unistd.h>
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080031
32#include "effect_api.h"
33#include "virtualizer.h"
34
Trinath Thammishetty765efd22018-08-20 13:23:24 +053035#define VIRUALIZER_MAX_LATENCY 30
36
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080037#define PRIMARY_HAL_PATH XSTR(LIB_AUDIO_HAL)
38#define XSTR(x) STR(x)
39#define STR(x) #x
40
41static bool (*is_feature_enabled)(audio_ext_feature);
42
Vatsal Buchac09ae062018-11-14 13:25:08 +053043#ifdef AUDIO_FEATURE_ENABLED_GCOV
44extern void __gcov_flush();
45static void enable_gcov()
46{
47 __gcov_flush();
48}
49#else
50static void enable_gcov()
51{
52}
53#endif
54
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080055/* Offload Virtualizer UUID: 509a4498-561a-4bea-b3b1-0002a5d5c51b */
56const 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 Jiang90ac1ea2017-04-13 14:18:23 +080060 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_DEVICE_IND | EFFECT_FLAG_HW_ACC_TUNNEL |
61 EFFECT_FLAG_VOLUME_CTRL),
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080062 0, /* TODO */
63 1,
64 "MSM offload virtualizer",
65 "The Android Open Source Project",
66};
67
68/*
69 * Virtualizer operations
70 */
71
72int virtualizer_get_strength(virtualizer_context_t *context)
73{
Dhananjay Kumar574f3922014-03-25 17:41:44 +053074 ALOGV("%s: ctxt %p, strength: %d", __func__, context, context->strength);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080075 return context->strength;
76}
77
78int virtualizer_set_strength(virtualizer_context_t *context, uint32_t strength)
79{
Dhananjay Kumar574f3922014-03-25 17:41:44 +053080 ALOGV("%s: ctxt %p, strength: %d", __func__, context, strength);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080081 context->strength = strength;
82
Weiyin Jiangc8082d92015-07-21 11:26:02 +080083 /*
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 Kumarf7026ef2015-12-15 10:51:10 +053089 if (context->enabled_by_client)
90 offload_virtualizer_set_enable_flag(&(context->offload_virt),
Weiyin Jiangc8082d92015-07-21 11:26:02 +080091 ((strength > 0) && !(context->temp_disabled)) ?
92 true : false);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080093 offload_virtualizer_set_strength(&(context->offload_virt), strength);
94 if (context->ctl)
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -070095 offload_virtualizer_send_params(context->ctl, &context->offload_virt,
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080096 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
97 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -070098 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 Naripeddy3eedc002013-11-12 20:45:15 -0800103 return 0;
104}
105
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800106/*
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 */
115bool virtualizer_is_device_supported(audio_devices_t device) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800116 if (is_feature_enabled != NULL &&
117 is_feature_enabled(AFE_PROXY)) {
118 if (device == AUDIO_DEVICE_OUT_PROXY)
119 return false;
120 }
121
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800122 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 Jiangb64b7dd2015-03-23 00:54:14 +0800126 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 */
145bool 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 */
165int 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 Kumarf7026ef2015-12-15 10:51:10 +0530170 int is_virt_enabled = virt_ctxt->enabled_by_client;
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800171
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 */
261void 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 */
281audio_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 Naripeddy3eedc002013-11-12 20:45:15 -0800299int 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 Naripeddy3eedc002013-11-12 20:45:15 -0800307
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530308 ALOGV("%s: ctxt %p, param %d", __func__, virt_ctxt, param);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800309
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 Jiangb64b7dd2015-03-23 00:54:14 +0800323 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 Thammishetty765efd22018-08-20 13:23:24 +0530332 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 Naripeddy3eedc002013-11-12 20:45:15 -0800337 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 Naripeddy3eedc002013-11-12 20:45:15 -0800348 *(uint32_t *)value = 1;
349 break;
350
351 case VIRTUALIZER_PARAM_STRENGTH:
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800352 *(int16_t *)value = virtualizer_get_strength(virt_ctxt);
353 break;
354
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800355 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 Thammishetty765efd22018-08-20 13:23:24 +0530380 case VIRTUALIZER_PARAM_LATENCY:
381 *(uint32_t *)value = VIRUALIZER_MAX_LATENCY;
382 break;
383
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800384 default:
385 p->status = -EINVAL;
386 break;
387 }
388
389 return 0;
390}
391
392int virtualizer_set_parameter(effect_context_t *context, effect_param_t *p,
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800393 uint32_t size __unused)
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800394{
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 Kumar574f3922014-03-25 17:41:44 +0530402 ALOGV("%s: ctxt %p, param %d", __func__, virt_ctxt, param);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800403
404 p->status = 0;
405
406 switch (param) {
407 case VIRTUALIZER_PARAM_STRENGTH:
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800408 strength = (uint32_t)(*(int16_t *)value);
409 virtualizer_set_strength(virt_ctxt, strength);
410 break;
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800411 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 Naripeddy3eedc002013-11-12 20:45:15 -0800419 default:
420 p->status = -EINVAL;
421 break;
422 }
423
424 return 0;
425}
426
427int virtualizer_set_device(effect_context_t *context, uint32_t device)
428{
429 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
430
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530431 ALOGV("%s: ctxt %p, device: 0x%x", __func__, virt_ctxt, device);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800432 virt_ctxt->device = device;
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800433
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 Kumarf7026ef2015-12-15 10:51:10 +0530438 if (effect_is_active(&virt_ctxt->common) && virt_ctxt->enabled_by_client) {
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800439 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);
wjiang50b81f42014-08-06 08:03:14 +0800451 }
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800452 } else {
453 if (virt_ctxt->temp_disabled) {
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530454 if (effect_is_active(&virt_ctxt->common) && virt_ctxt->enabled_by_client) {
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800455 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;
wjiang50b81f42014-08-06 08:03:14 +0800466 }
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800467 }
468 }
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800469 // else virtualization mode is forced to a certain device, nothing to do
470
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800471 offload_virtualizer_set_device(&(virt_ctxt->offload_virt), device);
472 return 0;
473}
474
Aalique Grahame22e49102018-12-18 14:23:57 -0800475int virtualizer_reset(effect_context_t *context __unused)
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800476{
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800477 return 0;
478}
479
480int virtualizer_init(effect_context_t *context)
481{
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530482 ALOGV("%s: ctxt %p", __func__, context);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800483 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
484
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800485 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 Grahame6e763712019-01-31 16:18:17 -0800492 "audio_feature_manager_is_feature_enabled");
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800493 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 Naripeddy3eedc002013-11-12 20:45:15 -0800499 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 Kumarf7026ef2015-12-15 10:51:10 +0530518 virt_ctxt->enabled_by_client = false;
wjiang50b81f42014-08-06 08:03:14 +0800519 virt_ctxt->temp_disabled = false;
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700520 virt_ctxt->hw_acc_fd = -1;
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800521 virt_ctxt->forced_device = AUDIO_DEVICE_NONE;
522 virt_ctxt->device = AUDIO_DEVICE_NONE;
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800523 memset(&(virt_ctxt->offload_virt), 0, sizeof(struct virtualizer_params));
Vatsal Buchac09ae062018-11-14 13:25:08 +0530524 enable_gcov();
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800525 return 0;
526}
527
528int virtualizer_enable(effect_context_t *context)
529{
530 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
531
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530532 ALOGV("%s: ctxt %p, strength %d", __func__, virt_ctxt, virt_ctxt->strength);
wjiang95d74c22014-03-28 12:29:58 +0800533
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530534 virt_ctxt->enabled_by_client = true;
wjiang95d74c22014-03-28 12:29:58 +0800535 if (!offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)) &&
536 !(virt_ctxt->temp_disabled)) {
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800537 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true);
wjiangd45948e2014-02-24 22:19:43 +0800538 if (virt_ctxt->ctl && virt_ctxt->strength)
539 offload_virtualizer_send_params(virt_ctxt->ctl,
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700540 &virt_ctxt->offload_virt,
wjiangd45948e2014-02-24 22:19:43 +0800541 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700542 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);
wjiangd45948e2014-02-24 22:19:43 +0800548 }
Vatsal Buchac09ae062018-11-14 13:25:08 +0530549 enable_gcov();
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800550 return 0;
551}
552
553int virtualizer_disable(effect_context_t *context)
554{
555 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
556
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530557 ALOGV("%s: ctxt %p", __func__, virt_ctxt);
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530558
559 virt_ctxt->enabled_by_client = false;
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800560 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 Naripeddye40a7cd2014-06-03 19:42:41 -0700564 &virt_ctxt->offload_virt,
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800565 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700566 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 Naripeddy3eedc002013-11-12 20:45:15 -0800570 }
Vatsal Buchac09ae062018-11-14 13:25:08 +0530571 enable_gcov();
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800572 return 0;
573}
574
575int virtualizer_start(effect_context_t *context, output_context_t *output)
576{
577 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
578
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530579 ALOGV("%s: ctxt %p, ctl %p", __func__, virt_ctxt, output->ctl);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800580 virt_ctxt->ctl = output->ctl;
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700581 if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))) {
Amit Shekhard02f2cd2014-01-16 16:51:43 -0800582 if (virt_ctxt->ctl)
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700583 offload_virtualizer_send_params(virt_ctxt->ctl, &virt_ctxt->offload_virt,
Amit Shekhard02f2cd2014-01-16 16:51:43 -0800584 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
585 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700586 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 Buchac09ae062018-11-14 13:25:08 +0530592 enable_gcov();
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800593 return 0;
594}
595
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700596int virtualizer_stop(effect_context_t *context, output_context_t *output __unused)
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800597{
598 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
599
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530600 ALOGV("%s: ctxt %p", __func__, virt_ctxt);
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700601 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 Naripeddy3eedc002013-11-12 20:45:15 -0800608 virt_ctxt->ctl = NULL;
Vatsal Buchac09ae062018-11-14 13:25:08 +0530609 enable_gcov();
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800610 return 0;
611}
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700612
613int 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}