More work on A2DP Sink:

Output audio data through A2DP audio HAL rather than playing directly
to native AudioTrack API.

Add separate HAL interface for A2DP sink

Change-Id: I6c6cb6088c350e104b4a7354f328b29c7178e295
diff --git a/btif/include/bluetoothTrack.h b/btif/include/bluetoothTrack.h
deleted file mode 100644
index e4e1660..0000000
--- a/btif/include/bluetoothTrack.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/******************************************************************************
- *
- *  Copyright (C) 2014 The Android Open Source Project
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at:
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- ******************************************************************************/
-
-#if defined (__cplusplus) || (cplusplus)
-extern "C" {
-#endif
-
-int btCreateTrack(int trackFreq, int channelType);
-void btDeleteTrack();
-void btStopTrack();
-void btStartTrack();
-void btPauseTrack();
-int btWriteData(void *audioBuffer, int bufferlen);
-
-#if defined (__cplusplus) || (cplusplus)
-}
-#endif
diff --git a/btif/include/btif_av.h b/btif/include/btif_av.h
index eda9c18..12174c9 100644
--- a/btif/include/btif_av.h
+++ b/btif/include/btif_av.h
@@ -44,9 +44,7 @@
     BTIF_AV_START_STREAM_REQ_EVT,
     BTIF_AV_STOP_STREAM_REQ_EVT,
     BTIF_AV_SUSPEND_STREAM_REQ_EVT,
-    BTIF_AV_RECONFIGURE_REQ_EVT,
-    BTIF_AV_REQUEST_AUDIO_FOCUS_EVT,
-    BTIF_AV_REQUEST_ACTIVATE_SINK_EVT,
+    BTIF_AV_SINK_CONFIG_REQ_EVT,
 } btif_av_sm_event_t;
 
 
diff --git a/btif/include/btif_media.h b/btif/include/btif_media.h
index a785d34..f01286b 100644
--- a/btif/include/btif_media.h
+++ b/btif/include/btif_media.h
@@ -98,13 +98,6 @@
 } tBTIF_MEDIA_SINK_CFG_UPDATE;
 #endif
 
-typedef enum {
-    BTIF_MEDIA_AUDIOFOCUS_LOSS = 0,
-    BTIF_MEDIA_AUDIOFOCUS_GAIN,
-    BTIF_MEDIA_AUDIOFOCUS_LOSS_TRANSIENT
-} btif_media_AudioFocus_state;
-
-
 /*******************************************************************************
  **  Public functions
  *******************************************************************************/
@@ -281,6 +274,9 @@
 void btif_media_check_iop_exceptions(UINT8 *peer_bda);
 void btif_reset_decoder(UINT8 *p_av);
 BOOLEAN btif_media_task_start_decoding_req(void);
-void btif_a2dp_set_audio_focus_state(btif_media_AudioFocus_state state);
+
+int btif_a2dp_get_track_frequency(UINT8 frequency);
+int btif_a2dp_get_track_channel_count(UINT8 channeltype);
+void btif_a2dp_set_peer_sep(UINT8 sep);
 
 #endif
diff --git a/btif/src/bluetooth.c b/btif/src/bluetooth.c
index 9e63d54..c0cdc5a 100644
--- a/btif/src/bluetooth.c
+++ b/btif/src/bluetooth.c
@@ -79,7 +79,8 @@
 /* handsfree profile - client */
 extern bthf_client_interface_t *btif_hf_client_get_interface();
 /* advanced audio profile */
-extern btav_interface_t *btif_av_get_interface();
+extern btav_interface_t *btif_av_get_src_interface();
+extern btav_interface_t *btif_av_get_sink_interface();
 /*rfc l2cap*/
 extern btsock_interface_t *btif_sock_get_interface();
 /* hid host profile */
@@ -328,7 +329,10 @@
         return btif_pan_get_interface();
 
     if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
-        return btif_av_get_interface();
+        return btif_av_get_src_interface();
+
+    if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
+        return btif_av_get_sink_interface();
 
     if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
         return btif_hh_get_interface();
diff --git a/btif/src/bluetoothTrack.cpp b/btif/src/bluetoothTrack.cpp
deleted file mode 100644
index 685780e..0000000
--- a/btif/src/bluetoothTrack.cpp
+++ /dev/null
@@ -1,122 +0,0 @@
-/******************************************************************************
- *
- *  Copyright (C) 2014 The Android Open Source Project
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at:
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- ******************************************************************************/
-
-#include "bluetoothTrack.h"
-#include <media/AudioTrack.h>
-
-//#define DUMP_PCM_DATA TRUE
-#if (defined(DUMP_PCM_DATA) && (DUMP_PCM_DATA == TRUE))
-FILE *outputPcmSampleFile;
-char outputFilename [50] = "/data/misc/bluedroid/output_sample.pcm";
-#endif
-
-struct BluetoothTrack {
-    android::sp<android::AudioTrack> mTrack;
-};
-
-typedef struct BluetoothTrack BluetoothTrack;
-
-BluetoothTrack *track = NULL;
-
-int btCreateTrack(int trackFreq, int channelType)
-{
-    int ret = -1;
-    if (track == NULL)
-        track = new BluetoothTrack;
-    track->mTrack = NULL;
-    track->mTrack = new android::AudioTrack(AUDIO_STREAM_MUSIC, trackFreq, AUDIO_FORMAT_PCM_16_BIT,
-            channelType, (int)0, (audio_output_flags_t)AUDIO_OUTPUT_FLAG_FAST, NULL, NULL, 0, 0, android::AudioTrack::TRANSFER_SYNC);
-    if (track->mTrack == NULL)
-    {
-        delete track;
-        track = NULL;
-        return ret;
-    }
-    if (track->mTrack->initCheck() != 0)
-    {
-        delete track;
-        track = NULL;
-        return ret;
-    }
-#if (defined(DUMP_PCM_DATA) && (DUMP_PCM_DATA == TRUE))
-    outputPcmSampleFile = fopen(outputFilename, "ab");
-#endif
-    ret = 0;
-    track->mTrack->setVolume(1, 1);
-    return ret;
-}
-
-void btStartTrack()
-{
-    if ((track != NULL) && (track->mTrack.get() != NULL))
-    {
-        track->mTrack->start();
-    }
-}
-
-
-void btDeleteTrack()
-{
-    if ((track != NULL) && (track->mTrack.get() != NULL))
-    {
-        track->mTrack.clear();
-        delete track;
-        track = NULL;
-    }
-#if (defined(DUMP_PCM_DATA) && (DUMP_PCM_DATA == TRUE))
-    if (outputPcmSampleFile)
-    {
-        fclose(outputPcmSampleFile);
-    }
-    outputPcmSampleFile = NULL;
-#endif
-}
-
-void btPauseTrack()
-{
-    if ((track != NULL) && (track->mTrack.get() != NULL))
-    {
-        track->mTrack->pause();
-        track->mTrack->flush();
-    }
-}
-
-void btStopTrack()
-{
-    if ((track != NULL) && (track->mTrack.get() != NULL))
-    {
-        track->mTrack->stop();
-    }
-}
-
-int btWriteData(void *audioBuffer, int bufferlen)
-{
-    int retval = -1;
-    if ((track != NULL) && (track->mTrack.get() != NULL))
-    {
-#if (defined(DUMP_PCM_DATA) && (DUMP_PCM_DATA == TRUE))
-        if (outputPcmSampleFile)
-        {
-            fwrite ((audioBuffer), 1, (size_t)bufferlen, outputPcmSampleFile);
-        }
-#endif
-        retval = track->mTrack->write(audioBuffer, (size_t)bufferlen);
-    }
-    return retval;
-}
-
diff --git a/btif/src/btif_av.c b/btif/src/btif_av.c
index 0b7b9d7..24ceb8a 100644
--- a/btif/src/btif_av.c
+++ b/btif/src/btif_av.c
@@ -26,6 +26,7 @@
  *****************************************************************************/
 
 #include <hardware/bluetooth.h>
