Merge commit 'd34f05cb6e67e28a4955332173950466edf0be3e' into audio-hal.lnx.5.1

Change-Id: I4656a1a78c2b1d4b4834b2fd538699f96e2aa90e
diff --git a/hal/Makefile.am b/hal/Makefile.am
index 995622d..aafc3e9 100644
--- a/hal/Makefile.am
+++ b/hal/Makefile.am
@@ -187,6 +187,7 @@
 
 if AUDIO_HW_LOOPBACK
 AM_CFLAGS += -DAUDIO_HW_LOOPBACK_ENABLED
+AM_CFLAGS += -DCOMPRESS_METADATA_NEEDED
 c_sources += audio_extn/hw_loopback.c
 endif
 
@@ -208,6 +209,10 @@
 AM_CFLAGS += -DINSTANCE_ID_ENABLED
 endif
 
+if LL_AS_PRIMARY_OUTPUT
+AM_CFLAGS += -DUSE_LL_AS_PRIMARY_OUTPUT
+endif
+
 h_sources = audio_extn/audio_defs.h \
             audio_extn/audio_extn.h \
             audio_hw.h \
diff --git a/hal/audio_extn/a2dp.c b/hal/audio_extn/a2dp.c
index c5edd42..e986a54 100644
--- a/hal/audio_extn/a2dp.c
+++ b/hal/audio_extn/a2dp.c
@@ -50,15 +50,18 @@
 
 #ifdef SPLIT_A2DP_ENABLED
 #define AUDIO_PARAMETER_A2DP_STARTED "A2dpStarted"
-#define BT_IPC_LIB_NAME  "libbthost_if.so"
-#define ENC_MEDIA_FMT_NONE                                     0
-#define ENC_MEDIA_FMT_AAC                                  0x00010DA6
-#define ENC_MEDIA_FMT_APTX                                 0x000131ff
-#define ENC_MEDIA_FMT_APTX_HD                              0x00013200
-#define ENC_MEDIA_FMT_APTX_AD                              0x00013204
-#define ENC_MEDIA_FMT_SBC                                  0x00010BF2
-#define ENC_MEDIA_FMT_CELT                                 0x00013221
-#define ENC_MEDIA_FMT_LDAC                                 0x00013224
+#define BT_IPC_SOURCE_LIB_NAME  "libbthost_if.so"
+#define BT_IPC_SINK_LIB_NAME    "libbthost_if_sink.so"
+#define MEDIA_FMT_NONE                                     0
+#define MEDIA_FMT_AAC                                      0x00010DA6
+#define MEDIA_FMT_APTX                                     0x000131ff
+#define MEDIA_FMT_APTX_HD                                  0x00013200
+#define MEDIA_FMT_APTX_AD                                  0x00013204
+#define MEDIA_FMT_SBC                                      0x00010BF2
+#define MEDIA_FMT_CELT                                     0x00013221
+#define MEDIA_FMT_LDAC                                     0x00013224
+#define MEDIA_FMT_MP3                                      0x00010BE9
+#define MEDIA_FMT_APTX_ADAPTIVE                            0x00013204
 #define MEDIA_FMT_AAC_AOT_LC                               2
 #define MEDIA_FMT_AAC_AOT_SBR                              5
 #define MEDIA_FMT_AAC_AOT_PS                               29
@@ -71,16 +74,20 @@
 #define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO            9
 #define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS           0
 #define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR                1
-#define MIXER_ENC_CONFIG_BLOCK     "SLIM_7_RX Encoder Config"
-#define MIXER_DEC_CONFIG_BLOCK     "SLIM_7_TX Decoder Config"
+#define MIXER_ENC_CONFIG_BLOCK            "SLIM_7_RX Encoder Config"
+#define MIXER_SOURCE_DEC_CONFIG_BLOCK     "SLIM_7_TX Decoder Config"
+#define MIXER_SINK_DEC_CONFIG_BLOCK       "SLIM_9_TX Decoder Config"
 #define MIXER_ENC_BIT_FORMAT       "AFE Input Bit Format"
+#define MIXER_DEC_BIT_FORMAT       "AFE Output Bit Format"
 #define MIXER_SCRAMBLER_MODE       "AFE Scrambler Mode"
 #define MIXER_SAMPLE_RATE_RX       "BT SampleRate RX"
-#define MIXER_SAMPLE_RATE_TX       "BT SampleRate TX"
+#define MIXER_SOURCE_SAMPLE_RATE_TX       "BT SampleRate TX"
 #define MIXER_SAMPLE_RATE_DEFAULT  "BT SampleRate"
 #define MIXER_AFE_IN_CHANNELS      "AFE Input Channels"
 #define MIXER_ABR_TX_FEEDBACK_PATH "A2DP_SLIM7_UL_HL Switch"
 #define MIXER_SET_FEEDBACK_CHANNEL "BT set feedback channel"
+#define MIXER_SINK_SAMPLE_RATE     "BT_TX SampleRate"
+#define MIXER_AFE_SINK_CHANNELS    "AFE Output Channels"
 #define MIXER_ENC_FMT_SBC          "SBC"
 #define MIXER_ENC_FMT_AAC          "AAC"
 #define MIXER_ENC_FMT_APTX         "APTX"
@@ -118,25 +125,28 @@
 #define SAMPLING_RATE_441K              44100
 #define CH_STEREO                       2
 #define CH_MONO                         1
+#define SOURCE 0
+#define SINK   1
+
 /*
  * Below enum values are extended from audio_base.h to
- * to keep encoder codec type local to bthost_ipc
+ * to keep encoder and decoder type local to bthost_ipc
  * and audio_hal as these are intended only for handshake
  * between IPC lib and Audio HAL.
  */
 typedef enum {
-    ENC_CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
-    ENC_CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
-    ENC_CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
-    ENC_CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
-    ENC_CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
+    CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
+    CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
+    CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
+    CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
+    CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
 #ifndef LINUX_ENABLED
-    ENC_CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
+    CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
 #endif
-    ENC_CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
-    ENC_CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
-    ENC_CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
-}enc_codec_t;
+    CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
+    CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
+    CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
+}codec_t;
 
 /*
  * enums which describes the APTX Adaptive
@@ -162,19 +172,24 @@
     APTX_AD_44_1 = 0x2, // 44.1kHz
 } enc_aptx_ad_s_rate;
 
-typedef int (*audio_stream_open_t)(void);
-typedef int (*audio_stream_close_t)(void);
-typedef int (*audio_start_stream_t)(void);
-typedef int (*audio_stop_stream_t)(void);
-typedef int (*audio_suspend_stream_t)(void);
-typedef void (*audio_handoff_triggered_t)(void);
-typedef void (*clear_a2dpsuspend_flag_t)(void);
-typedef void * (*audio_get_codec_config_t)(uint8_t *multicast_status,uint8_t *num_dev,
-                               enc_codec_t *codec_type);
-typedef int (*audio_check_a2dp_ready_t)(void);
-typedef uint16_t (*audio_get_a2dp_sink_latency_t)(void);
-typedef int (*audio_is_scrambling_enabled_t)(void);
+typedef int (*audio_source_open_t)(void);
+typedef int (*audio_source_close_t)(void);
+typedef int (*audio_source_start_t)(void);
+typedef int (*audio_source_stop_t)(void);
+typedef int (*audio_source_suspend_t)(void);
+typedef void (*audio_source_handoff_triggered_t)(void);
+typedef void (*clear_source_a2dpsuspend_flag_t)(void);
+typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
+                                uint8_t *num_dev, codec_t *codec_type);
+typedef int (*audio_source_check_a2dp_ready_t)(void);
+typedef int (*audio_is_source_scrambling_enabled_t)(void);
 typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
+typedef int (*audio_sink_start_t)(void);
+typedef int (*audio_sink_stop_t)(void);
+typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
+typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
+typedef int (*audio_sink_check_a2dp_ready_t)(void);
+typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
 
 enum A2DP_STATE {
     A2DP_STATE_CONNECTED,
@@ -230,26 +245,25 @@
  */
 struct a2dp_data {
     struct audio_device *adev;
-    void *bt_lib_handle;
-    audio_stream_open_t audio_stream_open;
-    audio_stream_close_t audio_stream_close;
-    audio_start_stream_t audio_start_stream;
-    audio_stop_stream_t audio_stop_stream;
-    audio_suspend_stream_t audio_suspend_stream;
-    audio_handoff_triggered_t audio_handoff_triggered;
-    clear_a2dpsuspend_flag_t clear_a2dpsuspend_flag;
-    audio_get_codec_config_t audio_get_codec_config;
-    audio_check_a2dp_ready_t audio_check_a2dp_ready;
-    audio_get_a2dp_sink_latency_t audio_get_a2dp_sink_latency;
-    audio_is_scrambling_enabled_t audio_is_scrambling_enabled;
+    void *bt_lib_source_handle;
+    audio_source_open_t audio_source_open;
+    audio_source_close_t audio_source_close;
+    audio_source_start_t audio_source_start;
+    audio_source_stop_t audio_source_stop;
+    audio_source_suspend_t audio_source_suspend;
+    audio_source_handoff_triggered_t audio_source_handoff_triggered;
+    clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
+    audio_get_enc_config_t audio_get_enc_config;
+    audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
     audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
-    enum A2DP_STATE bt_state;
-    enc_codec_t bt_encoder_format;
+    audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
+    enum A2DP_STATE bt_state_source;
+    codec_t bt_encoder_format;
     uint32_t enc_sampling_rate;
     uint32_t enc_channels;
-    bool a2dp_started;
-    bool a2dp_suspended;
-    int  a2dp_total_active_session_request;
+    bool a2dp_source_started;
+    bool a2dp_source_suspended;
+    int  a2dp_source_total_active_session_requests;
     bool is_a2dp_offload_supported;
     bool is_handoff_in_progress;
     bool is_aptx_dual_mono_supported;
@@ -258,6 +272,20 @@
     bool is_aptx_adaptive;
     /* Adaptive bitrate config for A2DP codecs */
     struct a2dp_abr_config abr_config;
+
+    void *bt_lib_sink_handle;
+    audio_sink_start_t audio_sink_start;
+    audio_sink_stop_t audio_sink_stop;
+    audio_get_dec_config_t audio_get_dec_config;
+    audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
+    audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
+    audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
+    enum A2DP_STATE bt_state_sink;
+    codec_t bt_decoder_format;
+    uint32_t dec_sampling_rate;
+    uint32_t dec_channels;
+    bool a2dp_sink_started;
+    int  a2dp_sink_total_active_session_requests;
 };
 
 struct a2dp_data a2dp;
@@ -364,6 +392,37 @@
     struct aac_frame_size_control_t frame_ctl;
 } __attribute__ ((packed));
 
+typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
+struct audio_aac_decoder_config_t {
+    uint16_t      aac_fmt_flag; /* LATM*/
+    uint16_t      audio_object_type; /* LC */
+    uint16_t      channels; /* Stereo */
+    uint16_t      total_size_of_pce_bits; /* 0 - only for channel conf PCE */
+    uint32_t      sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
+                                  44.1k, 48k, 64k, 88.2k, 96k */
+} __attribute__ ((packed));
+
+typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
+struct audio_sbc_decoder_config_t {
+    uint16_t      channels; /* Mono, Stereo */
+    uint32_t      sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
+                                  44.1k, 48k, 64k, 88.2k, 96k */
+} __attribute__ ((packed));
+
+/* AAC decoder configuration structure. */
+typedef struct aac_dec_cfg_t aac_dec_cfg_t;
+struct aac_dec_cfg_t {
+    uint32_t dec_format;
+    audio_aac_decoder_config_t data;
+} __attribute__ ((packed));
+
+/* SBC decoder configuration structure. */
+typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
+struct sbc_dec_cfg_t {
+    uint32_t dec_format;
+    audio_sbc_decoder_config_t data;
+} __attribute__ ((packed));
+
 /* SBC encoder configuration structure. */
 typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
 
@@ -581,6 +640,29 @@
     uint32_t bits_per_sample;
 } audio_ldac_encoder_config;
 
+/* Information about BT AAC decoder configuration
+ * This data is used between audio HAL module and
+ * BT IPC library to configure DSP decoder
+ */
+typedef struct {
+    uint16_t      aac_fmt_flag; /* LATM*/
+    uint16_t      audio_object_type; /* LC */
+    uint16_t      channels; /* Stereo */
+    uint16_t      total_size_of_pce_bits; /* 0 - only for channel conf PCE */
+    uint32_t      sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
+                                  44.1k, 48k, 64k, 88.2k, 96k */
+} audio_aac_dec_config_t;
+
+/* Information about BT SBC decoder configuration
+ * This data is used between audio HAL module and
+ * BT IPC library to configure DSP decoder
+ */
+typedef struct {
+    uint16_t      channels; /* Mono, Stereo */
+    uint32_t      sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
+                                  44.1k, 48k, 64k, 88.2k, 96k */
+}audio_sbc_dec_config_t;
+
 /*********** END of DSP configurable structures ********************/
 
 /* API to identify DSP encoder captabilities */
@@ -750,98 +832,150 @@
     return -ENOSYS;
 }
 
