blob: 0750052b58d3dbe334f743c169df021d4c2e0fbc [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>
28
29#include "effect_api.h"
30#include "virtualizer.h"
31
Trinath Thammishetty765efd22018-08-20 13:23:24 +053032#define VIRUALIZER_MAX_LATENCY 30
33
Vatsal Buchac09ae062018-11-14 13:25:08 +053034#ifdef AUDIO_FEATURE_ENABLED_GCOV
35extern void __gcov_flush();
36static void enable_gcov()
37{
38 __gcov_flush();
39}
40#else
41static void enable_gcov()
42{
43}
44#endif
45
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080046/* Offload Virtualizer UUID: 509a4498-561a-4bea-b3b1-0002a5d5c51b */
47const effect_descriptor_t virtualizer_descriptor = {
48 {0x37cc2c00, 0xdddd, 0x11db, 0x8577, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
49 {0x509a4498, 0x561a, 0x4bea, 0xb3b1, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
50 EFFECT_CONTROL_API_VERSION,
Weiyin Jiang90ac1ea2017-04-13 14:18:23 +080051 (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_DEVICE_IND | EFFECT_FLAG_HW_ACC_TUNNEL |
52 EFFECT_FLAG_VOLUME_CTRL),
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080053 0, /* TODO */
54 1,
55 "MSM offload virtualizer",
56 "The Android Open Source Project",
57};
58
59/*
60 * Virtualizer operations
61 */
62
63int virtualizer_get_strength(virtualizer_context_t *context)
64{
Dhananjay Kumar574f3922014-03-25 17:41:44 +053065 ALOGV("%s: ctxt %p, strength: %d", __func__, context, context->strength);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080066 return context->strength;
67}
68
69int virtualizer_set_strength(virtualizer_context_t *context, uint32_t strength)
70{
Dhananjay Kumar574f3922014-03-25 17:41:44 +053071 ALOGV("%s: ctxt %p, strength: %d", __func__, context, strength);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080072 context->strength = strength;
73
Weiyin Jiangc8082d92015-07-21 11:26:02 +080074 /*
75 * Zero strength is not equivalent to disable state as down mix
76 * is still happening for multichannel inputs.
77 * For better user experience, explicitly disable virtualizer module
78 * when strength is 0.
79 */
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +053080 if (context->enabled_by_client)
81 offload_virtualizer_set_enable_flag(&(context->offload_virt),
Weiyin Jiangc8082d92015-07-21 11:26:02 +080082 ((strength > 0) && !(context->temp_disabled)) ?
83 true : false);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080084 offload_virtualizer_set_strength(&(context->offload_virt), strength);
85 if (context->ctl)
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -070086 offload_virtualizer_send_params(context->ctl, &context->offload_virt,
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080087 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
88 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -070089 if (context->hw_acc_fd > 0)
90 hw_acc_virtualizer_send_params(context->hw_acc_fd,
91 &context->offload_virt,
92 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
93 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -080094 return 0;
95}
96
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +080097/*
98 * Check if an audio device is supported by this implementation
99 *
100 * [in]
101 * device device that is intented for processing (e.g. for binaural vs transaural)
102 * [out]
103 * false device is not applicable for effect
104 * true device is applicable for effect
105 */
106bool virtualizer_is_device_supported(audio_devices_t device) {
107 switch (device) {
108 case AUDIO_DEVICE_OUT_SPEAKER:
109 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
110 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
111#ifdef AFE_PROXY_ENABLED
112 case AUDIO_DEVICE_OUT_PROXY:
113#endif
114 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
115 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
116 case AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET:
117 return false;
118 default :
119 return true;
120 }
121}
122
123/*
124 * Check if a channel mask + audio device is supported by this implementation
125 *
126 * [in]
127 * channel_mask channel mask of input buffer
128 * device device that is intented for processing (e.g. for binaural vs transaural)
129 * [out]
130 * false if the configuration is not supported or it is unknown
131 * true if the configuration is supported
132 */
133bool virtualizer_is_configuration_supported(audio_channel_mask_t channel_mask,
134 audio_devices_t device) {
135 uint32_t channelCount = audio_channel_count_from_out_mask(channel_mask);
136 if ((channelCount == 0) || (channelCount > 2)) {
137 return false;
138 }
139
140 return virtualizer_is_device_supported(device);
141}
142
143/*
144 * Force the virtualization mode to that of the given audio device
145 *
146 * [in]
147 * context effect engine context
148 * forced_device device whose virtualization mode we'll always use
149 * [out]
150 * -EINVAL if the device is not supported or is unknown
151 * 0 if the device is supported and the virtualization mode forced
152 */
153int virtualizer_force_virtualization_mode(virtualizer_context_t *context,
154 audio_devices_t forced_device) {
155 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
156 int status = 0;
157 bool use_virt = false;
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530158 int is_virt_enabled = virt_ctxt->enabled_by_client;
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800159
160 ALOGV("%s: ctxt %p, forcedDev=0x%x enabled=%d tmpDisabled=%d", __func__, virt_ctxt,
161 forced_device, is_virt_enabled, virt_ctxt->temp_disabled);
162
163 if (virtualizer_is_device_supported(forced_device) == false) {
164 if (forced_device != AUDIO_DEVICE_NONE) {
165 //forced device is not supported, make it behave as a reset of forced mode
166 forced_device = AUDIO_DEVICE_NONE;
167 // but return an error
168 status = -EINVAL;
169 }
170 }
171
172 if (forced_device == AUDIO_DEVICE_NONE) {
173 // disabling forced virtualization mode:
174 // verify whether the virtualization should be enabled or disabled
175 if (virtualizer_is_device_supported(virt_ctxt->device)) {
176 use_virt = (is_virt_enabled == true);
177 }
178 virt_ctxt->forced_device = AUDIO_DEVICE_NONE;
179 } else {
180 // forcing virtualization mode:
181 // TODO: we assume device is supported, so hard coded a fixed one.
182 virt_ctxt->forced_device = AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
183 // TODO: only enable for a supported mode, when the effect is enabled
184 use_virt = (is_virt_enabled == true);
185 }
186
187 if (use_virt) {
188 if (virt_ctxt->temp_disabled == true) {
189 if (effect_is_active(&virt_ctxt->common)) {
190 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true);
191 if (virt_ctxt->ctl)
192 offload_virtualizer_send_params(virt_ctxt->ctl,
193 &virt_ctxt->offload_virt,
194 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
195 if (virt_ctxt->hw_acc_fd > 0)
196 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
197 &virt_ctxt->offload_virt,
198 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
199 }
200 ALOGV("%s: re-enable VIRTUALIZER", __func__);
201 virt_ctxt->temp_disabled = false;
202 } else {
203 ALOGV("%s: leaving VIRTUALIZER enabled", __func__);
204 }
205 } else {
206 if (virt_ctxt->temp_disabled == false) {
207 if (effect_is_active(&virt_ctxt->common)) {
208 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false);
209 if (virt_ctxt->ctl)
210 offload_virtualizer_send_params(virt_ctxt->ctl,
211 &virt_ctxt->offload_virt,
212 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
213 if (virt_ctxt->hw_acc_fd > 0)
214 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
215 &virt_ctxt->offload_virt,
216 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
217 }
218 ALOGV("%s: disable VIRTUALIZER", __func__);
219 virt_ctxt->temp_disabled = true;
220 } else {
221 ALOGV("%s: leaving VIRTUALIZER disabled", __func__);
222 }
223 }
224
225 ALOGV("after %s: ctxt %p, enabled=%d tmpDisabled=%d", __func__, virt_ctxt,
226 is_virt_enabled, virt_ctxt->temp_disabled);
227
228 return status;
229}
230
231/*
232 * Get the virtual speaker angles for a channel mask + audio device configuration
233 * which is guaranteed to be supported by this implementation
234 *
235 * [in]
236 * channel_mask the channel mask of the input to virtualize
237 * device the type of device that affects the processing (e.g. for binaural vs transaural)
238 * [in/out]
239 * speaker_angles the array of integer where each speaker angle is written as a triplet in the
240 * following format:
241 * int32_t a bit mask with a single value selected for each speaker, following
242 * the convention of the audio_channel_mask_t type
243 * int32_t a value in degrees expressing the speaker azimuth, where 0 is in front
244 * of the user, 180 behind, -90 to the left, 90 to the right of the user
245 * int32_t a value in degrees expressing the speaker elevation, where 0 is the
246 * horizontal plane, +90 is directly above the user, -90 below
247 *
248 */
249void virtualizer_get_speaker_angles(audio_channel_mask_t channel_mask __unused,
250 audio_devices_t device __unused, int32_t *speaker_angles) {
251 // the channel count is guaranteed to be 1 or 2
252 // the device is guaranteed to be of type headphone
253 // this virtualizer is always 2in with speakers at -90 and 90deg of azimuth, 0deg of elevation
254 *speaker_angles++ = (int32_t) AUDIO_CHANNEL_OUT_FRONT_LEFT;
255 *speaker_angles++ = -90; // azimuth
256 *speaker_angles++ = 0; // elevation
257 *speaker_angles++ = (int32_t) AUDIO_CHANNEL_OUT_FRONT_RIGHT;
258 *speaker_angles++ = 90; // azimuth
259 *speaker_angles = 0; // elevation
260}
261
262/*
263 * Retrieve the current device whose processing mode is used by this effect
264 *
265 * [out]
266 * AUDIO_DEVICE_NONE if the effect is not virtualizing
267 * or the device type if the effect is virtualizing
268 */
269audio_devices_t virtualizer_get_virtualization_mode(virtualizer_context_t *context) {
270 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
271 audio_devices_t device = AUDIO_DEVICE_NONE;
272
273 if ((offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)))
274 && (virt_ctxt->temp_disabled == false)) {
275 if (virt_ctxt->forced_device != AUDIO_DEVICE_NONE) {
276 // virtualization mode is forced, return that device
277 device = virt_ctxt->forced_device;
278 } else {
279 // no forced mode, return the current device
280 device = virt_ctxt->device;
281 }
282 }
283 ALOGV("%s: returning 0x%x", __func__, device);
284 return device;
285}
286
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800287int virtualizer_get_parameter(effect_context_t *context, effect_param_t *p,
288 uint32_t *size)
289{
290 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
291 int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
292 int32_t *param_tmp = (int32_t *)p->data;
293 int32_t param = *param_tmp++;
294 void *value = p->data + voffset;
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800295
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530296 ALOGV("%s: ctxt %p, param %d", __func__, virt_ctxt, param);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800297
298 p->status = 0;
299
300 switch (param) {
301 case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED:
302 if (p->vsize < sizeof(uint32_t))
303 p->status = -EINVAL;
304 p->vsize = sizeof(uint32_t);
305 break;
306 case VIRTUALIZER_PARAM_STRENGTH:
307 if (p->vsize < sizeof(int16_t))
308 p->status = -EINVAL;
309 p->vsize = sizeof(int16_t);
310 break;
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800311 case VIRTUALIZER_PARAM_VIRTUAL_SPEAKER_ANGLES:
312 // return value size can only be interpreted as relative to input value,
313 // deferring validity check to below
314 break;
315 case VIRTUALIZER_PARAM_VIRTUALIZATION_MODE:
316 if (p->vsize != sizeof(uint32_t))
317 p->status = -EINVAL;
318 p->vsize = sizeof(uint32_t);
319 break;
Trinath Thammishetty765efd22018-08-20 13:23:24 +0530320 case VIRTUALIZER_PARAM_LATENCY:
321 if (p->vsize < sizeof(uint32_t))
322 p->status = -EINVAL;
323 p->vsize = sizeof(uint32_t);
324 break;
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800325 default:
326 p->status = -EINVAL;
327 }
328
329 *size = sizeof(effect_param_t) + voffset + p->vsize;
330
331 if (p->status != 0)
332 return 0;
333
334 switch (param) {
335 case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED:
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800336 *(uint32_t *)value = 1;
337 break;
338
339 case VIRTUALIZER_PARAM_STRENGTH:
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800340 *(int16_t *)value = virtualizer_get_strength(virt_ctxt);
341 break;
342
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800343 case VIRTUALIZER_PARAM_VIRTUAL_SPEAKER_ANGLES:
344 {
345 const audio_channel_mask_t channel_mask = (audio_channel_mask_t) *param_tmp++;
346 const audio_devices_t device = (audio_devices_t) *param_tmp;
347 uint32_t channel_cnt = audio_channel_count_from_out_mask(channel_mask);
348
349 if (p->vsize < 3 * channel_cnt * sizeof(int32_t)){
350 p->status = -EINVAL;
351 break;
352 }
353 // verify the configuration is supported
354 if(virtualizer_is_configuration_supported(channel_mask, device)) {
355 // configuration is supported, get the angles
356 virtualizer_get_speaker_angles(channel_mask, device, (int32_t *)value);
357 } else {
358 p->status = -EINVAL;
359 }
360
361 break;
362 }
363
364 case VIRTUALIZER_PARAM_VIRTUALIZATION_MODE:
365 *(uint32_t *)value = (uint32_t) virtualizer_get_virtualization_mode(virt_ctxt);
366 break;
367
Trinath Thammishetty765efd22018-08-20 13:23:24 +0530368 case VIRTUALIZER_PARAM_LATENCY:
369 *(uint32_t *)value = VIRUALIZER_MAX_LATENCY;
370 break;
371
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800372 default:
373 p->status = -EINVAL;
374 break;
375 }
376
377 return 0;
378}
379
380int virtualizer_set_parameter(effect_context_t *context, effect_param_t *p,
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800381 uint32_t size __unused)
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800382{
383 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
384 int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
385 void *value = p->data + voffset;
386 int32_t *param_tmp = (int32_t *)p->data;
387 int32_t param = *param_tmp++;
388 uint32_t strength;
389
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530390 ALOGV("%s: ctxt %p, param %d", __func__, virt_ctxt, param);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800391
392 p->status = 0;
393
394 switch (param) {
395 case VIRTUALIZER_PARAM_STRENGTH:
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800396 strength = (uint32_t)(*(int16_t *)value);
397 virtualizer_set_strength(virt_ctxt, strength);
398 break;
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800399 case VIRTUALIZER_PARAM_FORCE_VIRTUALIZATION_MODE:
400 {
401 const audio_devices_t device = *(audio_devices_t *)value;
402 if (0 != virtualizer_force_virtualization_mode(virt_ctxt, device)) {
403 p->status = -EINVAL;
404 }
405 break;
406 }
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800407 default:
408 p->status = -EINVAL;
409 break;
410 }
411
412 return 0;
413}
414
415int virtualizer_set_device(effect_context_t *context, uint32_t device)
416{
417 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
418
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530419 ALOGV("%s: ctxt %p, device: 0x%x", __func__, virt_ctxt, device);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800420 virt_ctxt->device = device;
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800421
422 if (virt_ctxt->forced_device == AUDIO_DEVICE_NONE) {
423 // default case unless configuration is forced
424 if (virtualizer_is_device_supported(device) == false) {
425 if (!virt_ctxt->temp_disabled) {
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530426 if (effect_is_active(&virt_ctxt->common) && virt_ctxt->enabled_by_client) {
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800427 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false);
428 if (virt_ctxt->ctl)
429 offload_virtualizer_send_params(virt_ctxt->ctl,
430 &virt_ctxt->offload_virt,
431 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
432 if (virt_ctxt->hw_acc_fd > 0)
433 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
434 &virt_ctxt->offload_virt,
435 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
436 }
437 virt_ctxt->temp_disabled = true;
438 ALOGI("%s: ctxt %p, disabled based on device", __func__, virt_ctxt);
wjiang50b81f42014-08-06 08:03:14 +0800439 }
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800440 } else {
441 if (virt_ctxt->temp_disabled) {
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530442 if (effect_is_active(&virt_ctxt->common) && virt_ctxt->enabled_by_client) {
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800443 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true);
444 if (virt_ctxt->ctl)
445 offload_virtualizer_send_params(virt_ctxt->ctl,
446 &virt_ctxt->offload_virt,
447 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
448 if (virt_ctxt->hw_acc_fd > 0)
449 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
450 &virt_ctxt->offload_virt,
451 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
452 }
453 virt_ctxt->temp_disabled = false;
wjiang50b81f42014-08-06 08:03:14 +0800454 }
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800455 }
456 }
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800457 // else virtualization mode is forced to a certain device, nothing to do
458
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800459 offload_virtualizer_set_device(&(virt_ctxt->offload_virt), device);
460 return 0;
461}
462
Aalique Grahame22e49102018-12-18 14:23:57 -0800463int virtualizer_reset(effect_context_t *context __unused)
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800464{
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800465 return 0;
466}
467
468int virtualizer_init(effect_context_t *context)
469{
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530470 ALOGV("%s: ctxt %p", __func__, context);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800471 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
472
473 context->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
474 context->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
475 context->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
476 context->config.inputCfg.samplingRate = 44100;
477 context->config.inputCfg.bufferProvider.getBuffer = NULL;
478 context->config.inputCfg.bufferProvider.releaseBuffer = NULL;
479 context->config.inputCfg.bufferProvider.cookie = NULL;
480 context->config.inputCfg.mask = EFFECT_CONFIG_ALL;
481 context->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
482 context->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
483 context->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
484 context->config.outputCfg.samplingRate = 44100;
485 context->config.outputCfg.bufferProvider.getBuffer = NULL;
486 context->config.outputCfg.bufferProvider.releaseBuffer = NULL;
487 context->config.outputCfg.bufferProvider.cookie = NULL;
488 context->config.outputCfg.mask = EFFECT_CONFIG_ALL;
489
490 set_config(context, &context->config);
491
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530492 virt_ctxt->enabled_by_client = false;
wjiang50b81f42014-08-06 08:03:14 +0800493 virt_ctxt->temp_disabled = false;
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700494 virt_ctxt->hw_acc_fd = -1;
Weiyin Jiangb64b7dd2015-03-23 00:54:14 +0800495 virt_ctxt->forced_device = AUDIO_DEVICE_NONE;
496 virt_ctxt->device = AUDIO_DEVICE_NONE;
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800497 memset(&(virt_ctxt->offload_virt), 0, sizeof(struct virtualizer_params));
Vatsal Buchac09ae062018-11-14 13:25:08 +0530498 enable_gcov();
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800499 return 0;
500}
501
502int virtualizer_enable(effect_context_t *context)
503{
504 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
505
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530506 ALOGV("%s: ctxt %p, strength %d", __func__, virt_ctxt, virt_ctxt->strength);
wjiang95d74c22014-03-28 12:29:58 +0800507
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530508 virt_ctxt->enabled_by_client = true;
wjiang95d74c22014-03-28 12:29:58 +0800509 if (!offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)) &&
510 !(virt_ctxt->temp_disabled)) {
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800511 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true);
wjiangd45948e2014-02-24 22:19:43 +0800512 if (virt_ctxt->ctl && virt_ctxt->strength)
513 offload_virtualizer_send_params(virt_ctxt->ctl,
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700514 &virt_ctxt->offload_virt,
wjiangd45948e2014-02-24 22:19:43 +0800515 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700516 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
517 if ((virt_ctxt->hw_acc_fd > 0) && virt_ctxt->strength)
518 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
519 &virt_ctxt->offload_virt,
520 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
521 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
wjiangd45948e2014-02-24 22:19:43 +0800522 }
Vatsal Buchac09ae062018-11-14 13:25:08 +0530523 enable_gcov();
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800524 return 0;
525}
526
527int virtualizer_disable(effect_context_t *context)
528{
529 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
530
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530531 ALOGV("%s: ctxt %p", __func__, virt_ctxt);
Dhananjay Kumarf7026ef2015-12-15 10:51:10 +0530532
533 virt_ctxt->enabled_by_client = false;
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800534 if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))) {
535 offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false);
536 if (virt_ctxt->ctl)
537 offload_virtualizer_send_params(virt_ctxt->ctl,
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700538 &virt_ctxt->offload_virt,
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800539 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700540 if (virt_ctxt->hw_acc_fd > 0)
541 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
542 &virt_ctxt->offload_virt,
543 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800544 }
Vatsal Buchac09ae062018-11-14 13:25:08 +0530545 enable_gcov();
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800546 return 0;
547}
548
549int virtualizer_start(effect_context_t *context, output_context_t *output)
550{
551 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
552
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530553 ALOGV("%s: ctxt %p, ctl %p", __func__, virt_ctxt, output->ctl);
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800554 virt_ctxt->ctl = output->ctl;
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700555 if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))) {
Amit Shekhard02f2cd2014-01-16 16:51:43 -0800556 if (virt_ctxt->ctl)
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700557 offload_virtualizer_send_params(virt_ctxt->ctl, &virt_ctxt->offload_virt,
Amit Shekhard02f2cd2014-01-16 16:51:43 -0800558 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
559 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700560 if (virt_ctxt->hw_acc_fd > 0)
561 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
562 &virt_ctxt->offload_virt,
563 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
564 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
565 }
Vatsal Buchac09ae062018-11-14 13:25:08 +0530566 enable_gcov();
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800567 return 0;
568}
569
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700570int virtualizer_stop(effect_context_t *context, output_context_t *output __unused)
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800571{
572 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
573
Dhananjay Kumar574f3922014-03-25 17:41:44 +0530574 ALOGV("%s: ctxt %p", __func__, virt_ctxt);
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700575 if (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)) &&
576 virt_ctxt->ctl) {
577 struct virtualizer_params virt;
578 virt.enable_flag = false;
579 offload_virtualizer_send_params(virt_ctxt->ctl, &virt,
580 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
581 }
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800582 virt_ctxt->ctl = NULL;
Vatsal Buchac09ae062018-11-14 13:25:08 +0530583 enable_gcov();
Subhash Chandra Bose Naripeddy3eedc002013-11-12 20:45:15 -0800584 return 0;
585}
Subhash Chandra Bose Naripeddye40a7cd2014-06-03 19:42:41 -0700586
587int virtualizer_set_mode(effect_context_t *context, int32_t hw_acc_fd)
588{
589 virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
590
591 ALOGV("%s: ctxt %p", __func__, virt_ctxt);
592 virt_ctxt->hw_acc_fd = hw_acc_fd;
593 if ((virt_ctxt->hw_acc_fd > 0) &&
594 (offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))))
595 hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
596 &virt_ctxt->offload_virt,
597 OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
598 OFFLOAD_SEND_VIRTUALIZER_STRENGTH);
599 return 0;
600}