hal: enable audio zoom feature dynamically

- based on libaudiozoom and runtime feature flag
  enable \ disable audio zoom feature

Change-Id: I3dcab630e84e99108a541614b3467f0e70c1f274
diff --git a/configs/kona/audio_configs.xml b/configs/kona/audio_configs.xml
index 8c24bb2..bcc617d 100644
--- a/configs/kona/audio_configs.xml
+++ b/configs/kona/audio_configs.xml
@@ -70,6 +70,7 @@
         <flag name="a2dp_offload_enabled" value="true" />
         <flag name="anc_headset_enabled" value="true" />
         <flag name="audiosphere_enabled" value="true" />
+        <flag name="audio_zoom_enabled" value="false" />
         <flag name="battery_listener_enabled" value="true" />
         <flag name="compress_capture_enabled" value="false" />
         <flag name="compress_in_enabled" value="true" />
diff --git a/configs/kona/audio_configs_stock.xml b/configs/kona/audio_configs_stock.xml
index b9ecf45..e26ef14 100644
--- a/configs/kona/audio_configs_stock.xml
+++ b/configs/kona/audio_configs_stock.xml
@@ -32,6 +32,7 @@
         <flag name="a2dp_offload_enabled" value="true" />
         <flag name="afe_proxy_enabled" value="false" />
         <flag name="anc_headset_enabled" value="false" />
+        <flag name="audio_zoom_enabled" value="false" />
         <flag name="audiosphere_enabled" value="false" />
         <flag name="battery_listener_enabled" value="false" />
         <flag name="compress_capture_enabled" value="false" />
diff --git a/configs/msmnile/audio_configs.xml b/configs/msmnile/audio_configs.xml
index 8c24bb2..40d04d7 100644
--- a/configs/msmnile/audio_configs.xml
+++ b/configs/msmnile/audio_configs.xml
@@ -69,6 +69,7 @@
         <!-- AHAL Configs -->
         <flag name="a2dp_offload_enabled" value="true" />
         <flag name="anc_headset_enabled" value="true" />
+        <flag name="audio_zoom_enabled" value="false" />
         <flag name="audiosphere_enabled" value="true" />
         <flag name="battery_listener_enabled" value="true" />
         <flag name="compress_capture_enabled" value="false" />
diff --git a/configs/msmnile/audio_configs_stock.xml b/configs/msmnile/audio_configs_stock.xml
index 6414675..bbc44c3 100644
--- a/configs/msmnile/audio_configs_stock.xml
+++ b/configs/msmnile/audio_configs_stock.xml
@@ -32,6 +32,7 @@
         <flag name="a2dp_offload_enabled" value="true" />
         <flag name="afe_proxy_enabled" value="false" />
         <flag name="anc_headset_enabled" value="false" />
+        <flag name="audio_zoom_enabled" value="true" />
         <flag name="audiosphere_enabled" value="false" />
         <flag name="battery_listener_enabled" value="false" />
         <flag name="compress_capture_enabled" value="false" />
diff --git a/hal/Android.mk b/hal/Android.mk
index 383a272..a6bd9bc 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -340,12 +340,6 @@
     LOCAL_STATIC_LIBRARIES += libprofile_rt
 endif
 
-ifeq ($(strip $(AUDIO_FEATURE_ENABLED_AUDIO_ZOOM)), true)
-    LOCAL_CFLAGS += -DAUDIOZOOM_QDSP_ENABLED
-    LOCAL_SRC_FILES += audio_extn/audiozoom.c
-endif
-
-
 #ifeq ($(strip $(AUDIO_FEATURE_ENABLED_AUTO_HAL)),true)
 #    LOCAL_CFLAGS += -DAUDIO_EXTN_AUTO_HAL_ENABLED
 #    LOCAL_SRC_FILES += audio_extn/auto_hal.c
diff --git a/hal/ahal_config_helper.cpp b/hal/ahal_config_helper.cpp
index e46b8f3..9313e5f 100644
--- a/hal/ahal_config_helper.cpp
+++ b/hal/ahal_config_helper.cpp
@@ -109,7 +109,8 @@
         true,        /* COMPRESS_METADATA_NEEDED */
         false,       /* INCALL_MUSIC */
         false,       /* COMPRESS_VOIP */
