Merge "audiopolicy: enable deep buffer output by default for music streams"
diff --git a/hal/Android.mk b/hal/Android.mk
index 0545423..0edc5a3 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -54,7 +54,7 @@
 
 ifeq ($(strip $(AUDIO_FEATURE_ENABLED_HDMI_EDID)),true)
     LOCAL_SRC_FILES += edid.c
-    LOCAL_CFLAGS := -DHDMI_EDID_ENABLED
+    LOCAL_CFLAGS += -DHDMI_EDID
 endif
 
 ifeq ($(strip $(AUDIO_USE_LL_AS_PRIMARY_OUTPUT)),true)
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index e64d318..e2db95b 100755
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -569,6 +569,9 @@
         if (snd_device == SND_DEVICE_OUT_HDMI)
             adev->mChannelStatusSet = false;
 
+        if (snd_device == SND_DEVICE_OUT_HDMI)
+            adev->mChannelStatusSet = false;
+
         audio_extn_dev_arbi_release(snd_device);
         audio_extn_sound_trigger_update_device_status(snd_device,
                                         ST_EVENT_SND_DEVICE_FREE);
@@ -663,9 +666,10 @@
            specified usecase to new snd devices */
         list_for_each(node, &adev->usecase_list) {
             usecase = node_to_item(node, struct audio_usecase, list);
-            /* Update the out_snd_device only for the usecases that are enabled here */
-            if (switch_device[usecase->id] && (usecase->type != VOICE_CALL)) {
-                    usecase->out_snd_device = snd_device;
+            /* Update the out_snd_device only before enabling the audio route */
+            if (switch_device[usecase->id] ) {
+                usecase->out_snd_device = snd_device;
+                if (usecase->type != VOICE_CALL)
                     enable_audio_route(adev, usecase);
             }
         }
@@ -700,7 +704,8 @@
                 usecase != uc_info &&
                 usecase->in_snd_device != snd_device &&
                 ((uc_info->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
-                ((usecase->devices & ~AUDIO_DEVICE_BIT_IN) & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND)) &&
+                 (((usecase->devices & ~AUDIO_DEVICE_BIT_IN) & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND) ||
+                  (usecase->type == VOICE_CALL))) &&
                 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
             ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
                   __func__, use_case_table[usecase->id],
@@ -916,8 +921,14 @@
         (usecase->in_snd_device != SND_DEVICE_NONE) &&
         (usecase->out_snd_device != SND_DEVICE_NONE)) {
         status = platform_switch_voice_call_device_pre(adev->platform);
-        /* Disable sidetone only if voice call already exists */
-        if (voice_is_call_state_active(adev))
+    }
+
+    if (((usecase->type == VOICE_CALL) ||
+         (usecase->type == VOIP_CALL)) &&
+        (usecase->out_snd_device != SND_DEVICE_NONE)) {
+        /* Disable sidetone only if voice/voip call already exists */
+        if (voice_is_call_state_active(adev) ||
+            voice_extn_compress_voip_is_started(adev))
             voice_set_sidetone(adev, usecase->out_snd_device, false);
     }
 
@@ -961,8 +972,9 @@
                                                         out_snd_device,
                                                         in_snd_device);
         enable_audio_route_for_voice_usecases(adev, usecase);
-        /* Enable sidetone only if voice call already exists */
-        if (voice_is_call_state_active(adev))
+        /* Enable sidetone only if voice/voip call already exists */
+        if (voice_is_call_state_active(adev) ||
+            voice_extn_compress_voip_is_started(adev))
             voice_set_sidetone(adev, out_snd_device, true);
     }
 
diff --git a/hal/edid.c b/hal/edid.c
index 9b05950..06e1e05 100644
--- a/hal/edid.c
+++ b/hal/edid.c
@@ -662,12 +662,12 @@
         ALOGV("AUDIO DESC BLOCK # %d\n",i);
 
         info->audio_blocks_array[i].channels = channels[i];