-/* API to open BT IPC library to start IPC communication */
-static void open_a2dp_output()
+/* API to open BT IPC library to start IPC communication for BT Source*/
+static void open_a2dp_source()
 {
     int ret = 0;
 
-    ALOGD(" Open A2DP output start ");
-    if (a2dp.bt_lib_handle == NULL){
+    ALOGD(" Open A2DP source start ");
+    if (a2dp.bt_lib_source_handle == NULL){
         ALOGD(" Requesting for BT lib handle");
-        a2dp.bt_lib_handle = dlopen(BT_IPC_LIB_NAME, RTLD_NOW);
+        a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
 
-        if (a2dp.bt_lib_handle == NULL) {
-            ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_LIB_NAME);
+        if (a2dp.bt_lib_source_handle == NULL) {
+            ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
             ret = -ENOSYS;
             goto init_fail;
         } else {
-            a2dp.audio_stream_open = (audio_stream_open_t)
-                          dlsym(a2dp.bt_lib_handle, "audio_stream_open");
-            a2dp.audio_start_stream = (audio_start_stream_t)
-                          dlsym(a2dp.bt_lib_handle, "audio_start_stream");
-            a2dp.audio_get_codec_config = (audio_get_codec_config_t)
-                          dlsym(a2dp.bt_lib_handle, "audio_get_codec_config");
-            a2dp.audio_suspend_stream = (audio_suspend_stream_t)
-                          dlsym(a2dp.bt_lib_handle, "audio_suspend_stream");
-            a2dp.audio_handoff_triggered = (audio_handoff_triggered_t)
-                          dlsym(a2dp.bt_lib_handle, "audio_handoff_triggered");
-            a2dp.clear_a2dpsuspend_flag = (clear_a2dpsuspend_flag_t)
-                          dlsym(a2dp.bt_lib_handle, "clear_a2dpsuspend_flag");
-            a2dp.audio_stop_stream = (audio_stop_stream_t)
-                          dlsym(a2dp.bt_lib_handle, "audio_stop_stream");
-            a2dp.audio_stream_close = (audio_stream_close_t)
-                          dlsym(a2dp.bt_lib_handle, "audio_stream_close");
-            a2dp.audio_check_a2dp_ready = (audio_check_a2dp_ready_t)
-                        dlsym(a2dp.bt_lib_handle,"audio_check_a2dp_ready");
-            a2dp.audio_get_a2dp_sink_latency = (audio_get_a2dp_sink_latency_t)
-                        dlsym(a2dp.bt_lib_handle,"audio_get_a2dp_sink_latency");
-            a2dp.audio_is_scrambling_enabled = (audio_is_scrambling_enabled_t)
-                        dlsym(a2dp.bt_lib_handle,"audio_is_scrambling_enabled");
+            a2dp.audio_source_open = (audio_source_open_t)
+                          dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
+            a2dp.audio_source_start = (audio_source_start_t)
+                          dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
+            a2dp.audio_get_enc_config = (audio_get_enc_config_t)
+                          dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
+            a2dp.audio_source_suspend = (audio_source_suspend_t)
+                          dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
+            a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
+                          dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
+            a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
+                          dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
+            a2dp.audio_source_stop = (audio_source_stop_t)
+                          dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
+            a2dp.audio_source_close = (audio_source_close_t)
+                          dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
+            a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
+                        dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
+            a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
+                        dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
+            a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
+                        dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
            a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
-                        dlsym(a2dp.bt_lib_handle,"isTwsMonomodeEnable");
+                        dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
         }
     }
 
-    if (a2dp.bt_lib_handle && a2dp.audio_stream_open) {
-        if (a2dp.bt_state == A2DP_STATE_DISCONNECTED) {
+    if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
+        if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
             ALOGD("calling BT stream open");
-            ret = a2dp.audio_stream_open();
+            ret = a2dp.audio_source_open();
             if(ret != 0) {
-                ALOGE("Failed to open output stream for a2dp: status %d", ret);
+                ALOGE("Failed to open source stream for a2dp: status %d", ret);
                 goto init_fail;
             }
-            a2dp.bt_state = A2DP_STATE_CONNECTED;
+            a2dp.bt_state_source = A2DP_STATE_CONNECTED;
         } else {
-            ALOGD("Called a2dp open with improper state, Ignoring request state %d", a2dp.bt_state);
+            ALOGD("Called a2dp open with improper state, Ignoring request state %d", a2dp.bt_state_source);
         }
     } else {
         ALOGE("a2dp handle is not identified, Ignoring open request");
-        a2dp.bt_state = A2DP_STATE_DISCONNECTED;
+        a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
         goto init_fail;
     }
 
 init_fail:
-    if(ret != 0 && (a2dp.bt_lib_handle != NULL)) {
-        dlclose(a2dp.bt_lib_handle);
-        a2dp.bt_lib_handle = NULL;
+    if(ret != 0 && (a2dp.bt_lib_source_handle != NULL)) {
+        dlclose(a2dp.bt_lib_source_handle);
+        a2dp.bt_lib_source_handle = NULL;
+    }
+}
+
+/* API to open BT IPC library to start IPC communication for BT Sink*/
+static void open_a2dp_sink()
+{
+    ALOGD(" Open A2DP input start ");
+    if (a2dp.bt_lib_sink_handle == NULL){
+        ALOGD(" Requesting for BT lib handle");
+        a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
+
+        if (a2dp.bt_lib_sink_handle == NULL) {
+            ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
+        } else {
+            a2dp.audio_sink_start = (audio_sink_start_t)
+                          dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
+            a2dp.audio_get_dec_config = (audio_get_dec_config_t)
+                          dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
+            a2dp.audio_sink_stop = (audio_sink_stop_t)
+                          dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
+            a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
+                        dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
+            a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
+                          dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
+        }
     }
 }
 
 static int close_a2dp_output()
 {
     ALOGV("%s\n",__func__);
-    if (!(a2dp.bt_lib_handle && a2dp.audio_stream_close)) {
-        ALOGE("a2dp handle is not identified, Ignoring close request");
+
+    if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
+        ALOGE("a2dp source handle is not identified, Ignoring close request");
         return -ENOSYS;
     }
-    if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
-        ALOGD("calling BT stream close");
-        if(a2dp.audio_stream_close() == false)
-            ALOGE("failed close a2dp control path from BT library");
+
+    if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
+        ALOGD("calling BT source stream close");
+        if(a2dp.audio_source_close() == false)
+            ALOGE("failed close a2dp source control path from BT library");
     }
-    a2dp.a2dp_started = false;
-    a2dp.a2dp_total_active_session_request = 0;
-    a2dp.a2dp_suspended = false;
-    a2dp.bt_encoder_format = ENC_CODEC_TYPE_INVALID;
+    a2dp.a2dp_source_started = false;
+    a2dp.a2dp_source_total_active_session_requests = 0;
+    a2dp.a2dp_source_suspended = false;
+    a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
     a2dp.enc_sampling_rate = 48000;
     a2dp.enc_channels = 2;
-    a2dp.bt_state = A2DP_STATE_DISCONNECTED;
+    a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
     if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
         stop_abr();
     a2dp.abr_config.is_abr_enabled = false;
     a2dp.abr_config.abr_started = false;
     a2dp.abr_config.imc_instance = 0;
     a2dp.abr_config.abr_tx_handle = NULL;
+    a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
+
+    return 0;
+}
+
+static int close_a2dp_input()
+{
+    ALOGV("%s\n",__func__);
+
+    if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
+        ALOGE("a2dp sink handle is not identified, Ignoring close request");
+        return -ENOSYS;
+    }
+
+    if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
+        ALOGD("calling BT sink stream close");
+        if(a2dp.audio_source_close() == false)
+            ALOGE("failed close a2dp sink control path from BT library");
+    }
+    a2dp.a2dp_sink_started = false;
+    a2dp.a2dp_sink_total_active_session_requests = 0;
+    a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
+    a2dp.dec_sampling_rate = 48000;
+    a2dp.dec_channels = 2;
+    a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
 
     return 0;
 }
@@ -850,15 +984,15 @@
 {
     bool scrambler_mode = false;
     struct mixer_ctl *ctrl_scrambler_mode = NULL;
-    if (a2dp.audio_is_scrambling_enabled && (a2dp.bt_state != A2DP_STATE_DISCONNECTED))
-        scrambler_mode = a2dp.audio_is_scrambling_enabled();
+    if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
+        scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
 
     if (scrambler_mode) {
         //enable scrambler in dsp
         ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
                                             MIXER_SCRAMBLER_MODE);
         if (!ctrl_scrambler_mode) {
-            ALOGE(" ERROR scrambler mode mixer control not identifed");
+            ALOGE(" ERROR scrambler mode mixer control not identified");
             return;
         } else {
             if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
@@ -869,21 +1003,29 @@
     }
 }
 
-static int a2dp_set_backend_cfg()
+static bool a2dp_set_backend_cfg(uint8_t direction)
 {
-    char *rate_str = NULL, *in_channels = NULL;
-    uint32_t sampling_rate_rx = a2dp.enc_sampling_rate;
-    struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL;
+    char *rate_str = NULL, *channels = NULL;
+    uint32_t sampling_rate;
+    struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
+    bool is_configured = false;
 
-    //For LDAC encoder open slimbus port at 96Khz for 48Khz input
-    //and 88.2Khz for 44.1Khz input.
-    if ((a2dp.bt_encoder_format == ENC_CODEC_TYPE_LDAC) &&
-        (sampling_rate_rx == 48000 || sampling_rate_rx == 44100 )) {
-        sampling_rate_rx *= 2;
+    if (direction == SINK) {
+        sampling_rate = a2dp.dec_sampling_rate;
+    } else {
+        sampling_rate = a2dp.enc_sampling_rate;
+    }
+    //For LDAC encoder and AAC decoder open slimbus port at
+    //96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
+    if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
+         (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
+         (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC)) &&
+        (sampling_rate == 48000 || sampling_rate == 44100 )) {
+        sampling_rate = sampling_rate *2;
     }
 
-    // Set Rx backend sample rate
-    switch (sampling_rate_rx) {
+    //Configure backend sampling rate
+    switch (sampling_rate) {
     case 44100:
         rate_str = "KHZ_44P1";
         break;
@@ -899,31 +1041,42 @@
         break;
     }
 
-    ALOGD("%s: set backend rx sample rate = %s", __func__, rate_str);
-    ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
-                                        MIXER_SAMPLE_RATE_RX);
+    if (direction == SINK) {
+        ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
+        ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
+                                            MIXER_SINK_SAMPLE_RATE);
+    } else {
+        ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
+        ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
+                                            MIXER_SAMPLE_RATE_RX);
+    }
     if (ctl_sample_rate) {
 
         if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
             ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
-            return -ENOSYS;
+            is_configured = false;
+            goto fail;
         }
 
-        /* Set Tx backend sample rate */
-        if (a2dp.abr_config.is_abr_enabled)
-        rate_str = ABR_TX_SAMPLE_RATE;
+        if (direction == SOURCE) {
+            /* Set Tx backend sample rate */
+            if (a2dp.abr_config.is_abr_enabled)
+            rate_str = ABR_TX_SAMPLE_RATE;
 
-        ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
-        ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
-                                            MIXER_SAMPLE_RATE_TX);
-        if (!ctl_sample_rate) {
+            ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
+            ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
+                                            MIXER_SOURCE_SAMPLE_RATE_TX);
+            if (!ctl_sample_rate) {
                 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
-                return -ENOSYS;
-        }
+                is_configured = false;
+                goto fail;
+            }
 
-        if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
-            ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
-            return -ENOSYS;
+            if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
+                ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
+                is_configured = false;
+                goto fail;
+            }
         }
     } else {
         /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
@@ -932,39 +1085,127 @@
                                         MIXER_SAMPLE_RATE_DEFAULT);
         if (!ctl_sample_rate) {
             ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
-            return -ENOSYS;
+            is_configured = false;
+            goto fail;
         }
 
         if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
             ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
-            return -ENOSYS;
+            is_configured = false;
+            goto fail;
         }
     }
 
-    //Configure AFE input channels
-    switch (a2dp.enc_channels) {
-    case 1:
-        in_channels = "One";
+    if (direction == SINK) {
+        switch (a2dp.dec_channels) {
+        case 1:
+            channels = "One";
+            break;
+        case 2:
+        default:
+            channels = "Two";
+            break;
+        }
+
+        ALOGD("%s: set afe dec channels =%d", __func__, channels);
+        ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
+                                            MIXER_AFE_SINK_CHANNELS);
+    } else {
+        //Configure AFE enc channels
+        switch (a2dp.enc_channels) {
+        case 1:
+            channels = "One";
+            break;
+        case 2:
+        default:
+            channels = "Two";
+            break;
+        }
+
+        ALOGD("%s: set afe enc channels =%d", __func__, channels);
+        ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
+                                            MIXER_AFE_IN_CHANNELS);
+    }
+
+    if (!ctrl_channels) {
+        ALOGE(" ERROR AFE channels mixer control not identified");
+    } else {
+        if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
+            ALOGE("%s: Failed to set AFE channels =%d", __func__, channels);
+            is_configured = false;
+            goto fail;
+        }
+    }
+    is_configured = true;
+fail:
+    return is_configured;
+}
+
+bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
+{
+    struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
+    struct aac_dec_cfg_t aac_dsp_cfg;
+    bool is_configured = false;
+    int ret = 0;
+
+    if(aac_bt_cfg == NULL)
+        return false;
+
+    ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
+    if (!ctl_dec_data) {
+        ALOGE(" ERROR  a2dp decoder CONFIG data mixer control not identified");
+        is_configured = false;
+        goto fail;
+    }
+
+    memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
+    aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
+    aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
+    aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
+    switch(aac_bt_cfg->audio_object_type) {
+    case 0:
+        aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
         break;
     case 2:
+        aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
+        break;
+    case 1:
     default:
-        in_channels = "Two";
+        aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
         break;
     }
-
-    ALOGD("%s: set AFE input channels = %d", __func__, a2dp.enc_channels);
-    ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
-                                        MIXER_AFE_IN_CHANNELS);
-    if (!ctrl_in_channels) {
-        ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
-        return -ENOSYS;
-    }
-    if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
-        ALOGE("%s: Failed to set AFE in channels = %d", __func__, a2dp.enc_channels);
-        return -ENOSYS;
+    aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
+    aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
+    ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
+                              sizeof(struct aac_dec_cfg_t));
+    if (ret != 0) {
+        ALOGE("%s: failed to set AAC decoder config", __func__);
+        is_configured = false;
+        goto fail;
     }
 