-        true         /* DYNAMIC_ECNS */
+        true,        /* DYNAMIC_ECNS */
+        false,       /* AUDIO_ZOOM */
     };
 #else
     if (isVendorEnhancedFwk) {
@@ -155,6 +156,7 @@
             true,        /* INCALL_MUSIC */
             false,       /* COMPRESS_VOIP */
             true,        /* DYNAMIC_ECNS */
+            false,       /* AUDIO_ZOOM */
         };
     } else {
         defaultConfigs = {
@@ -198,7 +200,8 @@
             false,       /* COMPRESS_METADATA_NEEDED */
             true,        /* INCALL_MUSIC */
             false,       /* COMPRESS_VOIP */
-            false        /* DYNAMIC_ECNS */
+            false,       /* DYNAMIC_ECNS */
+            true,        /* AUDIO_ZOOM */
         };
     }
 #endif
diff --git a/hal/ahal_config_helper.h b/hal/ahal_config_helper.h
index 39ed68e..d658f7d 100644
--- a/hal/ahal_config_helper.h
+++ b/hal/ahal_config_helper.h
@@ -76,6 +76,7 @@
     bool incall_music_enabled;
     bool compress_voip_enabled;
     bool dynamic_ecns_enabled;
+    bool audio_zoom_enabled;
 } AHalValues;
 
 #ifdef __cplusplus
diff --git a/hal/audio_extn/Android.mk b/hal/audio_extn/Android.mk
index feae999..f9e9c8a 100644
--- a/hal/audio_extn/Android.mk
+++ b/hal/audio_extn/Android.mk
@@ -765,3 +765,62 @@
 LOCAL_HEADER_LIBRARIES += libhardware_headers
 LOCAL_HEADER_LIBRARIES += libsystem_headers
 include $(BUILD_SHARED_LIBRARY)
+#-------------------------------------------
+#            Build AUDIOZOOM
+#-------------------------------------------
+include $(CLEAR_VARS)
+
+LOCAL_MODULE:= libaudiozoom
+LOCAL_VENDOR_MODULE := true
+
+PRIMARY_HAL_PATH := vendor/qcom/opensource/audio-hal/primary-hal/hal
+AUDIO_PLATFORM := $(TARGET_BOARD_PLATFORM)
+
+ifneq ($(filter sdm845 sdm710 msmnile kona sdm660 msm8937 $(MSMSTEPPE),$(TARGET_BOARD_PLATFORM)),)
+  # B-family platform uses msm8974 code base
+  AUDIO_PLATFORM = msm8974
+  MULTIPLE_HW_VARIANTS_ENABLED := true
+endif
+
+LOCAL_SRC_FILES:= \
+        audiozoom.c
+
+LOCAL_CFLAGS += \
+    -Wall \
+    -Werror \
+    -Wno-unused-function \
+    -Wno-unused-variable
+
+LOCAL_SHARED_LIBRARIES := \
+    libaudioutils \
+    libcutils \
+    liblog \
+    libtinyalsa \
+    libtinycompress \
+    libaudioroute \
+    libdl \
+    libexpat
+
+LOCAL_C_INCLUDES := \
+    $(PRIMARY_HAL_PATH) \
+    $(PRIMARY_HAL_PATH)/$(AUDIO_PLATFORM) \
+    external/tinyalsa/include \
+    external/tinycompress/include \
+    external/expat/lib \
+    system/media/audio_utils/include \
+    $(call include-path-for, audio-route) \
+
+LOCAL_C_INCLUDES += $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include
+LOCAL_C_INCLUDES += $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/techpack/audio/include
+LOCAL_ADDITIONAL_DEPENDENCIES += $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr
+
+ifeq ($(strip $(AUDIO_FEATURE_ENABLED_DLKM)),true)
+  LOCAL_HEADER_LIBRARIES += audio_kernel_headers
+  LOCAL_C_INCLUDES += $(TARGET_OUT_INTERMEDIATES)/vendor/qcom/opensource/audio-kernel/include
+endif
+
+LOCAL_HEADER_LIBRARIES += libhardware_headers
+LOCAL_HEADER_LIBRARIES += libsystem_headers
+include $(BUILD_SHARED_LIBRARY)
+
+
diff --git a/hal/audio_extn/audio_extn.c b/hal/audio_extn/audio_extn.c
index 24ed4c5..981e0d7 100644
--- a/hal/audio_extn/audio_extn.c
+++ b/hal/audio_extn/audio_extn.c
@@ -190,6 +190,7 @@
 static bool audio_extn_compress_in_enabled = false;
 static bool audio_extn_battery_listener_enabled = false;
 static bool audio_extn_maxx_audio_enabled = false;