+#include <system/audio.h>
 #include "hardware/bt_av.h"
 
 #define LOG_TAG "BTIF_AV"
@@ -69,12 +70,6 @@
 /*****************************************************************************
 **  Local type definitions
 ******************************************************************************/
-typedef enum
-{
-    SEP_SRC = 0x0,
-    SEP_SNK,
-    SEP_NOT_OPENED
-}tbtif_AV_SEP_TYPE;
 
 typedef struct
 {
@@ -83,7 +78,7 @@
     btif_sm_handle_t sm_handle;
     UINT8 flags;
     tBTA_AV_EDR edr;
-    tbtif_AV_SEP_TYPE   sep;  /* sep type of peer device */
+    UINT8   peer_sep;  /* sep type of peer device */
 } btif_av_cb_t;
 
 typedef struct
@@ -91,15 +86,24 @@
     bt_bdaddr_t *target_bda;
     uint16_t uuid;
 } btif_av_connect_req_t;
+
+typedef struct
+{
+    int sample_rate;
+    int channel_count;
+} btif_av_sink_config_req_t;
+
 /*****************************************************************************
 **  Static variables
 ******************************************************************************/
-static btav_callbacks_t *bt_av_callbacks = NULL;
+static btav_callbacks_t *bt_av_src_callbacks = NULL;
+static btav_callbacks_t *bt_av_sink_callbacks = NULL;
 static btif_av_cb_t btif_av_cb;
 static TIMER_LIST_ENT tle_av_open_on_rc;
 
 /* both interface and media task needs to be ready to alloc incoming request */
-#define CHECK_BTAV_INIT() if ((bt_av_callbacks == NULL) || (btif_av_cb.sm_handle == NULL))\
+#define CHECK_BTAV_INIT() if (((bt_av_src_callbacks == NULL) &&(bt_av_sink_callbacks == NULL)) \
+        || (btif_av_cb.sm_handle == NULL))\
 {\
      BTIF_TRACE_WARNING1("%s: BTAV not initialized", __FUNCTION__);\
      return BT_STATUS_NOT_READY;\
@@ -191,39 +195,11 @@
         CASE_RETURN_STR(BTIF_AV_START_STREAM_REQ_EVT)
         CASE_RETURN_STR(BTIF_AV_STOP_STREAM_REQ_EVT)
         CASE_RETURN_STR(BTIF_AV_SUSPEND_STREAM_REQ_EVT)
-        CASE_RETURN_STR(BTIF_AV_RECONFIGURE_REQ_EVT)
-        CASE_RETURN_STR(BTIF_AV_REQUEST_AUDIO_FOCUS_EVT)
-        CASE_RETURN_STR(BTIF_AV_REQUEST_ACTIVATE_SINK_EVT)
+        CASE_RETURN_STR(BTIF_AV_SINK_CONFIG_REQ_EVT)
         default: return "UNKNOWN_EVENT";
    }
 }
 
-/*******************************************************************************
-**
-** Function         btif_av_request_audio_focus
-**
-** Description      send request to gain audio focus
-**
-** Returns          void
-**
-*******************************************************************************/
-void btif_av_request_audio_focus( BOOLEAN enable)
-{
-    btif_sm_state_t state;
-    state= btif_sm_get_state(btif_av_cb.sm_handle);
-    /* We shld be in started state */
-    if (state != BTIF_AV_STATE_STARTED)
-        return;
-    /* If we are in started state, suspend shld not have been initiated */
-    if ((btif_av_cb.flags & BTIF_AV_FLAG_REMOTE_SUSPEND )||
-        (btif_av_cb.flags & BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING))
-        return;
-    if(enable)
-    {
-         btif_dispatch_sm_event(BTIF_AV_REQUEST_AUDIO_FOCUS_EVT, NULL, 0);
-    }
-}
-
 /****************************************************************************
 **  Local helper functions
 *****************************************************************************/
@@ -262,6 +238,24 @@
 **  Static functions
 ******************************************************************************/
 
