Merge "Fix build error for a new SP"
diff --git a/configs/msm8998/msm8998.mk b/configs/msm8998/msm8998.mk
index 5f4e6e1..f39a97b 100644
--- a/configs/msm8998/msm8998.mk
+++ b/configs/msm8998/msm8998.mk
@@ -64,6 +64,7 @@
 AUDIO_FEATURE_ENABLED_RAS := true
 AUDIO_FEATURE_ENABLED_DYNAMIC_LOG := true
 AUDIO_FEATURE_ENABLED_SND_MONITOR := true
+AUDIO_FEATURE_ENABLED_A2DP_DECODERS := true
 ##AUDIO_FEATURE_FLAGS
 
 #Audio Specific device overlays
diff --git a/hal/Android.mk b/hal/Android.mk
index a05da26..cfe4a07 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -474,6 +474,10 @@
     LOCAL_STATIC_LIBRARIES += libprofile_rt
 endif
 
+ifeq ($(strip $(AUDIO_FEATURE_ENABLED_A2DP_DECODERS)), true)
+    LOCAL_CFLAGS += -DAPTX_DECODER_ENABLED
+endif
+
 LOCAL_MODULE := audio.primary.$(TARGET_BOARD_PLATFORM)
 
 LOCAL_MODULE_RELATIVE_PATH := hw
diff --git a/hal/audio_extn/a2dp.c b/hal/audio_extn/a2dp.c
index f83ea6a..4b4abbe 100644
--- a/hal/audio_extn/a2dp.c
+++ b/hal/audio_extn/a2dp.c
@@ -1295,7 +1295,6 @@
             else {
                a2dp.is_tws_mono_mode_on = true;
                ALOGD("Update tws for mono_mode_on: %d",a2dp.is_tws_mono_mode_on);
-               audio_a2dp_update_tws_channel_mode();
             }
             break;
     }
