Merge "mm-audio: Update bitrate based on profile and samplerate"
diff --git a/Android.mk b/Android.mk
index 9bb3250..c63df05 100644
--- a/Android.mk
+++ b/Android.mk
@@ -1,4 +1,4 @@
-ifneq ($(filter mpq8092 msm8960 msm8226 msm8x26 msm8610 msm8974 msm8x74 apq8084,$(TARGET_BOARD_PLATFORM)),)
+ifneq ($(filter mpq8092 msm8960 msm8226 msm8x26 msm8610 msm8974 msm8x74 apq8084 msm8916,$(TARGET_BOARD_PLATFORM)),)
 
 MY_LOCAL_PATH := $(call my-dir)
 
diff --git a/hal/Android.mk b/hal/Android.mk
index a7e0a02..32a9ee9 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -8,7 +8,7 @@
 
 AUDIO_PLATFORM := $(TARGET_BOARD_PLATFORM)
 
-ifneq ($(filter msm8974 msm8226 msm8610 apq8084,$(TARGET_BOARD_PLATFORM)),)
+ifneq ($(filter msm8974 msm8226 msm8610 apq8084 msm8916,$(TARGET_BOARD_PLATFORM)),)
   # B-family platform uses msm8974 code base
   AUDIO_PLATFORM = msm8974
   MULTIPLE_HW_VARIANTS_ENABLED := true
@@ -23,6 +23,7 @@
 endif
 endif
 
+
 LOCAL_SRC_FILES := \
 	audio_hw.c \
 	voice.c \
@@ -53,6 +54,10 @@
     LOCAL_SRC_FILES += audio_extn/hfp.c
 endif
 
+ifneq ($(strip $(AUDIO_FEATURE_DISABLED_CUSTOMSTEREO)),true)
+    LOCAL_CFLAGS += -DCUSTOM_STEREO_ENABLED
+endif
+
 ifneq ($(strip $(AUDIO_FEATURE_DISABLED_SSR)),true)
     LOCAL_CFLAGS += -DSSR_ENABLED
     LOCAL_SRC_FILES += audio_extn/ssr.c
@@ -116,7 +121,8 @@
 	libtinycompress \
 	libaudioroute \
 	libdl \
-	libexpat
+	libexpat \
+        libmdmdetect
 
 LOCAL_C_INCLUDES += \
 	external/tinyalsa/include \
@@ -126,7 +132,8 @@
 	$(call include-path-for, audio-effects) \
 	$(LOCAL_PATH)/$(AUDIO_PLATFORM) \
 	$(LOCAL_PATH)/audio_extn \
-	$(LOCAL_PATH)/voice_extn
+	$(LOCAL_PATH)/voice_extn \
+        $(TARGET_OUT_HEADERS)/libmdmdetect/inc
 
 ifeq ($(strip $(AUDIO_FEATURE_ENABLED_LISTEN)),true)
     LOCAL_CFLAGS += -DAUDIO_LISTEN_ENABLED
diff --git a/hal/audio_extn/audio_extn.c b/hal/audio_extn/audio_extn.c
index ad487b1..245f5ca 100644
--- a/hal/audio_extn/audio_extn.c
+++ b/hal/audio_extn/audio_extn.c
@@ -35,18 +35,22 @@
 struct audio_extn_module {
     bool anc_enabled;
     bool aanc_enabled;
+    bool custom_stereo_enabled;
     uint32_t proxy_channel_num;
 };
 
 static struct audio_extn_module aextnmod = {
     .anc_enabled = 0,
     .aanc_enabled = 0,
+    .custom_stereo_enabled = 0,
     .proxy_channel_num = 2,
 };
 
 #define AUDIO_PARAMETER_KEY_ANC        "anc_enabled"
 #define AUDIO_PARAMETER_KEY_WFD        "wfd_channel_cap"
 #define AUDIO_PARAMETER_CAN_OPEN_PROXY "can_open_proxy"
+#define AUDIO_PARAMETER_CUSTOM_STEREO  "stereo_as_dual_mono"
+
 #ifndef FM_ENABLED
 #define audio_extn_fm_set_parameters(adev, parms) (0)
 #else
@@ -60,6 +64,45 @@
                                            struct str_parms *parms);
 #endif
 
+#ifndef CUSTOM_STEREO_ENABLED
+#define audio_extn_customstereo_set_parameters(adev, parms)         (0)
+#else
+void audio_extn_customstereo_set_parameters(struct audio_device *adev,
+                                           struct str_parms *parms)
+{
+    int ret = 0;
+    char value[32]={0};
+    bool custom_stereo_state = false;
+    const char *mixer_ctl_name = "Set Custom Stereo OnOff";
+    struct mixer_ctl *ctl;
+
+    ALOGV("%s", __func__);
+    ret = str_parms_get_str(parms, AUDIO_PARAMETER_CUSTOM_STEREO, value,
+                            sizeof(value));
+    if (ret >= 0) {
+        if (!strncmp("true", value, sizeof("true")) || atoi(value))
+            custom_stereo_state = true;
+
+        if (custom_stereo_state == aextnmod.custom_stereo_enabled)
+            return;
+
+        ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
+        if (!ctl) {
+            ALOGE("%s: Could not get ctl for mixer cmd - %s",
+                  __func__, mixer_ctl_name);
+            return;
+        }
+        if (mixer_ctl_set_value(ctl, 0, custom_stereo_state) < 0) {
+            ALOGE("%s: Could not set custom stereo state %d",
+                  __func__, custom_stereo_state);
+            return;
+        }
+        aextnmod.custom_stereo_enabled = custom_stereo_state;
+        ALOGV("%s: Setting custom stereo state success", __func__);
+    }
+}
+#endif /* CUSTOM_STEREO_ENABLED */
+
 #ifndef ANC_HEADSET_ENABLED
 #define audio_extn_set_anc_parameters(adev, parms)       (0)
 #else
@@ -131,7 +174,7 @@
 #endif /* ANC_HEADSET_ENABLED */
 
 #ifndef AFE_PROXY_ENABLED
-#define audio_extn_set_afe_proxy_parameters(parms)        (0)
+#define audio_extn_set_afe_proxy_parameters(adev, parms)  (0)
 #define audio_extn_get_afe_proxy_parameters(query, reply) (0)
 #else
 /* Front left channel. */
@@ -247,7 +290,8 @@
     return ret;
 }
 