+static void btif_report_connection_state(btav_connection_state_t state, bt_bdaddr_t *bd_addr)
+{
+    if (btif_av_cb.peer_sep == AVDT_TSEP_SRC && bt_av_sink_callbacks != NULL) {
+        HAL_CBACK(bt_av_sink_callbacks, connection_state_cb, state, bd_addr);
+    } else if (btif_av_cb.peer_sep == AVDT_TSEP_SNK && bt_av_src_callbacks != NULL) {
+        HAL_CBACK(bt_av_src_callbacks, connection_state_cb, state, bd_addr);
+    }
+}
+
+static void btif_report_audio_state(btav_audio_state_t state, bt_bdaddr_t *bd_addr)
+{
+    if (btif_av_cb.peer_sep == AVDT_TSEP_SRC && bt_av_sink_callbacks != NULL) {
+        HAL_CBACK(bt_av_sink_callbacks, audio_state_cb, state, bd_addr);
+    } else if (btif_av_cb.peer_sep == AVDT_TSEP_SNK && bt_av_src_callbacks != NULL) {
+        HAL_CBACK(bt_av_src_callbacks, audio_state_cb, state, bd_addr);
+    }
+}
+
 /*****************************************************************************
 **
 ** Function     btif_av_state_idle_handler
@@ -284,7 +278,6 @@
             memset(&btif_av_cb.peer_bda, 0, sizeof(bt_bdaddr_t));
             btif_av_cb.flags = 0;
             btif_av_cb.edr = 0;
-            btif_av_cb.sep = SEP_NOT_OPENED;
             btif_a2dp_on_idle();
             break;
 
@@ -294,14 +287,6 @@
         case BTA_AV_ENABLE_EVT:
             break;
 
-        case BTIF_AV_REQUEST_ACTIVATE_SINK_EVT:
-        {
-            int enable = *((int*)p_data);
-            BTIF_TRACE_DEBUG1(" Active_Sink enable %d", enable)
-            BTA_AvEnable_Sink(enable);
-        }
-            break;
-
         case BTA_AV_REGISTER_EVT:
             btif_av_cb.bta_handle = ((tBTA_AV*)p_data)->registr.hndl;
             break;
@@ -387,8 +372,7 @@
     {
         case BTIF_SM_ENTER_EVT:
             /* inform the application that we are entering connecting state */
-            HAL_CBACK(bt_av_callbacks, connection_state_cb,
-                      BTAV_CONNECTION_STATE_CONNECTING, &(btif_av_cb.peer_bda));
+            btif_report_connection_state(BTAV_CONNECTION_STATE_CONNECTING, &(btif_av_cb.peer_bda));
             break;
 
         case BTIF_SM_EXIT_EVT:
@@ -396,8 +380,7 @@
 
         case BTA_AV_REJECT_EVT:
             BTIF_TRACE_DEBUG0(" Received  BTA_AV_REJECT_EVT ");
-            HAL_CBACK(bt_av_callbacks, connection_state_cb,
-                BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda));
+            btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda));
             btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE);
             break;
 
@@ -415,10 +398,8 @@
                  av_state = BTIF_AV_STATE_OPENED;
                  btif_av_cb.edr = p_bta_data->open.edr;
 
-                 if (p_bta_data->open.sep == AVDT_TSEP_SRC)
-                    btif_av_cb.sep = SEP_SRC;
-                 else if (p_bta_data->open.sep == AVDT_TSEP_SNK)
-                     btif_av_cb.sep = SEP_SNK;
+                 btif_av_cb.peer_sep = p_bta_data->open.sep;
+                 btif_a2dp_set_peer_sep(p_bta_data->open.sep);
             }
             else
             {
@@ -429,17 +410,16 @@
             }
 
             /* inform the application of the event */
-            HAL_CBACK(bt_av_callbacks, connection_state_cb,
-                             state, &(btif_av_cb.peer_bda));
+            btif_report_connection_state(state, &(btif_av_cb.peer_bda));
             /* change state to open/idle based on the status */
             btif_sm_change_state(btif_av_cb.sm_handle, av_state);
-            if (btif_av_cb.sep == SEP_SNK)
+            if (btif_av_cb.peer_sep == AVDT_TSEP_SNK)
             {
                 /* if queued PLAY command,  send it now */
                 btif_rc_check_handle_pending_play(p_bta_data->open.bd_addr,
                                              (p_bta_data->open.status == BTA_AV_SUCCESS));
             }
-            else if (btif_av_cb.sep == SEP_SRC)
+            else if (btif_av_cb.peer_sep == AVDT_TSEP_SRC)
             {
                 /* if queued PLAY command,  send it now */
                 btif_rc_check_handle_pending_play(p_bta_data->open.bd_addr, FALSE);
@@ -447,6 +427,20 @@
             btif_queue_advance();
         } break;
 
+        case BTIF_AV_SINK_CONFIG_REQ_EVT:
+        {
+            btif_av_sink_config_req_t req;
+            // copy to avoid alignment problems
+            memcpy(&req, p_data, sizeof(req));
+
+            BTIF_TRACE_WARNING2("BTIF_AV_SINK_CONFIG_REQ_EVT %d %d", req.sample_rate,
+                    req.channel_count);
+            if (btif_av_cb.peer_sep == AVDT_TSEP_SRC && bt_av_sink_callbacks != NULL) {
+                HAL_CBACK(bt_av_sink_callbacks, audio_config_cb, &(btif_av_cb.peer_bda),
+                        req.sample_rate, req.channel_count);
+            }
+        } break;
+
         CHECK_RC_EVENT(event, p_data);
 
         default:
@@ -478,13 +472,13 @@
     switch (event)
     {
         case BTIF_SM_ENTER_EVT:
-            if (btif_av_cb.sep == SEP_SNK)
+            if (btif_av_cb.peer_sep == AVDT_TSEP_SNK)
             {
                 /* immediately stop transmission of frames */
                 btif_a2dp_set_tx_flush(TRUE);
                 /* wait for audioflinger to stop a2dp */
             }
-            if (btif_av_cb.sep == SEP_SRC)
+            if (btif_av_cb.peer_sep == AVDT_TSEP_SRC)
             {
                 btif_a2dp_set_rx_flush(TRUE);
             }
@@ -492,18 +486,18 @@
 
         case BTA_AV_STOP_EVT:
         case BTIF_AV_STOP_STREAM_REQ_EVT:
-            if (btif_av_cb.sep == SEP_SNK)
+            if (btif_av_cb.peer_sep == AVDT_TSEP_SNK)
             {
               /* immediately flush any pending tx frames while suspend is pending */
               btif_a2dp_set_tx_flush(TRUE);
             }
-            if (btif_av_cb.sep == SEP_SRC)
+            if (btif_av_cb.peer_sep == AVDT_TSEP_SRC)
             {
                 btif_a2dp_set_rx_flush(TRUE);
             }
 
             btif_a2dp_on_stopped(NULL);
-              break;
+            break;
 
         case BTIF_SM_EXIT_EVT:
             break;
@@ -511,8 +505,7 @@
         case BTA_AV_CLOSE_EVT:
 
             /* inform the application that we are disconnecting */
-            HAL_CBACK(bt_av_callbacks, connection_state_cb,
-                BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda));
+            btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda));
 
             btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE);
             break;