-    return 0;
+    ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
+                                            MIXER_DEC_BIT_FORMAT);
+    if (!ctrl_bit_format) {
+        ALOGE(" ERROR Dec bit format mixer control not identified");
+        is_configured = false;
+        goto fail;
+    }
+    ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
+    if (ret != 0) {
+        ALOGE("%s: Failed to set bit format to decoder", __func__);
+        is_configured = false;
+        goto fail;
+    }
+
+    is_configured = true;
+    a2dp.bt_decoder_format = CODEC_TYPE_AAC;
+    a2dp.dec_channels = aac_dsp_cfg.data.channels;
+    a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
+    ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
+           aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
+fail:
+    return is_configured;
 }
 
 static int a2dp_set_bit_format(uint32_t enc_bit_format)
@@ -1000,25 +1241,31 @@
     return 0;
 }
 
-static int a2dp_reset_backend_cfg()
+static int a2dp_reset_backend_cfg(uint8_t direction)
 {
-    const char *rate_str = "KHZ_8", *in_channels = "Zero";
-    struct mixer_ctl *ctl_sample_rate_rx = NULL, *ctl_sample_rate_tx = NULL;
-    struct mixer_ctl *ctrl_in_channels = NULL;
+    const char *rate_str = "KHZ_8", *channels = "Zero";
+    struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
+    struct mixer_ctl *ctrl_channels = NULL;
 
     // Reset backend sampling rate
-    ALOGD("%s: reset backend sample rate = %s", __func__, rate_str);
-    ctl_sample_rate_rx = mixer_get_ctl_by_name(a2dp.adev->mixer,
-                                        MIXER_SAMPLE_RATE_RX);
-    if (ctl_sample_rate_rx) {
+    if (direction == SINK) {
+        ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
+        ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
+                                              MIXER_SINK_SAMPLE_RATE);
+    } else {
+        ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
+        ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
+                                              MIXER_SAMPLE_RATE_RX);
+    }
+    if (ctl_sample_rate) {
 
-        if (mixer_ctl_set_enum_by_string(ctl_sample_rate_rx, rate_str) != 0) {
-            ALOGE("%s: Failed to reset Rx backend sample rate = %s", __func__, rate_str);
+        if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
+            ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
             return -ENOSYS;
         }
 
         ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
-                                        MIXER_SAMPLE_RATE_TX);
+                                        MIXER_SOURCE_SAMPLE_RATE_TX);
         if (!ctl_sample_rate_tx) {
                 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
                 return -ENOSYS;
@@ -1030,28 +1277,34 @@
         }
     } else {
 
-        ctl_sample_rate_rx = mixer_get_ctl_by_name(a2dp.adev->mixer,
+        ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
                                         MIXER_SAMPLE_RATE_DEFAULT);
-        if (!ctl_sample_rate_rx) {
+        if (!ctl_sample_rate) {
             ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
             return -ENOSYS;
         }
 
-        if (mixer_ctl_set_enum_by_string(ctl_sample_rate_rx, rate_str) != 0) {
+        if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
             ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
             return -ENOSYS;
         }
     }
 
     // Reset AFE input channels
-    ALOGD("%s: reset AFE input channels = %s", __func__, in_channels);
-    ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
-                                        MIXER_AFE_IN_CHANNELS);
-    if (!ctrl_in_channels) {
+    if (direction == SINK) {
+        ALOGD("%s: reset afe sink channels =%s", __func__, channels);
+        ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
+                                            MIXER_AFE_SINK_CHANNELS);
+    } else {
+        ALOGD("%s: reset afe source channels =%s", __func__, channels);
+        ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
+                                            MIXER_AFE_IN_CHANNELS);
+    }
+    if (!ctrl_channels) {
         ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
         return -ENOSYS;
     }
-    if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
+    if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
         ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
         return -ENOSYS;
     }
@@ -1060,14 +1313,14 @@
 }
 
 /* API to configure AFE decoder in DSP */
-static bool configure_a2dp_decoder_format(int dec_format)
+static bool configure_a2dp_source_decoder_format(int dec_format)
 {
     struct mixer_ctl *ctl_dec_data = NULL;
     struct abr_dec_cfg_t dec_cfg;
     int ret = 0;
 
     if (a2dp.abr_config.is_abr_enabled) {
-        ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_DEC_CONFIG_BLOCK);
+        ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
         if (!ctl_dec_data) {
             ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
             return false;
@@ -1085,9 +1338,104 @@
             ALOGE("%s: Failed to set decoder config", __func__);
             return false;
         }
+     }
+
+     return true;
+}
+
+bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
+{
+    struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
+    struct sbc_dec_cfg_t sbc_dsp_cfg;
+    bool is_configured = false;
+    int ret = 0;
+
+    if(sbc_bt_cfg == NULL)
+        goto fail;
+
+    ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
+    if (!ctl_dec_data) {
+        ALOGE(" ERROR  a2dp decoder CONFIG data mixer control not identified");
+        is_configured = false;
+        goto fail;
     }
 
-    return true;
+    memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
+    sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
+    sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
+    sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
+    ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
+                              sizeof(struct sbc_dec_cfg_t));
+
+    if (ret != 0) {
+        ALOGE("%s: failed to set SBC decoder config", __func__);
+        is_configured = false;
+        goto fail;
+    }
+
+    ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
+                                            MIXER_DEC_BIT_FORMAT);
+    if (!ctrl_bit_format) {
+        ALOGE(" ERROR Dec bit format mixer control not identified");
+        is_configured = false;
+        goto fail;
+    }
+    ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
+    if (ret != 0) {
+        ALOGE("%s: Failed to set bit format to decoder", __func__);
+        is_configured = false;
+        goto fail;
+    }
+
+    is_configured = true;
+    a2dp.bt_decoder_format = CODEC_TYPE_SBC;
+    if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
+        a2dp.dec_channels = 1;
+    else
+        a2dp.dec_channels = 2;
+    a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
+    ALOGV("Successfully updated SBC dec format");
+fail:
+    return is_configured;
+}
+
+/* API to configure AFE decoder in DSP */
+static bool configure_a2dp_sink_decoder_format()
+{
+    void *codec_info = NULL;
+    codec_t codec_type = CODEC_TYPE_INVALID;
+    bool is_configured = false;
+    struct mixer_ctl *ctl_dec_data = NULL;
+    int ret = 0;
+
+    if (!a2dp.audio_get_dec_config) {
+        ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
+        return false;
+    }
+
+    ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
+    if (!ctl_dec_data) {
+        ALOGE(" ERROR  a2dp decoder CONFIG data mixer control not identified");
+        is_configured = false;
+        return false;
+    }
+    codec_info = a2dp.audio_get_dec_config(&codec_type);
+    switch(codec_type) {
+        case CODEC_TYPE_SBC:
+            ALOGD(" SBC decoder supported BT device");
+            is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
+            break;
+        case CODEC_TYPE_AAC:
+            ALOGD(" AAC decoder supported BT device");
+            is_configured =
+              configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
+            break;
+        default:
+            ALOGD(" Received Unsupported decoder format");
+            is_configured = false;
+            break;
+    }
+    return is_configured;
 }
 
 /* API to configure SBC DSP encoder */
@@ -1103,12 +1451,12 @@
 
    ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
     if (!ctl_enc_data) {
-        ALOGE(" ERROR  a2dp encoder CONFIG data mixer control not identifed");
+        ALOGE(" ERROR  a2dp encoder CONFIG data mixer control not identified");
         is_configured = false;
         goto fail;
     }
     memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
-    sbc_dsp_cfg.enc_format = ENC_MEDIA_FMT_SBC;
+    sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
     sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
     sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
     switch(sbc_bt_cfg->channels) {
@@ -1145,7 +1493,7 @@
         goto fail;
     }
     is_configured = true;
-    a2dp.bt_encoder_format = ENC_CODEC_TYPE_SBC;
+    a2dp.bt_encoder_format = CODEC_TYPE_SBC;
     a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
 
     if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
@@ -1259,7 +1607,7 @@
     }
 
     memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
-    aptx_dsp_cfg->custom_cfg.enc_format = ENC_MEDIA_FMT_APTX;
+    aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
 
     if (!a2dp.is_aptx_dual_mono_supported) {
         aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
@@ -1316,7 +1664,7 @@
     }
 
     memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
-    aptx_dsp_cfg->enc_format = ENC_MEDIA_FMT_APTX;
+    aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
     aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
     aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
     switch(aptx_dsp_cfg->num_channels) {
@@ -1410,9 +1758,9 @@
     }
     is_configured = true;
     if (a2dp.is_aptx_adaptive)
-        a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX_AD;
+        a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
     else
-        a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX;
+        a2dp.bt_encoder_format = CODEC_TYPE_APTX;
 fail:
     /*restore sample rate */
     if(!is_configured)
@@ -1438,13 +1786,13 @@
 
     ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
     if (!ctl_enc_data) {
-        ALOGE(" ERROR  a2dp encoder CONFIG data mixer control not identifed");
+        ALOGE(" ERROR  a2dp encoder CONFIG data mixer control not identified");
         is_configured = false;
         goto fail;
     }
 
     memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
-    aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX_HD;
+    aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
     aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
     aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
     switch(aptx_dsp_cfg.num_channels) {
@@ -1470,7 +1818,7 @@
         goto fail;
     }
     is_configured = true;
-    a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX_HD;
+    a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
     a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
     a2dp.enc_channels = aptx_bt_cfg->channels;
     ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
@@ -1492,12 +1840,12 @@
 
     ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
     if (!ctl_enc_data) {
-        ALOGE(" ERROR  a2dp encoder CONFIG data mixer control not identifed");
+        ALOGE(" ERROR  a2dp encoder CONFIG data mixer control not identified");
         is_configured = false;
         goto fail;
     }
     memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
-    aac_dsp_cfg.enc_format = ENC_MEDIA_FMT_AAC;
+    aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
     aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
     aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
     switch (aac_bt_cfg->enc_mode) {
@@ -1528,7 +1876,7 @@
         goto fail;
     }
     is_configured = true;
-    a2dp.bt_encoder_format = ENC_CODEC_TYPE_AAC;
+    a2dp.bt_encoder_format = CODEC_TYPE_AAC;
     a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
     a2dp.enc_channels = aac_bt_cfg->channels;
     ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
@@ -1607,13 +1955,13 @@
 
     ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
     if (!ctl_enc_data) {
-        ALOGE(" ERROR  a2dp encoder CONFIG data mixer control not identifed");
+        ALOGE(" ERROR  a2dp encoder CONFIG data mixer control not identified");
         is_configured = false;
         goto fail;
     }
     memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
 
-    celt_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_CELT;
+    celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
     celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
     celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
     switch(celt_dsp_cfg.custom_cfg.num_channels) {
@@ -1648,7 +1996,7 @@
         goto fail;
     }
     is_configured = true;
-    a2dp.bt_encoder_format = ENC_CODEC_TYPE_CELT;
+    a2dp.bt_encoder_format = CODEC_TYPE_CELT;
     a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
     a2dp.enc_channels = celt_bt_cfg->channels;
     ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
@@ -1668,13 +2016,13 @@
 
     ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
     if (!ldac_enc_data) {
-        ALOGE(" ERROR  a2dp encoder CONFIG data mixer control not identifed");
+        ALOGE(" ERROR  a2dp encoder CONFIG data mixer control not identified");
         is_configured = false;
         goto fail;
     }
     memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
 
-    ldac_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_LDAC;
+    ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
     ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
     ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
     switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
@@ -1716,7 +2064,7 @@
         goto fail;
     }
     is_configured = true;
-    a2dp.bt_encoder_format = ENC_CODEC_TYPE_LDAC;
+    a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
     a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
     a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
     a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