-        ALOGV("info->audio_blocks_array[i].channels %d\n",
+        ALOGD("info->audio_blocks_array[i].channels %d\n",
               info->audio_blocks_array[i].channels);
 
         ALOGV("Format Byte %d\n", formats[i]);
         info->audio_blocks_array[i].format_id = (edid_audio_format_id)formats[i];
-        ALOGV("info->audio_blocks_array[i].format_id %s",
+        ALOGD("info->audio_blocks_array[i].format_id %s",
               edid_format_to_str(formats[i]));
 
         ALOGV("Frequency Byte %d\n", frequency[i]);
diff --git a/hal/edid.h b/hal/edid.h
index ec83ec8..aa945bd 100644
--- a/hal/edid.h
+++ b/hal/edid.h
@@ -92,5 +92,10 @@
     int  channel_allocation;
 } edid_audio_info;
 
+
+#ifndef HDMI_EDID
+#define edid_get_sink_caps(info, edid_data) (0)
+#else
 bool edid_get_sink_caps(edid_audio_info* info, char *edid_data);
+#endif
 #endif /* EDID_H */
diff --git a/hal/msm8916/platform.c b/hal/msm8916/platform.c
index 1ce6fd1..d882c5f 100644
--- a/hal/msm8916/platform.c
+++ b/hal/msm8916/platform.c
@@ -1340,12 +1340,30 @@
         ALOGE("%s: Could not send anc cal", __FUNCTION__);
 }
 
+const char * get_snd_card_name_for_acdb_loader(const char *snd_card_name) {
+
+    if(snd_card_name == NULL)
+        return NULL;
+
+    // Both tasha & tasha-lite uses tasha ACDB files
+    // simulate sound card name for tasha lite, so that
+    // ACDB module loads tasha ACDB files for tasha lite
+    if(!strncmp(snd_card_name, "msm8976-tashalite-snd-card",
+             sizeof("msm8976-tashalite-snd-card"))) {
+       ALOGD("using tasha ACDB files for tasha-lite");
+       return "msm8976-tasha-snd-card";
+   } else {
+       return snd_card_name;
+   }
+}
+
+
 int platform_acdb_init(void *platform)
 {
     struct platform_data *my_data = (struct platform_data *)platform;
     char *cvd_version = NULL;
     int key = 0;
-    const char *snd_card_name;
+    const char *snd_card_name, *acdb_snd_card_name;
     int result;
     char value[PROPERTY_VALUE_MAX];
     cvd_version = calloc(1, MAX_CVD_VERSION_STRING_SIZE);
@@ -1357,7 +1375,10 @@
     property_get("audio.ds1.metainfo.key",value,"0");
     key = atoi(value);
     snd_card_name = mixer_get_name(my_data->adev->mixer);
-    result = my_data->acdb_init(snd_card_name, cvd_version, key);
+    acdb_snd_card_name = get_snd_card_name_for_acdb_loader(snd_card_name);
+
+    result = my_data->acdb_init(acdb_snd_card_name, cvd_version, key);
+
     if (cvd_version)
         free(cvd_version);
     if (!result) {
@@ -1376,7 +1397,7 @@
 #define TZ_TYPE "/sys/class/thermal/thermal_zone%d/type"
 #define TZ_WSA "/sys/class/thermal/thermal_zone%d/temp"
 
-bool is_wsa_found(void)
+static bool is_wsa_found(int *wsaCount)
 {
     DIR *tdir = NULL;
     struct dirent *tdirent = NULL;
@@ -1385,6 +1406,7 @@
     char cwd[MAX_PATH] = {0};
     char file[10] = "wsa";
     bool found = false;
+    int wsa_count = 0;
 
     if (!getcwd(cwd, sizeof(cwd)))
         return false;
@@ -1412,14 +1434,19 @@
             ALOGD("Opening %s\n", name);
             read_line_from_file(name, buf, sizeof(buf));
             if (strstr(buf, file)) {
-                found = true;
-                break;
+                wsa_count++;
+                /*We support max only two WSA speakers*/
+                if (wsa_count == 2)
+                    break;
             }
             tzn++;
         }
         closedir(tzdir);
-        if (found == true)
-            break;
+    }
+    if (wsa_count > 0){
+         ALOGD("Found %d WSA present on the platform", wsa_count);
+         found = true;
+         *wsaCount = wsa_count;
     }
     closedir(tdir);
     chdir(cwd); /* Restore current working dir */
@@ -1439,6 +1466,7 @@
     const char *mixer_ctl_name = "Set HPX ActiveBe";
     struct mixer_ctl *ctl = NULL;
     int idx;
+    int wsaCount =0;
 
     my_data = calloc(1, sizeof(struct platform_data));
     if (!my_data) {
@@ -1549,6 +1577,17 @@
             my_data->fluence_mode = FLUENCE_BROADSIDE;
         }
     }