@@ -567,7 +560,7 @@
             break;
 
         case BTIF_AV_START_STREAM_REQ_EVT:
-            if (btif_av_cb.sep == SEP_SRC)
+            if (btif_av_cb.peer_sep == AVDT_TSEP_SRC)
             {
                 BTA_AvStart();
                 btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_START;
@@ -587,7 +580,7 @@
                 return TRUE;
 
             /*  In case peer is A2DP SRC we do not want to ack commands on UIPC*/
-            if (btif_av_cb.sep == SEP_SNK)
+            if (btif_av_cb.peer_sep == AVDT_TSEP_SNK)
             {
                 if (btif_a2dp_on_started(&p_av->start,
                     ((btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) != 0)))
@@ -601,14 +594,14 @@
             if (p_av->start.status != BTA_AV_SUCCESS)
                 return FALSE;
 
-            if (btif_av_cb.sep == SEP_SRC)
+            if (btif_av_cb.peer_sep == AVDT_TSEP_SRC)
             {
                 btif_a2dp_set_rx_flush(FALSE); /*  remove flush state, ready for streaming*/
             }
 
             /* change state to started, send acknowledgement if start is pending */
             if (btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) {
-                if (btif_av_cb.sep == SEP_SNK)
+                if (btif_av_cb.peer_sep == AVDT_TSEP_SNK)
                     btif_a2dp_on_started(NULL, TRUE);
                 /* pending start flag will be cleared when exit current state */
             }
@@ -618,10 +611,12 @@
 
         case BTIF_AV_DISCONNECT_REQ_EVT:
             BTA_AvClose(btif_av_cb.bta_handle);
+            if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) {
+                BTA_AvCloseRc(btif_av_cb.bta_handle);
+            }
 
             /* inform the application that we are disconnecting */
-            HAL_CBACK(bt_av_callbacks, connection_state_cb,
-               BTAV_CONNECTION_STATE_DISCONNECTING, &(btif_av_cb.peer_bda));
+            btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTING, &(btif_av_cb.peer_bda));
             break;
 
         case BTA_AV_CLOSE_EVT:
@@ -629,8 +624,7 @@
             btif_a2dp_on_stopped(NULL);
 
             /* inform the application that we are disconnected */
-            HAL_CBACK(bt_av_callbacks, connection_state_cb,
-                BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda));
+            btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda));
 
             /* change state to idle, send acknowledgement if start is pending */
             if (btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) {
@@ -689,8 +683,7 @@
             /* we are again in started state, clear any remote suspend flags */
             btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
 
-            HAL_CBACK(bt_av_callbacks, audio_state_cb,
-                BTAV_AUDIO_STATE_STARTED, &(btif_av_cb.peer_bda));
+            btif_report_audio_state(BTAV_AUDIO_STATE_STARTED, &(btif_av_cb.peer_bda));
 
             /* increase the a2dp consumer task priority temporarily when start
             ** audio playing, to avoid overflow the audio packet queue. */
@@ -706,7 +699,7 @@
 
         case BTIF_AV_START_STREAM_REQ_EVT:
             /* we were remotely started, just ack back the local request */
-            if (btif_av_cb.sep == SEP_SNK)
+            if (btif_av_cb.peer_sep == AVDT_TSEP_SNK)
                 btif_a2dp_on_started(NULL, TRUE);
             break;
 
@@ -722,14 +715,16 @@
                always overrides */
             btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
 
-            if (btif_av_cb.sep == SEP_SNK)
+            if (btif_av_cb.peer_sep == AVDT_TSEP_SNK)
             {
             /* immediately stop transmission of frames while suspend is pending */
                 btif_a2dp_set_tx_flush(TRUE);
             }
 
-            if (btif_av_cb.sep == SEP_SRC)
+            if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) {
                 btif_a2dp_set_rx_flush(TRUE);
+                btif_a2dp_on_stopped(NULL);
+            }
 
             BTA_AvStop(TRUE);
             break;
@@ -738,10 +733,12 @@
 
             /* request avdtp to close */
             BTA_AvClose(btif_av_cb.bta_handle);
+            if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) {
+                BTA_AvCloseRc(btif_av_cb.bta_handle);
+            }
 
             /* inform the application that we are disconnecting */
-            HAL_CBACK(bt_av_callbacks, connection_state_cb,
-                BTAV_CONNECTION_STATE_DISCONNECTING, &(btif_av_cb.peer_bda));
+            btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTING, &(btif_av_cb.peer_bda));
 
             /* wait in closing state until fully closed */
             btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_CLOSING);
@@ -760,7 +757,7 @@
             {
                 btif_av_cb.flags &= ~BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING;
 
-               if (btif_av_cb.sep == SEP_SNK)
+               if (btif_av_cb.peer_sep == AVDT_TSEP_SNK)
                {
                 /* suspend failed, reset back tx flush state */
                     btif_a2dp_set_tx_flush(FALSE);
@@ -778,13 +775,11 @@
                 if ((btif_av_cb.flags & BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING) == 0)
                     btif_av_cb.flags |= BTIF_AV_FLAG_REMOTE_SUSPEND;
 
-                HAL_CBACK(bt_av_callbacks, audio_state_cb,
-                        BTAV_AUDIO_STATE_REMOTE_SUSPEND, &(btif_av_cb.peer_bda));
+                btif_report_audio_state(BTAV_AUDIO_STATE_REMOTE_SUSPEND, &(btif_av_cb.peer_bda));
             }
             else
             {
-                HAL_CBACK(bt_av_callbacks, audio_state_cb,
-                        BTAV_AUDIO_STATE_STOPPED, &(btif_av_cb.peer_bda));
+                btif_report_audio_state(BTAV_AUDIO_STATE_STOPPED, &(btif_av_cb.peer_bda));
             }
 
             btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_OPENED);
@@ -798,8 +793,7 @@
             btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_STOP;
             btif_a2dp_on_stopped(&p_av->suspend);
 