@@ -1730,16 +2078,16 @@
 {
     void *codec_info = NULL;
     uint8_t multi_cast = 0, num_dev = 1;
-    enc_codec_t codec_type = ENC_CODEC_TYPE_INVALID;
+    codec_t codec_type = CODEC_TYPE_INVALID;
     bool is_configured = false;
     audio_aptx_encoder_config aptx_encoder_cfg;
 
-    if (!a2dp.audio_get_codec_config) {
+    if (!a2dp.audio_get_enc_config) {
         ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
         return false;
     }
     ALOGD("configure_a2dp_encoder_format start");
-    codec_info = a2dp.audio_get_codec_config(&multi_cast, &num_dev,
+    codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
                                &codec_type);
 
     // ABR disabled by default for all codecs
@@ -1747,12 +2095,12 @@
     a2dp.is_aptx_adaptive = false;
 
     switch(codec_type) {
-        case ENC_CODEC_TYPE_SBC:
+        case CODEC_TYPE_SBC:
             ALOGD(" Received SBC encoder supported BT device");
             is_configured =
               configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
             break;
-        case ENC_CODEC_TYPE_APTX:
+        case CODEC_TYPE_APTX:
             ALOGD(" Received APTX encoder supported BT device");
 #ifndef LINUX_ENABLED
             a2dp.is_aptx_dual_mono_supported = false;
@@ -1761,7 +2109,7 @@
             is_configured =
               configure_aptx_enc_format(&aptx_encoder_cfg);
             break;
-        case ENC_CODEC_TYPE_APTX_HD:
+        case CODEC_TYPE_APTX_HD:
             ALOGD(" Received APTX HD encoder supported BT device");
 #ifndef LINUX_ENABLED
             is_configured =
@@ -1772,7 +2120,7 @@
 #endif
             break;
 #ifndef LINUX_ENABLED
-        case ENC_CODEC_TYPE_APTX_DUAL_MONO:
+        case CODEC_TYPE_APTX_DUAL_MONO:
             ALOGD(" Received APTX dual mono encoder supported BT device");
             a2dp.is_aptx_dual_mono_supported = true;
             if (a2dp.audio_is_tws_mono_mode_enable != NULL)
@@ -1782,7 +2130,7 @@
               configure_aptx_enc_format(&aptx_encoder_cfg);
             break;
 #endif
-        case ENC_CODEC_TYPE_AAC:
+        case CODEC_TYPE_AAC:
             ALOGD(" Received AAC encoder supported BT device");
             bool is_aac_frame_ctl_enabled =
                     property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
@@ -1790,22 +2138,22 @@
                   configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
                   configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
             break;
-        case ENC_CODEC_TYPE_CELT:
+        case CODEC_TYPE_CELT:
             ALOGD(" Received CELT encoder supported BT device");
             is_configured =
               configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
             break;
-        case ENC_CODEC_TYPE_LDAC:
+        case CODEC_TYPE_LDAC:
             ALOGD(" Received LDAC encoder supported BT device");
             if (!instance_id || instance_id > MAX_INSTANCE_ID)
                 instance_id = MAX_INSTANCE_ID;
             a2dp.abr_config.imc_instance = instance_id--;
             is_configured =
                 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
-                 configure_a2dp_decoder_format(ENC_CODEC_TYPE_LDAC));
+                 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
             break;
 #ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
-         case ENC_CODEC_TYPE_APTX_AD:
+         case CODEC_TYPE_APTX_AD:
              ALOGD(" Received APTX AD encoder supported BT device");
              if (!instance_id || instance_id > MAX_INSTANCE_ID)
                  instance_id = MAX_INSTANCE_ID;
@@ -1815,7 +2163,7 @@
               aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
               is_configured =
                 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
-                 configure_a2dp_decoder_format(ENC_MEDIA_FMT_APTX_AD));
+                 configure_a2dp_source_decoder_format(ENC_MEDIA_FMT_APTX_AD));
             break;
 #endif
         default:
@@ -1832,49 +2180,135 @@
 
     ALOGD("audio_extn_a2dp_start_playback start");
 
-    if(!(a2dp.bt_lib_handle && a2dp.audio_start_stream
-       && a2dp.audio_get_codec_config)) {
-        ALOGE("a2dp handle is not identified, Ignoring start request");
+    if(!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
+       && a2dp.audio_get_enc_config)) {
+        ALOGE("a2dp handle is not identified, Ignoring start playback request");
         return -ENOSYS;
     }
 
-    if(a2dp.a2dp_suspended == true) {
+    if(a2dp.a2dp_source_suspended == true) {
         //session will be restarted after suspend completion
         ALOGD("a2dp start requested during suspend state");
         return -ENOSYS;
     }
 
-    if (!a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
+    if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
         ALOGD("calling BT module stream start");
         /* This call indicates BT IPC lib to start playback */
-        ret =  a2dp.audio_start_stream();
+        ret =  a2dp.audio_source_start();
         ALOGE("BT controller start return = %d",ret);
         if (ret != 0 ) {
            ALOGE("BT controller start failed");
-           a2dp.a2dp_started = false;
+           a2dp.a2dp_source_started = false;
         } else {
            if(configure_a2dp_encoder_format() == true) {
-                a2dp.a2dp_started = true;
+                a2dp.a2dp_source_started = true;
                 ret = 0;
                 ALOGD("Start playback successful to BT library");
            } else {
                 ALOGD(" unable to configure DSP encoder");
-                a2dp.a2dp_started = false;
+                a2dp.a2dp_source_started = false;
                 ret = -ETIMEDOUT;
            }
         }
     }
 
-    if (a2dp.a2dp_started) {
-        a2dp.a2dp_total_active_session_request++;
+    if (a2dp.a2dp_source_started) {
+        a2dp.a2dp_source_total_active_session_requests++;
         a2dp_check_and_set_scrambler();
-        a2dp_set_backend_cfg();
+        a2dp_set_backend_cfg(SOURCE);
         if (a2dp.abr_config.is_abr_enabled)
             start_abr();
     }
 
     ALOGD("start A2DP playback total active sessions :%d",
-          a2dp.a2dp_total_active_session_request);
+          a2dp.a2dp_source_total_active_session_requests);
+    return ret;
+}
+
+uint64_t audio_extn_a2dp_get_decoder_latency()
+{
+    uint32_t latency = 0;
+
+    switch(a2dp.bt_decoder_format) {
+        case CODEC_TYPE_SBC:
+            latency = DEFAULT_SINK_LATENCY_SBC;
+            break;
+        case CODEC_TYPE_AAC:
+            latency = DEFAULT_SINK_LATENCY_AAC;
+            break;
+        default:
+            latency = 200;
+            ALOGD("No valid decoder defined, setting latency to %dms", latency);
+            break;
+    }
+    return (uint64_t)latency;
+}
+
+bool a2dp_send_sink_setup_complete(void) {
+    uint64_t system_latency = 0;
+    bool is_complete = false;
+
+    system_latency = audio_extn_a2dp_get_decoder_latency();
+
+    if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
+        is_complete = true;
+    }
+    return is_complete;
+}
+
+int audio_extn_a2dp_start_capture()
+{
+    int ret = 0;
+
+    ALOGD("audio_extn_a2dp_start_capture start");
+
+    if(!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
+       && a2dp.audio_get_dec_config)) {
+        ALOGE("a2dp handle is not identified, Ignoring start capture request");
+        return -ENOSYS;
+    }
+
+    if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
+        ALOGD("calling BT module stream start");
+        /* This call indicates BT IPC lib to start capture */
+        ret =  a2dp.audio_sink_start();
+        ALOGE("BT controller start capture return = %d",ret);
+        if (ret != 0 ) {
+           ALOGE("BT controller start capture failed");
+           a2dp.a2dp_sink_started = false;
+        } else {
+
+           if(!audio_extn_a2dp_sink_is_ready()) {
+                ALOGD("Wait for capture ready not successful");
+                ret = -ETIMEDOUT;
+           }
+
+           if(configure_a2dp_sink_decoder_format() == true) {
+                a2dp.a2dp_sink_started = true;
+                ret = 0;
+                ALOGD("Start capture successful to BT library");
+           } else {
+                ALOGD(" unable to configure DSP decoder");
+                a2dp.a2dp_sink_started = false;
+                ret = -ETIMEDOUT;
+           }
+
+           if (!a2dp_send_sink_setup_complete()) {
+               ALOGD("sink_setup_complete not successful");
+               ret = -ETIMEDOUT;
+           }
+        }
+    }
+
+    if (a2dp.a2dp_sink_started) {
+        if (a2dp_set_backend_cfg(SINK) == true) {
+        	a2dp.a2dp_sink_total_active_session_requests++;
+        }
+    }
+
+    ALOGD("start A2DP sink total active sessions :%d",
+          a2dp.a2dp_sink_total_active_session_requests);
     return ret;
 }
 
@@ -1890,16 +2324,16 @@
     ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
                                            MIXER_ENC_CONFIG_BLOCK);
     if (!ctl_enc_config) {
-        ALOGE(" ERROR  a2dp encoder format mixer control not identifed");
+        ALOGE(" ERROR  a2dp encoder format mixer control not identified");
     } else {
         ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
                                         sizeof(struct sbc_enc_cfg_t));
-         a2dp.bt_encoder_format = ENC_MEDIA_FMT_NONE;
+         a2dp.bt_encoder_format = MEDIA_FMT_NONE;
     }
     ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
                                             MIXER_ENC_BIT_FORMAT);
     if (!ctrl_bit_format) {
-        ALOGE(" ERROR  bit format CONFIG data mixer control not identifed");
+        ALOGE(" ERROR  bit format CONFIG data mixer control not identified");
     } else {
         ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
         if (ret != 0) {
@@ -1919,14 +2353,14 @@
     }
 }
 
-static int reset_a2dp_dec_config_params()
+static int reset_a2dp_source_dec_config_params()
 {
     struct mixer_ctl *ctl_dec_data = NULL;
     struct abr_dec_cfg_t dummy_reset_cfg;
     int ret = 0;
 
     if (a2dp.abr_config.is_abr_enabled) {
-        ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_DEC_CONFIG_BLOCK);
+        ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
         if (!ctl_dec_data) {
             ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
             return -EINVAL;
@@ -1943,38 +2377,98 @@
     return ret;
 }
 
+static void reset_a2dp_sink_dec_config_params()
+{
+    int ret =0;
+
+    struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
+    struct aac_dec_cfg_t dummy_reset_config;
+
+    memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
+    ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
+                                           MIXER_SINK_DEC_CONFIG_BLOCK);
+    if (!ctl_dec_config) {
+        ALOGE(" ERROR  a2dp decoder format mixer control not identified");
+    } else {
+        ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
+                                        sizeof(struct aac_dec_cfg_t));
+         a2dp.bt_decoder_format = MEDIA_FMT_NONE;
+    }
+    ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
+                                            MIXER_DEC_BIT_FORMAT);
+    if (!ctrl_bit_format) {
+        ALOGE(" ERROR  bit format CONFIG data mixer control not identified");
+    } else {
+        ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
+        if (ret != 0) {
+            ALOGE("%s: Failed to set bit format to decoder", __func__);
+        }
+    }
+}
+
 int audio_extn_a2dp_stop_playback()
 {
     int ret =0;
 
     ALOGV("audio_extn_a2dp_stop_playback start");
-    if(!(a2dp.bt_lib_handle && a2dp.audio_stop_stream)) {
-        ALOGE("a2dp handle is not identified, Ignoring start request");
+    if(!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
+        ALOGE("a2dp handle is not identified, Ignoring stop request");
         return -ENOSYS;
     }
 
-    if (a2dp.a2dp_total_active_session_request > 0)
-        a2dp.a2dp_total_active_session_request--;
+    if (a2dp.a2dp_source_total_active_session_requests > 0)
+        a2dp.a2dp_source_total_active_session_requests--;
 
-    if ( a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
+    if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
         ALOGV("calling BT module stream stop");
-        ret = a2dp.audio_stop_stream();
+        ret = a2dp.audio_source_stop();
         if (ret < 0)
             ALOGE("stop stream to BT IPC lib failed");
         else
             ALOGV("stop steam to BT IPC lib successful");
         reset_a2dp_enc_config_params();
-        reset_a2dp_dec_config_params();
-        a2dp_reset_backend_cfg();
+        reset_a2dp_source_dec_config_params();
+        a2dp_reset_backend_cfg(SOURCE);
         if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
             stop_abr();
         a2dp.abr_config.is_abr_enabled = false;
-        a2dp.a2dp_started = false;
+        a2dp.a2dp_source_started = false;
+        a2dp_reset_backend_cfg(SOURCE);
     }
-    if(!a2dp.a2dp_total_active_session_request)
-       a2dp.a2dp_started = false;
-    ALOGD("Stop A2DP playback total active sessions :%d",
-          a2dp.a2dp_total_active_session_request);
+    if(!a2dp.a2dp_source_total_active_session_requests)
+       a2dp.a2dp_source_started = false;
+    ALOGD("Stop A2DP playback, total active sessions :%d",
+          a2dp.a2dp_source_total_active_session_requests);
+    return 0;
+}
+
+int audio_extn_a2dp_stop_capture()
+{
+    int ret =0;
+
+    ALOGV("audio_extn_a2dp_stop_capture start");
+    if(!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
+        ALOGE("a2dp handle is not identified, Ignoring stop request");
+        return -ENOSYS;
+    }
+
+    if (a2dp.a2dp_sink_total_active_session_requests > 0)
+        a2dp.a2dp_sink_total_active_session_requests--;
+
+    if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
+        ALOGV("calling BT module stream stop");
+        ret = a2dp.audio_sink_stop();
+        if (ret < 0)
+            ALOGE("stop stream to BT IPC lib failed");
+        else
+            ALOGV("stop steam to BT IPC lib successful");
+        reset_a2dp_sink_dec_config_params();
+        a2dp_reset_backend_cfg(SINK);
+    }
+    if(!a2dp.a2dp_sink_total_active_session_requests)
+       a2dp.a2dp_source_started = false;
+    ALOGD("Stop A2DP capture, total active sessions :%d",
+          a2dp.a2dp_sink_total_active_session_requests);
     return 0;
 }
 