+static bool audio_extn_audiozoom_enabled = false;
 
 #define AUDIO_PARAMETER_KEY_AANC_NOISE_LEVEL "aanc_noise_level"
 #define AUDIO_PARAMETER_KEY_ANC        "anc_enabled"
@@ -4751,6 +4752,105 @@
 }
 // END: BATTERY_LISTENER ================================================================
 
+// START: AUDIOZOOM_FEATURE =====================================================================
+#ifdef __LP64__
+#define AUDIOZOOM_LIB_PATH "/vendor/lib64/libaudiozoom.so"
+#else
+#define AUDIOZOOM_LIB_PATH "/vendor/lib/libaudiozoom.so"
+#endif
+
+static void *audiozoom_lib_handle = NULL;
+
+typedef int (*audiozoom_init_t)(audiozoom_init_config_t);
+static audiozoom_init_t audiozoom_init;
+
+typedef int (*audiozoom_set_microphone_direction_t)(struct stream_in *,
+                                                    audio_microphone_direction_t);
+static audiozoom_set_microphone_direction_t audiozoom_set_microphone_direction;
+
+typedef int (*audiozoom_set_microphone_field_dimension_t)(struct stream_in *, float);
+static audiozoom_set_microphone_field_dimension_t audiozoom_set_microphone_field_dimension;
+
+int audiozoom_feature_init(bool is_feature_enabled)
+{
+    audio_extn_audiozoom_enabled = is_feature_enabled;
+    ALOGD("%s: Called with feature %s", __func__,
+                  is_feature_enabled ? "Enabled" : "NOT Enabled");
+    if (is_feature_enabled) {
+        // dlopen lib
+        audiozoom_lib_handle = dlopen(AUDIOZOOM_LIB_PATH, RTLD_NOW);
+
+        if (!audiozoom_lib_handle) {
+            ALOGE("%s: dlopen failed", __func__);
+            goto feature_disabled;
+        }
+
+        if (!(audiozoom_init =
+                    (audiozoom_init_t)dlsym(audiozoom_lib_handle, "audiozoom_init")) ||
+            !(audiozoom_set_microphone_direction =
+                 (audiozoom_set_microphone_direction_t)dlsym(audiozoom_lib_handle,
+                                              "audiozoom_set_microphone_direction")) ||
+            !(audiozoom_set_microphone_field_dimension =
+                 (audiozoom_set_microphone_field_dimension_t)dlsym(audiozoom_lib_handle,
+                                        "audiozoom_set_microphone_field_dimension"))) {
+            ALOGE("%s: dlsym failed", __func__);
+            goto feature_disabled;
+        }
+
+        ALOGD("%s:: ---- Feature AUDIOZOOM is Enabled ----", __func__);
+        return 0;
+    }
+feature_disabled:
+    if (audiozoom_lib_handle) {
+        dlclose(audiozoom_lib_handle);
+        audiozoom_lib_handle = NULL;
+    }
+
+    audiozoom_init = NULL;
+    audiozoom_set_microphone_direction = NULL;
+    audiozoom_set_microphone_field_dimension = NULL;
+    ALOGW(":: %s: ---- Feature AUDIOZOOM is disabled ----", __func__);
+    return -ENOSYS;
+}
+
+bool audio_extn_is_audiozoom_enabled()
+{
+    return audio_extn_audiozoom_enabled;
+}
+
+int audio_extn_audiozoom_init()
+{
+     int ret_val = 0;
+     if (audiozoom_init) {
+        audiozoom_init_config_t init_config;
+        init_config.fp_platform_set_parameters = platform_set_parameters;
+        ret_val = audiozoom_init(init_config);
+     }
+
+     return ret_val;
+}
+
+int audio_extn_audiozoom_set_microphone_direction(struct stream_in *stream,
+                                           audio_microphone_direction_t dir)
+{
+     int ret_val = -ENOSYS;
+     if (audiozoom_set_microphone_direction)
+        ret_val = audiozoom_set_microphone_direction(stream, dir);
+
+     return ret_val;
+}
+
+int audio_extn_audiozoom_set_microphone_field_dimension(struct stream_in *stream,
+                                                         float zoom)
+{
+    int ret_val = -ENOSYS;
+    if (audiozoom_set_microphone_field_dimension)
+        ret_val = audiozoom_set_microphone_field_dimension(stream, zoom);
+
+    return ret_val;
+}
+// END:   AUDIOZOOM_FEATURE =====================================================================
+
 // START: MAXX_AUDIO =====================================================================
 #ifdef __LP64__
 #define MAXX_AUDIO_LIB_PATH "/vendor/lib64/libmaxxaudio.so"