+
+    if (is_wsa_found(&wsaCount)) {
+        /*Set ACDB ID of Stereo speaker if two WSAs are present*/
+        /*Default ACDB ID for wsa speaker is that for mono*/
+        if (wsaCount == 2) {
+            platform_set_snd_device_acdb_id(SND_DEVICE_OUT_SPEAKER_WSA, 15);
+            platform_set_snd_device_acdb_id(SND_DEVICE_OUT_SPEAKER_VBAT, 15);
+        }
+        my_data->is_wsa_speaker = true;
+    }
+
     property_get("persist.audio.FFSP.enable", ffspEnable, "");
     if (!strncmp("true", ffspEnable, sizeof("true"))) {
         acdb_device_table[SND_DEVICE_OUT_SPEAKER] = 131;
@@ -1631,9 +1670,6 @@
     }
     audio_extn_pm_vote();
 
-    if (is_wsa_found())
-        my_data->is_wsa_speaker = true;
-
     /* Configure active back end for HPX*/
     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
     if (ctl) {
@@ -2392,7 +2428,7 @@
         goto exit;
     }
 
-    if (popcount(devices) == 2 && !voice_is_in_call(adev)) {
+    if (popcount(devices) == 2) {
         if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
                         AUDIO_DEVICE_OUT_SPEAKER)) {
             if (my_data->external_spk_1)
@@ -4178,12 +4214,10 @@
     edid_data[0] = count;
     memcpy(&edid_data[1], block, count);
 
-#ifdef HDMI_EDID_ENABLED
     if (!edid_get_sink_caps(info, edid_data)) {
         ALOGE("%s: Failed to get HDMI sink capabilities", __func__);
         goto fail;
     }
-#endif
 
     my_data->edid_valid = true;
     return 0;
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index d215a89..2975a00 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -2049,7 +2049,7 @@
         goto exit;
     }
 
-    if (popcount(devices) == 2 && !voice_is_in_call(adev)) {
+    if (popcount(devices) == 2) {
         if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
                         AUDIO_DEVICE_OUT_SPEAKER)) {
             if (my_data->external_spk_1)
diff --git a/hal/voice_extn/compress_voip.c b/hal/voice_extn/compress_voip.c
index 04e0cd0..bb1caf9 100644
--- a/hal/voice_extn/compress_voip.c
+++ b/hal/voice_extn/compress_voip.c
@@ -238,6 +238,7 @@
                   __func__, USECASE_COMPRESS_VOIP_CALL);
             return -EINVAL;
         }
+        voice_set_sidetone(adev, uc_info->out_snd_device, false);
 
         /* 1. Close the PCM devices */
         if (voip_data.pcm_rx) {
@@ -309,6 +310,19 @@
             goto error_start_voip;
         }
 