-void audio_extn_set_afe_proxy_parameters(struct str_parms *parms)
+void audio_extn_set_afe_proxy_parameters(struct audio_device *adev,
+                                         struct str_parms *parms)
 {
     int ret, val;
     char value[32]={0};
@@ -257,6 +301,7 @@
     if (ret >= 0) {
         val = atoi(value);
         aextnmod.proxy_channel_num = val;
+        adev->cur_wfd_channels = val;
         ALOGD("%s: channel capability set to: %d", __func__,
                aextnmod.proxy_channel_num);
     }
@@ -315,11 +360,12 @@
                                struct str_parms *parms)
 {
    audio_extn_set_anc_parameters(adev, parms);
-   audio_extn_set_afe_proxy_parameters(parms);
+   audio_extn_set_afe_proxy_parameters(adev, parms);
    audio_extn_fm_set_parameters(adev, parms);
    audio_extn_listen_set_parameters(adev, parms);
    audio_extn_hfp_set_parameters(adev, parms);
    audio_extn_ddp_set_parameters(adev, parms);
+   audio_extn_customstereo_set_parameters(adev, parms);
 }
 
 void audio_extn_get_parameters(const struct audio_device *adev,
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index fb428db..d432670 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -184,8 +184,10 @@
 
 #ifndef HFP_ENABLED
 #define audio_extn_hfp_is_active(adev)                  (0)
+#define audio_extn_hfp_get_usecase()                    (0)
 #else
 bool audio_extn_hfp_is_active(struct audio_device *adev);
+audio_usecase_t audio_extn_hfp_get_usecase();
 #endif
 
 #endif /* AUDIO_EXTN_H */
diff --git a/hal/audio_extn/dolby.c b/hal/audio_extn/dolby.c
index bcc7381..99fa2b7 100644
--- a/hal/audio_extn/dolby.c
+++ b/hal/audio_extn/dolby.c
@@ -64,7 +64,7 @@
 
 /* DS1-DDP Endp Params */
 #define DDP_ENDP_NUM_PARAMS 17
-#define DDP_ENDP_NUM_DEVICES 22
+#define DDP_ENDP_NUM_DEVICES 23
 static int ddp_endp_params_id[DDP_ENDP_NUM_PARAMS] = {
     PARAM_ID_MAX_OUTPUT_CHANNELS, PARAM_ID_CTL_RUNNING_MODE,
     PARAM_ID_CTL_ERROR_CONCEAL, PARAM_ID_CTL_ERROR_MAX_RPTS,
@@ -147,7 +147,10 @@
               {8, 0, 0, 0, 0, 0, 0, 21, 1, 6, 0, 0, 0, 0, 0, 0, 0},
               {1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0} },
           {AUDIO_DEVICE_OUT_PROXY, 2,
-              {8, 0, 0, 0, 0, 0, 0, 21, 1, 6, 0, 0, 0, 0, 0, 0, 0},
+              {8, 0, 0, 0, 0, 0, 0, 21, 1, 2, 0, 0, 0, 0, 0, 0, 0},
+              {1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0} },
+          {AUDIO_DEVICE_OUT_PROXY, 6,
+              {8, 0, 0, 0, 0, 0, 0, 21, 1, 2, 0, 0, 0, 0, 0, 0, 0},
               {1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0} },
 };
 
@@ -264,9 +267,16 @@
             (usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
             ((usecase->stream.out->format == AUDIO_FORMAT_AC3) ||
              (usecase->stream.out->format == AUDIO_FORMAT_EAC3))) {
+            /*
+             * Use wfd /hdmi sink channel cap for dolby params if device is wfd
+             * or hdmi. Otherwise use stereo configuration
+             */
+            int channel_cap = usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ?
+                              adev->cur_hdmi_channels :
+                              usecase->devices & AUDIO_DEVICE_OUT_PROXY ?
+                              adev->cur_wfd_channels : 2;
             send_ddp_endp_params_stream(usecase->stream.out, usecase->devices,
-                           usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ?
-                           adev->cur_hdmi_channels : 2, false /* set cache */);
+                                        channel_cap, false /* set cache */);
         }
     }
 }
@@ -334,7 +344,9 @@
         update_ddp_endp_table(ddp_dev, dev_ch_cap,
                               PARAM_ID_OUT_CTL_STEREO_MODE, val);
     }
-
+    /* TODO: Do we need device channel caps here?
+     * We dont have that information as this is from dolby modules
+     */
     send_ddp_endp_params(adev, ddp_dev, dev_ch_cap);
 }
 
@@ -343,13 +355,20 @@
                                       audio_format_t format)
 {
     int id = 0;
+    /*
+     * Use wfd /hdmi sink channel cap for dolby params if device is wfd
+     * or hdmi. Otherwise use stereo configuration
+     */
+    int channel_cap = out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ?
+                      adev->cur_hdmi_channels :
+                      out->devices & AUDIO_DEVICE_OUT_PROXY ?
+                      adev->cur_wfd_channels : 2;
 
     switch (format) {
     case AUDIO_FORMAT_AC3:
         id = SND_AUDIOCODEC_AC3;
         send_ddp_endp_params_stream(out, out->devices,
-                            out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ?
-                            adev->cur_hdmi_channels : 2, true /* set_cache */);
+                            channel_cap, true /* set_cache */);
 #ifndef DS1_DOLBY_DAP_ENABLED
         audio_extn_dolby_set_dmid(adev);
 #endif
@@ -357,8 +376,7 @@
     case AUDIO_FORMAT_EAC3:
         id = SND_AUDIOCODEC_EAC3;
         send_ddp_endp_params_stream(out, out->devices,
-                            out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ?
-                            adev->cur_hdmi_channels : 2, true /* set_cache */);
+                            channel_cap, true /* set_cache */);
 #ifndef DS1_DOLBY_DAP_ENABLED
         audio_extn_dolby_set_dmid(adev);
 #endif
@@ -430,8 +448,7 @@
 
     list_for_each(node, &adev->usecase_list) {
         usecase = node_to_item(node, struct audio_usecase, list);
-        if ((usecase->type == PCM_PLAYBACK) &&
-            (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY))
+        if (usecase->type == PCM_PLAYBACK)
             send = true;
     }
     if (!send)
diff --git a/hal/audio_extn/hfp.c b/hal/audio_extn/hfp.c
index 2d6e1e0..c480490 100644
--- a/hal/audio_extn/hfp.c
+++ b/hal/audio_extn/hfp.c
@@ -220,7 +220,7 @@
 bool audio_extn_hfp_is_active(struct audio_device *adev)
 {
     struct audio_usecase *hfp_usecase = NULL;
-    hfp_usecase = get_usecase_from_list(adev, USECASE_AUDIO_HFP_SCO);
+    hfp_usecase = get_usecase_from_list(adev, hfpmod.ucid);
 
     if (hfp_usecase != NULL)
         return true;
@@ -228,6 +228,11 @@
         return false;
 }
 