@@ -4989,6 +5089,8 @@
             case MAXX_AUDIO:
                 maxx_audio_feature_init(enable);
                 break;
+            case AUDIO_ZOOM:
+                audiozoom_feature_init(enable);
             default:
                 break;
         }
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index e1e3ca0..0bd8877 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -326,6 +326,22 @@
 };
 typedef struct a2dp_offload_init_config a2dp_offload_init_config_t;
 // END: A2DP_OFFLOAD FEATURE ====================================================
+
+typedef int (*fp_platform_set_parameters_t)(void*, struct str_parms*);
+
+// START: AUDIOZOOM FEATURE ==================================================
+int audio_extn_audiozoom_init();
+int audio_extn_audiozoom_set_microphone_direction(struct stream_in *stream,
+                                           audio_microphone_direction_t dir);
+int audio_extn_audiozoom_set_microphone_field_dimension(struct stream_in *stream, float zoom);
+bool audio_extn_is_audiozoom_enabled();
+
+struct audiozoom_init_config {
+    fp_platform_set_parameters_t fp_platform_set_parameters;
+};
+typedef struct audiozoom_init_config audiozoom_init_config_t;
+// END:   AUDIOZOOM FEATURE ==================================================
+
 // START: MAXX_AUDIO FEATURE ==================================================
 void audio_extn_ma_init(void *platform);
 void audio_extn_ma_deinit();
@@ -336,7 +352,6 @@
                                   struct str_parms *parms);
 bool audio_extn_ma_supported_usb();
 bool audio_extn_is_maxx_audio_enabled();
-typedef int (*fp_platform_set_parameters_t)(void*, struct str_parms*);
 // --- Function pointers from audio_extn needed by MAXX_AUDIO
 struct maxx_audio_init_config {
     fp_platform_set_parameters_t fp_platform_set_parameters;
diff --git a/hal/audio_extn/audio_feature_manager.c b/hal/audio_extn/audio_feature_manager.c
index e121426..2de5af3 100644
--- a/hal/audio_extn/audio_feature_manager.c
+++ b/hal/audio_extn/audio_feature_manager.c
@@ -178,6 +178,8 @@
             return confValues->compress_voip_enabled;
         case DYNAMIC_ECNS:
             return confValues->dynamic_ecns_enabled;
+        case AUDIO_ZOOM:
+            return confValues->audio_zoom_enabled;
         default:
             return false;
     }
diff --git a/hal/audio_extn/audio_feature_manager.h b/hal/audio_extn/audio_feature_manager.h
index 8df076c..908b0cb 100644
--- a/hal/audio_extn/audio_feature_manager.h
+++ b/hal/audio_extn/audio_feature_manager.h
@@ -33,6 +33,7 @@
 #include <ahal_config_helper.h>
 
 enum audio_ext_feature_t {
+    // Start Audio feature flags
     SND_MONITOR = 0,
     COMPRESS_CAPTURE,
     SOURCE_TRACK,
@@ -71,10 +72,14 @@
     BATTERY_LISTENER,
     COMPRESS_METADATA_NEEDED,
     MAXX_AUDIO,
+    AUDIO_ZOOM,
+    // End Audio feature flags
+    // Start Voice feature flags
     COMPRESS_VOIP,
     VOICE_START = COMPRESS_VOIP,
     DYNAMIC_ECNS,
     INCALL_MUSIC,
+    // End Voice feature flags
     MAX_SUPPORTED_FEATURE
 };
 
diff --git a/hal/audio_extn/audiozoom.c b/hal/audio_extn/audiozoom.c
index 73e4862..9958cc4 100644
--- a/hal/audio_extn/audiozoom.c
+++ b/hal/audio_extn/audiozoom.c
@@ -23,7 +23,6 @@
 #include <expat.h>
 #include <audio_hw.h>
 #include <system/audio.h>
-#include <platform_api.h>
 #include "audio_extn.h"
 
 #include "audiozoom.h"
@@ -32,6 +31,9 @@
 
 #define AUDIOZOOM_PRESET_FILE "/vendor/etc/audiozoom.xml"
 
+// --- external function dependency ---
+fp_platform_set_parameters_t fp_platform_set_parameters;
+
 typedef struct qdsp_audiozoom_cfg {
     uint32_t             topo_id;
     uint32_t             module_id;
@@ -116,7 +118,7 @@
     }
 }
 