-            HAL_CBACK(bt_av_callbacks, audio_state_cb,
-                      BTAV_AUDIO_STATE_STOPPED, &(btif_av_cb.peer_bda));
+            btif_report_audio_state(BTAV_AUDIO_STATE_STOPPED, &(btif_av_cb.peer_bda));
 
             /* if stop was successful, change state to open */
             if (p_av->suspend.status == BTA_AV_SUCCESS)
@@ -807,11 +801,6 @@
 
             break;
 
-            case BTIF_AV_REQUEST_AUDIO_FOCUS_EVT:
-                HAL_CBACK(bt_av_callbacks, audio_focus_request_cb,
-                                       1, &(btif_av_cb.peer_bda));
-                break;
-
         case BTA_AV_CLOSE_EVT:
 
              btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_STOP;
@@ -820,8 +809,7 @@
             btif_a2dp_on_stopped(NULL);
 
             /* inform the application that we are disconnected */
-            HAL_CBACK(bt_av_callbacks, connection_state_cb,
-                BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda));
+            btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda));
 
             btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE);
             break;
@@ -857,6 +845,9 @@
 {
     btif_sm_state_t state;
     UINT8 que_len;
+    tA2D_STATUS a2d_status;
+    tA2D_SBC_CIE sbc_cie;
+    btif_av_sink_config_req_t config_req;
 
     if (event == BTA_AV_MEDIA_DATA_EVT)/* Switch to BTIF_MEDIA context */
     {
@@ -871,8 +862,21 @@
             return;
     }
 
-    if (event == BTA_AV_MEDIA_SINK_CFG_EVT) /* send a command to BT Media Task */
+    if (event == BTA_AV_MEDIA_SINK_CFG_EVT) {
+        /* send a command to BT Media Task */
         btif_reset_decoder((UINT8*)p_data);
+
+        a2d_status = A2D_ParsSbcInfo(&sbc_cie, (UINT8 *)p_data, FALSE);
+        if (a2d_status == A2D_SUCCESS) {
+            /* Switch to BTIF context */
+            config_req.sample_rate = btif_a2dp_get_track_frequency(sbc_cie.samp_freq);
+            config_req.channel_count = btif_a2dp_get_track_channel_count(sbc_cie.ch_mode);
+            btif_transfer_context(btif_av_handle_event, BTIF_AV_SINK_CONFIG_REQ_EVT,
+                                     (char*)&config_req, sizeof(config_req), NULL);
+        } else {
+            APPL_TRACE_ERROR1("ERROR dump_codec_info A2D_ParsSbcInfo fail:%d", a2d_status);
+        }
+    }
 }
 /*******************************************************************************
 **
@@ -884,8 +888,10 @@
 **
 *******************************************************************************/
 
-bt_status_t btif_av_init(void)
+bt_status_t btif_av_init()
 {
+    btif_av_cb.sm_handle = NULL;
+
     if (btif_av_cb.sm_handle == NULL)
     {
         if (btif_a2dp_start_media_task() != GKI_SUCCESS)
@@ -898,7 +904,7 @@
 
         btif_a2dp_on_init();
 
-        return BT_STATUS_SUCCESS;
+       return BT_STATUS_SUCCESS;
     }
 
     return BT_STATUS_DONE;
@@ -906,27 +912,63 @@
 
 /*******************************************************************************
 **
-** Function         init
+** Function         init_src
 **
-** Description      Initializes the AV interface
+** Description      Initializes the AV interface for source mode
 **
 ** Returns          bt_status_t
 **
 *******************************************************************************/
 
-static bt_status_t init(btav_callbacks_t* callbacks )
+static bt_status_t init_src(btav_callbacks_t* callbacks)
 {
-    int status;
+    bt_status_t status;
 
     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
 
-    if (bt_av_callbacks)
-        return BT_STATUS_DONE;
+    if (bt_av_sink_callbacks != NULL) {
+        // already did btif_av_init()
+        status = BT_STATUS_SUCCESS;
+    } else {
+        status = btif_av_init();
+    }
 
-    bt_av_callbacks = callbacks;
-    btif_av_cb.sm_handle = NULL;
+    if (status == BT_STATUS_SUCCESS) {
+        bt_av_src_callbacks = callbacks;
+    }
 
-    return btif_av_init();
+    return status;
+}
+
+/*******************************************************************************
+**
+** Function         init_sink
+**
+** Description      Initializes the AV interface for sink mode
+**
+** Returns          bt_status_t
+**
+*******************************************************************************/
+
+static bt_status_t init_sink(btav_callbacks_t* callbacks)
+{
+    bt_status_t status;
+
+    BTIF_TRACE_EVENT1("%s", __FUNCTION__);
+
+    if (bt_av_src_callbacks != NULL) {
+        // already did btif_av_init()
+        status = BT_STATUS_SUCCESS;
+    } else {
+        status = btif_av_init();
+    }
+
+    if (status == BT_STATUS_SUCCESS) {
+        bt_av_sink_callbacks = callbacks;
+        BTA_AvEnable_Sink(TRUE);
+    }
+
+    return status;
 }
 
 /*******************************************************************************
@@ -951,15 +993,15 @@
     return BT_STATUS_SUCCESS;
 }
 
-static bt_status_t connect_sink(bt_bdaddr_t *bd_addr)
+static bt_status_t connect_src(bt_bdaddr_t *bd_addr)
 {
     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
     CHECK_BTAV_INIT();
-    return btif_queue_connect(UUID_SERVCLASS_AUDIO_SINK, bd_addr,
-                                 connect_int);
+
+    return btif_queue_connect(UUID_SERVCLASS_AUDIO_SINK, bd_addr, connect_int);
 }
 
-static bt_status_t connect_src(bt_bdaddr_t *bd_addr)
+static bt_status_t connect_sink(bt_bdaddr_t *bd_addr)
 {
     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
     CHECK_BTAV_INIT();
@@ -1000,124 +1042,51 @@
 {
     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
 
-    if (bt_av_callbacks)
-    {
-        btif_a2dp_stop_media_task();
+    btif_a2dp_stop_media_task();
 
-        btif_disable_service(BTA_A2DP_SERVICE_ID);
-        bt_av_callbacks = NULL;
+    btif_disable_service(BTA_A2DP_SERVICE_ID);
 
-        /* Also shut down the AV state machine */
-        btif_sm_shutdown(btif_av_cb.sm_handle);
-        btif_av_cb.sm_handle = NULL;
-    }
-    return;
+    /* Also shut down the AV state machine */
+    btif_sm_shutdown(btif_av_cb.sm_handle);
+    btif_av_cb.sm_handle = NULL;
 }
 