+audio_usecase_t audio_extn_hfp_get_usecase()
+{
+    return hfpmod.ucid;
+}
+
 void audio_extn_hfp_set_parameters(struct audio_device *adev, struct str_parms *parms)
 {
     int ret;
diff --git a/hal/audio_extn/listen.c b/hal/audio_extn/listen.c
index 4a1980b..91bb04f 100644
--- a/hal/audio_extn/listen.c
+++ b/hal/audio_extn/listen.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -64,6 +64,7 @@
 typedef void (*destroy_listen_hw_t)();
 
 typedef int (*open_listen_session_t)(struct audio_hw_device *,
+                                    struct listen_open_params*,
                                     struct listen_session**);
 
 typedef int (*close_listen_session_t)(struct audio_hw_device *dev,
@@ -119,7 +120,6 @@
 void audio_extn_listen_set_parameters(struct audio_device *adev,
                                struct str_parms *parms)
 {
-
     ALOGV("%s: enter", __func__);
     if (listen_dev) {
          char *kv_pairs = str_parms_to_str(parms);
diff --git a/hal/audio_extn/usb.c b/hal/audio_extn/usb.c
index 88e3cad..699c3b7 100644
--- a/hal/audio_extn/usb.c
+++ b/hal/audio_extn/usb.c
@@ -152,7 +152,7 @@
 
     file_size = st.st_size;
 
-    read_buf = (char *)calloc(1, USB_BUFF_SIZE);
+    read_buf = (char *)calloc(1, USB_BUFF_SIZE + 1);
     err = read(fd, read_buf, USB_BUFF_SIZE);
     str_start = strstr(read_buf, type);
     if (str_start == NULL) {
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 04a7aba..3bb9098 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -53,16 +53,14 @@
 #include "voice_extn.h"
 
 #include "sound/compress_params.h"
+#include "sound/asound.h"
 
-#define MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024)
-#define MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE (2 * 1024)
-#define COMPRESS_OFFLOAD_FRAGMENT_SIZE_FOR_AV_STREAMING (2 * 1024)
-#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
 #define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
 /* ToDo: Check and update a proper value in msec */
 #define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
 #define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
 
+
 #define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
 
 struct pcm_config pcm_config_deep_buffer = {
@@ -155,35 +153,6 @@
 
 static int set_voice_volume_l(struct audio_device *adev, float volume);
 
-/* Read  offload buffer size from a property.
- * If value is not power of 2  round it to
- * power of 2.
- */
-static uint32_t get_offload_buffer_size(audio_offload_info_t* info)
-{
-    char value[PROPERTY_VALUE_MAX] = {0};
-    uint32_t fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
-    if((property_get("audio.offload.buffer.size.kb", value, "")) &&
-            atoi(value)) {
-        fragment_size =  atoi(value) * 1024;
-        //ring buffer size needs to be 4k aligned.
-        CHECK(!(fragment_size * COMPRESS_OFFLOAD_NUM_FRAGMENTS % 4096));
-    }
-
-    if (info != NULL && info->has_video && info->is_streaming) {
-        fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE_FOR_AV_STREAMING;
-        ALOGV("%s: offload fragment size reduced for AV streaming to %d",
-               __func__, out->compr_config.fragment_size);
-    }
-
-    if(fragment_size < MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE)
-        fragment_size = MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE;
-    else if(fragment_size > MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE)
-        fragment_size = MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE;
-    ALOGVV("%s: fragment_size %d", __func__, fragment_size);
-    return fragment_size;
-}
-
 static int check_and_set_gapless_mode(struct audio_device *adev) {
 
 
@@ -214,8 +183,10 @@
 static bool is_supported_format(audio_format_t format)
 {
     if (format == AUDIO_FORMAT_MP3 ||
-            format == AUDIO_FORMAT_AAC)
-        return true;
+        format == AUDIO_FORMAT_AAC ||
+        format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD ||
+        format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
+           return true;
 
     return false;
 }
@@ -231,6 +202,10 @@
     case AUDIO_FORMAT_AAC:
         id = SND_AUDIOCODEC_AAC;
         break;
+    case AUDIO_FORMAT_PCM_16_BIT_OFFLOAD:
+    case AUDIO_FORMAT_PCM_24_BIT_OFFLOAD:
+        id = SND_AUDIOCODEC_PCM;
+        break;
     default:
         ALOGE("%s: Unsupported audio format :%x", __func__, format);
     }
@@ -627,6 +602,7 @@
     struct audio_usecase *vc_usecase = NULL;
     struct audio_usecase *voip_usecase = NULL;
     struct audio_usecase *hfp_usecase = NULL;
+    audio_usecase_t hfp_ucid;
     struct listnode *node;
     int status = 0;
 
@@ -666,7 +642,8 @@
                     out_snd_device = voip_usecase->out_snd_device;
             }
         } else if (audio_extn_hfp_is_active(adev)) {
-            hfp_usecase = get_usecase_from_list(adev, USECASE_AUDIO_HFP_SCO);
+            hfp_ucid = audio_extn_hfp_get_usecase();
+            hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
             if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
                    in_snd_device = hfp_usecase->in_snd_device;
                    out_snd_device = hfp_usecase->out_snd_device;
@@ -729,6 +706,15 @@
         disable_snd_device(adev, usecase->in_snd_device, false);
     }
 
+    /* Applicable only on the targets that has external modem.
+     * New device information should be sent to modem before enabling
+     * the devices to reduce in-call device switch time.
+     */
+    if (usecase->type == VOICE_CALL)
+        status = platform_switch_voice_call_enable_device_config(adev->platform,
+                                                                 out_snd_device,
+                                                                 in_snd_device);
+
     /* Enable new sound devices */
     if (out_snd_device != SND_DEVICE_NONE) {
         if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
@@ -936,24 +922,30 @@
         send_callback = false;
         switch(cmd->cmd) {
         case OFFLOAD_CMD_WAIT_FOR_BUFFER:
+            ALOGD("copl(%x):calling compress_wait", (unsigned int)out);
             compress_wait(out->compr, -1);
+            ALOGD("copl(%x):out of compress_wait", (unsigned int)out);
             send_callback = true;
             event = STREAM_CBK_EVENT_WRITE_READY;
             break;
         case OFFLOAD_CMD_PARTIAL_DRAIN:
             ret = compress_next_track(out->compr);
-            if(ret == 0)
+            if(ret == 0) {
+                ALOGD("copl(%x):calling compress_partial_drain", (unsigned int)out);
                 compress_partial_drain(out->compr);
+                ALOGD("copl(%x):out of compress_partial_drain", (unsigned int)out);
+            }
             else if(ret == -ETIMEDOUT)
                 compress_drain(out->compr);
             else
                 ALOGE("%s: Next track returned error %d",__func__, ret);
-
             send_callback = true;
             event = STREAM_CBK_EVENT_DRAIN_READY;
             break;
         case OFFLOAD_CMD_DRAIN:
+            ALOGD("copl(%x):calling compress_drain", (unsigned int)out);
             compress_drain(out->compr);
+            ALOGD("copl(%x):calling compress_drain", (unsigned int)out);
             send_callback = true;
             event = STREAM_CBK_EVENT_DRAIN_READY;
             break;
@@ -1028,6 +1020,12 @@
                       "no change in HDMI channels", __func__);
                 ret = false;
                 break;
+            } else if (usecase->id == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
+                       popcount(usecase->stream.out->channel_mask) > 2) {
+                ALOGD("%s: multi-channel(%x) compress offload playback is active, "
+                      "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
+                ret = false;
+                break;
             }
         }
     }
@@ -1125,7 +1123,7 @@
     struct audio_usecase *uc_info;
     struct audio_device *adev = out->dev;
 
-    ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
+    ALOGD("%s: enter: usecase(%d: %s) devices(%#x)",
           __func__, out->usecase, use_case_table[out->usecase], out->devices);
     out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
     if (out->pcm_device_id < 0) {
@@ -1327,6 +1325,7 @@
                 out->pcm = NULL;
             }
         } else {
+            ALOGD("copl(%x):standby", (unsigned int)out);
             stop_compressed_output_l(out);
             out->gapless_mdata.encoder_delay = 0;
             out->gapless_mdata.encoder_padding = 0;
@@ -1610,9 +1609,9 @@
     }
 
     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
-        ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
+        ALOGD("copl(%x): writing buffer (%d bytes) to compress device", (unsigned int)out, bytes);
         if (out->send_new_metadata) {
-            ALOGVV("send new gapless metadata");
+            ALOGD("copl(%x):send new gapless metadata", (unsigned int)out);
             compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
             out->send_new_metadata = 0;
         }
@@ -1620,6 +1619,7 @@
         ret = compress_write(out->compr, buffer, bytes);
         ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
         if (ret >= 0 && ret < (ssize_t)bytes) {
+            ALOGD("No space available in compress driver, post msg to cb thread");
             send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
         }
         if (!out->playback_started) {
@@ -1752,6 +1752,7 @@
     int status = -ENOSYS;
     ALOGV("%s", __func__);
     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        ALOGD("copl(%x):pause compress driver", (unsigned int)out);
         pthread_mutex_lock(&out->lock);
         if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
             status = compress_pause(out->compr);
@@ -1768,6 +1769,7 @@
     int status = -ENOSYS;
     ALOGV("%s", __func__);
     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        ALOGD("copl(%x):resume compress driver", (unsigned int)out);
         status = 0;
         pthread_mutex_lock(&out->lock);
         if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
@@ -1800,9 +1802,11 @@
     struct stream_out *out = (struct stream_out *)stream;
     ALOGV("%s", __func__);
     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        ALOGD("copl(%x):calling compress flush", (unsigned int)out);
         pthread_mutex_lock(&out->lock);
         stop_compressed_output_l(out);
         pthread_mutex_unlock(&out->lock);
+        ALOGD("copl(%x):out of compress flush", (unsigned int)out);
         return 0;
     }
     return -ENOSYS;
@@ -1914,11 +1918,10 @@
                 (voice_extn_compress_voip_is_format_supported(in->format)) &&
                 (in->config.rate == 8000 || in->config.rate == 16000) &&
                 (popcount(in->channel_mask) == 1)) {
-                ret = voice_extn_compress_voip_open_input_stream(in);
-                if (ret != 0) {
+                err = voice_extn_compress_voip_open_input_stream(in);
+                if (err != 0) {
                     ALOGE("%s: Compress voip input cannot be opened, error:%d",
-                          __func__, ret);
-                    goto done;
+                          __func__, err);
                 }
             }
         }