@@ -1876,6 +1875,7 @@
     if (a2dp.a2dp_started) {
         a2dp.a2dp_total_active_session_request++;
         a2dp_check_and_set_scrambler();
+        audio_a2dp_update_tws_channel_mode();
         a2dp_set_backend_cfg();
         if (a2dp.abr_config.is_abr_enabled)
             start_abr();
diff --git a/hal/audio_extn/audio_extn.c b/hal/audio_extn/audio_extn.c
index 20d760c..38d0b6f 100644
--- a/hal/audio_extn/audio_extn.c
+++ b/hal/audio_extn/audio_extn.c
@@ -62,6 +62,16 @@
 #include <log_utils.h>
 #endif
 
+#ifndef APTX_DECODER_ENABLED
+#define audio_extn_aptx_dec_set_license(adev) (0)
+#define audio_extn_set_aptx_dec_bt_addr(adev, parms) (0)
+#define audio_extn_parse_aptx_dec_bt_addr(value) (0)
+#else
+static void audio_extn_aptx_dec_set_license(struct audio_device *adev);
+static void audio_extn_set_aptx_dec_bt_addr(struct audio_device *adev, struct str_parms *parms);
+static void audio_extn_parse_aptx_dec_bt_addr(char *value);
+#endif
+
 #define MAX_SLEEP_RETRY 100
 #define WIFI_INIT_WAIT_SLEEP 50
 #define MAX_NUM_CHANNELS 8
@@ -1538,7 +1548,6 @@
 static void audio_extn_aptx_dec_set_license(struct audio_device *adev)
 {
     int ret, key = 0;
-    char value[128] = {0};
     struct mixer_ctl *ctl;
     const char *mixer_ctl_name = "APTX Dec License";
 
@@ -1556,7 +1565,7 @@
         ALOGE("%s: cannot set license, error:%d",__func__, ret);
 }
 
-static void audio_extn_set_aptx_dec_bt_addr(struct audio_device *adev, struct str_parms *parms)
+static void audio_extn_set_aptx_dec_bt_addr(struct audio_device *adev __unused, struct str_parms *parms)
 {
     int ret = 0;
     char value[256];
@@ -1575,6 +1584,7 @@
     aextnmod.addr.nap = aptx_cfg->bt_addr.nap;
     aextnmod.addr.uap = aptx_cfg->bt_addr.uap;
     aextnmod.addr.lap = aptx_cfg->bt_addr.lap;
+    return 0;
 }
 
 static void audio_extn_parse_aptx_dec_bt_addr(char *value)
@@ -1602,11 +1612,7 @@
 
 void audio_extn_send_aptx_dec_bt_addr_to_dsp(struct stream_out *out)
 {
-    char mixer_ctl_name[128];
-    struct mixer_ctl *ctl;
-    uint32_t addr[3];
-
-    ALOGV("%s", __func__);
+    ALOGD("%s", __func__);
     out->compr_config.codec->options.aptx_dec.nap = aextnmod.addr.nap;
     out->compr_config.codec->options.aptx_dec.uap = aextnmod.addr.uap;
     out->compr_config.codec->options.aptx_dec.lap = aextnmod.addr.lap;
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index 6b08671..1e4b8ba 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2013-2019, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright (C) 2013 The Android Open Source Project
@@ -395,6 +395,8 @@
 #define audio_extn_sound_trigger_check_and_get_session(in)             (0)
 #define audio_extn_sound_trigger_stop_lab(in)                          (0)
 #define audio_extn_sound_trigger_read(in, buffer, bytes)               (0)
+#define audio_extn_sound_trigger_check_ec_ref_enable()                 (0)
+#define audio_extn_sound_trigger_update_ec_ref_status(on)              (0)
 #else
 
 enum st_event_type {
@@ -420,6 +422,8 @@
                                   size_t bytes);
 void audio_extn_sound_trigger_get_parameters(const struct audio_device *adev,
                      struct str_parms *query, struct str_parms *reply);
+bool audio_extn_sound_trigger_check_ec_ref_enable();
+void audio_extn_sound_trigger_update_ec_ref_status(bool on);
 #endif
 
 #ifndef AUXPCM_BT_ENABLED
@@ -971,16 +975,10 @@
 #endif
 
 #ifndef APTX_DECODER_ENABLED
-#define audio_extn_aptx_dec_set_license(adev); (0)
-#define audio_extn_set_aptx_dec_bt_addr(adev, parms); (0)
-#define audio_extn_send_aptx_dec_bt_addr_to_dsp(out); (0)
-#define audio_extn_parse_aptx_dec_bt_addr(value); (0)
-#define audio_extn_set_aptx_dec_params(payload); (0)
+#define audio_extn_send_aptx_dec_bt_addr_to_dsp(out) (0)
+#define audio_extn_set_aptx_dec_params(payload) (0)
 #else
-static void audio_extn_aptx_dec_set_license(struct audio_device *adev);
-static void audio_extn_set_aptx_dec_bt_addr(struct audio_device *adev, struct str_parms *parms);
 void audio_extn_send_aptx_dec_bt_addr_to_dsp(struct stream_out *out);
-static void audio_extn_parse_aptx_dec_bt_addr(char *value);
 int audio_extn_set_aptx_dec_params(struct aptx_dec_param *payload);
 #endif
 int audio_extn_out_set_param_data(struct stream_out *out,
diff --git a/hal/audio_extn/soundtrigger.c b/hal/audio_extn/soundtrigger.c
index a728bd8..38e7ae7 100644
--- a/hal/audio_extn/soundtrigger.c
+++ b/hal/audio_extn/soundtrigger.c
@@ -53,7 +53,8 @@
 
 /* Proprietary interface version used for compatibility with STHAL */
 #define STHAL_PROP_API_VERSION_1_0 MAKE_HAL_VERSION(1, 0)
-#define STHAL_PROP_API_CURRENT_VERSION STHAL_PROP_API_VERSION_1_0
+#define STHAL_PROP_API_VERSION_1_1 MAKE_HAL_VERSION(1, 1)
+#define STHAL_PROP_API_CURRENT_VERSION STHAL_PROP_API_VERSION_1_1
 
 #define ST_EVENT_CONFIG_MAX_STR_VALUE 32
 #define ST_DEVICE_HANDSET_MIC 1
@@ -63,6 +64,7 @@
     ST_EVENT_SESSION_DEREGISTER,
     ST_EVENT_START_KEEP_ALIVE,
     ST_EVENT_STOP_KEEP_ALIVE,
+    ST_EVENT_UPDATE_ECHO_REF
 } sound_trigger_event_type_t;
 
 typedef enum {
@@ -81,7 +83,8 @@
     AUDIO_EVENT_CAPTURE_STREAM_INACTIVE,
     AUDIO_EVENT_CAPTURE_STREAM_ACTIVE,
     AUDIO_EVENT_BATTERY_STATUS_CHANGED,
-    AUDIO_EVENT_GET_PARAM
+    AUDIO_EVENT_GET_PARAM,
+    AUDIO_EVENT_UPDATE_ECHO_REF
 } audio_event_type_t;
 
 typedef enum {
@@ -117,6 +120,7 @@
 
 struct sound_trigger_event_info {
     struct sound_trigger_session_info st_ses;
+    bool st_ec_ref_enabled;
 };
 typedef struct sound_trigger_event_info sound_trigger_event_info_t;
 
@@ -139,6 +143,7 @@
         struct audio_read_samples_info aud_info;
         char str_value[ST_EVENT_CONFIG_MAX_STR_VALUE];
         struct audio_hal_usecase usecase;
+        bool audio_ec_ref_enabled;
         struct sound_trigger_get_param_data st_get_param_data;
     } u;
     struct sound_trigger_device_info device_info;
@@ -199,6 +204,7 @@
     struct listnode st_ses_list;
     pthread_mutex_t lock;
     unsigned int sthal_prop_api_version;
+    bool st_ec_ref_enabled;
 };
 
 static struct sound_trigger_audio_device *st_dev;
@@ -334,6 +340,15 @@
         pthread_mutex_unlock(&st_dev->adev->lock);
         goto done;
 
+    case ST_EVENT_UPDATE_ECHO_REF:
+        if (!config) {
+            ALOGE("%s: NULL config", __func__);
+            status = -EINVAL;
+            break;
+        }
+        st_dev->st_ec_ref_enabled = config->st_ec_ref_enabled;
+        break;
+
     default:
         ALOGW("%s: Unknown event %d", __func__, event);
         break;
@@ -427,6 +442,42 @@
 }
 
 