-/*******************************************************************************
-**
-** Function         is_src
-**
-** Description      Checks if peer device is A2DP SRC
-**
-** Returns          Success in case peer is A2DP Src, FAIL otherwise
-**
-*******************************************************************************/
-bt_status_t is_src( bt_bdaddr_t *bd_addr )
-{
-    BTIF_TRACE_DEBUG0(" isSrc:  Check if peer device with bd_addr is audio src or sink");
-    if (btif_av_cb.sep == SEP_SRC)
+static void cleanup_src(void) {
+    BTIF_TRACE_EVENT1("%s", __FUNCTION__);
+
+    if (bt_av_src_callbacks)
     {
-        BTIF_TRACE_DEBUG0(" Current Peer is SRC");
-        return BT_STATUS_SUCCESS;
-    }
-    else if (btif_av_cb.sep == SEP_SNK)
-    {
-        BTIF_TRACE_DEBUG0(" Current Peer is SNK");
-        return BT_STATUS_FAIL;
-    }
-    else
-    {
-        BTIF_TRACE_DEBUG0(" Stream not opened till now");
-        return BT_STATUS_NOT_READY;
+        bt_av_src_callbacks = NULL;
+        if (bt_av_sink_callbacks == NULL)
+            cleanup();
     }
 }
 
-/*******************************************************************************
-**
-** Function         activate_sink
-**
-** Description      Activates/Deactivates A2DP Sink
-**
-** Returns          None
-**
-*******************************************************************************/
-void activate_sink(int enable)
-{
-    BTIF_TRACE_DEBUG1(" Activate Sink %d", enable);
-    btif_dispatch_sm_event(BTIF_AV_REQUEST_ACTIVATE_SINK_EVT, (char*)&enable, sizeof(int));
+static void cleanup_sink(void) {
+    BTIF_TRACE_EVENT1("%s", __FUNCTION__);
+
+    if (bt_av_sink_callbacks)
+    {
+        bt_av_sink_callbacks = NULL;
+        if (bt_av_src_callbacks == NULL)
+            cleanup();
+    }
 }
 
-/*******************************************************************************
-**
-** Function         suspend_sink
-**
-** Description      Suspends stream  in case of A2DP Sink
-**
-** Returns          None
-**
-*******************************************************************************/
-void suspend_sink()
-{
-    BTIF_TRACE_DEBUG0(" suspend Stream Suspend called");
-    if (btif_av_cb.sep == SEP_SRC)
-        btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0);
-}
-
-/*******************************************************************************
-**
-** Function         resume_sink
-**
-** Description      Resumes stream  in case of A2DP Sink
-**
-** Returns          None
-**
-*******************************************************************************/
-void resume_sink()
-{
-    BTIF_TRACE_DEBUG0(" resume Stream called");
-    if (btif_av_cb.sep == SEP_SRC)
-        btif_dispatch_sm_event(BTIF_AV_START_STREAM_REQ_EVT, NULL, 0);
-}
-
-/*******************************************************************************
-**
-** Function         audio_focus_status
-**
-** Description      Updates audio focus state
-**
-** Returns          None
-**
-*******************************************************************************/
-static void audio_focus_status(int state)
-{
-    BTIF_TRACE_DEBUG1(" Audio Focus granted %d",state);
-
-    btif_a2dp_set_audio_focus_state(state);
-}
-
-static const btav_interface_t bt_av_interface = {
+static const btav_interface_t bt_av_src_interface = {
     sizeof(btav_interface_t),
-    init,
+    init_src,
     connect_src,
+    disconnect,
+    cleanup_src,
+};
+
+static const btav_interface_t bt_av_sink_interface = {
+    sizeof(btav_interface_t),
+    init_sink,
     connect_sink,
     disconnect,
-    cleanup,
-    is_src,
-    suspend_sink,
-    resume_sink,
-    audio_focus_status,
-    activate_sink,
+    cleanup_sink,
 };
 
 /*******************************************************************************
@@ -1252,17 +1221,32 @@
 
 /*******************************************************************************
 **
-** Function         btif_av_get_interface
+** Function         btif_av_get_src_interface
 **
-** Description      Get the AV callback interface
+** Description      Get the AV callback interface for A2DP source profile
 **
 ** Returns          btav_interface_t
 **
 *******************************************************************************/
-const btav_interface_t *btif_av_get_interface(void)
+const btav_interface_t *btif_av_get_src_interface(void)
 {
     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
-    return &bt_av_interface;
+    return &bt_av_src_interface;
+}
+
+/*******************************************************************************
+**
+** Function         btif_av_get_sink_interface
+**
+** Description      Get the AV callback interface for A2DP sink profile
+**
+** Returns          btav_interface_t
+**
+*******************************************************************************/
+const btav_interface_t *btif_av_get_sink_interface(void)
+{
+    BTIF_TRACE_EVENT1("%s", __FUNCTION__);
+    return &bt_av_sink_interface;
 }
 
 /*******************************************************************************
diff --git a/btif/src/btif_core.c b/btif/src/btif_core.c
index 455255a..9b432cd 100644
--- a/btif/src/btif_core.c
+++ b/btif/src/btif_core.c
@@ -611,8 +611,6 @@
 #if (BLE_INCLUDED == TRUE )
         BTA_BrcmInit();
 #endif
-        /* initialize a2dp service */
-        btif_av_init();
 
         /* init rfcomm & l2cap api */
         btif_sock_init();
diff --git a/btif/src/btif_media_task.c b/btif/src/btif_media_task.c
index e45ddfc..7e29917 100644
--- a/btif/src/btif_media_task.c
+++ b/btif/src/btif_media_task.c
@@ -72,7 +72,6 @@
 #endif
 #include "stdio.h"
 #include <dlfcn.h>
-#include "bluetoothTrack.h"
 
 //#define DEBUG_MEDIA_AV_FLOW TRUE
 
@@ -279,9 +278,12 @@
     UINT8 a2dp_cmd_pending; /* we can have max one command pending */
     BOOLEAN tx_flush; /* discards any outgoing data when true */
     BOOLEAN rx_flush; /* discards any incoming data when true */
-    BOOLEAN is_source;
+    UINT8 peer_sep;
+    BOOLEAN data_channel_open;
     UINT8   frames_to_process;
-    BOOLEAN rx_audio_focus_gained;
+
+    UINT32  sample_rate;
+    UINT8   channel_count;
 #endif
 
 } tBTIF_MEDIA_CB;
@@ -355,7 +357,6 @@
 #endif
 static void btif_media_task_aa_handle_start_decoding(void );
 #endif
-extern void btif_av_request_audio_focus(BOOLEAN enable);
 BOOLEAN btif_media_task_start_decoding_req(void);
 BOOLEAN btif_media_task_clear_track(void);
 /*****************************************************************************
@@ -510,6 +511,8 @@
 
                 /* post start event and wait for audio path to open */
                 btif_dispatch_sm_event(BTIF_AV_START_STREAM_REQ_EVT, NULL, 0);
+//FIXME
+                a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
             }
             else if (btif_av_stream_started_ready())
             {
@@ -527,8 +530,7 @@
             break;
 
         case A2DP_CTRL_CMD_STOP:
-
-            if (btif_media_cb.is_tx_timer == FALSE)
+            if (btif_media_cb.peer_sep == AVDT_TSEP_SNK && btif_media_cb.is_tx_timer == FALSE)
             {
                 /* we are already stopped, just ack back */
                 a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
@@ -536,6 +538,7 @@
             }
 
             btif_dispatch_sm_event(BTIF_AV_STOP_STREAM_REQ_EVT, NULL, 0);
+            a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
             break;
 
         case A2DP_CTRL_CMD_SUSPEND:
@@ -553,6 +556,17 @@
             }
             break;
 
