post_proc: add support for virtualizer capability query

Add extra virtualizer interfaces to keep align with aosp design.
Extended capabilities include:
- Stub interface to allow querying speaker angles.
- Force set output device as specific virtualize mode.
- Query virtualize mode

Stub function remains to be refined in sync with upcoming aosp
changes.

Change-Id: I3316f6d944db1c9954eda7643a5ce433defa1a6c
CRs-Fixed: 810294
diff --git a/post_proc/bundle.c b/post_proc/bundle.c
index 410e17b..606c66b 100644
--- a/post_proc/bundle.c
+++ b/post_proc/bundle.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright (C) 2013 The Android Open Source Project
@@ -713,8 +713,8 @@
 /* Stub function for effect interface: never called for offloaded effects */
 /* called for hw accelerated effects */
 int effect_process(effect_handle_t self,
-                       audio_buffer_t *inBuffer,
-                       audio_buffer_t *outBuffer)
+                       audio_buffer_t *inBuffer __unused,
+                       audio_buffer_t *outBuffer __unused)
 {
     effect_context_t * context = (effect_context_t *)self;
     int status = 0;
@@ -847,6 +847,8 @@
         } break;
     case EFFECT_CMD_SET_PARAM: {
         if (pCmdData == NULL ||
+            cmdSize > (int)(sizeof(effect_param_t) + sizeof(uint32_t) +
+                            sizeof(uint32_t)) ||
             cmdSize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) +
                             sizeof(uint16_t)) ||
             pReplyData == NULL || *replySize != sizeof(int32_t)) {
diff --git a/post_proc/virtualizer.c b/post_proc/virtualizer.c
index f6e2881..2748568 100644
--- a/post_proc/virtualizer.c
+++ b/post_proc/virtualizer.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright (C) 2013 The Android Open Source Project
@@ -69,6 +69,197 @@
     return 0;
 }
 