+bool audio_extn_sound_trigger_check_ec_ref_enable()
+{
+    bool ret = false;
+
+    if (!st_dev) {
+        ALOGE("%s: st_dev NULL", __func__);
+        return ret;
+    }
+
+    pthread_mutex_lock(&st_dev->lock);
+    if (st_dev->st_ec_ref_enabled) {
+        ret = true;
+        ALOGD("%s: EC Reference is enabled", __func__);
+    } else {
+        ALOGD("%s: EC Reference is disabled", __func__);
+    }
+    pthread_mutex_unlock(&st_dev->lock);
+
+    return ret;
+}
+
+void audio_extn_sound_trigger_update_ec_ref_status(bool on)
+{
+    struct audio_event_info ev_info;
+
+    if (!st_dev) {
+        ALOGE("%s: st_dev NULL", __func__);
+        return;
+    }
+
+    ev_info.u.audio_ec_ref_enabled = on;
+    st_dev->st_callback(AUDIO_EVENT_UPDATE_ECHO_REF, &ev_info);
+    ALOGD("%s: update audio echo ref status %s",__func__,
+                ev_info.u.audio_ec_ref_enabled == true ? "true" : "false");
+}
+
 void audio_extn_sound_trigger_update_device_status(snd_device_t snd_device,
                                      st_event_type_t event)
 {
@@ -716,6 +767,7 @@
     }
 
     st_dev->adev = adev;