-static int audio_extn_audiozoom_parse_info(const char *filename)
+static int audiozoom_parse_info(const char *filename)
 {
     XML_Parser      parser;
     FILE            *file;
@@ -176,7 +178,7 @@
     return ret;
 }
 
-int audio_extn_audiozoom_set_microphone_direction(
+int audiozoom_set_microphone_direction(
     struct stream_in *in, audio_microphone_direction_t dir)
 {
     (void)in;
@@ -184,7 +186,7 @@
     return 0;
 }
 
-static int audio_extn_audiozoom_set_microphone_field_dimension_zoom(
+static int audiozoom_set_microphone_field_dimension_zoom(
     struct stream_in *in, float zoom)
 {
     struct audio_device *adev = in->dev;
@@ -212,7 +214,7 @@
     if (ret > 0) {
         str_parms_add_str(parms, "cal_data", data);
 
-        platform_set_parameters(adev->platform, parms);
+        fp_platform_set_parameters(adev->platform, parms);
     } else {
         ALOGE("%s: failed to convert data to string, ret %d", __func__, ret);
     }
@@ -222,7 +224,7 @@
     return 0;
 }
 
-static int audio_extn_audiozoom_set_microphone_field_dimension_wide_angle(
+static int audiozoom_set_microphone_field_dimension_wide_angle(
     struct stream_in *in, float zoom)
 {
     (void)in;
@@ -230,24 +232,25 @@
     return 0;
 }
 
-int audio_extn_audiozoom_set_microphone_field_dimension(
+int audiozoom_set_microphone_field_dimension(
     struct stream_in *in, float zoom)
 {
     if (zoom > 1.0 || zoom < -1.0)
         return -EINVAL;
 
     if (zoom >= 0 && zoom <= 1.0)
-        return audio_extn_audiozoom_set_microphone_field_dimension_zoom(in, zoom);
+        return audiozoom_set_microphone_field_dimension_zoom(in, zoom);
 
     if (zoom >= -1.0 && zoom <= 0)
-        return audio_extn_audiozoom_set_microphone_field_dimension_wide_angle(in, zoom);
+        return audiozoom_set_microphone_field_dimension_wide_angle(in, zoom);
 
     return 0;
 }
 
-int audio_extn_audiozoom_init()
+int audiozoom_init(audiozoom_init_config_t init_config)
 {
-    audio_extn_audiozoom_parse_info(AUDIOZOOM_PRESET_FILE);
+    fp_platform_set_parameters = init_config.fp_platform_set_parameters;
+    audiozoom_parse_info(AUDIOZOOM_PRESET_FILE);
 
     ALOGV("%s: topo_id=%d, module_id=%d, instance_id=%d, zoom__id=%d, dir_id=%d, app_type=%d",
         __func__, qdsp_audiozoom.topo_id, qdsp_audiozoom.module_id, qdsp_audiozoom.instance_id,
diff --git a/hal/audio_extn/audiozoom.h b/hal/audio_extn/audiozoom.h
index 2c0ad71..cb00828 100644
--- a/hal/audio_extn/audiozoom.h
+++ b/hal/audio_extn/audiozoom.h
@@ -17,15 +17,9 @@
 #ifndef AUDIOZOOM_H_
 #define AUDIOZOOM_H_
 
-#ifndef AUDIOZOOM_QDSP_ENABLED
-#define audio_extn_audiozoom_init()                                          (0)
-#define audio_extn_audiozoom_set_microphone_direction(stream, dir)           (-ENOSYS)
-#define audio_extn_audiozoom_set_microphone_field_dimension(stream, zoom)    (-ENOSYS)
-#else
-int audio_extn_audiozoom_init();
-int audio_extn_audiozoom_set_microphone_direction(struct stream_in *stream,
+int audiozoom_init(audiozoom_init_config_t init_config);
+int audiozoom_set_microphone_direction(struct stream_in *stream,
                                            audio_microphone_direction_t dir);
-int audio_extn_audiozoom_set_microphone_field_dimension(struct stream_in *stream, float zoom);
-#endif
+int audiozoom_set_microphone_field_dimension(struct stream_in *stream, float zoom);
 
 #endif /* AUDIOZOOM_H_ */
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 9f90d93..5959101 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -75,7 +75,6 @@
 #include "voice_extn.h"
 #include "ip_hdlr_intf.h"
 #include "audio_feature_manager.h"
-#include "audio_extn/audiozoom.h"
 
 #include "sound/compress_params.h"
 #include "sound/asound.h"