+/*
+ *  Check if an audio device is supported by this implementation
+ *
+ *  [in]
+ *  device    device that is intented for processing (e.g. for binaural vs transaural)
+ *  [out]
+ *  false     device is not applicable for effect
+ *  true      device is applicable for effect
+ */
+bool virtualizer_is_device_supported(audio_devices_t device) {
+    switch (device) {
+    case AUDIO_DEVICE_OUT_SPEAKER:
+    case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
+    case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
+#ifdef AFE_PROXY_ENABLED
+    case AUDIO_DEVICE_OUT_PROXY:
+#endif
+    case AUDIO_DEVICE_OUT_AUX_DIGITAL:
+    case AUDIO_DEVICE_OUT_USB_ACCESSORY:
+    case AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET:
+        return false;
+    default :
+        return true;
+    }
+}
+
+/*
+ *  Check if a channel mask + audio device is supported by this implementation
+ *
+ *  [in]
+ *  channel_mask channel mask of input buffer
+ *  device       device that is intented for processing (e.g. for binaural vs transaural)
+ *  [out]
+ *  false        if the configuration is not supported or it is unknown
+ *  true         if the configuration is supported
+ */
+bool virtualizer_is_configuration_supported(audio_channel_mask_t channel_mask,
+        audio_devices_t device) {
+    uint32_t channelCount = audio_channel_count_from_out_mask(channel_mask);
+    if ((channelCount == 0) || (channelCount > 2)) {
+        return false;
+    }
+
+    return virtualizer_is_device_supported(device);
+}
+
+/*
+ *  Force the virtualization mode to that of the given audio device
+ *
+ *  [in]
+ *  context       effect engine context
+ *  forced_device device whose virtualization mode we'll always use
+ *  [out]
+ *  -EINVAL       if the device is not supported or is unknown
+ *  0             if the device is supported and the virtualization mode forced
+ */
+int virtualizer_force_virtualization_mode(virtualizer_context_t *context,
+        audio_devices_t forced_device) {
+    virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
+    int status = 0;
+    bool use_virt = false;
+    int is_virt_enabled =
+        offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt));
+
+    ALOGV("%s: ctxt %p, forcedDev=0x%x enabled=%d tmpDisabled=%d", __func__, virt_ctxt,
+            forced_device, is_virt_enabled, virt_ctxt->temp_disabled);
+
+    if (virtualizer_is_device_supported(forced_device) == false) {
+        if (forced_device != AUDIO_DEVICE_NONE) {
+            //forced device is not supported, make it behave as a reset of forced mode
+            forced_device = AUDIO_DEVICE_NONE;
+            // but return an error
+            status = -EINVAL;
+        }
+    }
+
+    if (forced_device == AUDIO_DEVICE_NONE) {
+        // disabling forced virtualization mode:
+        // verify whether the virtualization should be enabled or disabled
+        if (virtualizer_is_device_supported(virt_ctxt->device)) {
+            use_virt = (is_virt_enabled == true);
+        }
+        virt_ctxt->forced_device = AUDIO_DEVICE_NONE;
+    } else {
+        // forcing virtualization mode:
+        // TODO: we assume device is supported, so hard coded a fixed one.
+        virt_ctxt->forced_device = AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
+        // TODO: only enable for a supported mode, when the effect is enabled
+        use_virt = (is_virt_enabled == true);
+    }
+
+    if (use_virt) {
+        if (virt_ctxt->temp_disabled == true) {
+            if (effect_is_active(&virt_ctxt->common)) {
+                offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true);
+                if (virt_ctxt->ctl)
+                    offload_virtualizer_send_params(virt_ctxt->ctl,
+                                                    &virt_ctxt->offload_virt,
+                                                    OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
+                if (virt_ctxt->hw_acc_fd > 0)
+                    hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
+                                                   &virt_ctxt->offload_virt,
+                                                   OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
+            }
+            ALOGV("%s: re-enable VIRTUALIZER", __func__);
+            virt_ctxt->temp_disabled = false;
+        } else {
+            ALOGV("%s: leaving VIRTUALIZER enabled", __func__);
+        }
+    } else {
+        if (virt_ctxt->temp_disabled == false) {
+            if (effect_is_active(&virt_ctxt->common)) {
+                offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false);
+                if (virt_ctxt->ctl)
+                    offload_virtualizer_send_params(virt_ctxt->ctl,
+                                                    &virt_ctxt->offload_virt,
+                                                    OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
+                if (virt_ctxt->hw_acc_fd > 0)
+                    hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
+                                                   &virt_ctxt->offload_virt,
+                                                   OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
+            }
+            ALOGV("%s: disable VIRTUALIZER", __func__);
+            virt_ctxt->temp_disabled = true;
+        } else {
+            ALOGV("%s: leaving VIRTUALIZER disabled", __func__);
+        }
+    }
+
+    ALOGV("after %s: ctxt %p, enabled=%d tmpDisabled=%d", __func__, virt_ctxt,
+            is_virt_enabled, virt_ctxt->temp_disabled);
+
+    return status;
+}
+
+/*
+ *  Get the virtual speaker angles for a channel mask + audio device configuration
+ *  which is guaranteed to be supported by this implementation
+ *
+ *  [in]
+ *  channel_mask   the channel mask of the input to virtualize
+ *  device         the type of device that affects the processing (e.g. for binaural vs transaural)
+ *  [in/out]
+ *  speaker_angles the array of integer where each speaker angle is written as a triplet in the
+ *                 following format:
+ *                  int32_t a bit mask with a single value selected for each speaker, following
+ *                  the convention of the audio_channel_mask_t type
+ *                  int32_t a value in degrees expressing the speaker azimuth, where 0 is in front
+ *                  of the user, 180 behind, -90 to the left, 90 to the right of the user
+ *                  int32_t a value in degrees expressing the speaker elevation, where 0 is the
+ *                  horizontal plane, +90 is directly above the user, -90 below
+ *
+ */
+void virtualizer_get_speaker_angles(audio_channel_mask_t channel_mask __unused,
+        audio_devices_t device __unused, int32_t *speaker_angles) {
+    // the channel count is guaranteed to be 1 or 2
+    // the device is guaranteed to be of type headphone
+    // this virtualizer is always 2in with speakers at -90 and 90deg of azimuth, 0deg of elevation
+    *speaker_angles++ = (int32_t) AUDIO_CHANNEL_OUT_FRONT_LEFT;
+    *speaker_angles++ = -90; // azimuth
+    *speaker_angles++ = 0;   // elevation
+    *speaker_angles++ = (int32_t) AUDIO_CHANNEL_OUT_FRONT_RIGHT;
+    *speaker_angles++ = 90;  // azimuth
+    *speaker_angles   = 0;   // elevation
+}
+
+/*
+ *  Retrieve the current device whose processing mode is used by this effect
+ *
+ *  [out]
+ *  AUDIO_DEVICE_NONE if the effect is not virtualizing
+ *  or the device type if the effect is virtualizing
+ */
+audio_devices_t virtualizer_get_virtualization_mode(virtualizer_context_t *context) {
+    virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
+    audio_devices_t device = AUDIO_DEVICE_NONE;
+
+    if ((offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)))
+            && (virt_ctxt->temp_disabled == false)) {
+        if (virt_ctxt->forced_device != AUDIO_DEVICE_NONE) {
+            // virtualization mode is forced, return that device
+            device = virt_ctxt->forced_device;
+        } else {
+            // no forced mode, return the current device
+            device = virt_ctxt->device;
+        }
+    }
+    ALOGV("%s: returning 0x%x", __func__, device);
+    return device;
+}
+
 int virtualizer_get_parameter(effect_context_t *context, effect_param_t *p,
                               uint32_t *size)
 {
@@ -94,6 +285,15 @@
            p->status = -EINVAL;
         p->vsize = sizeof(int16_t);
         break;
+    case VIRTUALIZER_PARAM_VIRTUAL_SPEAKER_ANGLES:
+        // return value size can only be interpreted as relative to input value,
+        // deferring validity check to below
+        break;
+    case VIRTUALIZER_PARAM_VIRTUALIZATION_MODE:
+        if (p->vsize != sizeof(uint32_t))
+           p->status = -EINVAL;
+        p->vsize = sizeof(uint32_t);
+        break;
     default:
         p->status = -EINVAL;
     }