+    st_dev->st_ec_ref_enabled = false;
     list_init(&st_dev->st_ses_list);
     audio_extn_snd_mon_register_listener(st_dev, stdev_snd_mon_cb);
 
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 1acc3b3..b0f7efd 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -3848,9 +3848,17 @@
           use_case_table[out->usecase],
           status == CARD_STATUS_OFFLINE ? "offline" : "online");
 
-    if (status == CARD_STATUS_OFFLINE)
+    if (status == CARD_STATUS_OFFLINE) {
         out_on_error(stream);
-
+        if (voice_is_call_state_active(adev) &&
+            out == adev->primary_output) {
+            ALOGD("%s: SSR/PDR occurred, end all calls", __func__);
+            pthread_mutex_lock(&adev->lock);
+            voice_stop_call(adev);
+            adev->mode = AUDIO_MODE_NORMAL;
+            pthread_mutex_unlock(&adev->lock);
+        }
+    }
     return;
 }
 
@@ -7506,14 +7514,20 @@
 
     ALOGD("%s: enter:stream_handle(%p)",__func__, in);
 
-    // must deregister from sndmonitor first to prevent races
-    // between the callback and close_stream
+    /* must deregister from sndmonitor first to prevent races
+     * between the callback and close_stream
+     */
     audio_extn_snd_mon_unregister_listener(stream);
 
-    // Disable echo reference if there are no active input and hfp call
-    // while closing input stream
-    if (!adev->active_input && !audio_extn_hfp_is_active(adev))
+    /* Disable echo reference if there are no active input, hfp call
+     * and sound trigger while closing input stream
+     */
+    if (!adev->active_input &&
+        !audio_extn_hfp_is_active(adev) &&
+        !audio_extn_sound_trigger_check_ec_ref_enable())
         platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
+    else
+        audio_extn_sound_trigger_update_ec_ref_status(false);
 
     if (in == NULL) {
         ALOGE("%s: audio_stream_in ptr is NULL", __func__);
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index a770b4c..49042cd 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -1250,11 +1250,13 @@
 }
 
 void platform_set_echo_reference(struct audio_device *adev, bool enable,
-                                 audio_devices_t out_device __unused)
+                                 audio_devices_t out_device)
 {
     struct platform_data *my_data = (struct platform_data *)adev->platform;
     char ec_ref_mixer_path[MIXER_PATH_MAX_LENGTH] = "echo-reference";
 
+    audio_extn_sound_trigger_update_ec_ref_status(enable);
+
     if (strcmp(my_data->ec_ref_mixer_path, "")) {
         ALOGV("%s: disabling %s", __func__, my_data->ec_ref_mixer_path);
         audio_route_reset_and_update_path(adev->audio_route,
@@ -1266,9 +1268,11 @@
         if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
             strlcat(ec_ref_mixer_path, "-voip", MIXER_PATH_MAX_LENGTH);
 #endif
+        strlcpy(my_data->ec_ref_mixer_path, ec_ref_mixer_path,
+                    MIXER_PATH_MAX_LENGTH);
         /*
          * If native audio device reference count > 0, then apply codec EC otherwise
-         * fallback to Speakers with VBat if enabled or default
+         * apply EC based on output device.
          */
         if (adev->snd_dev_ref_cnt[SND_DEVICE_OUT_HEADPHONES_44_1] > 0)
             strlcat(ec_ref_mixer_path, " headphones-44.1",
@@ -1279,12 +1283,24 @@
         else if (adev->snd_dev_ref_cnt[SND_DEVICE_OUT_DISPLAY_PORT] > 0)
             strlcat(ec_ref_mixer_path, " display-port",
                     MIXER_PATH_MAX_LENGTH);
+        else if (out_device & AUDIO_DEVICE_OUT_EARPIECE)
+            strlcat(ec_ref_mixer_path, " handset",
+                    MIXER_PATH_MAX_LENGTH);
+        else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE)
+            strlcat(ec_ref_mixer_path, " headphones",
+                    MIXER_PATH_MAX_LENGTH);
+        else if (out_device & AUDIO_DEVICE_OUT_USB_HEADSET)
+            strlcat(ec_ref_mixer_path, " usb-headphones",
+                    MIXER_PATH_MAX_LENGTH);
 
-        strlcpy(my_data->ec_ref_mixer_path, ec_ref_mixer_path,
-                MIXER_PATH_MAX_LENGTH);
+        if (audio_route_apply_and_update_path(adev->audio_route,
+                                              ec_ref_mixer_path) == 0)
+            strlcpy(my_data->ec_ref_mixer_path, ec_ref_mixer_path,
+                    MIXER_PATH_MAX_LENGTH);
+        else
+            audio_route_apply_and_update_path(adev->audio_route, my_data->ec_ref_mixer_path);
 
         ALOGD("%s: enabling %s", __func__, my_data->ec_ref_mixer_path);
-        audio_route_apply_and_update_path(adev->audio_route, my_data->ec_ref_mixer_path);
     }
 }
 
diff --git a/mm-audio/aenc-aac/qdsp6/test/omx_aac_enc_test.c b/mm-audio/aenc-aac/qdsp6/test/omx_aac_enc_test.c
index 97b7617..967a7e3 100644
--- a/mm-audio/aenc-aac/qdsp6/test/omx_aac_enc_test.c
+++ b/mm-audio/aenc-aac/qdsp6/test/omx_aac_enc_test.c
@@ -278,12 +278,6 @@
         status = -1;
         goto exit;
     }