+        case A2DP_CTRL_GET_AUDIO_CONFIG:
+        {
+            uint32_t sample_rate = btif_media_cb.sample_rate;
+            uint8_t channel_count = btif_media_cb.channel_count;
+
+            a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
+            UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, (UINT8 *)&sample_rate, 4);
+            UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, &channel_count, 1);
+            break;
+        }
+
         default:
             APPL_TRACE_ERROR1("UNSUPPORTED CMD (%d)", cmd);
             a2dp_cmd_acknowledge(A2DP_CTRL_ACK_FAILURE);
@@ -605,11 +619,15 @@
             UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_REG_REMOVE_ACTIVE_READSET, NULL);
             UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_SET_READ_POLL_TMO,
                        (void *)A2DP_DATA_READ_POLL_MS);
-            /* Start the media task to encode SBC */
-            btif_media_task_start_aa_req();
 
-            /* make sure we update any changed sbc encoder params */
-            btif_a2dp_encoder_update();
+            if (btif_media_cb.peer_sep == AVDT_TSEP_SNK) {
+                /* Start the media task to encode SBC */
+                btif_media_task_start_aa_req();
+
+                /* make sure we update any changed sbc encoder params */
+                btif_a2dp_encoder_update();
+            }
+            btif_media_cb.data_channel_open = TRUE;
 
             /* ack back when media task is fully started */
             break;
@@ -617,6 +635,7 @@
         case UIPC_CLOSE_EVT:
             a2dp_cmd_acknowledge(A2DP_CTRL_ACK_SUCCESS);
             btif_audiopath_detached();
+            btif_media_cb.data_channel_open = FALSE;
             break;
 
         default :