@@ -112,6 +312,31 @@
         *(int16_t *)value = virtualizer_get_strength(virt_ctxt);
         break;
 
+    case VIRTUALIZER_PARAM_VIRTUAL_SPEAKER_ANGLES:
+    {
+        const audio_channel_mask_t channel_mask = (audio_channel_mask_t) *param_tmp++;
+        const audio_devices_t device = (audio_devices_t) *param_tmp;
+        uint32_t channel_cnt = audio_channel_count_from_out_mask(channel_mask);
+
+        if (p->vsize < 3 * channel_cnt * sizeof(int32_t)){
+            p->status = -EINVAL;
+            break;
+        }
+        // verify the configuration is supported
+        if(virtualizer_is_configuration_supported(channel_mask, device)) {
+            // configuration is supported, get the angles
+            virtualizer_get_speaker_angles(channel_mask, device, (int32_t *)value);
+        } else {
+            p->status = -EINVAL;
+        }
+
+        break;
+    }
+
+    case VIRTUALIZER_PARAM_VIRTUALIZATION_MODE:
+        *(uint32_t *)value  = (uint32_t) virtualizer_get_virtualization_mode(virt_ctxt);
+        break;
+
     default:
         p->status = -EINVAL;
         break;
@@ -121,7 +346,7 @@
 }
 
 int virtualizer_set_parameter(effect_context_t *context, effect_param_t *p,
-                              uint32_t size)
+                              uint32_t size __unused)
 {
     virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
     int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t);
@@ -139,6 +364,14 @@
         strength = (uint32_t)(*(int16_t *)value);
         virtualizer_set_strength(virt_ctxt, strength);
         break;
+    case VIRTUALIZER_PARAM_FORCE_VIRTUALIZATION_MODE:
+    {
+        const audio_devices_t device = *(audio_devices_t *)value;
+        if (0 != virtualizer_force_virtualization_mode(virt_ctxt, device)) {
+            p->status = -EINVAL;
+        }
+        break;
+    }
     default:
         p->status = -EINVAL;
         break;
@@ -153,46 +386,44 @@
 
     ALOGV("%s: ctxt %p, device: 0x%x", __func__, virt_ctxt, device);
     virt_ctxt->device = device;