-    ptr = (char *)malloc(strlen(input) + 1);
-    if (ptr == NULL) {
-        DEBUG_PRINT("Low memory\n");
-        status = -1;
-        goto exit;
-    }
     /* Check for negative input */
     if (*input == '-') {
         DEBUG_PRINT("Negative Number is not allowed\n");
@@ -322,8 +316,6 @@
             break;
     }
 exit:
-    if (ptr != NULL)
-        free(ptr);
     if (status != 0)
         exit(0);
     return value;
diff --git a/mm-audio/aenc-evrc/qdsp6/test/omx_evrc_enc_test.c b/mm-audio/aenc-evrc/qdsp6/test/omx_evrc_enc_test.c
index 8c89e61..d1b4837 100644
--- a/mm-audio/aenc-evrc/qdsp6/test/omx_evrc_enc_test.c
+++ b/mm-audio/aenc-evrc/qdsp6/test/omx_evrc_enc_test.c
@@ -291,12 +291,6 @@
         status = -1;
         goto exit;
     }
-    ptr = (char *)malloc(strlen(input) + 1);
-    if (ptr == NULL) {
-        DEBUG_PRINT("Low memory\n");
-        status = -1;
-        goto exit;
-    }
     /* Check for negative input */
     if (*input == '-') {
         DEBUG_PRINT("Negative Number is not allowed\n");
@@ -335,8 +329,6 @@
             break;
     }
 exit:
-    if (ptr != NULL)
-        free(ptr);
     if (status != 0)
         exit(0);
     return value;
diff --git a/mm-audio/aenc-qcelp13/qdsp6/test/omx_qcelp13_enc_test.c b/mm-audio/aenc-qcelp13/qdsp6/test/omx_qcelp13_enc_test.c
index d5ec4c7..f35af33 100644
--- a/mm-audio/aenc-qcelp13/qdsp6/test/omx_qcelp13_enc_test.c
+++ b/mm-audio/aenc-qcelp13/qdsp6/test/omx_qcelp13_enc_test.c
@@ -290,12 +290,6 @@
         status = -1;
         goto exit;
     }
-    ptr = (char *)malloc(strlen(input) + 1);
-    if (ptr == NULL) {
-        DEBUG_PRINT("Low memory\n");
-        status = -1;
-        goto exit;
-    }
     /* Check for negative input */
     if (*input == '-') {
         DEBUG_PRINT("Negative Number is not allowed\n");
@@ -334,8 +328,6 @@
             break;
     }
 exit:
-    if (ptr != NULL)
-        free(ptr);
     if (status != 0)
         exit(0);
     return value;