@@ -2142,6 +2145,9 @@
             goto error_open;
         }
     } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
+        ALOGD("%s: copl(%x): sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
+              __func__, (unsigned int)out, config->sample_rate, config->channel_mask, devices, flags);
+
         if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
             config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
             ALOGE("%s: Unsupported Offload information", __func__);
@@ -2161,8 +2167,10 @@
         out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
         if (config->offload_info.channel_mask)
             out->channel_mask = config->offload_info.channel_mask;
-        else if (config->channel_mask)
+        else if (config->channel_mask) {
             out->channel_mask = config->channel_mask;
+            config->offload_info.channel_mask = config->channel_mask;
+        }
         out->format = config->offload_info.format;
         out->sample_rate = config->offload_info.sample_rate;
 
@@ -2179,7 +2187,13 @@
         else
             out->compr_config.codec->id =
                 get_snd_codec_id(config->offload_info.format);
-        out->compr_config.fragment_size = get_offload_buffer_size(&config->offload_info);
+        if (audio_is_offload_pcm(config->offload_info.format)) {
+            out->compr_config.fragment_size =
+                       platform_get_pcm_offload_buffer_size(&config->offload_info);
+        } else {
+            out->compr_config.fragment_size =
+                       platform_get_compress_offload_buffer_size(&config->offload_info);
+        }
         out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
         out->compr_config.codec->sample_rate =
                     compress_get_alsa_rate(config->offload_info.sample_rate);
@@ -2190,6 +2204,11 @@
         out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
         out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
 