@@ -860,7 +879,7 @@
 void btif_a2dp_on_idle(void)
 {
     APPL_TRACE_EVENT0("## ON A2DP IDLE ##");
-    if(btif_media_cb.is_source)
+    if (btif_media_cb.peer_sep == AVDT_TSEP_SNK)
     {
         /* Make sure media task is stopped */
         btif_media_task_stop_aa_req();
@@ -868,16 +887,13 @@
 
     bta_av_co_init();
 #ifdef BTA_AVK_INCLUDED
-    if (!btif_media_cb.is_source)
+    if (btif_media_cb.peer_sep == AVDT_TSEP_SRC)
     {
         btif_media_cb.rx_flush = TRUE;
         btif_media_task_aa_rx_flush_req();
         btif_media_task_stop_decoding_req();
         btif_media_task_clear_track();
         APPL_TRACE_DEBUG0("Stopped BT track");
-        APPL_TRACE_DEBUG0("Reset to Source role");
-        btif_media_cb.is_source = TRUE;
-        btif_media_cb.rx_audio_focus_gained = BTIF_MEDIA_AUDIOFOCUS_LOSS;
     }
 #endif
 }
@@ -1092,11 +1108,13 @@
 void btif_a2dp_on_stopped(tBTA_AV_SUSPEND *p_av)
 {
     APPL_TRACE_EVENT0("## ON A2DP STOPPED ##");
-    if ((!btif_media_cb.is_source)) /*  Handling for A2DP SINK cases*/
+    if (btif_media_cb.peer_sep == AVDT_TSEP_SRC) /*  Handling for A2DP SINK cases*/
     {
         btif_media_cb.rx_flush = TRUE;
         btif_media_task_aa_rx_flush_req();
         btif_media_task_stop_decoding_req();
+        UIPC_Close(UIPC_CH_ID_AV_AUDIO);
+        btif_media_cb.data_channel_open = FALSE;
         return;
     }
     /* allow using this api for other than suspend */
@@ -1136,7 +1154,7 @@
 void btif_a2dp_on_suspended(tBTA_AV_SUSPEND *p_av)
 {
     APPL_TRACE_EVENT0("## ON A2DP SUSPENDED ##");
-    if ((!btif_media_cb.is_source))
+    if (btif_media_cb.peer_sep == AVDT_TSEP_SRC)
     {
         btif_media_cb.rx_flush = TRUE;
         btif_media_task_aa_rx_flush_req();
@@ -1174,13 +1192,6 @@
     btif_media_cb.tx_flush = enable;
 }
 
-/* when true media task discards any rx frames */
-void btif_a2dp_set_audio_focus_state(btif_media_AudioFocus_state state)
-{
-    APPL_TRACE_EVENT1("## Audio_focus_state Rx %d ##", state);
-    btif_media_cb.rx_audio_focus_gained = state;
-}
-
 #ifdef BTA_AVK_INCLUDED
 /*******************************************************************************
  **
@@ -1210,18 +1221,7 @@
             btif_media_flush_q(&(btif_media_cb.RxSbcQ));
             return;
         }
-        if (btif_media_cb.rx_audio_focus_gained == BTIF_MEDIA_AUDIOFOCUS_LOSS_TRANSIENT)
-        {
-            APPL_TRACE_DEBUG0("Received Transient Focus Loss, Ignoring");
-            return;
-        }
 
-        if (btif_media_cb.rx_audio_focus_gained == BTIF_MEDIA_AUDIOFOCUS_LOSS)
-        {
-            /* Send a Audio Focus Request */
-            btif_av_request_audio_focus(TRUE);
-            return;
-        }
         num_frames_to_process = btif_media_cb.frames_to_process;
         APPL_TRACE_DEBUG0(" Process Frames + ");
 
@@ -1338,8 +1338,6 @@
 #if (BTA_AV_INCLUDED == TRUE)
     UIPC_Open(UIPC_CH_ID_AV_CTRL , btif_a2dp_ctrl_cb);
 #endif
-    btif_media_cb.is_source = TRUE;
-    APPL_TRACE_DEBUG0("Reset to Source role");
 }
 /*******************************************************************************
  **
@@ -1558,14 +1556,18 @@
     OI_STATUS status;
     int num_sbc_frames = p_msg->num_frames_to_be_processed;
     UINT32 sbc_frame_len = p_msg->len - 1;
-    int retwriteAudioTrack = 0;
     availPcmBytes = 2*sizeof(pcmData);
 
-    if ((btif_media_cb.is_source) || (btif_media_cb.rx_flush))
+    if ((btif_media_cb.peer_sep == AVDT_TSEP_SNK) || (btif_media_cb.rx_flush))
     {
         APPL_TRACE_DEBUG0(" State Changed happened in this tick ");
         return;
     }
+
+    // ignore data if no one is listening
+    if (!btif_media_cb.data_channel_open)
+        return;
+
     APPL_TRACE_DEBUG2("Number of sbc frames %d, frame_len %d", num_sbc_frames, sbc_frame_len);
 
     for(count = 0; count < num_sbc_frames && sbc_frame_len != 0; count ++)
@@ -1585,7 +1587,7 @@
         p_msg->len = sbc_frame_len + 1;
     }
 
-    retwriteAudioTrack = btWriteData((void*)pcmData, (2*sizeof(pcmData) - availPcmBytes));
+    UIPC_Send(UIPC_CH_ID_AV_AUDIO, 0, (UINT8 *)pcmData, (2*sizeof(pcmData) - availPcmBytes));
 }
 #endif
 
@@ -2107,7 +2109,7 @@
     }
 }
 
-int a2dp_get_track_frequency(UINT8 frequency) {
+int btif_a2dp_get_track_frequency(UINT8 frequency) {
     int freq = 48000;
     switch (frequency) {
         case A2D_SBC_IE_SAMP_FREQ_16:
@@ -2126,19 +2128,23 @@
     return freq;
 }
 
-int a2dp_get_track_channel_type(UINT8 channeltype) {
-    int channel = AUDIO_CHANNEL_OUT_MONO;
+int btif_a2dp_get_track_channel_count(UINT8 channeltype) {
+    int count = 1;
     switch (channeltype) {
         case A2D_SBC_IE_CH_MD_MONO:
-            channel = AUDIO_CHANNEL_OUT_MONO;
+            count = 1;
             break;
         case A2D_SBC_IE_CH_MD_DUAL:
         case A2D_SBC_IE_CH_MD_STEREO:
         case A2D_SBC_IE_CH_MD_JOINT:
-            channel = AUDIO_CHANNEL_OUT_STEREO;
+            count = 2;
             break;
     }
-    return channel;
+    return count;
+}
+
+void btif_a2dp_set_peer_sep(UINT8 sep) {
+    btif_media_cb.peer_sep = sep;
 }
 
 /*******************************************************************************
@@ -2154,7 +2160,6 @@
 {
     btif_media_cb.is_rx_timer = FALSE;
     GKI_stop_timer(BTIF_MEDIA_AVK_TASK_TIMER_ID);
-    btPauseTrack();
 }
 
 /*******************************************************************************
@@ -2170,7 +2175,6 @@
 {
     if(btif_media_cb.is_rx_timer == TRUE)
         return;
-    btStartTrack();
     btif_media_cb.is_rx_timer = TRUE;
     GKI_start_timer(BTIF_MEDIA_AVK_TASK_TIMER_ID, GKI_MS_TO_TICKS(BTIF_SINK_MEDIA_TIME_TICK), TRUE);
 }
@@ -2180,8 +2184,6 @@
 static void btif_media_task_aa_handle_clear_track (void)
 {
     APPL_TRACE_DEBUG0("btif_media_task_aa_handle_clear_track");
-    btStopTrack();
-    btDeleteTrack();
 }
 
 /*******************************************************************************
@@ -2213,18 +2215,18 @@
         APPL_TRACE_ERROR1("ERROR dump_codec_info A2D_ParsSbcInfo fail:%d", a2d_status);
         return;
     }
-    btif_media_cb.is_source = FALSE;
+
+    btif_media_cb.sample_rate = btif_a2dp_get_track_frequency(sbc_cie.samp_freq);
+    btif_media_cb.channel_count = btif_a2dp_get_track_channel_count(sbc_cie.ch_mode);
+
     btif_media_cb.rx_flush = FALSE;
     APPL_TRACE_DEBUG0("Reset to sink role");
     status = OI_CODEC_SBC_DecoderReset(&context, contextData, sizeof(contextData), 2, 2, FALSE);
     if (!OI_SUCCESS(status)) {
         APPL_TRACE_ERROR1("OI_CODEC_SBC_DecoderReset failed with error code %d\n", status);
     }
-    APPL_TRACE_DEBUG0("A2dpSink: Crate Track");
-    if (btCreateTrack(a2dp_get_track_frequency(sbc_cie.samp_freq), a2dp_get_track_channel_type(sbc_cie.ch_mode)) == -1) {
-        APPL_TRACE_ERROR0("A2dpSink: Track creation fails!!!");
-        return;
-    }
+
+    UIPC_Open(UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb);
 
     switch(sbc_cie.samp_freq)
     {