-    if((device == AUDIO_DEVICE_OUT_SPEAKER) ||
-       (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) ||
-       (device == AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER) ||
-#ifdef AFE_PROXY_ENABLED
-       (device == AUDIO_DEVICE_OUT_PROXY) ||
-#endif
-       (device == AUDIO_DEVICE_OUT_AUX_DIGITAL) ||
-       (device == AUDIO_DEVICE_OUT_USB_ACCESSORY) ||
-       (device == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET)) {
-        if (!virt_ctxt->temp_disabled) {
-            if (effect_is_active(&virt_ctxt->common)) {
-                offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false);
-                if (virt_ctxt->ctl)
-                    offload_virtualizer_send_params(virt_ctxt->ctl,
-                                                    &virt_ctxt->offload_virt,
-                                                    OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
-                if (virt_ctxt->hw_acc_fd > 0)
-                    hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
-                                                   &virt_ctxt->offload_virt,
-                                                   OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
+
+    if (virt_ctxt->forced_device == AUDIO_DEVICE_NONE) {
+        // default case unless configuration is forced
+        if (virtualizer_is_device_supported(device) == false) {
+            if (!virt_ctxt->temp_disabled) {
+                if (effect_is_active(&virt_ctxt->common)) {
+                    offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), false);
+                    if (virt_ctxt->ctl)
+                        offload_virtualizer_send_params(virt_ctxt->ctl,
+                                                        &virt_ctxt->offload_virt,
+                                                        OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
+                    if (virt_ctxt->hw_acc_fd > 0)
+                        hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
+                                                       &virt_ctxt->offload_virt,
+                                                       OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
+                }
+                virt_ctxt->temp_disabled = true;
+                ALOGI("%s: ctxt %p, disabled based on device", __func__, virt_ctxt);
             }
-            virt_ctxt->temp_disabled = true;
-            ALOGI("%s: ctxt %p, disabled based on device", __func__, virt_ctxt);
-        }
-    } else {
-        if (virt_ctxt->temp_disabled) {
-            if (effect_is_active(&virt_ctxt->common)) {
-                offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true);
-                if (virt_ctxt->ctl)
-                    offload_virtualizer_send_params(virt_ctxt->ctl,
-                                                    &virt_ctxt->offload_virt,
-                                                    OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
-                if (virt_ctxt->hw_acc_fd > 0)
-                    hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
-                                                   &virt_ctxt->offload_virt,
-                                                   OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
+        } else {
+            if (virt_ctxt->temp_disabled) {
+                if (effect_is_active(&virt_ctxt->common)) {
+                    offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true);
+                    if (virt_ctxt->ctl)
+                        offload_virtualizer_send_params(virt_ctxt->ctl,
+                                                        &virt_ctxt->offload_virt,
+                                                        OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
+                    if (virt_ctxt->hw_acc_fd > 0)
+                        hw_acc_virtualizer_send_params(virt_ctxt->hw_acc_fd,
+                                                       &virt_ctxt->offload_virt,
+                                                       OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG);
+                }
+                virt_ctxt->temp_disabled = false;
             }
-            virt_ctxt->temp_disabled = false;
         }
     }
+    // else virtualization mode is forced to a certain device, nothing to do
+
     offload_virtualizer_set_device(&(virt_ctxt->offload_virt), device);
     return 0;
 }
@@ -230,6 +461,8 @@
 
     virt_ctxt->temp_disabled = false;
     virt_ctxt->hw_acc_fd = -1;
+    virt_ctxt->forced_device = AUDIO_DEVICE_NONE;
+    virt_ctxt->device = AUDIO_DEVICE_NONE;
     memset(&(virt_ctxt->offload_virt), 0, sizeof(struct virtualizer_params));
 
     return 0;
diff --git a/post_proc/virtualizer.h b/post_proc/virtualizer.h
index 440c8a2..b5293fb 100644
--- a/post_proc/virtualizer.h
+++ b/post_proc/virtualizer.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright (C) 2013 The Android Open Source Project
@@ -33,7 +33,8 @@
     struct mixer_ctl *ctl;
     int hw_acc_fd;
     bool temp_disabled;
-    uint32_t device;
+    audio_devices_t forced_device;
+    audio_devices_t device;
     struct virtualizer_params offload_virt;
 } virtualizer_context_t;