+        if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
+            out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
+        else if(config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
+            out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
+
         if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
             out->non_blocking = 1;
 
@@ -2676,6 +2695,7 @@
     adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
     voice_init(adev);
     list_init(&adev->usecase_list);
+    adev->cur_wfd_channels = 2;
 
     /* Loads platform specific libraries dynamically */
     adev->platform = platform_init(adev);
diff --git a/hal/audio_hw.h b/hal/audio_hw.h
index 205977b..21b7790 100644
--- a/hal/audio_hw.h
+++ b/hal/audio_hw.h
@@ -223,6 +223,7 @@
     bool speaker_lr_swap;
     struct voice voice;
     unsigned int cur_hdmi_channels;
+    unsigned int cur_wfd_channels;
 
     int snd_card;
     void *platform;
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index c9719bb..4161d87 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -31,12 +31,31 @@
 #include "platform.h"
 #include "audio_extn.h"
 #include "voice_extn.h"
+#include "sound/compress_params.h"
+#include "mdm_detect.h"
 
 #define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
 #define MIXER_XML_PATH_AUXPCM "/system/etc/mixer_paths_auxpcm.xml"
 #define LIB_ACDB_LOADER "libacdbloader.so"
 #define AUDIO_DATA_BLOCK_MIXER_CTL "HDMI EDID"
 
+#define MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024)
+#define MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE (2 * 1024)
+#define COMPRESS_OFFLOAD_FRAGMENT_SIZE_FOR_AV_STREAMING (2 * 1024)
+#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
+
+/* Used in calculating fragment size for pcm offload */
+#define PCM_OFFLOAD_BUFFER_DURATION_FOR_AV 2000 /* 2 secs */
+#define PCM_OFFLOAD_BUFFER_DURATION_FOR_AV_STREAMING 100 /* 100 millisecs */
+
+/* MAX PCM fragment size cannot be increased  further due
+ * to flinger's cblk size of 1mb,and it has to be a multiple of
+ * 24 - lcm of channels supported by DSP
+ */
+#define MAX_PCM_OFFLOAD_FRAGMENT_SIZE (240 * 1024)
+#define MIN_PCM_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
+
+#define ALIGN( num, to ) (((num) + (to-1)) & (~(to-1)))
 /*
  * This file will have a maximum of 38 bytes:
  *
@@ -416,6 +435,13 @@
                   __func__, dlerror());
             goto error;
         }
+        csd->enable_device_config = (enable_device_config_t)dlsym(csd->csd_client,
+                                               "csd_client_enable_device_config");
+        if (csd->enable_device_config == NULL) {
+            ALOGE("%s: dlsym error %s for csd_client_enable_device_config",
+                  __func__, dlerror());
+            goto error;
+        }
         csd->enable_device = (enable_device_t)dlsym(csd->csd_client,
                                              "csd_client_enable_device");
         if (csd->enable_device == NULL) {
@@ -514,10 +540,28 @@
     }
 }
 
+static void platform_csd_init(struct platform_data *plat_data)
+{
+    struct dev_info mdm_detect_info;
+    int ret = 0;
+
+    /* Call ESOC API to get the number of modems.
+     * If the number of modems is not zero, load CSD Client specific
+     * symbols. Voice call is handled by MDM and apps processor talks to
+     * MDM through CSD Client
+     */
+    ret = get_system_info(&mdm_detect_info);
+    if (ret > 0) {
+        ALOGE("%s: Failed to get system info, ret %d", __func__, ret);
+    }
+    ALOGD("%s: num_modems %d\n", __func__, mdm_detect_info.num_modems);
+
+    if (mdm_detect_info.num_modems > 0)
+        plat_data->csd = open_csd_client();
+}
+
 void *platform_init(struct audio_device *adev)
 {
-    char platform[PROPERTY_VALUE_MAX];
-    char baseband[PROPERTY_VALUE_MAX];
     char value[PROPERTY_VALUE_MAX];
     struct platform_data *my_data = NULL;
     int retry_num = 0, snd_card_num = 0;
@@ -653,16 +697,8 @@
     /* Initialize ACDB ID's */
     platform_info_init();
 
-    /* If platform is apq8084 and baseband is MDM, load CSD Client specific
-     * symbols. Voice call is handled by MDM and apps processor talks to
-     * MDM through CSD Client
-     */
-    property_get("ro.board.platform", platform, "");
-    property_get("ro.baseband", baseband, "");
-    if (!strncmp("apq8084", platform, sizeof("apq8084")) &&
-        !strncmp("mdm", baseband, sizeof("mdm"))) {
-         my_data->csd = open_csd_client();
-    }
+    /* load csd client */
+    platform_csd_init(my_data);
 
     /* init usb */
     audio_extn_usb_init(adev);
@@ -831,6 +867,32 @@
     return ret;
 }
 
+int platform_switch_voice_call_enable_device_config(void *platform,
+                                                    snd_device_t out_snd_device,
+                                                    snd_device_t in_snd_device)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    int acdb_rx_id, acdb_tx_id;
+    int ret = 0;
+
+    acdb_rx_id = acdb_device_table[out_snd_device];
+    acdb_tx_id = acdb_device_table[in_snd_device];
+
+    if (my_data->csd != NULL) {
+        if (acdb_rx_id > 0 && acdb_tx_id > 0) {
+            ret = my_data->csd->enable_device_config(acdb_rx_id, acdb_tx_id);
+            if (ret < 0) {
+                ALOGE("%s: csd_enable_device_config, failed, error %d",
+                      __func__, ret);
+            }
+        } else {
+            ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
+                  acdb_rx_id, acdb_tx_id);
+        }
+    }
+    return ret;
+}
+
 int platform_switch_voice_call_device_post(void *platform,
                                            snd_device_t out_snd_device,
                                            snd_device_t in_snd_device)
@@ -1666,7 +1728,7 @@
 
     kv_pairs = str_parms_to_str(reply);
     ALOGV_IF(kv_pairs != NULL, "%s: exit: returns - %s", __func__, kv_pairs);
-    free(reply);
+    free(kv_pairs);
 }
 
 /* Delay in Us */
@@ -1700,3 +1762,69 @@
     else
         return false;
 }