@@ -1986,7 +2480,7 @@
      struct listnode *node;
 
      if(a2dp.is_a2dp_offload_supported == false) {
-        ALOGV("no supported encoders identified,ignoring a2dp setparam");
+        ALOGV("no supported codecs identified,ignoring a2dp setparam");
         return;
      }
 
@@ -1995,8 +2489,8 @@
      if (ret >= 0) {
          val = atoi(value);
          if (audio_is_a2dp_out_device(val)) {
-             ALOGV("Received device connect request for A2DP");
-             open_a2dp_output();
+             ALOGV("Received device connect request for A2DP source");
+             open_a2dp_source();
          }
          goto param_handled;
      }
@@ -2007,15 +2501,20 @@
      if (ret >= 0) {
          val = atoi(value);
          if (audio_is_a2dp_out_device(val)) {
-             ALOGV("Received device dis- connect request");
-             reset_a2dp_enc_config_params();
-             reset_a2dp_dec_config_params();
+             ALOGV("Received source device dis- connect request");
              close_a2dp_output();
-             a2dp_reset_backend_cfg();
+             reset_a2dp_enc_config_params();
+             reset_a2dp_source_dec_config_params();
+             a2dp_reset_backend_cfg(SOURCE);
+         } else if (audio_is_a2dp_in_device(val)) {
+             ALOGV("Received sink device dis- connect request");
+             close_a2dp_input();
+             reset_a2dp_sink_dec_config_params();
+             a2dp_reset_backend_cfg(SINK);
          }
          goto param_handled;
      }
-
+#ifndef LINUX_ENABLED
      ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
      if (ret>=0) {
          ALOGD("Setting tws channel mode to %s",value);
@@ -2026,14 +2525,14 @@
          audio_a2dp_update_tws_channel_mode();
      goto param_handled;
      }
-
+#endif
      ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
      if (ret >= 0) {
-         if (a2dp.bt_lib_handle) {
+         if (a2dp.bt_lib_source_handle) {
              if ((!strncmp(value,"true",sizeof(value)))) {
                 ALOGD("Setting a2dp to suspend state");
-                a2dp.a2dp_suspended = true;
-                if (a2dp.bt_state == A2DP_STATE_DISCONNECTED)
+                a2dp.a2dp_source_suspended = true;
+                if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
                     goto param_handled;
                 list_for_each(node, &a2dp.adev->usecase_list) {
                     uc_info = node_to_item(node, struct audio_usecase, list);
@@ -2045,16 +2544,16 @@
                     }
                 }
                 reset_a2dp_enc_config_params();
-                reset_a2dp_dec_config_params();
-                if(a2dp.audio_suspend_stream)
-                   a2dp.audio_suspend_stream();
-            } else if (a2dp.a2dp_suspended == true) {
+                reset_a2dp_source_dec_config_params();
+                if(a2dp.audio_source_suspend)
+                   a2dp.audio_source_suspend();
+            } else if (a2dp.a2dp_source_suspended == true) {
                 ALOGD("Resetting a2dp suspend state");
                 struct audio_usecase *uc_info;
                 struct listnode *node;
-                if(a2dp.clear_a2dpsuspend_flag)
-                    a2dp.clear_a2dpsuspend_flag();
-                a2dp.a2dp_suspended = false;
+                if(a2dp.clear_source_a2dpsuspend_flag)
+                    a2dp.clear_source_a2dpsuspend_flag();
+                a2dp.a2dp_source_suspended = false;
                 /*
                  * It is possible that before suspend,a2dp sessions can be active
                  * for example during music + voice activation concurrency
@@ -2066,13 +2565,13 @@
                  * Fix is to call a2dp start for IPC library post suspend
                  * based on number of active session count
                  */
-                if (a2dp.a2dp_total_active_session_request > 0) {
+                if (a2dp.a2dp_source_total_active_session_requests > 0) {
                     ALOGD(" Calling IPC lib start post suspend state");
-                    if(a2dp.audio_start_stream) {
-                        ret =  a2dp.audio_start_stream();
+                    if(a2dp.audio_source_start) {
+                        ret =  a2dp.audio_source_start();
                         if (ret != 0) {
                             ALOGE("BT controller start failed");
-                            a2dp.a2dp_started = false;
+                            a2dp.a2dp_source_started = false;
                         }
                     }
                 }
@@ -2103,44 +2602,59 @@
     //During encoder reconfiguration mode, force a2dp device switch
     // Or if a2dp device is selected but earlier start failed ( as a2dp
     // was suspended, force retry.
-    return a2dp.is_handoff_in_progress || !a2dp.a2dp_started;
+    return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
 }
 
-void audio_extn_a2dp_get_sample_rate(int *sample_rate)
+void audio_extn_a2dp_get_enc_sample_rate(int *sample_rate)
 {
     *sample_rate = a2dp.enc_sampling_rate;
 }
 
-bool audio_extn_a2dp_is_ready()
+void audio_extn_a2dp_get_dec_sample_rate(int *sample_rate)
+{
+    *sample_rate = a2dp.dec_sampling_rate;
+}
+
+bool audio_extn_a2dp_source_is_ready()
 {
     bool ret = false;
 
-    if (a2dp.a2dp_suspended)
+    if (a2dp.a2dp_source_suspended)
         return ret;
 
-    if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) &&
+    if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
         (a2dp.is_a2dp_offload_supported) &&
-        (a2dp.audio_check_a2dp_ready))
-           ret = a2dp.audio_check_a2dp_ready();
+        (a2dp.audio_source_check_a2dp_ready))
+           ret = a2dp.audio_source_check_a2dp_ready();
     return ret;
 }
 
-bool audio_extn_a2dp_is_suspended()
+bool audio_extn_a2dp_sink_is_ready()
 {
-    return a2dp.a2dp_suspended;
+    bool ret = false;
+
+    if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
+        (a2dp.is_a2dp_offload_supported) &&
+        (a2dp.audio_sink_check_a2dp_ready))
+           ret = a2dp.audio_sink_check_a2dp_ready();
+    return ret;
+}
+
+bool audio_extn_a2dp_source_is_suspended()
+{
+    return a2dp.a2dp_source_suspended;
 }
 
 void audio_extn_a2dp_init (void *adev)
 {
   a2dp.adev = (struct audio_device*)adev;
-  a2dp.bt_lib_handle = NULL;
-  a2dp.a2dp_started = false;
-  a2dp.bt_state = A2DP_STATE_DISCONNECTED;
-  a2dp.a2dp_total_active_session_request = 0;
-  a2dp.a2dp_suspended = false;
-  a2dp.bt_encoder_format = ENC_CODEC_TYPE_INVALID;
+  a2dp.bt_lib_source_handle = NULL;
+  a2dp.a2dp_source_started = false;
+  a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
+  a2dp.a2dp_source_total_active_session_requests = 0;
+  a2dp.a2dp_source_suspended = false;
+  a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
   a2dp.enc_sampling_rate = 48000;
-  a2dp.is_a2dp_offload_supported = false;
   a2dp.is_handoff_in_progress = false;
   a2dp.is_aptx_dual_mono_supported = false;
   a2dp.is_aptx_adaptive = false;
@@ -2150,7 +2664,16 @@
   a2dp.abr_config.abr_tx_handle = NULL;
   a2dp.is_tws_mono_mode_on = false;
   reset_a2dp_enc_config_params();
-  reset_a2dp_dec_config_params();
+  reset_a2dp_source_dec_config_params();
+  reset_a2dp_sink_dec_config_params();
+
+  a2dp.bt_lib_sink_handle = NULL;
+  a2dp.a2dp_sink_started = false;
+  a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
+  a2dp.a2dp_sink_total_active_session_requests = 0;
+  open_a2dp_sink();
+
+  a2dp.is_a2dp_offload_supported = false;
   update_offload_codec_capabilities();
 }
 
@@ -2173,32 +2696,32 @@
     }
 
     uint32_t slatency = 0;
-    if (a2dp.audio_get_a2dp_sink_latency && a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
-        slatency = a2dp.audio_get_a2dp_sink_latency();
+    if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
+        slatency = a2dp.audio_sink_get_a2dp_latency();
     }
 
     switch(a2dp.bt_encoder_format) {
-        case ENC_CODEC_TYPE_SBC:
+        case CODEC_TYPE_SBC:
             latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
             latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
             break;
-        case ENC_CODEC_TYPE_APTX:
+        case CODEC_TYPE_APTX:
             latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
             latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
             break;
-        case ENC_CODEC_TYPE_APTX_HD:
+        case CODEC_TYPE_APTX_HD:
             latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
             latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
             break;
-        case ENC_CODEC_TYPE_AAC:
+        case CODEC_TYPE_AAC:
             latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
             latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
             break;
-        case ENC_CODEC_TYPE_CELT:
+        case CODEC_TYPE_CELT:
             latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
             latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
             break;
-        case ENC_CODEC_TYPE_LDAC:
+        case CODEC_TYPE_LDAC:
             latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
             latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
             break;
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index 9047cb3..9eb3e9e 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -293,10 +293,14 @@
 #define audio_extn_a2dp_set_parameters(parms)            (0)
 #define audio_extn_a2dp_is_force_device_switch()         (0)
 #define audio_extn_a2dp_set_handoff_mode(is_on)          (0)
-#define audio_extn_a2dp_get_sample_rate(sample_rate)     (0)
+#define audio_extn_a2dp_get_enc_sample_rate(sample_rate) (0)
+#define audio_extn_a2dp_get_dec_sample_rate(sample_rate) (0)
 #define audio_extn_a2dp_get_encoder_latency()            (0)
-#define audio_extn_a2dp_is_ready()                       (0)
-#define audio_extn_a2dp_is_suspended()                   (0)
+#define audio_extn_a2dp_sink_is_ready()                  (0)
+#define audio_extn_a2dp_source_is_ready()                (0)
+#define audio_extn_a2dp_source_is_suspended()            (0)
+#define audio_extn_a2dp_start_capture()                  (0)
+#define audio_extn_a2dp_stop_capture()                   (0)
 #else
 void audio_extn_a2dp_init(void *adev);
 int audio_extn_a2dp_start_playback();
@@ -304,10 +308,14 @@
 void audio_extn_a2dp_set_parameters(struct str_parms *parms);
 bool audio_extn_a2dp_is_force_device_switch();
 void audio_extn_a2dp_set_handoff_mode(bool is_on);
-void audio_extn_a2dp_get_sample_rate(int *sample_rate);
+void audio_extn_a2dp_get_enc_sample_rate(int *sample_rate);
+void audio_extn_a2dp_get_dec_sample_rate(int *sample_rate);
 uint32_t audio_extn_a2dp_get_encoder_latency();
-bool audio_extn_a2dp_is_ready();
-bool audio_extn_a2dp_is_suspended();
+bool audio_extn_a2dp_sink_is_ready();
+bool audio_extn_a2dp_source_is_ready();
+bool audio_extn_a2dp_source_is_suspended();
+int audio_extn_a2dp_start_capture();
+int audio_extn_a2dp_stop_capture();
 #endif
 
 #ifndef SSR_ENABLED
diff --git a/hal/audio_extn/hw_loopback.c b/hal/audio_extn/hw_loopback.c
index c76319f..77e5ab2 100644
--- a/hal/audio_extn/hw_loopback.c
+++ b/hal/audio_extn/hw_loopback.c
@@ -77,9 +77,9 @@
 typedef struct loopback_patch {
     audio_patch_handle_t patch_handle_id;            /* patch unique ID */
     struct audio_port_config loopback_source;        /* Source port config */
-    struct audio_port_config loopback_sink;          /* Source port config */
+    struct audio_port_config loopback_sink;          /* Sink port config */
     struct compress *source_stream;                  /* Source stream */
-    struct compress *sink_stream;                    /* Source stream */
+    struct compress *sink_stream;                    /* Sink stream */
     struct stream_inout patch_stream;                /* InOut type stream */
     patch_state_t patch_state;                       /* Patch operation state */
 } loopback_patch_t;
@@ -196,7 +196,9 @@
             case AUDIO_PORT_TYPE_DEVICE :
                 if ((loopback_patch->loopback_source.config_mask & AUDIO_PORT_CONFIG_FORMAT)) {
                     if ((loopback_patch->loopback_source.ext.device.type & AUDIO_DEVICE_IN_HDMI) ||
-                        (loopback_patch->loopback_source.ext.device.type & AUDIO_DEVICE_IN_SPDIF)) {
+                        (loopback_patch->loopback_source.ext.device.type & AUDIO_DEVICE_IN_SPDIF) ||
+                        (loopback_patch->loopback_source.ext.device.type & AUDIO_DEVICE_IN_BLUETOOTH_A2DP)) {
+
                        switch (loopback_patch->loopback_source.format) {
                            case AUDIO_FORMAT_PCM:
                            case AUDIO_FORMAT_PCM_16_BIT:
@@ -205,6 +207,10 @@
                            case AUDIO_FORMAT_IEC61937:
                            case AUDIO_FORMAT_AC3:
                            case AUDIO_FORMAT_E_AC3:
+                           case AUDIO_FORMAT_AAC_LATM_LC:
+                           case AUDIO_FORMAT_AAC_LATM_HE_V1:
+                           case AUDIO_FORMAT_AAC_LATM_HE_V2:
+                           case AUDIO_FORMAT_SBC:
                               is_source_supported = true;
                            break;
                        }
@@ -214,8 +220,8 @@
                 }
             break;
             default :
-            break;
-            //Unsupported as of now, need to extend for other source types
+                //Unsupported as of now, need to extend for other source types
+                break;
         }
     }
 