+        ALOGD("%s: Opening PCM capture device card_id(%d) device_id(%d)",
+              __func__, adev->snd_card, pcm_dev_tx_id);
+        voip_data.pcm_tx = pcm_open(adev->snd_card,
+                                    pcm_dev_tx_id,
+                                    PCM_IN, voip_config);
+        if (voip_data.pcm_tx && !pcm_is_ready(voip_data.pcm_tx)) {
+            ALOGE("%s: %s", __func__, pcm_get_error(voip_data.pcm_tx));
+            pcm_close(voip_data.pcm_tx);
+            voip_data.pcm_tx = NULL;
+            ret = -EIO;
+            goto error_start_voip;
+        }
+
         ALOGD("%s: Opening PCM playback device card_id(%d) device_id(%d)",
               __func__, adev->snd_card, pcm_dev_rx_id);
         voip_data.pcm_rx = pcm_open(adev->snd_card,
@@ -318,35 +332,19 @@
             ALOGE("%s: %s", __func__, pcm_get_error(voip_data.pcm_rx));
             pcm_close(voip_data.pcm_rx);
             voip_data.pcm_rx = NULL;
-            ret = -EIO;
-            goto error_start_voip;
-        }
-
-        ALOGD("%s: Opening PCM capture device card_id(%d) device_id(%d)",
-              __func__, adev->snd_card, pcm_dev_tx_id);
-        voip_data.pcm_tx = pcm_open(adev->snd_card,
-                                    pcm_dev_tx_id,
-                                    PCM_IN, voip_config);
-        if (voip_data.pcm_tx && !pcm_is_ready(voip_data.pcm_tx)) {
-            ALOGE("%s: %s", __func__, pcm_get_error(voip_data.pcm_tx));
-            pcm_close(voip_data.pcm_rx);
-            voip_data.pcm_tx = NULL;
-            if (voip_data.pcm_rx) {
-                pcm_close(voip_data.pcm_rx);
-                voip_data.pcm_rx = NULL;
+            if (voip_data.pcm_tx) {
+                pcm_close(voip_data.pcm_tx);
+                voip_data.pcm_tx = NULL;
             }
             ret = -EIO;
             goto error_start_voip;
         }
-        pcm_start(voip_data.pcm_rx);
+
         pcm_start(voip_data.pcm_tx);
+        pcm_start(voip_data.pcm_rx);
 
+        voice_set_sidetone(adev, uc_info->out_snd_device, true);
         voice_extn_compress_voip_set_volume(adev, adev->voice.volume);
-
-        if (ret < 0) {
-            ALOGE("%s: error %d\n", __func__, ret);
-            goto error_start_voip;
-        }
     } else {
         ALOGV("%s: voip usecase is already enabled", __func__);
         if (voip_data.out_stream)
@@ -730,3 +728,13 @@
     }
     return ret;
 }
+
+bool voice_extn_compress_voip_is_started(struct audio_device *adev)
+{
+    bool ret = false;
+    if (voice_extn_compress_voip_is_active(adev) &&
+        voip_data.pcm_tx && voip_data.pcm_rx)
+        ret = true;
+
+    return ret;
+}
diff --git a/hal/voice_extn/voice_extn.h b/hal/voice_extn/voice_extn.h
index 4a04adb..af0ad08 100644
--- a/hal/voice_extn/voice_extn.h
+++ b/hal/voice_extn/voice_extn.h
@@ -145,6 +145,7 @@
 bool voice_extn_compress_voip_is_active(struct audio_device *adev);
 bool voice_extn_compress_voip_is_format_supported(audio_format_t format);
 bool voice_extn_compress_voip_is_config_supported(struct audio_config *config);
+bool voice_extn_compress_voip_is_started(struct audio_device *adev);
 #else
 static int voice_extn_compress_voip_close_output_stream(struct audio_stream *stream __unused)
 {
@@ -264,6 +265,12 @@
     ALOGE("%s: COMPRESS_VOIP_ENABLED is not defined", __func__);
     return true;
 }
+
+static bool voice_extn_compress_voip_is_started(struct audio_device *adev __unused)
+{
+    ALOGE("%s: COMPRESS_VOIP_ENABLED is not defined", __func__);
+    return false;
+}
 #endif
 
 #endif //VOICE_EXTN_H