+
+/* Read  offload buffer size from a property.
+ * If value is not power of 2  round it to
+ * power of 2.
+ */
+uint32_t platform_get_compress_offload_buffer_size(audio_offload_info_t* info)
+{
+    char value[PROPERTY_VALUE_MAX] = {0};
+    uint32_t fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
+    if((property_get("audio.offload.buffer.size.kb", value, "")) &&
+            atoi(value)) {
+        fragment_size =  atoi(value) * 1024;
+    }
+
+    if (info != NULL && info->has_video && info->is_streaming) {
+        fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE_FOR_AV_STREAMING;
+        ALOGV("%s: offload fragment size reduced for AV streaming to %d",
+               __func__, out->compr_config.fragment_size);
+    }
+
+    fragment_size = ALIGN( fragment_size, 1024);
+
+    if(fragment_size < MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE)
+        fragment_size = MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE;
+    else if(fragment_size > MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE)
+        fragment_size = MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE;
+    ALOGV("%s: fragment_size %d", __func__, fragment_size);
+    return fragment_size;
+}
+
+uint32_t platform_get_pcm_offload_buffer_size(audio_offload_info_t* info)
+{
+    uint32_t fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
+    uint32_t bits_per_sample = 16;
+
+    if (info->format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD) {
+        bits_per_sample = 32;
+    }
+
+    if (!info->has_video) {
+        fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
+
+    } else if (info->has_video && info->is_streaming) {
+        fragment_size = (PCM_OFFLOAD_BUFFER_DURATION_FOR_AV_STREAMING
+                                     * info->sample_rate
+                                     * bits_per_sample
+                                     * popcount(info->channel_mask))/1000;
+
+    } else if (info->has_video) {
+        fragment_size = (PCM_OFFLOAD_BUFFER_DURATION_FOR_AV
+                                     * info->sample_rate
+                                     * bits_per_sample
+                                     * popcount(info->channel_mask))/1000;
+    }
+
+    fragment_size = ALIGN( fragment_size, 1024);
+
+    if(fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
+        fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
+    else if(fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
+        fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
+
+    ALOGV("%s: fragment_size %d", __func__, fragment_size);
+    return fragment_size;
+}
+
diff --git a/hal/msm8974/platform.h b/hal/msm8974/platform.h
index 3ea068d..e80d3f1 100644
--- a/hal/msm8974/platform.h
+++ b/hal/msm8974/platform.h
@@ -168,8 +168,6 @@
 #define FM_PLAYBACK_PCM_DEVICE 5
 #define FM_CAPTURE_PCM_DEVICE  6
 #define HFP_PCM_RX 5
-#define HFP_SCO_RX 23
-#define HFP_ASM_RX_TX 24
 
 #define INCALL_MUSIC_UPLINK_PCM_DEVICE 1
 #define INCALL_MUSIC_UPLINK2_PCM_DEVICE 16
@@ -213,11 +211,20 @@
 #define QCHAT_CALL_PCM_DEVICE 20
 #endif
 
+#ifdef PLATFORM_MSM8x26
+#define HFP_SCO_RX 28
+#define HFP_ASM_RX_TX 29
+#else
+#define HFP_SCO_RX 23
+#define HFP_ASM_RX_TX 24
+#endif
+
 #define LIB_CSD_CLIENT "libcsd-client.so"
 /* CSD-CLIENT related functions */
 typedef int (*init_t)();
 typedef int (*deinit_t)();
 typedef int (*disable_device_t)();
+typedef int (*enable_device_config_t)(int, int);
 typedef int (*enable_device_t)(int, int, uint32_t);
 typedef int (*volume_t)(uint32_t, int);
 typedef int (*mic_mute_t)(uint32_t, int);
@@ -234,6 +241,7 @@
     init_t init;
     deinit_t deinit;
     disable_device_t disable_device;
+    enable_device_config_t enable_device_config;
     enable_device_t enable_device;
     volume_t volume;
     mic_mute_t mic_mute;
diff --git a/hal/platform_api.h b/hal/platform_api.h
index a5f5074..81291a2 100644
--- a/hal/platform_api.h
+++ b/hal/platform_api.h
@@ -31,6 +31,9 @@
 int platform_set_snd_device_acdb_id(snd_device_t snd_device, unsigned int acdb_id);
 int platform_send_audio_calibration(void *platform, snd_device_t snd_device);
 int platform_switch_voice_call_device_pre(void *platform);
+int platform_switch_voice_call_enable_device_config(void *platform,
+                                                    snd_device_t out_snd_device,
+                                                    snd_device_t in_snd_device);
 int platform_switch_voice_call_device_post(void *platform,
                                            snd_device_t out_snd_device,
                                            snd_device_t in_snd_device);
@@ -62,4 +65,8 @@
 /* From platform_info_parser.c */
 int platform_info_init(void);
 
+struct audio_offload_info_t;
+uint32_t platform_get_compress_offload_buffer_size(audio_offload_info_t* info);
+uint32_t platform_get_pcm_offload_buffer_size(audio_offload_info_t* info);
+
 #endif // AUDIO_PLATFORM_API_H
diff --git a/hal/voice_extn/compress_voip.c b/hal/voice_extn/compress_voip.c
index 4db46f6..47ac2c8 100644
--- a/hal/voice_extn/compress_voip.c
+++ b/hal/voice_extn/compress_voip.c
@@ -59,8 +59,9 @@
     struct pcm *pcm_rx;
     struct pcm *pcm_tx;
     struct stream_out *out_stream;
-    int ref_count;
-    int out_stream_count;
+    uint32_t out_stream_count;
+    uint32_t in_stream_count;
+    uint32_t sample_rate;
 };
 
 #define MODE_IS127              0x2
@@ -78,13 +79,15 @@
 #define AUDIO_PARAMETER_VALUE_VOIP_TRUE             "true"
 #define AUDIO_PARAMETER_KEY_VOIP_CHECK              "voip_flag"
 #define AUDIO_PARAMETER_KEY_VOIP_OUT_STREAM_COUNT   "voip_out_stream_count"
+#define AUDIO_PARAMETER_KEY_VOIP_SAMPLE_RATE        "voip_sample_rate"
 
 static struct voip_data voip_data = {
   .pcm_rx = NULL,
   .pcm_tx = NULL,
   .out_stream = NULL,
-  .ref_count = 0,
-  .out_stream_count = 0
+  .out_stream_count = 0,
+  .in_stream_count = 0,
+  .sample_rate = 0
 };
 
 static int voip_set_volume(struct audio_device *adev, int volume);
@@ -280,10 +283,10 @@
     int i, ret = 0;
     struct audio_usecase *uc_info;
 
-    ALOGD("%s: enter, ref_count=%d", __func__, voip_data.ref_count);
-    voip_data.ref_count--;
+    ALOGD("%s: enter, out_stream_count=%d, in_stream_count=%d",
+           __func__, voip_data.out_stream_count, voip_data.in_stream_count);
 
-    if (!voip_data.ref_count) {
+    if (!voip_data.out_stream_count && !voip_data.in_stream_count) {
         uc_info = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
         if (uc_info == NULL) {
             ALOGE("%s: Could not find the usecase (%d) in the list",
@@ -310,8 +313,10 @@
 
         list_remove(&uc_info->list);
         free(uc_info);
+        voip_data.sample_rate = 0;
     } else
-        ALOGV("%s: NO-OP because ref_count=%d", __func__, voip_data.ref_count);
+        ALOGV("%s: NO-OP because out_stream_count=%d, in_stream_count=%d",
+               __func__, voip_data.out_stream_count, voip_data.in_stream_count);
 
     ALOGV("%s: exit: status(%d)", __func__, ret);
     return ret;
@@ -327,12 +332,15 @@
     ALOGD("%s: enter", __func__);
 
     uc_info = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
-    if ((uc_info == NULL) && (voip_data.out_stream)) {
+    if (uc_info == NULL) {
         ALOGV("%s: voip usecase is added to the list", __func__);
         uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
         uc_info->id = USECASE_COMPRESS_VOIP_CALL;
         uc_info->type = VOIP_CALL;
-        uc_info->stream.out = voip_data.out_stream;
+        if (voip_data.out_stream)
+            uc_info->stream.out = voip_data.out_stream;
+        else
+            uc_info->stream.out = adev->primary_output;
         uc_info->in_snd_device = SND_DEVICE_NONE;
         uc_info->out_snd_device = SND_DEVICE_NONE;
 
@@ -388,12 +396,15 @@
             ALOGE("%s: error %d\n", __func__, ret);
             goto error_start_voip;
         }
-        voip_data.ref_count = 0;
-    }
-    else
+    } else {
         ALOGV("%s: voip usecase is already enabled", __func__);
+        if (voip_data.out_stream)
+            uc_info->stream.out = voip_data.out_stream;
+        else
+            uc_info->stream.out = adev->primary_output;
+        select_devices(adev, USECASE_COMPRESS_VOIP_CALL);
+    }
 
-    voip_data.ref_count++;
     return 0;
 
 error_start_voip:
@@ -472,6 +483,13 @@
         str_parms_add_int(reply, AUDIO_PARAMETER_KEY_VOIP_OUT_STREAM_COUNT,
                           voip_data.out_stream_count);
     }
+
+    ret = str_parms_get_str(query, AUDIO_PARAMETER_KEY_VOIP_SAMPLE_RATE,
+                            value, sizeof(value));
+    if (ret >= 0) {
+        str_parms_add_int(reply, AUDIO_PARAMETER_KEY_VOIP_SAMPLE_RATE,
+                          voip_data.sample_rate);
+    }
 }
 
 void voice_extn_compress_voip_out_get_parameters(struct stream_out *out,
@@ -576,9 +594,9 @@
 
     ALOGD("%s: enter", __func__);
 
+    voip_data.out_stream_count--;
     ret = voip_stop_call(adev);
     voip_data.out_stream = NULL;
-    voip_data.out_stream_count--;
 
     ALOGV("%s: exit: status(%d)", __func__, ret);
     return ret;
@@ -600,7 +618,7 @@
 
     voip_data.out_stream = out;
     voip_data.out_stream_count++;
-
+    voip_data.sample_rate = out->sample_rate;
     ret = voip_set_mode(out->dev, out->format);
 
     ALOGV("%s: exit", __func__);
@@ -615,6 +633,7 @@
 
     ALOGD("%s: enter", __func__);
 
+    voip_data.in_stream_count--;
     status = voip_stop_call(adev);
 
     ALOGV("%s: exit: status(%d)", __func__, status);
@@ -630,15 +649,25 @@
 
     ALOGD("%s: enter", __func__);
 
+    if ((voip_data.sample_rate != 0) &&
+        (voip_data.sample_rate != in->config.rate)) {
+        ret = -ENOTSUP;
+        goto done;
+    } else {
+        voip_data.sample_rate = in->config.rate;
+    }
+
     in->usecase = USECASE_COMPRESS_VOIP_CALL;
     if (in->config.rate == 16000)
         in->config = pcm_config_voip_wb;
     else
         in->config = pcm_config_voip_nb;
 
+    voip_data.in_stream_count++;
     ret = voip_set_mode(in->dev, in->format);
 
-    ALOGV("%s: exit", __func__);
+done:
+    ALOGV("%s: exit, ret=%d", __func__, ret);
     return ret;
 }
 
@@ -734,7 +763,8 @@
     if (ret) {
         if ((popcount(config->channel_mask) == 1) &&
             (config->sample_rate == 8000 || config->sample_rate == 16000))
-            ret = true;
+            ret = ((voip_data.sample_rate == 0) ? true:
+                    (voip_data.sample_rate == config->sample_rate));
         else
             ret = false;
     }
diff --git a/mm-audio/aenc-aac/Android.mk b/mm-audio/aenc-aac/Android.mk
index 8ab45b3..7cd804a 100644
--- a/mm-audio/aenc-aac/Android.mk
+++ b/mm-audio/aenc-aac/Android.mk
@@ -24,6 +24,8 @@
 ifeq ($(call is-board-platform,mpq8092),true)
 include $(AENC_AAC_PATH)/qdsp6/Android.mk
 endif
-
+ifeq ($(call is-board-platform,msm8916),true)
+include $(AENC_AAC_PATH)/qdsp6/Android.mk
+endif
 
 endif
diff --git a/mm-audio/aenc-amrnb/Android.mk b/mm-audio/aenc-amrnb/Android.mk
index 2601ede..79474c6 100644
--- a/mm-audio/aenc-amrnb/Android.mk
+++ b/mm-audio/aenc-amrnb/Android.mk
@@ -24,6 +24,8 @@
 ifeq ($(call is-board-platform,mpq8092),true)
 include $(AENC_AMR_PATH)/qdsp6/Android.mk
 endif
-
+ifeq ($(call is-board-platform,msm8916),true)
+include $(AENC_AMR_PATH)/qdsp6/Android.mk
+endif
 
 endif
diff --git a/mm-audio/aenc-evrc/Android.mk b/mm-audio/aenc-evrc/Android.mk
index 2f42d6b..649f860 100644
--- a/mm-audio/aenc-evrc/Android.mk
+++ b/mm-audio/aenc-evrc/Android.mk
@@ -24,6 +24,8 @@
 ifeq ($(call is-board-platform,mpq8092),true)
 include $(AENC_EVRC_PATH)/qdsp6/Android.mk
 endif
-
+ifeq ($(call is-board-platform,msm8916),true)
+include $(AENC_EVRC_PATH)/qdsp6/Android.mk
+endif
 
 endif
diff --git a/mm-audio/aenc-qcelp13/Android.mk b/mm-audio/aenc-qcelp13/Android.mk
index fe18efc..c919f19 100644
--- a/mm-audio/aenc-qcelp13/Android.mk
+++ b/mm-audio/aenc-qcelp13/Android.mk
@@ -24,5 +24,8 @@
 ifeq ($(call is-board-platform,mpq8092),true)
 include $(AENC_QCELP13_PATH)/qdsp6/Android.mk
 endif
+ifeq ($(call is-board-platform,msm8916),true)
+include $(AENC_QCELP13_PATH)/qdsp6/Android.mk
+endif
 
 endif
diff --git a/policy_hal/AudioPolicyManager.cpp b/policy_hal/AudioPolicyManager.cpp
index 5142353..a774c9e 100644
--- a/policy_hal/AudioPolicyManager.cpp
+++ b/policy_hal/AudioPolicyManager.cpp
@@ -1116,16 +1116,6 @@
         return false;
     }
 #endif