@@ -241,14 +247,13 @@
             }
             break;
         default :
-            break;
             //Unsupported as of now, need to extend for other sink types
+            break;
         }
     }
     if (is_source_supported && is_sink_supported) {
         return source_device | sink_device;
     }
-
     ALOGE("%s, Unsupported source or sink port config", __func__);
     return loopback_patch->patch_handle_id;
 }
diff --git a/hal/audio_extn/spkr_protection.c b/hal/audio_extn/spkr_protection.c
index f084396..4067055 100644
--- a/hal/audio_extn/spkr_protection.c
+++ b/hal/audio_extn/spkr_protection.c
@@ -97,7 +97,11 @@
 #define MAX_RESISTANCE_SPKR_Q24 (40 * (1 << 24))
 
 /*Path where the calibration file will be stored*/
-#define CALIB_FILE "/data/vendor/audio/audio.cal"
+#ifdef LINUX_ENABLED
+#define CALIB_FILE "/data/audio/audio.cal"
+#else
+#define CALIB_FILE "/data/vendor/misc/audio/audio.cal"
+#endif
 
 /*Time between retries for calibartion or intial wait time
   after boot up*/
@@ -982,6 +986,7 @@
     struct audio_device *adev = handle.adev_handle;
     unsigned long min_idle_time = MIN_SPKR_IDLE_SEC;
     char value[PROPERTY_VALUE_MAX];
+    char afe_version_value[PROPERTY_VALUE_MAX];
     char wsa_path[MAX_PATH] = {0};
     int spk_1_tzn, spk_2_tzn;
     char buf[32] = {0};
@@ -1022,7 +1027,10 @@
     }
 
     spv3_enable = property_get_bool("persist.vendor.audio.spv3.enable", false);
-    afe_api_version = property_get_int32("persist.vendor.audio.avs.afe_api_version", 0);
+    property_get("persist.vendor.audio.avs.afe_api_version", afe_version_value,
+                 "0");
+    if (atoi(afe_version_value) > 0)
+        afe_api_version = atoi(afe_version_value);
 
     fp = fopen(CALIB_FILE,"rb");
     if (fp) {
diff --git a/hal/audio_extn/utils.c b/hal/audio_extn/utils.c
index 1af22c2..7c5756b 100644
--- a/hal/audio_extn/utils.c
+++ b/hal/audio_extn/utils.c
@@ -994,7 +994,14 @@
         if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
             usecase->stream.out->app_type_cfg.sample_rate = usecase->stream.out->sample_rate;
         } else if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
-            usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
+            if (platform_spkr_use_default_sample_rate(adev->platform)) {
+                 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
+            } else {
+                 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
+                                      usecase->stream.out->sample_rate,
+                                      &usecase->stream.out->app_type_cfg.sample_rate);
+            }
+
         } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
                     snd_device == SND_DEVICE_OUT_USB_HEADSET ||
                     snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
@@ -1018,7 +1025,7 @@
                   * For a2dp playback get encoder sampling rate and set copp sampling rate,
                   * for bit width use the stream param only.
                   */
-                   audio_extn_a2dp_get_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
+                   audio_extn_a2dp_get_enc_sample_rate(&usecase->stream.out->app_type_cfg.sample_rate);
                    ALOGI("%s using %d sample rate rate for A2DP CoPP",
                         __func__, usecase->stream.out->app_type_cfg.sample_rate);
         }
@@ -1070,6 +1077,11 @@
         } else {
             audio_extn_btsco_get_sample_rate(snd_device, &usecase->stream.in->app_type_cfg.sample_rate);
         }
+        if (usecase->stream.in->device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP & ~AUDIO_DEVICE_BIT_IN) {
+            audio_extn_a2dp_get_dec_sample_rate(&usecase->stream.in->app_type_cfg.sample_rate);
+            ALOGI("%s using %d sample rate rate for A2DP dec CoPP",
+                  __func__, usecase->stream.in->app_type_cfg.sample_rate);
+        }
         sample_rate = usecase->stream.in->app_type_cfg.sample_rate;
         app_type_cfg[len++] = sample_rate;
         if (snd_device_be_idx > 0)
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 7f41763..49145ec 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -1091,7 +1091,13 @@
 
        if ((SND_DEVICE_OUT_BT_A2DP == snd_device) &&
            (audio_extn_a2dp_start_playback() < 0)) {
-           ALOGE(" fail to configure A2dp control path ");
+           ALOGE(" fail to configure A2dp Source control path ");
+           return -EINVAL;
+       }
+
+       if ((SND_DEVICE_IN_BT_A2DP == snd_device) &&
+           (audio_extn_a2dp_start_capture() < 0)) {
+           ALOGE(" fail to configure A2dp Sink control path ");
            return -EINVAL;
        }
 
@@ -1179,6 +1185,9 @@
         if (SND_DEVICE_OUT_BT_A2DP == snd_device)
             audio_extn_a2dp_stop_playback();
 
+        if (SND_DEVICE_IN_BT_A2DP == snd_device)
+            audio_extn_a2dp_stop_capture();
+
         if (snd_device == SND_DEVICE_OUT_HDMI || snd_device == SND_DEVICE_OUT_DISPLAY_PORT)
             adev->is_channel_status_set = false;
         else if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
@@ -1987,12 +1996,12 @@
         audio_extn_a2dp_is_force_device_switch()) {
          ALOGD("Force a2dp device switch to update new encoder config");
          ret = true;
-     }
+    }
 
-     if (usecase->stream.out->stream_config_changed) {
+    if (usecase->stream.out->stream_config_changed) {
          ALOGD("Force stream_config_changed to update iec61937 transmission config");
          return true;
-     }
+    }
     return ret;
 }
 
@@ -2199,7 +2208,7 @@
     }
 
     if ((is_btsco_device(out_snd_device,in_snd_device) && !adev->bt_sco_on) ||
-         (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_is_ready())) {
+         (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_source_is_ready())) {
           ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route");
           return 0;
     }
@@ -2233,7 +2242,7 @@
     }
 
     if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP) &&
-        (!audio_extn_a2dp_is_ready())) {
+        (!audio_extn_a2dp_source_is_ready())) {
         ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__);
         out_snd_device = SND_DEVICE_OUT_SPEAKER;
     }
