Merge "Updated Module markings"
diff --git a/hal/audio_extn/a2dp.c b/hal/audio_extn/a2dp.c
index 028a96f..e1f902e 100644
--- a/hal/audio_extn/a2dp.c
+++ b/hal/audio_extn/a2dp.c
@@ -1623,7 +1623,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;
     }
@@ -2213,6 +2212,7 @@
     if (a2dp.a2dp_source_started) {
         a2dp.a2dp_source_total_active_session_requests++;
         a2dp_check_and_set_scrambler();
+        audio_a2dp_update_tws_channel_mode();
         a2dp_set_backend_cfg(SOURCE);
         if (a2dp.abr_config.is_abr_enabled)
             start_abr();
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index 0c6bee5..158370a 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -432,6 +432,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 {
@@ -457,6 +459,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
diff --git a/hal/audio_extn/soundtrigger.c b/hal/audio_extn/soundtrigger.c
index e01b23b..a07796a 100644
--- a/hal/audio_extn/soundtrigger.c
+++ b/hal/audio_extn/soundtrigger.c
@@ -54,7 +54,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
@@ -64,6 +65,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 {
@@ -82,7 +84,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 {
@@ -120,6 +123,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;
 
@@ -141,6 +145,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;
@@ -200,6 +205,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;
@@ -335,6 +341,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;
@@ -358,8 +373,6 @@
         ALOGE(" %s: Sound trigger is not active", __func__);
         goto exit;
     }
-    if(in->standby)
-        in->standby = false;
 
     pthread_mutex_lock(&st_dev->lock);
     st_info = get_sound_trigger_info(in->capture_handle);
@@ -443,6 +456,42 @@
     return false;
 }
 
+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)
 {
@@ -753,6 +802,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 17f838c..d1a0bd4 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -3987,9 +3987,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\n", __func__);
+            pthread_mutex_lock(&adev->lock);
+            voice_stop_call(adev);
+            adev->mode = AUDIO_MODE_NORMAL;
+            pthread_mutex_unlock(&adev->lock);
+        }
+    }
     return;
 }
 
@@ -5608,6 +5616,7 @@
     if (!in->standby && in->is_st_session) {
         ALOGD("%s: sound trigger pcm stop lab", __func__);
         audio_extn_sound_trigger_stop_lab(in);
+        adev->num_va_sessions--;
         in->standby = 1;
     }
 
@@ -5640,6 +5649,10 @@
             platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
             status = stop_input_stream(in);
         }
+
+        if (in->source == AUDIO_SOURCE_VOICE_RECOGNITION)
+            adev->num_va_sessions--;
+
         pthread_mutex_unlock(&adev->lock);
     }
     pthread_mutex_unlock(&in->lock);
@@ -5889,6 +5902,10 @@
         ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
         /* Read from sound trigger HAL */
         audio_extn_sound_trigger_read(in, buffer, bytes);
+        if (in->standby) {
+            adev->num_va_sessions++;
+            in->standby = 0;
+        }
         pthread_mutex_unlock(&in->lock);
         return bytes;
     }
@@ -5904,6 +5921,8 @@
             ret = voice_extn_compress_voip_start_input_stream(in);
         else
             ret = start_input_stream(in);
+        if (!ret && in->source == AUDIO_SOURCE_VOICE_RECOGNITION)
+            adev->num_va_sessions++;
         pthread_mutex_unlock(&adev->lock);
         if (ret != 0) {
             goto exit;
@@ -5952,11 +5971,18 @@
     release_in_focus(in);
 
     /*
-     * Instead of writing zeroes here, we could trust the hardware
-     * to always provide zeroes when muted.
+     * Instead of writing zeroes here, we could trust the hardware to always
+     * provide zeroes when muted. This is also muted with voice recognition
+     * usecases so that other clients do not have access to voice recognition
+     * data.
      */
-    if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in) &&
-            in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
+    if ((ret == 0 && voice_get_mic_mute(adev) &&
+         !voice_is_in_call_rec_stream(in) &&
+         in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY) ||
+        (adev->num_va_sessions &&
+         in->source != AUDIO_SOURCE_VOICE_RECOGNITION &&
+         property_get_bool("persist.vendor.audio.va_concurrency_mute_enabled",
+            false)))
         memset(buffer, 0, bytes);
 
 exit:
@@ -7971,14 +7997,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);
 
     error_log_destroy(in->error_log);
     in->error_log = NULL;
diff --git a/hal/audio_hw.h b/hal/audio_hw.h
index 4f93aa9..23306de 100644
--- a/hal/audio_hw.h
+++ b/hal/audio_hw.h
@@ -517,6 +517,7 @@
     bool enable_hfp;
     bool mic_muted;
     bool enable_voicerx;
+    unsigned int num_va_sessions;
 
     int snd_card;
     card_status_t card_status;
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 9eb1dc2..613d7ba 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -1473,11 +1473,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,
@@ -1489,9 +1491,11 @@
             if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
                 strlcat(ec_ref_mixer_path, "-voip", MIXER_PATH_MAX_LENGTH);    
         }        
+        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",
@@ -1502,12 +1506,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);
     }
 }