-
-    // Check if offload has been disabled
-    char propValue[PROPERTY_VALUE_MAX];
-    if (property_get("audio.offload.disable", propValue, "0")) {
-        if (atoi(propValue) != 0) {
-            ALOGV("offload disabled by audio.offload.disable=%s", propValue );
-            return false;
-        }
-    }
-
     // Check if stream type is music, then only allow offload as of now.
     if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
     {
@@ -1133,26 +1123,65 @@
         return false;
     }
 
-    //TODO: enable audio offloading with video when ready
-    if (offloadInfo.has_video)
-    {
-        if(property_get("av.offload.enable", propValue, NULL)) {
+    char propValue[PROPERTY_VALUE_MAX];
+    bool pcmOffload = false;
+    if (audio_is_offload_pcm(offloadInfo.format)) {
+        if(property_get("audio.offload.pcm.enable", propValue, NULL)) {
             bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
-            if (!prop_enabled) {
-               ALOGW("offload disabled by av.offload.enable = %s ", propValue );
-               return false;
+            if (prop_enabled) {
+                ALOGW("PCM offload property is enabled");
+                pcmOffload = true;
             }
         }
-        if(offloadInfo.is_streaming &&
-           property_get("av.streaming.offload.enable", propValue, NULL)) {
-            bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
-            if (!prop_enabled) {
-               ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
-               return false;
+        if (!pcmOffload) {
+            ALOGV("PCM offload disabled by property audio.offload.pcm.enable");
+            return false;
+        }
+    }
+
+    if (!pcmOffload) {
+        // Check if offload has been disabled
+        if (property_get("audio.offload.disable", propValue, "0")) {
+            if (atoi(propValue) != 0) {
+                ALOGV("offload disabled by audio.offload.disable=%s", propValue );
+                return false;
             }
         }
-        ALOGV("isOffloadSupported: has_video == true, property\
-                set to enable offload");
+
+        //check if it's multi-channel AAC format
+        if (AudioSystem::popCount(offloadInfo.channel_mask) > 2
+              && offloadInfo.format == AUDIO_FORMAT_AAC) {
+            ALOGV("offload disabled for multi-channel AAC format");
+            return false;
+        }
+
+        if (offloadInfo.has_video)
+        {
+            if(property_get("av.offload.enable", propValue, NULL)) {
+                bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
+                if (!prop_enabled) {
+                    ALOGW("offload disabled by av.offload.enable = %s ", propValue );
+                    return false;
+                }
+            } else {
+                return false;
+            }
+
+            if(offloadInfo.is_streaming) {
+                if (property_get("av.streaming.offload.enable", propValue, NULL)) {
+                    bool prop_enabled = atoi(propValue) || !strncmp("true", propValue, 4);
+                    if (!prop_enabled) {
+                       ALOGW("offload disabled by av.streaming.offload.enable = %s ", propValue );
+                       return false;
+                    }
+                } else {
+                    //Do not offload AV streamnig if the property is not defined
+                    return false;
+                }
+            }
+            ALOGV("isOffloadSupported: has_video == true, property\
+                    set to enable offload");
+        }
     }
 
     //If duration is less than minimum value defined in property, return false
@@ -1165,7 +1194,7 @@
         ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
         //duration checks only valid for MP3/AAC formats,
         //do not check duration for other audio formats, e.g. dolby AAC/AC3 and amrwb+ formats
-        if (offloadInfo.format == AUDIO_FORMAT_MP3 || offloadInfo.format == AUDIO_FORMAT_AAC)
+        if (offloadInfo.format == AUDIO_FORMAT_MP3 || offloadInfo.format == AUDIO_FORMAT_AAC || pcmOffload)
             return false;
     }
 
diff --git a/post_proc/Android.mk b/post_proc/Android.mk
index b6966e6..2cb910c 100644
--- a/post_proc/Android.mk
+++ b/post_proc/Android.mk
@@ -25,7 +25,7 @@
 
 LOCAL_C_INCLUDES := \
 	external/tinyalsa/include \
-	kernel/include/sound \
+        $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include \
 	$(call include-path-for, audio-effects)
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/post_proc/bass_boost.c b/post_proc/bass_boost.c
index c724b58..9e6c1fc 100644
--- a/post_proc/bass_boost.c
+++ b/post_proc/bass_boost.c
@@ -23,7 +23,7 @@
 #include <cutils/list.h>
 #include <cutils/log.h>
 #include <tinyalsa/asoundlib.h>
-#include <audio_effects.h>
+#include <sound/audio_effects.h>
 #include <audio_effects/effect_bassboost.h>
 
 #include "effect_api.h"
@@ -211,9 +211,14 @@
     bassboost_context_t *bass_ctxt = (bassboost_context_t *)context;
 
     ALOGV("%s", __func__);
-
-    if (!offload_bassboost_get_enable_flag(&(bass_ctxt->offload_bass)))
+    if (!offload_bassboost_get_enable_flag(&(bass_ctxt->offload_bass))) {
         offload_bassboost_set_enable_flag(&(bass_ctxt->offload_bass), true);
+        if (bass_ctxt->ctl && bass_ctxt->strength)
+            offload_bassboost_send_params(bass_ctxt->ctl,
+                                          bass_ctxt->offload_bass,
+                                          OFFLOAD_SEND_BASSBOOST_ENABLE_FLAG |
+                                          OFFLOAD_SEND_BASSBOOST_STRENGTH);
+    }
     return 0;
 }
 
diff --git a/post_proc/bundle.h b/post_proc/bundle.h
index a8e0f93..7ebea92 100644
--- a/post_proc/bundle.h
+++ b/post_proc/bundle.h
@@ -21,7 +21,7 @@
 #define OFFLOAD_EFFECT_BUNDLE_H
 
 #include <tinyalsa/asoundlib.h>
-#include <audio_effects.h>
+#include <sound/audio_effects.h>
 #include "effect_api.h"
 
 /* Retry for delay for mixer open */
diff --git a/post_proc/effect_api.c b/post_proc/effect_api.c
index a2e4f45..0b77969 100644
--- a/post_proc/effect_api.c
+++ b/post_proc/effect_api.c
@@ -33,7 +33,7 @@
 #include <stdbool.h>
 #include <cutils/log.h>
 #include <tinyalsa/asoundlib.h>
-#include <audio_effects.h>
+#include <sound/audio_effects.h>
 
 #include "effect_api.h"
 
diff --git a/post_proc/equalizer.c b/post_proc/equalizer.c
index 7c7ced2..bde8ef8 100644
--- a/post_proc/equalizer.c
+++ b/post_proc/equalizer.c
@@ -23,7 +23,7 @@
 #include <cutils/list.h>
 #include <cutils/log.h>
 #include <tinyalsa/asoundlib.h>
-#include <audio_effects.h>
+#include <sound/audio_effects.h>
 #include <audio_effects/effect_equalizer.h>
 
 #include "effect_api.h"
diff --git a/post_proc/reverb.c b/post_proc/reverb.c
index d104073..4c7fe25 100644
--- a/post_proc/reverb.c
+++ b/post_proc/reverb.c
@@ -23,7 +23,7 @@
 #include <cutils/list.h>
 #include <cutils/log.h>
 #include <tinyalsa/asoundlib.h>
-#include <audio_effects.h>
+#include <sound/audio_effects.h>
 #include <audio_effects/effect_environmentalreverb.h>
 #include <audio_effects/effect_presetreverb.h>
 
diff --git a/post_proc/virtualizer.c b/post_proc/virtualizer.c
index e9eb728..4190129 100644
--- a/post_proc/virtualizer.c
+++ b/post_proc/virtualizer.c
@@ -23,7 +23,7 @@
 #include <cutils/list.h>
 #include <cutils/log.h>
 #include <tinyalsa/asoundlib.h>
-#include <audio_effects.h>
+#include <sound/audio_effects.h>
 #include <audio_effects/effect_virtualizer.h>
 
 #include "effect_api.h"
@@ -210,9 +210,14 @@
     virtualizer_context_t *virt_ctxt = (virtualizer_context_t *)context;
 
     ALOGV("%s", __func__);
-
-    if (!offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt)))
+    if (!offload_virtualizer_get_enable_flag(&(virt_ctxt->offload_virt))) {
         offload_virtualizer_set_enable_flag(&(virt_ctxt->offload_virt), true);
+        if (virt_ctxt->ctl && virt_ctxt->strength)
+            offload_virtualizer_send_params(virt_ctxt->ctl,
+                                          virt_ctxt->offload_virt,
+                                          OFFLOAD_SEND_VIRTUALIZER_ENABLE_FLAG |
+                                          OFFLOAD_SEND_BASSBOOST_STRENGTH);
+    }
     return 0;
 }