@@ -3037,7 +3046,7 @@
     }
 
     if (out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
-        if (!audio_extn_a2dp_is_ready()) {
+        if (!audio_extn_a2dp_source_is_ready()) {
             if (out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
                 a2dp_combo = true;
             } else {
@@ -3107,7 +3116,7 @@
     }
 
     if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
-        (!audio_extn_a2dp_is_ready())) {
+        (!audio_extn_a2dp_source_is_ready())) {
         if (!a2dp_combo) {
             check_a2dp_restore_l(adev, out, false);
         } else {
@@ -3473,6 +3482,7 @@
                                     int channel_count,
                                     bool is_low_latency)
 {
+    int i = 0;
     size_t size = 0;
     uint32_t bytes_per_period_sample = 0;
 
@@ -3486,7 +3496,8 @@
     bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
     size *= bytes_per_period_sample;
 
-    /* make sure the size is multiple of 32 bytes
+    /* make sure the size is multiple of 32 bytes and additionally multiple of
+     * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
      * At 48 kHz mono 16-bit PCM:
      *  5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
      *  3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
@@ -3873,13 +3884,13 @@
         /*
          * When A2DP is disconnected the
          * music playback is paused and the policy manager sends routing=0
-         * But the audioflingercontinues to write data until standby time
+         * But the audioflinger continues to write data until standby time
          * (3sec). As BT is turned off, the write gets blocked.
          * Avoid this by routing audio to speaker until standby.
          */
         if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
                 (val == AUDIO_DEVICE_NONE) &&
-                !audio_extn_a2dp_is_ready()) {
+                !audio_extn_a2dp_source_is_ready()) {
                 val = AUDIO_DEVICE_OUT_SPEAKER;
         }
         /*
@@ -3898,7 +3909,7 @@
          * check with BT lib about a2dp streaming support before routing
          */
         if (val & AUDIO_DEVICE_OUT_ALL_A2DP) {
-            if (!audio_extn_a2dp_is_ready()) {
+            if (!audio_extn_a2dp_source_is_ready()) {
                 if (val & AUDIO_DEVICE_OUT_SPEAKER) {
                     //combo usecase just by pass a2dp
                     ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__);
@@ -4000,7 +4011,7 @@
                 }
                 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
                     out->a2dp_compress_mute &&
-                    (!(out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) || audio_extn_a2dp_is_ready())) {
+                    (!(out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) || audio_extn_a2dp_source_is_ready())) {
                     pthread_mutex_lock(&out->compr_mute_lock);
                     out->a2dp_compress_mute = false;
                     out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
@@ -4617,7 +4628,7 @@
     }
 
     if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
-        (audio_extn_a2dp_is_suspended())) {
+        (audio_extn_a2dp_source_is_suspended())) {
         if (!(out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
             if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
                 ret = -EIO;
@@ -4687,6 +4698,15 @@
                 audio_format_t dst_format = out->hal_op_format;
                 audio_format_t src_format = out->hal_ip_format;
 
+                /* prevent division-by-zero */
+                uint32_t bitwidth_src = format_to_bitwidth_table[src_format];
+                uint32_t bitwidth_dst = format_to_bitwidth_table[dst_format];
+                if ((bitwidth_src == 0) || (bitwidth_dst == 0)) {
+                    ALOGE("%s: Error bitwidth == 0", __func__);
+                    ATRACE_END();
+                    return -EINVAL;
+                }
+
                 uint32_t frames = bytes / format_to_bitwidth_table[src_format];
                 uint32_t bytes_to_write = frames * format_to_bitwidth_table[dst_format];
 
@@ -4853,10 +4873,18 @@
             out->standby = true;
         }
         out_on_error(&out->stream.common);
-        if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))
-            usleep((uint64_t)bytes * 1000000 / audio_stream_out_frame_size(stream) /
-                            out_get_sample_rate(&out->stream.common));
+        if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
+            /* prevent division-by-zero */
+            uint32_t stream_size = audio_stream_out_frame_size(stream);
+            uint32_t srate = out_get_sample_rate(&out->stream.common);
 
+            if ((stream_size == 0) || (srate == 0)) {
+                ALOGE("%s: stream_size= %d, srate = %d", __func__, stream_size, srate);
+                ATRACE_END();
+                return -EINVAL;
+             }
+             usleep((uint64_t)bytes * 1000000 / stream_size / srate);
+        }
         if (audio_extn_passthru_is_passthrough_stream(out)) {
                 //ALOGE("%s: write error, ret = %zd", __func__, ret);
                 ATRACE_END();
@@ -7399,6 +7427,13 @@
                                             config->format,
                                             channel_count,
                                             is_low_latency);
+            /* prevent division-by-zero */
+            if (frame_size == 0) {
+                ALOGE("%s: Error frame_size==0", __func__);
+                ret = -EINVAL;
+                goto err_open;
+            }
+
             in->config.period_size = buffer_size / frame_size;
 
             if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
diff --git a/hal/edid.h b/hal/edid.h
index f97d0e3..a7578e6 100644
--- a/hal/edid.h
+++ b/hal/edid.h
@@ -57,15 +57,17 @@
 #define PCM_CHANNEL_FRC  14  /* Front right of center.                        */
 #define PCM_CHANNEL_RLC  15  /* Rear left of center.                          */
 #define PCM_CHANNEL_RRC  16  /* Rear right of center.                         */
-#define PCM_CHANNEL_LFE2 17  /* Rear right of center.                         */
+#define PCM_CHANNEL_LFE2 17  /* Second low frequency channel.                 */
 #define PCM_CHANNEL_SL   18  /* Side left channel.                            */
-#define PCM_CHANNEL_SR   19  /* Side right channel                            */
+#define PCM_CHANNEL_SR   19  /* Side right channel.                           */
 #define PCM_CHANNEL_TFL  20  /* Top front left channel.                       */
+#define PCM_CHANNEL_LVH  20  /* Left vertical height channel.                 */
 #define PCM_CHANNEL_TFR  21  /* Top front right channel.                      */
+#define PCM_CHANNEL_RVH  21  /* Right vertical height channel.                */
 #define PCM_CHANNEL_TC   22  /* Top center channel.                           */
 #define PCM_CHANNEL_TBL  23  /* Top back left channel.                        */
 #define PCM_CHANNEL_TBR  24  /* Top back right channel.                       */
-#define PCM_CHANNEL_TSL  25  /* Top side left channel                         */
+#define PCM_CHANNEL_TSL  25  /* Top side left channel.                        */
 #define PCM_CHANNEL_TSR  26  /* Top side right channel.                       */
 #define PCM_CHANNEL_TBC  27  /* Top back center channel.                      */
 #define PCM_CHANNEL_BFC  28  /* Bottom front center channel.                  */
diff --git a/hal/msm8916/platform.c b/hal/msm8916/platform.c
index 3ea991b..eb2cecc 100644
--- a/hal/msm8916/platform.c
+++ b/hal/msm8916/platform.c
@@ -308,6 +308,7 @@
     struct audio_microphone_characteristic_t microphones[AUDIO_MICROPHONE_MAX_COUNT];
     struct snd_device_to_mic_map mic_map[SND_DEVICE_MAX];
     struct  spkr_device_chmap *spkr_ch_map;
+    bool use_sprk_default_sample_rate;
 };
 
 struct  spkr_device_chmap {
@@ -2342,6 +2343,7 @@
     my_data->voice_speaker_stereo = false;
     my_data->declared_mic_count = 0;
     my_data->spkr_ch_map = NULL;
+    my_data->use_sprk_default_sample_rate = true;
 
     be_dai_name_table = NULL;
 
@@ -2885,6 +2887,9 @@
     /* free acdb_meta_key_list */
     platform_release_acdb_metainfo_key(platform);
 
+    if (my_data->acdb_deallocate)
+        my_data->acdb_deallocate();
+
     free(platform);
     /* deinit usb */
     audio_extn_usb_deinit();
@@ -7488,6 +7493,11 @@
     platform_get_edid_info(platform);
 }
 
+bool platform_spkr_use_default_sample_rate(void *platform) {
+    struct platform_data *my_data = (struct platform_data *)platform;
+    return my_data->use_sprk_default_sample_rate;
+}
+
 void platform_invalidate_backend_config(void * platform,snd_device_t snd_device)
 {
     struct platform_data *my_data = (struct platform_data *)platform;
diff --git a/hal/msm8916/platform.h b/hal/msm8916/platform.h
index ad8c9aa..e337870 100644
--- a/hal/msm8916/platform.h
+++ b/hal/msm8916/platform.h
@@ -188,6 +188,7 @@
     SND_DEVICE_IN_BT_SCO_MIC_NREC,
     SND_DEVICE_IN_BT_SCO_MIC_WB,
     SND_DEVICE_IN_BT_SCO_MIC_WB_NREC,
+    SND_DEVICE_IN_BT_A2DP,
     SND_DEVICE_IN_CAMCORDER_MIC,
     SND_DEVICE_IN_VOICE_DMIC,
     SND_DEVICE_IN_VOICE_SPEAKER_DMIC,
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index e10a955..59fad85 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -67,6 +67,7 @@
 #define MIXER_XML_PATH_I2S "/etc/mixer_paths_i2s.xml"
 #define PLATFORM_INFO_XML_PATH_I2S "/etc/audio_platform_info_extcodec.xml"
 #define PLATFORM_INFO_XML_PATH_WSA  "/etc/audio_platform_info_wsa.xml"
+#define PLATFORM_INFO_XML_PATH_TDM  "/etc/audio_platform_info_tdm.xml"
 #else
 #define MIXER_XML_BASE_STRING "/vendor/etc/mixer_paths"
 #define MIXER_XML_DEFAULT_PATH "/vendor/etc/mixer_paths.xml"
@@ -79,6 +80,7 @@
 #define MIXER_XML_PATH_I2S "/vendor/etc/mixer_paths_i2s.xml"
 #define PLATFORM_INFO_XML_PATH_I2S "/vendor/etc/audio_platform_info_i2s.xml"
 #define PLATFORM_INFO_XML_PATH_WSA  "/vendor/etc/audio_platform_info_wsa.xml"
+#define PLATFORM_INFO_XML_PATH_TDM  "/vendor/etc/audio_platform_info_tdm.xml"
 #endif
 
 #include <linux/msm_audio.h>
@@ -124,7 +126,6 @@
 /* Mixer path names */
 #define AFE_SIDETONE_MIXER_PATH "afe-sidetone"
 
-#define AUDIO_PARAMETER_KEY_FLUENCE_TYPE  "fluence"
 #define AUDIO_PARAMETER_KEY_SLOWTALK      "st_enable"
 #define AUDIO_PARAMETER_KEY_HD_VOICE      "hd_voice"
 #define AUDIO_PARAMETER_KEY_VOLUME_BOOST  "volume_boost"
@@ -133,6 +134,15 @@
 
 #define AUDIO_PARAMETER_KEY_MONO_SPEAKER "mono_speaker"
 
+#define AUDIO_PARAMETER_KEY_FLUENCE_TYPE        "fluence_type"
+#define AUDIO_PARAMETER_KEY_FLUENCE_VOICE_CALL  "fluence_voice"
+#define AUDIO_PARAMETER_KEY_FLUENCE_VOICE_REC   "fluence_voice_rec"
+#define AUDIO_PARAMETER_KEY_FLUENCE_AUDIO_REC   "fluence_audio_rec"
+#define AUDIO_PARAMETER_KEY_FLUENCE_SPEAKER     "fluence_speaker"
+#define AUDIO_PARAMETER_KEY_FLUENCE_MODE        "fluence_mode"
+#define AUDIO_PARAMETER_KEY_FLUENCE_HFPCALL     "fluence_hfp"
+#define AUDIO_PARAMETER_KEY_FLUENCE_TRI_MIC     "fluence_tri_mic"
+
 #define AUDIO_PARAMETER_KEY_PERF_LOCK_OPTS "perf_lock_opts"
 
 /* Reload ACDB files from specified path */
@@ -290,6 +300,7 @@
     struct audio_microphone_characteristic_t microphones[AUDIO_MICROPHONE_MAX_COUNT];
     struct snd_device_to_mic_map mic_map[SND_DEVICE_MAX];
     struct  spkr_device_chmap *spkr_ch_map;
+    bool use_sprk_default_sample_rate;
 };
 
 struct  spkr_device_chmap {
@@ -507,6 +518,7 @@
     [SND_DEVICE_IN_BT_SCO_MIC_NREC] = "bt-sco-mic",
     [SND_DEVICE_IN_BT_SCO_MIC_WB] = "bt-sco-mic-wb",
     [SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = "bt-sco-mic-wb",
+    [SND_DEVICE_IN_BT_A2DP] = "bt-a2dp-cap",
     [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
     [SND_DEVICE_IN_VOICE_DMIC] = "voice-dmic-ef",
     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = "voice-speaker-dmic-ef",
@@ -693,6 +705,7 @@
     [SND_DEVICE_IN_BT_SCO_MIC_NREC] = 122,
     [SND_DEVICE_IN_BT_SCO_MIC_WB] = 38,
     [SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = 123,
+    [SND_DEVICE_IN_BT_A2DP] = 21,
     [SND_DEVICE_IN_CAMCORDER_MIC] = 4,
     [SND_DEVICE_IN_VOICE_DMIC] = 41,
     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = 43,
@@ -853,6 +866,7 @@
     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_NREC)},
     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_WB)},
     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_WB_NREC)},
+    {TO_NAME_INDEX(SND_DEVICE_IN_BT_A2DP)},
     {TO_NAME_INDEX(SND_DEVICE_IN_CAMCORDER_MIC)},
     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_DMIC)},
     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_DMIC)},
@@ -1511,6 +1525,7 @@
     backend_tag_table[SND_DEVICE_OUT_VOICE_SPEAKER_VBAT] = strdup("voice-speaker-vbat");
     backend_tag_table[SND_DEVICE_OUT_VOICE_SPEAKER_2_VBAT] = strdup("voice-speaker-2-vbat");
     backend_tag_table[SND_DEVICE_OUT_BT_A2DP] = strdup("bt-a2dp");
+    backend_tag_table[SND_DEVICE_IN_BT_A2DP] = strdup("bt-a2dp-cap");
     backend_tag_table[SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP] = strdup("speaker-and-bt-a2dp");
     backend_tag_table[SND_DEVICE_OUT_VOICE_SPEAKER_AND_VOICE_HEADPHONES] = strdup("speaker-and-headphones");
     backend_tag_table[SND_DEVICE_OUT_VOICE_SPEAKER_AND_VOICE_ANC_HEADSET] = strdup("speaker-and-headphones");
@@ -1612,6 +1627,7 @@
     hw_interface_table[SND_DEVICE_IN_BT_SCO_MIC_NREC] = strdup("SLIMBUS_7_TX");
     hw_interface_table[SND_DEVICE_IN_BT_SCO_MIC_WB] = strdup("SLIMBUS_7_TX");
     hw_interface_table[SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = strdup("SLIMBUS_7_TX");
+    hw_interface_table[SND_DEVICE_IN_BT_A2DP] = strdup("SLIMBUS_7_TX");
     hw_interface_table[SND_DEVICE_IN_CAMCORDER_MIC] = strdup("SLIMBUS_0_TX");
     hw_interface_table[SND_DEVICE_IN_VOICE_DMIC] = strdup("SLIMBUS_0_TX");
     hw_interface_table[SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = strdup("SLIMBUS_0_TX");
@@ -2204,7 +2220,7 @@
     my_data->voice_speaker_stereo = false;
     my_data->declared_mic_count = 0;
     my_data->spkr_ch_map = NULL;
-
+    my_data->use_sprk_default_sample_rate = true;
     be_dai_name_table = NULL;
 
     property_get("ro.vendor.audio.sdk.fluencetype", my_data->fluence_cap, "");
@@ -2295,11 +2311,23 @@
     else if (!strncmp(snd_card_name, "qcs405-wsa-snd-card",
                sizeof("qcs405-wsa-snd-card")))
         platform_info_init(PLATFORM_INFO_XML_PATH_WSA, my_data, PLATFORM);
+    else if (!strncmp(snd_card_name, "qcs405-tdm-snd-card",
+               sizeof("qcs405-tdm-snd-card")))
+        platform_info_init(PLATFORM_INFO_XML_PATH_TDM, my_data, PLATFORM);
     else if (my_data->is_internal_codec)
         platform_info_init(PLATFORM_INFO_XML_PATH_INTCODEC, my_data, PLATFORM);
     else
         platform_info_init(PLATFORM_INFO_XML_PATH, my_data, PLATFORM);
 
+    /* CSRA devices support multiple sample rates via I2S at spkr out */
+    if (!strncmp(snd_card_name, "qcs405-csra", strlen("qcs405-csra"))) {
+        ALOGE("%s: soundcard: %s supports multiple sample rates", __func__, snd_card_name);
+        my_data->use_sprk_default_sample_rate = false;
+    } else {
+        my_data->use_sprk_default_sample_rate = true;
+        ALOGE("%s: soundcard: %s supports only default sample rate", __func__, snd_card_name);
+    }
+
     my_data->voice_feature_set = VOICE_FEATURE_SET_DEFAULT;
     my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
     if (my_data->acdb_handle == NULL) {
@@ -2576,11 +2604,18 @@
         }
     } else {
         if (!strncmp(snd_card_name, "qcs405", strlen("qcs405"))) {
-            my_data->current_backend_cfg[DEFAULT_CODEC_BACKEND].bitwidth_mixer_ctl =
-                strdup("WSA_CDC_DMA_RX_0 Format");
-            my_data->current_backend_cfg[DEFAULT_CODEC_BACKEND].samplerate_mixer_ctl =
-                strdup("WSA_CDC_DMA_RX_0 SampleRate");
 
+            if (!strncmp(snd_card_name, "qcs405-csra", strlen("qcs405-csra"))) {
+               my_data->current_backend_cfg[DEFAULT_CODEC_BACKEND].bitwidth_mixer_ctl =
+                   strdup("PRIM_MI2S_RX Format");
+               my_data->current_backend_cfg[DEFAULT_CODEC_BACKEND].samplerate_mixer_ctl =
+                   strdup("PRIM_MI2S_RX SampleRate");
+            } else {
+               my_data->current_backend_cfg[DEFAULT_CODEC_BACKEND].bitwidth_mixer_ctl =
+                   strdup("WSA_CDC_DMA_RX_0 Format");
+               my_data->current_backend_cfg[DEFAULT_CODEC_BACKEND].samplerate_mixer_ctl =
+                   strdup("WSA_CDC_DMA_RX_0 SampleRate");
+            }
             my_data->current_backend_cfg[DEFAULT_CODEC_TX_BACKEND].bitwidth_mixer_ctl =
                 strdup("VA_CDC_DMA_TX_0 Format");
             my_data->current_backend_cfg[DEFAULT_CODEC_TX_BACKEND].samplerate_mixer_ctl =
@@ -4811,6 +4846,8 @@
             }
         } else if (in_device & AUDIO_DEVICE_IN_SPDIF) {
             snd_device = SND_DEVICE_IN_SPDIF;
+        } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
+            snd_device = SND_DEVICE_IN_BT_A2DP;
         } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
             snd_device = SND_DEVICE_IN_HDMI_MIC;
         } else if (in_device & AUDIO_DEVICE_IN_HDMI_ARC) {
@@ -5273,13 +5310,106 @@
             platform->spkr_ch_map->num_ch = num_ch;
             for (i = 0; i < num_ch; i++) {
                 opts = strtok_r(NULL, ", ", &test_r);
-                platform->spkr_ch_map->chmap[i] = strtoul(opts, NULL, 16);
+                if (opts == NULL) {
+                    ALOGE("%s: incorrect ch_map\n", __func__);
+                    free(platform->spkr_ch_map);
+                    platform->spkr_ch_map = NULL;
+                    str_parms_del(parms, AUDIO_PARAMETER_KEY_SPKR_DEVICE_CHMAP);
+                    return;
+                } else {
+                    platform->spkr_ch_map->chmap[i] = strtoul(opts, NULL, 16);
+                }
             }
         }
         str_parms_del(parms, AUDIO_PARAMETER_KEY_SPKR_DEVICE_CHMAP);
     }
 }
 
+static void platform_set_fluence_params(void *platform, struct str_parms *parms, char *value, int len)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    int err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FLUENCE_TYPE, value, len);
+
+    if (err >= 0) {
+        if (!strncmp("fluence", value, sizeof("fluence")))
+            my_data->fluence_type = FLUENCE_DUAL_MIC;
+        else if (!strncmp("fluencepro", value, sizeof("fluencepro")))
+                 my_data->fluence_type = FLUENCE_QUAD_MIC | FLUENCE_DUAL_MIC;
+        else if (!strncmp("none", value, sizeof("none")))
+                 my_data->fluence_type = FLUENCE_NONE;
+
+        str_parms_del(parms, AUDIO_PARAMETER_KEY_FLUENCE_TYPE);
+    }
+
+    err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FLUENCE_TRI_MIC, value, len);
+    if (err >= 0) {
+        if (!strncmp("true", value, sizeof("true")))
+            my_data->fluence_type |= FLUENCE_TRI_MIC;
+
+        str_parms_del(parms, AUDIO_PARAMETER_KEY_FLUENCE_TRI_MIC);
+    }
+
+    err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FLUENCE_VOICE_CALL, value, len);
+    if (err >= 0) {
+        if (!strncmp("true", value, sizeof("true")))
+            my_data->fluence_in_voice_call = true;
+        else
+            my_data->fluence_in_voice_call = false;
+
+        str_parms_del(parms, AUDIO_PARAMETER_KEY_FLUENCE_VOICE_CALL);
+    }
+
+    err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FLUENCE_VOICE_REC, value, len);
+    if (err >= 0) {
+        if (!strncmp("true", value, sizeof("true")))
+            my_data->fluence_in_voice_rec = true;
+        else
+            my_data->fluence_in_voice_rec = false;
+
+        str_parms_del(parms, AUDIO_PARAMETER_KEY_FLUENCE_VOICE_REC);
+    }
+
+    err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FLUENCE_AUDIO_REC, value, len);
+    if (err >= 0) {
+        if (!strncmp("true", value, sizeof("true")))
+            my_data->fluence_in_audio_rec = true;
+        else
+            my_data->fluence_in_audio_rec = false;
+
+        str_parms_del(parms, AUDIO_PARAMETER_KEY_FLUENCE_AUDIO_REC);
+    }
+
+    err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FLUENCE_SPEAKER, value, len);
+    if (err >= 0) {
+        if (!strncmp("true", value, sizeof("true")))
+            my_data->fluence_in_spkr_mode = true;
+        else
+            my_data->fluence_in_spkr_mode = false;
+
+        str_parms_del(parms, AUDIO_PARAMETER_KEY_FLUENCE_SPEAKER);
+    }
+
+    err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FLUENCE_MODE, value, len);
+    if (err >= 0) {
+        if (!strncmp("broadside", value, sizeof("broadside")))
+            my_data->fluence_mode = FLUENCE_BROADSIDE;
+        else if (!strncmp("endfire", value, sizeof("endfire")))
+            my_data->fluence_mode = FLUENCE_ENDFIRE;
+
+        str_parms_del(parms, AUDIO_PARAMETER_KEY_FLUENCE_MODE);
+    }
+
+    err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FLUENCE_HFPCALL, value, len);
+    if (err >= 0) {
+        if (!strncmp("true", value, sizeof("true")))
+            my_data->fluence_in_hfp_call = true;
+        else
+            my_data->fluence_in_hfp_call = false;
+
+        str_parms_del(parms, AUDIO_PARAMETER_KEY_FLUENCE_HFPCALL);
+    }
+}
+
 int platform_set_parameters(void *platform, struct str_parms *parms)
 {
     struct platform_data *my_data = (struct platform_data *)platform;
@@ -5413,6 +5543,8 @@
         ALOGV("%s: max_mic_count %d", __func__, my_data->max_mic_count);
     }
 
+    platform_set_fluence_params(platform, parms, value, len);
+
     /* handle audio calibration parameters */
     set_audiocal(platform, parms, value, len);
     native_audio_set_params(platform, parms, value, len);
@@ -6372,6 +6504,7 @@
     if (snd_device == SND_DEVICE_OUT_BT_A2DP ||
         snd_device == SND_DEVICE_OUT_BT_SCO ||
         snd_device == SND_DEVICE_OUT_BT_SCO_WB ||
+        snd_device == SND_DEVICE_IN_BT_A2DP ||
         snd_device == SND_DEVICE_OUT_AFE_PROXY) {
         backend_change = false;
         return backend_change;
@@ -6515,9 +6648,15 @@
             bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
             ALOGD("%s:becf: afe: reset to default bitwidth %d", __func__, bit_width);
         }
-        sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
-        ALOGD("%s:becf: afe: playback on codec device not supporting native playback set "
+        /*
+         * In case of CSRA speaker out, all sample rates are supported, so
+         *  check platform here
+         */
+        if (platform_spkr_use_default_sample_rate(adev->platform)) {
+            sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
+            ALOGD("%s:becf: afe: playback on codec device not supporting native playback set "
             "default Sample Rate(48k)", __func__);
+        }
     }
 
     if (backend_idx == USB_AUDIO_RX_BACKEND) {
@@ -7219,6 +7358,40 @@
                     channel_map[7] = PCM_CHANNEL_RS;
                 }
                 break;
+           case 12:
+                /* AUDIO_CHANNEL_OUT_7POINT1POINT4 */
+                channel_map[0] = PCM_CHANNEL_FL;
+                channel_map[1] = PCM_CHANNEL_FR;
+                channel_map[2] = PCM_CHANNEL_FC;
+                channel_map[3] = PCM_CHANNEL_LFE;
+                channel_map[4] = PCM_CHANNEL_LB;
+                channel_map[5] = PCM_CHANNEL_RB;
+                channel_map[6] = PCM_CHANNEL_LS;
+                channel_map[7] = PCM_CHANNEL_RS;
+                channel_map[8] = PCM_CHANNEL_TFL;
+                channel_map[9] = PCM_CHANNEL_TFR;
+                channel_map[10] = PCM_CHANNEL_TSL;
+                channel_map[11] = PCM_CHANNEL_TSR;
+                break;
+          case 16:
+                /* 16 channels */
+                channel_map[0] = PCM_CHANNEL_FL;
+                channel_map[1] = PCM_CHANNEL_FR;
+                channel_map[2] = PCM_CHANNEL_FC;
+                channel_map[3] = PCM_CHANNEL_LFE;
+                channel_map[4] = PCM_CHANNEL_LB;
+                channel_map[5] = PCM_CHANNEL_RB;
+                channel_map[6] = PCM_CHANNEL_LS;
+                channel_map[7] = PCM_CHANNEL_RS;
+                channel_map[8] = PCM_CHANNEL_TFL;
+                channel_map[9] = PCM_CHANNEL_TFR;
+                channel_map[10] = PCM_CHANNEL_TSL;
+                channel_map[11] = PCM_CHANNEL_TSR;
+                channel_map[12] = PCM_CHANNEL_FLC;
+                channel_map[13] = PCM_CHANNEL_FRC;
+                channel_map[14] = PCM_CHANNEL_RLC;
+                channel_map[15] = PCM_CHANNEL_RRC;
+                break;
             default:
                 ALOGE("unsupported channels %d for setting channel map", channels);
                 return -1;
@@ -7343,12 +7516,21 @@
     struct mixer_ctl *ctl;
     char mixer_ctl_name[44] = {0}; // max length of name is 44 as defined
     int ret;
-    unsigned int i;
-    long set_values[FCC_8] = {0};
+    unsigned int i=0, n=0;
+    long set_values[AUDIO_MAX_DSP_CHANNELS];
     struct platform_data *my_data = (struct platform_data *)platform;
     struct audio_device *adev = my_data->adev;
     ALOGV("%s channel_count:%d",__func__, ch_count);
-    if (NULL == ch_map || (ch_count < 1) || (ch_count > FCC_8)) {
+
+    /*
+     * FIXME:
+     * Currently the channel mask in audio.h is limited to 30 channels,
+     * (=AUDIO_CHANNEL_COUNT_MAX), whereas the mixer controls already
+     * allow up to AUDIO_MAX_DSP_CHANNELS channels as per final requirement.
+     * Until channel mask definition is not changed from a uint32_t value
+     * to something else, a sanity check is needed here.
+     */
+    if (NULL == ch_map || (ch_count < 1) || (ch_count > AUDIO_CHANNEL_COUNT_MAX)) {
         ALOGE("%s: Invalid channel mapping or channel count value", __func__);
         return -EINVAL;
     }
@@ -7366,12 +7548,34 @@
     ALOGD("%s mixer_ctl_name:%s", __func__, mixer_ctl_name);
 
     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 -EINVAL;
     }
-    for (i = 0; i < (unsigned int)ch_count; i++) {
+
+    /* find out how many values the control can set */
+    n = mixer_ctl_get_num_values(ctl);
+
+    if (n != ch_count)
+        ALOGV("%s chcnt %d != mixerctl elem size %d",__func__, ch_count, n);
+
+    if (n < ch_count) {
+        ALOGE("%s chcnt %d > mixerctl elem size %d",__func__, ch_count, n);
+        return -EINVAL;
+    }
+
+    if (n > AUDIO_MAX_DSP_CHANNELS) {
+        ALOGE("%s mixerctl elem size %d > AUDIO_MAX_DSP_CHANNELS %d",__func__, n, AUDIO_MAX_DSP_CHANNELS);
+        return -EINVAL;
+    }
+
+    /* initialize all set_values to zero */
+    memset (set_values, 0, sizeof(set_values));
+
+    /* copy only as many values as corresponding mixer_ctrl allows */
+    for (i = 0; i < ch_count; i++) {
         set_values[i] = ch_map[i];
     }
 
@@ -7379,7 +7583,8 @@
         set_values[0], set_values[1], set_values[2], set_values[3], set_values[4],
         set_values[5], set_values[6], set_values[7], ch_count);
 
-    ret = mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
+    ret = mixer_ctl_set_array(ctl, set_values, n);
+
     if (ret < 0) {
         ALOGE("%s: Could not set ctl, error:%d ch_count:%d",
               __func__, ret, ch_count);
@@ -7544,6 +7749,11 @@
     return 0;
 }
 
+bool platform_spkr_use_default_sample_rate(void *platform) {
+    struct platform_data *my_data = (struct platform_data *)platform;
+    return my_data->use_sprk_default_sample_rate;
+}
+
 int platform_set_edid_channels_configuration(void *platform, int channels, int backend_idx) {
 
     struct platform_data *my_data = (struct platform_data *)platform;
diff --git a/hal/msm8974/platform.h b/hal/msm8974/platform.h
index eb6edb0..60e6581 100644
--- a/hal/msm8974/platform.h
+++ b/hal/msm8974/platform.h
@@ -186,6 +186,7 @@
     SND_DEVICE_IN_BT_SCO_MIC_NREC,
     SND_DEVICE_IN_BT_SCO_MIC_WB,
     SND_DEVICE_IN_BT_SCO_MIC_WB_NREC,
+    SND_DEVICE_IN_BT_A2DP,
     SND_DEVICE_IN_CAMCORDER_MIC,
     SND_DEVICE_IN_VOICE_DMIC,
     SND_DEVICE_IN_VOICE_SPEAKER_DMIC,
@@ -293,6 +294,8 @@
 
 #define AUDIO_PARAMETER_KEY_TRUE_32_BIT "true_32_bit"
 
+#define AUDIO_MAX_DSP_CHANNELS 32
+
 #define ALL_SESSION_VSID                0xFFFFFFFF
 #define DEFAULT_MUTE_RAMP_DURATION_MS   20
 #define DEFAULT_VOLUME_RAMP_DURATION_MS 20
diff --git a/hal/platform_api.h b/hal/platform_api.h
index 6474b61..21f3e72 100644
--- a/hal/platform_api.h
+++ b/hal/platform_api.h
@@ -225,6 +225,7 @@
                                        snd_device_t snd_device,
                                        struct mix_matrix_params mm_params);
 int platform_set_edid_channels_configuration(void *platform, int channels, int backend_idx);
+bool platform_spkr_use_default_sample_rate(void *platform);
 unsigned char platform_map_to_edid_format(int format);
 bool platform_is_edid_supported_format(void *platform, int format);
 bool platform_is_edid_supported_sample_rate(void *platform, int sample_rate);