]> granicus.if.org Git - esp-idf/commitdiff
component/bt: transport bludroid bta/av and bta/ar modules to stack
authorwangmengyang <wangmengyang@espressif.com>
Wed, 2 Nov 2016 11:40:46 +0000 (19:40 +0800)
committerwangmengyang <wangmengyang@espressif.com>
Wed, 2 Nov 2016 11:40:46 +0000 (19:40 +0800)
note: callout functions defined in bta_av_co.c are temporarily removed;

18 files changed:
components/bt/bluedroid/bta/ar/bta_ar.c [new file with mode: 0755]
components/bt/bluedroid/bta/ar/bta_ar_int.h [new file with mode: 0755]
components/bt/bluedroid/bta/av/bta_av_aact.c [new file with mode: 0755]
components/bt/bluedroid/bta/av/bta_av_act.c [new file with mode: 0755]
components/bt/bluedroid/bta/av/bta_av_api.c [new file with mode: 0755]
components/bt/bluedroid/bta/av/bta_av_cfg.c [new file with mode: 0755]
components/bt/bluedroid/bta/av/bta_av_ci.c [new file with mode: 0755]
components/bt/bluedroid/bta/av/bta_av_int.h [new file with mode: 0755]
components/bt/bluedroid/bta/av/bta_av_main.c [new file with mode: 0755]
components/bt/bluedroid/bta/av/bta_av_sbc.c [new file with mode: 0755]
components/bt/bluedroid/bta/av/bta_av_ssm.c [new file with mode: 0755]
components/bt/bluedroid/bta/include/bta_ar_api.h [new file with mode: 0755]
components/bt/bluedroid/bta/include/bta_av_api.h [new file with mode: 0755]
components/bt/bluedroid/bta/include/bta_av_ci.h [new file with mode: 0755]
components/bt/bluedroid/bta/include/bta_av_co.h [new file with mode: 0755]
components/bt/bluedroid/bta/include/bta_av_sbc.h [new file with mode: 0755]
components/bt/bluedroid/btif/bta_av_co.c [new file with mode: 0755]
components/bt/component.mk

diff --git a/components/bt/bluedroid/bta/ar/bta_ar.c b/components/bt/bluedroid/bta/ar/bta_ar.c
new file mode 100755 (executable)
index 0000000..d58fe7b
--- /dev/null
@@ -0,0 +1,340 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2008-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This is the implementation for the audio/video registration module.
+ *
+ ******************************************************************************/
+
+#include <string.h>
+#include "bta_ar_api.h"
+#include "bta_ar_int.h"
+
+
+/* AV control block */
+#if BTA_DYNAMIC_MEMORY == FALSE
+tBTA_AR_CB  bta_ar_cb;
+#endif
+
+/*******************************************************************************
+**
+** Function         bta_ar_id
+**
+** Description      This function maps sys_id to ar id mask.
+**
+** Returns          void
+**
+*******************************************************************************/
+static UINT8 bta_ar_id(tBTA_SYS_ID sys_id)
+{
+    UINT8   mask = 0;
+    if (sys_id == BTA_ID_AV)
+    {
+        mask = BTA_AR_AV_MASK;
+    }
+    else if (sys_id == BTA_ID_AVK)
+    {
+        mask = BTA_AR_AVK_MASK;
+    }
+
+    return mask;
+}
+
+/*******************************************************************************
+**
+** Function         bta_ar_init
+**
+** Description      This function is called to register to AVDTP.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_ar_init(void)
+{
+    /* initialize control block */
+    memset(&bta_ar_cb, 0, sizeof(tBTA_AR_CB));
+}
+
+/*******************************************************************************
+**
+** Function         bta_ar_reg_avdt
+**
+** Description      This function is called to register to AVDTP.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_ar_avdt_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
+{
+    /* route the AVDT registration callback to av or avk */
+    if (bta_ar_cb.p_av_conn_cback)
+        (*bta_ar_cb.p_av_conn_cback)(handle, bd_addr, event, p_data);
+    if (bta_ar_cb.p_avk_conn_cback)
+        (*bta_ar_cb.p_avk_conn_cback)(handle, bd_addr, event, p_data);
+}
+
+/*******************************************************************************
+**
+** Function         bta_ar_reg_avdt
+**
+** Description      AR module registration to AVDT.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_ar_reg_avdt(tAVDT_REG *p_reg, tAVDT_CTRL_CBACK *p_cback, tBTA_SYS_ID sys_id)
+{
+    UINT8   mask = 0;
+
+    if (sys_id == BTA_ID_AV)
+    {
+        bta_ar_cb.p_av_conn_cback = p_cback;
+        mask = BTA_AR_AV_MASK;
+    }
+    else if (sys_id == BTA_ID_AVK)
+    {
+        bta_ar_cb.p_avk_conn_cback = p_cback;
+        mask = BTA_AR_AVK_MASK;
+    }
+#if (BTA_AR_DEBUG == TRUE)
+    else
+    {
+        APPL_TRACE_ERROR("bta_ar_reg_avdt: the registration is from wrong sys_id:%d", sys_id);
+    }
+#endif
+
+    if (mask)
+    {
+        if (bta_ar_cb.avdt_registered == 0)
+        {
+            AVDT_Register(p_reg, bta_ar_avdt_cback);
+        }
+        bta_ar_cb.avdt_registered |= mask;
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_ar_dereg_avdt
+**
+** Description      This function is called to de-register from AVDTP.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_ar_dereg_avdt(tBTA_SYS_ID sys_id)
+{
+    UINT8   mask = 0;
+
+    if (sys_id == BTA_ID_AV)
+    {
+        bta_ar_cb.p_av_conn_cback = NULL;
+        mask = BTA_AR_AV_MASK;
+    }
+    else if (sys_id == BTA_ID_AVK)
+    {
+        bta_ar_cb.p_avk_conn_cback = NULL;
+        mask = BTA_AR_AVK_MASK;
+    }
+    bta_ar_cb.avdt_registered &= ~mask;
+
+    if (bta_ar_cb.avdt_registered == 0)
+        AVDT_Deregister();
+}
+
+/*******************************************************************************
+**
+** Function         bta_ar_avdt_conn
+**
+** Description      This function is called to let ar know that some AVDTP profile
+**                  is connected for this sys_id.
+**                  If the other sys modules started a timer for PENDING_EVT,
+**                  the timer can be stopped now.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_ar_avdt_conn(tBTA_SYS_ID sys_id, BD_ADDR bd_addr)
+{
+    UINT8       event = BTA_AR_AVDT_CONN_EVT;
+    tAVDT_CTRL  data;
+
+    if (sys_id == BTA_ID_AV)
+    {
+        if (bta_ar_cb.p_avk_conn_cback)
+        {
+            (*bta_ar_cb.p_avk_conn_cback)(0, bd_addr, event, &data);
+        }
+    }
+    else if (sys_id == BTA_ID_AVK)
+    {
+        if (bta_ar_cb.p_av_conn_cback)
+        {
+            (*bta_ar_cb.p_av_conn_cback)(0, bd_addr, event, &data);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_ar_reg_avct
+**
+** Description      This function is called to register to AVCTP.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_ar_reg_avct(UINT16 mtu, UINT16 mtu_br, UINT8 sec_mask, tBTA_SYS_ID sys_id)
+{
+    UINT8   mask = bta_ar_id (sys_id);
+
+    if (mask)
+    {
+        if (bta_ar_cb.avct_registered == 0)
+        {
+            AVCT_Register(mtu, mtu_br, sec_mask);
+        }
+        bta_ar_cb.avct_registered |= mask;
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_ar_dereg_avct
+**
+** Description      This function is called to deregister from AVCTP.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_ar_dereg_avct(tBTA_SYS_ID sys_id)
+{
+    UINT8   mask = bta_ar_id (sys_id);
+
+    bta_ar_cb.avct_registered &= ~mask;
+
+    if (bta_ar_cb.avct_registered == 0)
+        AVCT_Deregister();
+}
+
+/******************************************************************************
+**
+** Function         bta_ar_reg_avrc
+**
+** Description      This function is called to register an SDP record for AVRCP.
+**
+** Returns          void
+**
+******************************************************************************/
+void bta_ar_reg_avrc(UINT16 service_uuid, char *service_name, char *provider_name,
+                                        UINT16 categories, tBTA_SYS_ID sys_id)
+{
+    UINT8   mask = bta_ar_id (sys_id);
+    UINT8   temp[8], *p;
+
+    if (!mask || !categories)
+        return;
+
+    if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET)
+    {
+        if (bta_ar_cb.sdp_tg_handle == 0)
+        {
+            bta_ar_cb.tg_registered = mask;
+            bta_ar_cb.sdp_tg_handle = SDP_CreateRecord();
+            AVRC_AddRecord(service_uuid, service_name, provider_name, categories, bta_ar_cb.sdp_tg_handle);
+            bta_sys_add_uuid(service_uuid);
+        }
+        /* only one TG is allowed (first-come, first-served).
+         * If sdp_tg_handle is non-0, ignore this request */
+    }
+    else if ((service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL) || (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_CONTROL))
+    {
+        bta_ar_cb.ct_categories [mask - 1] = categories;
+        categories = bta_ar_cb.ct_categories[0]|bta_ar_cb.ct_categories[1];
+        if (bta_ar_cb.sdp_ct_handle == 0)
+        {
+            bta_ar_cb.sdp_ct_handle = SDP_CreateRecord();
+            AVRC_AddRecord(service_uuid, service_name, provider_name, categories, bta_ar_cb.sdp_ct_handle);
+            bta_sys_add_uuid(service_uuid);
+        }
+        else
+        {
+            /* multiple CTs are allowed.
+             * Change supported categories on the second one */
+            p = temp;
+            UINT16_TO_BE_STREAM(p, categories);
+            SDP_AddAttribute(bta_ar_cb.sdp_ct_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE,
+                      (UINT32)2, (UINT8*)temp);
+        }
+    }
+}
+
+/******************************************************************************
+**
+** Function         bta_ar_dereg_avrc
+**
+** Description      This function is called to de-register/delete an SDP record for AVRCP.
+**
+** Returns          void
+**
+******************************************************************************/
+void bta_ar_dereg_avrc(UINT16 service_uuid, tBTA_SYS_ID sys_id)
+{
+    UINT8   mask = bta_ar_id (sys_id);
+    UINT16  categories = 0;
+    UINT8   temp[8], *p;
+
+    if (!mask)
+        return;
+
+    if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET)
+    {
+        if (bta_ar_cb.sdp_tg_handle && mask == bta_ar_cb.tg_registered)
+        {
+            bta_ar_cb.tg_registered = 0;
+            SDP_DeleteRecord(bta_ar_cb.sdp_tg_handle);
+            bta_ar_cb.sdp_tg_handle = 0;
+            bta_sys_remove_uuid(service_uuid);
+        }
+    }
+    else if (service_uuid == UUID_SERVCLASS_AV_REMOTE_CONTROL)
+    {
+        if (bta_ar_cb.sdp_ct_handle)
+        {
+            bta_ar_cb.ct_categories [mask - 1] = 0;
+            categories = bta_ar_cb.ct_categories[0]|bta_ar_cb.ct_categories[1];
+            if (!categories)
+            {
+                /* no CT is still registered - cleaup */
+                SDP_DeleteRecord(bta_ar_cb.sdp_ct_handle);
+                bta_ar_cb.sdp_ct_handle = 0;
+                bta_sys_remove_uuid(service_uuid);
+            }
+            else
+            {
+                /* change supported categories to the remaning one */
+                p = temp;
+                UINT16_TO_BE_STREAM(p, categories);
+                SDP_AddAttribute(bta_ar_cb.sdp_ct_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE,
+                          (UINT32)2, (UINT8*)temp);
+            }
+        }
+    }
+
+}
diff --git a/components/bt/bluedroid/bta/ar/bta_ar_int.h b/components/bt/bluedroid/bta/ar/bta_ar_int.h
new file mode 100755 (executable)
index 0000000..d230448
--- /dev/null
@@ -0,0 +1,64 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2008-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This is the private interface file for the BTA audio/video registration
+ *  module.
+ *
+ ******************************************************************************/
+#ifndef BTA_AR_INT_H
+#define BTA_AR_INT_H
+
+#include "bta_av_api.h"
+
+
+#ifndef BTA_AR_DEBUG
+#define BTA_AR_DEBUG    FALSE
+#endif
+
+#define BTA_AR_AV_MASK      0x01
+#define BTA_AR_AVK_MASK     0x02
+
+/* data associated with BTA_AR */
+typedef struct
+{
+    tAVDT_CTRL_CBACK *p_av_conn_cback;       /* av connection callback function */
+    tAVDT_CTRL_CBACK *p_avk_conn_cback;      /* avk connection callback function */
+    UINT8           avdt_registered;
+    UINT8           avct_registered;
+       UINT32          sdp_tg_handle;
+       UINT32          sdp_ct_handle;
+    UINT16          ct_categories[2];
+    UINT8           tg_registered;
+    tBTA_AV_HNDL    hndl;       /* Handle associated with the stream that rejected the connection. */
+} tBTA_AR_CB;
+
+/*****************************************************************************
+**  Global data
+*****************************************************************************/
+
+/* control block declaration */
+#if BTA_DYNAMIC_MEMORY == FALSE
+extern tBTA_AR_CB bta_ar_cb;
+#else
+extern tBTA_AR_CB *bta_ar_cb_ptr;
+#define bta_ar_cb (*bta_ar_cb_ptr)
+#endif
+
+#endif /* BTA_AR_INT_H */
diff --git a/components/bt/bluedroid/bta/av/bta_av_aact.c b/components/bt/bluedroid/bta/av/bta_av_aact.c
new file mode 100755 (executable)
index 0000000..4177104
--- /dev/null
@@ -0,0 +1,3107 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2004-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This file contains action functions for advanced audio/video stream
+ *  state machine. these functions are shared by both audio and video
+ *  streams.
+ *
+ ******************************************************************************/
+
+#include "bt_target.h"
+#if defined(BTA_AV_INCLUDED) && (BTA_AV_INCLUDED == TRUE)
+
+// #include <assert.h>
+#include "bt_trace.h"
+#include <string.h>
+
+// #include <cutils/properties.h>
+
+#include "bta_av_int.h"
+#include "avdt_api.h"
+#include "utl.h"
+#include "l2c_api.h"
+#include "l2cdefs.h"
+#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
+#include "bta_ar_api.h"
+#endif
+
+/*****************************************************************************
+**  Constants
+*****************************************************************************/
+
+/* the delay time in milliseconds to start service discovery on AVRCP */
+#ifndef BTA_AV_RC_DISC_TIME_VAL
+#define BTA_AV_RC_DISC_TIME_VAL     3500
+#endif
+
+/* the timer in milliseconds to guard against link busy and AVDT_CloseReq failed to be sent */
+#ifndef BTA_AV_CLOSE_REQ_TIME_VAL
+#define BTA_AV_CLOSE_REQ_TIME_VAL   4000
+#endif
+
+/* number to retry on reconfigure failure - some headsets requirs this number to be more than 1 */
+#ifndef BTA_AV_RECONFIG_RETRY
+#define BTA_AV_RECONFIG_RETRY       6
+#endif
+
+static void bta_av_st_rc_timer(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+
+/* state machine states */
+enum
+{
+    BTA_AV_INIT_SST,
+    BTA_AV_INCOMING_SST,
+    BTA_AV_OPENING_SST,
+    BTA_AV_OPEN_SST,
+    BTA_AV_RCFG_SST,
+    BTA_AV_CLOSING_SST
+};
+
+
+/* the call out functions for audio stream */
+const tBTA_AV_CO_FUNCTS bta_av_a2d_cos =
+{
+    bta_av_co_audio_init,
+    bta_av_co_audio_disc_res,
+    bta_av_co_audio_getconfig,
+    bta_av_co_audio_setconfig,
+    bta_av_co_audio_open,
+    bta_av_co_audio_close,
+    bta_av_co_audio_start,
+    bta_av_co_audio_stop,
+    bta_av_co_audio_src_data_path,
+    bta_av_co_audio_delay
+};
+
+/* ssm action functions for audio stream */
+const tBTA_AV_SACT bta_av_a2d_action[] =
+{
+    bta_av_do_disc_a2d,     /* BTA_AV_DO_DISC  */
+    bta_av_cleanup,         /* BTA_AV_CLEANUP */
+    bta_av_free_sdb,        /* BTA_AV_FREE_SDB */
+    bta_av_config_ind,      /* BTA_AV_CONFIG_IND */
+    bta_av_disconnect_req,  /* BTA_AV_DISCONNECT_REQ */
+    bta_av_security_req,    /* BTA_AV_SECURITY_REQ */
+    bta_av_security_rsp,    /* BTA_AV_SECURITY_RSP */
+    bta_av_setconfig_rsp,   /* BTA_AV_SETCONFIG_RSP */
+    bta_av_st_rc_timer,     /* BTA_AV_ST_RC_TIMER */
+    bta_av_str_opened,      /* BTA_AV_STR_OPENED */
+    bta_av_security_ind,    /* BTA_AV_SECURITY_IND */
+    bta_av_security_cfm,    /* BTA_AV_SECURITY_CFM */
+    bta_av_do_close,        /* BTA_AV_DO_CLOSE */
+    bta_av_connect_req,     /* BTA_AV_CONNECT_REQ */
+    bta_av_sdp_failed,      /* BTA_AV_SDP_FAILED */
+    bta_av_disc_results,    /* BTA_AV_DISC_RESULTS */
+    bta_av_disc_res_as_acp, /* BTA_AV_DISC_RES_AS_ACP */
+    bta_av_open_failed,     /* BTA_AV_OPEN_FAILED */
+    bta_av_getcap_results,  /* BTA_AV_GETCAP_RESULTS */
+    bta_av_setconfig_rej,   /* BTA_AV_SETCONFIG_REJ */
+    bta_av_discover_req,    /* BTA_AV_DISCOVER_REQ */
+    bta_av_conn_failed,     /* BTA_AV_CONN_FAILED */
+    bta_av_do_start,        /* BTA_AV_DO_START */
+    bta_av_str_stopped,     /* BTA_AV_STR_STOPPED */
+    bta_av_reconfig,        /* BTA_AV_RECONFIG */
+    bta_av_data_path,       /* BTA_AV_DATA_PATH */
+    bta_av_start_ok,        /* BTA_AV_START_OK */
+    bta_av_start_failed,    /* BTA_AV_START_FAILED */
+    bta_av_str_closed,      /* BTA_AV_STR_CLOSED */
+    bta_av_clr_cong,        /* BTA_AV_CLR_CONG */
+    bta_av_suspend_cfm,     /* BTA_AV_SUSPEND_CFM */
+    bta_av_rcfg_str_ok,     /* BTA_AV_RCFG_STR_OK */
+    bta_av_rcfg_failed,     /* BTA_AV_RCFG_FAILED */
+    bta_av_rcfg_connect,    /* BTA_AV_RCFG_CONNECT */
+    bta_av_rcfg_discntd,    /* BTA_AV_RCFG_DISCNTD */
+    bta_av_suspend_cont,    /* BTA_AV_SUSPEND_CONT */
+    bta_av_rcfg_cfm,        /* BTA_AV_RCFG_CFM */
+    bta_av_rcfg_open,       /* BTA_AV_RCFG_OPEN */
+    bta_av_security_rej,    /* BTA_AV_SECURITY_REJ */
+    bta_av_open_rc,         /* BTA_AV_OPEN_RC */
+    bta_av_chk_2nd_start,   /* BTA_AV_CHK_2ND_START */
+    bta_av_save_caps,       /* BTA_AV_SAVE_CAPS */
+    bta_av_set_use_rc,      /* BTA_AV_SET_USE_RC */
+    bta_av_cco_close,       /* BTA_AV_CCO_CLOSE */
+    bta_av_switch_role,     /* BTA_AV_SWITCH_ROLE */
+    bta_av_role_res,        /* BTA_AV_ROLE_RES */
+    bta_av_delay_co,        /* BTA_AV_DELAY_CO */
+    bta_av_open_at_inc,     /* BTA_AV_OPEN_AT_INC */
+    NULL
+};
+
+/* these tables translate AVDT events to SSM events */
+static const UINT16 bta_av_stream_evt_ok[] = {
+    BTA_AV_STR_DISC_OK_EVT,         /* AVDT_DISCOVER_CFM_EVT */
+    BTA_AV_STR_GETCAP_OK_EVT,       /* AVDT_GETCAP_CFM_EVT */
+    BTA_AV_STR_OPEN_OK_EVT,         /* AVDT_OPEN_CFM_EVT */
+    BTA_AV_STR_OPEN_OK_EVT,         /* AVDT_OPEN_IND_EVT */
+    BTA_AV_STR_CONFIG_IND_EVT,      /* AVDT_CONFIG_IND_EVT */
+    BTA_AV_STR_START_OK_EVT,        /* AVDT_START_CFM_EVT */
+    BTA_AV_STR_START_OK_EVT,        /* AVDT_START_IND_EVT */
+    BTA_AV_STR_SUSPEND_CFM_EVT,     /* AVDT_SUSPEND_CFM_EVT */
+    BTA_AV_STR_SUSPEND_CFM_EVT,     /* AVDT_SUSPEND_IND_EVT */
+    BTA_AV_STR_CLOSE_EVT,           /* AVDT_CLOSE_CFM_EVT */
+    BTA_AV_STR_CLOSE_EVT,           /* AVDT_CLOSE_IND_EVT */
+    BTA_AV_STR_RECONFIG_CFM_EVT,    /* AVDT_RECONFIG_CFM_EVT */
+    0,                              /* AVDT_RECONFIG_IND_EVT */
+    BTA_AV_STR_SECURITY_CFM_EVT,    /* AVDT_SECURITY_CFM_EVT */
+    BTA_AV_STR_SECURITY_IND_EVT,    /* AVDT_SECURITY_IND_EVT */
+    BTA_AV_STR_WRITE_CFM_EVT,       /* AVDT_WRITE_CFM_EVT */
+    BTA_AV_AVDT_CONNECT_EVT,        /* AVDT_CONNECT_IND_EVT */
+    BTA_AV_AVDT_DISCONNECT_EVT,     /* AVDT_DISCONNECT_IND_EVT */
+#if (AVDT_REPORTING == TRUE)
+    BTA_AV_AVDT_RPT_CONN_EVT,       /* AVDT_REPORT_CONN_EVT */
+    BTA_AV_AVDT_RPT_CONN_EVT,       /* AVDT_REPORT_DISCONN_EVT */
+#endif
+    BTA_AV_AVDT_DELAY_RPT_EVT,      /* AVDT_DELAY_REPORT_EVT */
+    0                               /* AVDT_DELAY_REPORT_CFM_EVT */
+};
+
+static const UINT16 bta_av_stream_evt_fail[] = {
+    BTA_AV_STR_DISC_FAIL_EVT,       /* AVDT_DISCOVER_CFM_EVT */
+    BTA_AV_STR_GETCAP_FAIL_EVT,     /* AVDT_GETCAP_CFM_EVT */
+    BTA_AV_STR_OPEN_FAIL_EVT,       /* AVDT_OPEN_CFM_EVT */
+    BTA_AV_STR_OPEN_OK_EVT,         /* AVDT_OPEN_IND_EVT */
+    BTA_AV_STR_CONFIG_IND_EVT,      /* AVDT_CONFIG_IND_EVT */
+    BTA_AV_STR_START_FAIL_EVT,      /* AVDT_START_CFM_EVT */
+    BTA_AV_STR_START_OK_EVT,        /* AVDT_START_IND_EVT */
+    BTA_AV_STR_SUSPEND_CFM_EVT,     /* AVDT_SUSPEND_CFM_EVT */
+    BTA_AV_STR_SUSPEND_CFM_EVT,     /* AVDT_SUSPEND_IND_EVT */
+    BTA_AV_STR_CLOSE_EVT,           /* AVDT_CLOSE_CFM_EVT */
+    BTA_AV_STR_CLOSE_EVT,           /* AVDT_CLOSE_IND_EVT */
+    BTA_AV_STR_RECONFIG_CFM_EVT,    /* AVDT_RECONFIG_CFM_EVT */
+    0,                              /* AVDT_RECONFIG_IND_EVT */
+    BTA_AV_STR_SECURITY_CFM_EVT,    /* AVDT_SECURITY_CFM_EVT */
+    BTA_AV_STR_SECURITY_IND_EVT,    /* AVDT_SECURITY_IND_EVT */
+    BTA_AV_STR_WRITE_CFM_EVT,       /* AVDT_WRITE_CFM_EVT */
+    BTA_AV_AVDT_CONNECT_EVT,        /* AVDT_CONNECT_IND_EVT */
+    BTA_AV_AVDT_DISCONNECT_EVT,     /* AVDT_DISCONNECT_IND_EVT */
+#if (AVDT_REPORTING == TRUE)
+    BTA_AV_AVDT_RPT_CONN_EVT,       /* AVDT_REPORT_CONN_EVT */
+    BTA_AV_AVDT_RPT_CONN_EVT,       /* AVDT_REPORT_DISCONN_EVT */
+#endif
+    BTA_AV_AVDT_DELAY_RPT_EVT,      /* AVDT_DELAY_REPORT_EVT */
+    0                               /* AVDT_DELAY_REPORT_CFM_EVT */
+};
+
+void bta_av_stream_data_cback(UINT8 handle, BT_HDR *p_pkt, UINT32 time_stamp, UINT8 m_pt);
+static void bta_av_stream0_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
+static void bta_av_stream1_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
+#if BTA_AV_NUM_STRS > 2
+static void bta_av_stream2_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
+#endif
+#if BTA_AV_NUM_STRS > 3
+static void bta_av_stream3_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
+#endif
+#if BTA_AV_NUM_STRS > 4
+static void bta_av_stream4_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
+#endif
+#if BTA_AV_NUM_STRS > 5
+static void bta_av_stream5_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
+#endif
+/* the array of callback functions to receive events from AVDT control channel */
+tAVDT_CTRL_CBACK * const bta_av_dt_cback[] =
+{
+    bta_av_stream0_cback
+    ,bta_av_stream1_cback
+#if BTA_AV_NUM_STRS > 2
+    ,bta_av_stream2_cback
+#endif
+#if BTA_AV_NUM_STRS > 3
+    ,bta_av_stream3_cback
+#endif
+#if BTA_AV_NUM_STRS > 4
+    ,bta_av_stream4_cback
+#endif
+#if BTA_AV_NUM_STRS > 5
+    ,bta_av_stream5_cback
+#endif
+};
+/***********************************************
+**
+** Function         bta_get_scb_handle
+**
+** Description      gives the registered AVDT handle.by checking with sep_type.
+**
+**
+** Returns          void
+***********************************************/
+static UINT8 bta_av_get_scb_handle(tBTA_AV_SCB *p_scb, UINT8 local_sep)
+{
+    UINT8 xx =0;
+    for (xx = 0; xx<BTA_AV_MAX_SEPS; xx++)
+    {
+        if ((p_scb->seps[xx].tsep == local_sep) &&
+            (p_scb->seps[xx].codec_type == p_scb->codec_type))
+            return (p_scb->seps[xx].av_handle);
+    }
+    APPL_TRACE_DEBUG(" bta_av_get_scb_handle appropiate sep_type not found")
+    return 0; /* return invalid handle */
+}
+
+/***********************************************
+**
+** Function         bta_av_get_scb_sep_type
+**
+** Description      gives the sep type by cross-checking with AVDT handle
+**
+**
+** Returns          void
+***********************************************/
+static UINT8 bta_av_get_scb_sep_type(tBTA_AV_SCB *p_scb, UINT8 tavdt_handle)
+{
+    UINT8 xx =0;
+    for (xx = 0; xx<BTA_AV_MAX_SEPS; xx++)
+    {
+        if (p_scb->seps[xx].av_handle == tavdt_handle)
+            return (p_scb->seps[xx].tsep);
+    }
+    APPL_TRACE_DEBUG(" bta_av_get_scb_sep_type appropiate handle not found")
+    return 3; /* return invalid sep type */
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_save_addr
+**
+** Description      copy the bd_addr and maybe reset the supported flags
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_save_addr(tBTA_AV_SCB *p_scb, const BD_ADDR b)
+{
+    APPL_TRACE_DEBUG("bta_av_save_addr r:%d, s:%d",
+        p_scb->recfg_sup, p_scb->suspend_sup);
+    if(bdcmp(p_scb->peer_addr, b) != 0)
+    {
+        APPL_TRACE_ERROR("reset flags");
+        /* a new addr, reset the supported flags */
+        p_scb->recfg_sup    = TRUE;
+        p_scb->suspend_sup  = TRUE;
+    }
+
+    /* do this copy anyway, just in case the first addr matches
+     * the control block one by accident */
+    bdcpy(p_scb->peer_addr, b);
+}
+
+/*******************************************************************************
+**
+** Function         notify_start_failed
+**
+** Description      notify up-layer AV start failed
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+static void notify_start_failed(tBTA_AV_SCB *p_scb)
+{
+    tBTA_AV_START   start;
+    /* if start failed, clear role */
+    p_scb->role &= ~BTA_AV_ROLE_START_INT;
+    start.chnl   = p_scb->chnl;
+    start.status = BTA_AV_FAIL;
+    start.initiator = TRUE;
+    start.hndl   = p_scb->hndl;
+    (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_st_rc_timer
+**
+** Description      start the AVRC timer if no RC connection & CT is supported &
+**                  RC is used or
+**                  as ACP (we do not really know if we want AVRC)
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_st_rc_timer(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UNUSED(p_data);
+
+    APPL_TRACE_DEBUG("bta_av_st_rc_timer rc_handle:%d, use_rc: %d",
+        p_scb->rc_handle, p_scb->use_rc);
+    /* for outgoing RC connection as INT/CT */
+    if( (p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE) &&
+        /*(bta_av_cb.features & BTA_AV_FEAT_RCCT) &&*/
+        (p_scb->use_rc == TRUE || (p_scb->role & BTA_AV_ROLE_AD_ACP)) )
+    {
+        if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0)
+            bta_sys_start_timer(&p_scb->timer, BTA_AV_AVRC_TIMER_EVT, BTA_AV_RC_DISC_TIME_VAL);
+        else
+            p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
+    }
+
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_next_getcap
+**
+** Description      The function gets the capabilities of the next available
+**                  stream found in the discovery results.
+**
+** Returns          TRUE if we sent request to AVDT, FALSE otherwise.
+**
+*******************************************************************************/
+static BOOLEAN bta_av_next_getcap(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    int     i;
+    tAVDT_GETCAP_REQ    *p_req;
+    BOOLEAN     sent_cmd = FALSE;
+    UINT16 uuid_int = p_scb->uuid_int;
+    UINT8 sep_requested = 0;
+
+    if(uuid_int == UUID_SERVCLASS_AUDIO_SOURCE)
+       sep_requested = AVDT_TSEP_SNK;
+    else if(uuid_int == UUID_SERVCLASS_AUDIO_SINK)
+       sep_requested = AVDT_TSEP_SRC;
+
+    for (i = p_scb->sep_info_idx; i < p_scb->num_seps; i++)
+    {
+        /* steam not in use, is a sink, and is the right media type (audio/video) */
+        if ((p_scb->sep_info[i].in_use == FALSE) &&
+            (p_scb->sep_info[i].tsep == sep_requested) &&
+            (p_scb->sep_info[i].media_type == p_scb->media_type))
+        {
+            p_scb->sep_info_idx = i;
+
+            /* we got a stream; get its capabilities */
+            if (p_scb->p_cap == NULL)
+            {
+                p_scb->p_cap = (tAVDT_CFG *) GKI_getbuf(sizeof(tAVDT_CFG));
+            }
+            if (p_scb->p_cap == NULL)
+            {
+                i = p_scb->num_seps;
+                break;
+            }
+            if (p_scb->avdt_version >= AVDT_VERSION_SYNC)
+            {
+                p_req = AVDT_GetAllCapReq;
+            }
+            else
+            {
+                p_req = AVDT_GetCapReq;
+            }
+            (*p_req)(p_scb->peer_addr,
+                           p_scb->sep_info[i].seid,
+                           p_scb->p_cap, bta_av_dt_cback[p_scb->hdi]);
+            sent_cmd = TRUE;
+            break;
+        }
+    }
+
+    /* if no streams available then stream open fails */
+    if (!sent_cmd)
+    {
+        bta_av_ssm_execute(p_scb, BTA_AV_STR_GETCAP_FAIL_EVT, p_data);
+    }
+
+    return sent_cmd;
+
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_proc_stream_evt
+**
+** Description      Utility function to compose stream events.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_proc_stream_evt(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data, int index)
+{
+    tBTA_AV_STR_MSG     *p_msg;
+    UINT16              sec_len = 0;
+    tBTA_AV_SCB         *p_scb = bta_av_cb.p_scb[index];
+    int                 xx;
+
+    if (p_data)
+    {
+        if (event == AVDT_SECURITY_IND_EVT)
+        {
+            sec_len = (p_data->security_ind.len < BTA_AV_SECURITY_MAX_LEN) ?
+                       p_data->security_ind.len : BTA_AV_SECURITY_MAX_LEN;
+        }
+        else if (event == AVDT_SECURITY_CFM_EVT && p_data->hdr.err_code == 0)
+        {
+            sec_len = (p_data->security_cfm.len < BTA_AV_SECURITY_MAX_LEN) ?
+                       p_data->security_cfm.len : BTA_AV_SECURITY_MAX_LEN;
+        }
+    }
+
+    if (p_scb && (p_msg = (tBTA_AV_STR_MSG *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_STR_MSG) + sec_len))) != NULL)
+    {
+
+        /* copy event data, bd addr, and handle to event message buffer */
+        p_msg->hdr.offset = 0;
+
+        if (bd_addr != NULL)
+        {
+            bdcpy(p_msg->bd_addr, bd_addr);
+            APPL_TRACE_DEBUG("  bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
+                          bd_addr[0], bd_addr[1],
+                          bd_addr[2], bd_addr[3],
+                          bd_addr[4], bd_addr[5]);
+        }
+
+        if (p_data != NULL)
+        {
+            memcpy(&p_msg->msg, p_data, sizeof (tAVDT_CTRL));
+            /* copy config params to event message buffer */
+            switch (event)
+            {
+            case AVDT_RECONFIG_CFM_EVT:
+            APPL_TRACE_DEBUG("reconfig cfm event codec info = 0x%06x-%06x-%06x-%02x",
+                (p_msg->msg.reconfig_cfm.p_cfg->codec_info[0]<<16)+(p_msg->msg.reconfig_cfm.p_cfg->codec_info[1]<<8)+p_msg->msg.reconfig_cfm.p_cfg->codec_info[2],
+                (p_msg->msg.reconfig_cfm.p_cfg->codec_info[3]<<16)+(p_msg->msg.reconfig_cfm.p_cfg->codec_info[4]<<8)+p_msg->msg.reconfig_cfm.p_cfg->codec_info[5],
+                (p_msg->msg.reconfig_cfm.p_cfg->codec_info[6]<<16)+(p_msg->msg.reconfig_cfm.p_cfg->codec_info[7]<<8)+p_msg->msg.reconfig_cfm.p_cfg->codec_info[8],
+                p_msg->msg.reconfig_cfm.p_cfg->codec_info[9]);
+            break;
+
+
+
+            case AVDT_CONFIG_IND_EVT:
+            /* We might have 2 SEP signallings(A2DP + VDP) with one peer device on one L2CAP.
+             * If we already have a signalling connection with the bd_addr and the streaming
+             * SST is at INIT state, change it to INCOMING state to handle the signalling
+             * from the 2nd SEP.                                                                */
+            if ((bta_av_find_lcb(bd_addr, BTA_AV_LCB_FIND) != NULL) && (bta_av_is_scb_init(p_scb)))
+            {
+                bta_av_set_scb_sst_incoming (p_scb);
+
+                /* When ACP_CONNECT_EVT was received, we put first available scb to incoming state.
+                 * Later when we receive AVDT_CONFIG_IND_EVT, we use a new p_scb and set its state to
+                 * incoming which we do it above.
+                 * We also have to set the old p_scb state to init to be used later             */
+                for (xx = 0; xx < BTA_AV_NUM_STRS; xx++)
+                {
+                    if ((bta_av_cb.p_scb[xx]) && (xx != index))
+                    {
+                        if (bta_av_cb.p_scb[xx]->state == BTA_AV_INCOMING_SST)
+                        {
+                            bta_av_cb.p_scb[xx]->state = BTA_AV_INIT_SST;
+                            bta_av_cb.p_scb[xx]->coll_mask = 0;
+                            break;
+                        }
+                    }
+                }
+            }
+
+            memcpy(&p_msg->cfg, p_data->config_ind.p_cfg, sizeof(tAVDT_CFG));
+            break;
+
+            case AVDT_SECURITY_IND_EVT:
+                p_msg->msg.security_ind.p_data = (UINT8 *) (p_msg + 1);
+                memcpy(p_msg->msg.security_ind.p_data, p_data->security_ind.p_data, sec_len);
+                break;
+
+            case AVDT_SECURITY_CFM_EVT:
+                p_msg->msg.security_cfm.p_data = (UINT8 *) (p_msg + 1);
+                if (p_data->hdr.err_code == 0)
+                {
+                    memcpy(p_msg->msg.security_cfm.p_data, p_data->security_cfm.p_data, sec_len);
+                }
+                break;
+            case AVDT_SUSPEND_IND_EVT:
+                    p_msg->msg.hdr.err_code = 0;
+                break;
+
+            default:
+                break;
+            }
+        }
+        else
+            p_msg->msg.hdr.err_code = 0;
+
+        /* look up application event */
+        if ((p_data == NULL) || (p_data->hdr.err_code == 0))
+        {
+            p_msg->hdr.event = bta_av_stream_evt_ok[event];
+        }
+        else
+        {
+            p_msg->hdr.event = bta_av_stream_evt_fail[event];
+        }
+
+        p_msg->initiator = FALSE;
+        if (event == AVDT_SUSPEND_CFM_EVT)
+            p_msg->initiator = TRUE;
+
+        APPL_TRACE_VERBOSE("hndl:x%x", p_scb->hndl);
+        p_msg->hdr.layer_specific = p_scb->hndl;
+        p_msg->handle   = handle;
+        p_msg->avdt_event = event;
+        bta_sys_sendmsg(p_msg);
+    }
+
+/* coverity[var_deref_model] */
+/* false-positive: bta_av_conn_cback only processes AVDT_CONNECT_IND_EVT and AVDT_DISCONNECT_IND_EVT event
+ *                 these 2 events always have associated p_data */
+    if (p_data)
+    {
+        bta_av_conn_cback(handle, bd_addr, event, p_data);
+    }
+    else
+    {
+        APPL_TRACE_ERROR("%s: p_data is null", __func__);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_stream_data_cback
+**
+** Description      This is the AVDTP callback function for stream events.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_stream_data_cback(UINT8 handle, BT_HDR *p_pkt, UINT32 time_stamp, UINT8 m_pt)
+{
+    int index = 0;
+    tBTA_AV_SCB         *p_scb ;
+    APPL_TRACE_DEBUG("bta_av_stream_data_cback avdt_handle: %d pkt_len=0x%x  ofst = 0x%x", handle,p_pkt->len,p_pkt->offset);
+    APPL_TRACE_DEBUG(" Number of frames 0x%x",*((UINT8*)(p_pkt + 1) + p_pkt->offset));
+    APPL_TRACE_DEBUG("Sequence Number 0x%x",p_pkt->layer_specific);
+    /* Get  SCB  and correct sep type*/
+    for(index = 0; index < BTA_AV_NUM_STRS;index ++ )
+    {
+        p_scb = bta_av_cb.p_scb[index];
+        if((p_scb->avdt_handle == handle)&&(p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK))
+            break;
+    }
+    if(index == BTA_AV_NUM_STRS) /* cannot find correct handler */
+    {
+        GKI_freebuf(p_pkt);
+        return;
+    }
+    p_pkt->event = BTA_AV_MEDIA_DATA_EVT;
+    p_scb->seps[p_scb->sep_idx].p_app_data_cback(BTA_AV_MEDIA_DATA_EVT, (tBTA_AV_MEDIA*)p_pkt);
+    GKI_freebuf(p_pkt);  /* a copy of packet had been delivered, we free this buffer */
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_stream0_cback
+**
+** Description      This is the AVDTP callback function for stream events.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_stream0_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
+{
+    APPL_TRACE_VERBOSE("bta_av_stream0_cback avdt_handle: %d event=0x%x", handle, event);
+    bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 0);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_stream1_cback
+**
+** Description      This is the AVDTP callback function for stream events.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_stream1_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
+{
+    APPL_TRACE_EVENT("bta_av_stream1_cback avdt_handle: %d event=0x%x", handle, event);
+    bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 1);
+}
+
+#if BTA_AV_NUM_STRS > 2
+/*******************************************************************************
+**
+** Function         bta_av_stream2_cback
+**
+** Description      This is the AVDTP callback function for stream events.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_stream2_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
+{
+    APPL_TRACE_EVENT("bta_av_stream2_cback avdt_handle: %d event=0x%x", handle, event);
+    bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 2);
+}
+#endif
+
+#if BTA_AV_NUM_STRS > 3
+/*******************************************************************************
+**
+** Function         bta_av_stream3_cback
+**
+** Description      This is the AVDTP callback function for stream events.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_stream3_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
+{
+    APPL_TRACE_EVENT("bta_av_stream3_cback avdt_handle: %d event=0x%x", handle, event);
+    bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 3);
+}
+#endif
+
+/*******************************************************************************
+**
+** Function         bta_av_stream4_cback
+**
+** Description      This is the AVDTP callback function for stream events.
+**
+** Returns          void
+**
+*******************************************************************************/
+#if BTA_AV_NUM_STRS > 4
+static void bta_av_stream4_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
+{
+    APPL_TRACE_EVENT("bta_av_stream4_cback avdt_handle: %d event=0x%x", handle, event);
+    bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 4);
+}
+#endif
+
+/*******************************************************************************
+**
+** Function         bta_av_stream5_cback
+**
+** Description      This is the AVDTP callback function for stream events.
+**
+** Returns          void
+**
+*******************************************************************************/
+#if BTA_AV_NUM_STRS > 5
+static void bta_av_stream5_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
+{
+    APPL_TRACE_EVENT("bta_av_stream5_cback avdt_handle: %d event=0x%x", handle, event);
+    bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 5);
+}
+#endif
+
+/*******************************************************************************
+**
+** Function         bta_av_a2d_sdp_cback
+**
+** Description      A2DP service discovery callback.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_a2d_sdp_cback(BOOLEAN found, tA2D_Service *p_service)
+{
+    tBTA_AV_SDP_RES *p_msg;
+    tBTA_AV_SCB     *p_scb;
+
+    if ((p_msg = (tBTA_AV_SDP_RES *) GKI_getbuf(sizeof(tBTA_AV_SDP_RES))) != NULL)
+    {
+        p_msg->hdr.event = (found) ? BTA_AV_SDP_DISC_OK_EVT : BTA_AV_SDP_DISC_FAIL_EVT;
+
+        p_scb = bta_av_hndl_to_scb(bta_av_cb.handle);
+        if (p_scb)
+        {
+            if (found && (p_service != NULL))
+                p_scb->avdt_version = p_service->avdt_version;
+            else
+                p_scb->avdt_version = 0x00;
+
+            p_msg->hdr.layer_specific = bta_av_cb.handle;
+            bta_sys_sendmsg(p_msg);
+        }
+        else
+        {
+            APPL_TRACE_ERROR ("bta_av_a2d_sdp_cback, no scb found for handle(0x%x)", bta_av_cb.handle);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_adjust_seps_idx
+**
+** Description      adjust the sep_idx
+**
+** Returns
+**
+*******************************************************************************/
+static void bta_av_adjust_seps_idx(tBTA_AV_SCB *p_scb, UINT8 avdt_handle)
+{
+    int xx;
+    APPL_TRACE_DEBUG("bta_av_adjust_seps_idx codec_type: %d", p_scb->codec_type);
+    for(xx=0; xx<BTA_AV_MAX_SEPS; xx++)
+    {
+        APPL_TRACE_DEBUG("av_handle: %d codec_type: %d",
+            p_scb->seps[xx].av_handle, p_scb->seps[xx].codec_type);
+        if((p_scb->seps[xx].av_handle && p_scb->codec_type == p_scb->seps[xx].codec_type)
+            && (p_scb->seps[xx].av_handle == avdt_handle))
+        {
+            p_scb->sep_idx      = xx;
+            p_scb->avdt_handle  = p_scb->seps[xx].av_handle;
+            break;
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_switch_role
+**
+** Description      Switch role was not started and a timer was started.
+**                  another attempt to switch role now - still opening.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_switch_role (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_RS_RES      switch_res = BTA_AV_RS_NONE;
+    tBTA_AV_API_OPEN  *p_buf = &p_scb->q_info.open;
+    UNUSED(p_data);
+
+    APPL_TRACE_DEBUG("bta_av_switch_role wait:x%x", p_scb->wait);
+    if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_START)
+        p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RETRY;
+
+    /* clear the masks set when the timer is started */
+    p_scb->wait &= ~(BTA_AV_WAIT_ROLE_SW_RES_OPEN|BTA_AV_WAIT_ROLE_SW_RES_START);
+
+    if (p_scb->q_tag == BTA_AV_Q_TAG_OPEN)
+    {
+        if (bta_av_switch_if_needed(p_scb) || !bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
+        {
+            p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
+        }
+        else
+        {
+            /* this should not happen in theory. Just in case...
+             * continue to do_disc_a2d */
+            switch_res = BTA_AV_RS_DONE;
+        }
+    }
+    else
+    {
+        /* report failure on OPEN */
+        switch_res = BTA_AV_RS_FAIL;
+    }
+
+    if (switch_res != BTA_AV_RS_NONE)
+    {
+        if (bta_av_cb.rs_idx == (p_scb->hdi + 1))
+        {
+            bta_av_cb.rs_idx = 0;
+        }
+        p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_RETRY;
+        p_scb->q_tag = 0;
+        p_buf->switch_res = switch_res;
+        bta_av_do_disc_a2d(p_scb, (tBTA_AV_DATA *)p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_role_res
+**
+** Description      Handle the role changed event
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_role_res (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    BOOLEAN         initiator = FALSE;
+    tBTA_AV_START   start;
+    tBTA_AV_OPEN    av_open;
+
+    APPL_TRACE_DEBUG("bta_av_role_res q_tag:%d, wait:x%x, role:x%x", p_scb->q_tag, p_scb->wait, p_scb->role);
+    if (p_scb->role & BTA_AV_ROLE_START_INT)
+        initiator = TRUE;
+
+    if (p_scb->q_tag == BTA_AV_Q_TAG_START)
+    {
+        if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_STARTED)
+        {
+            p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
+            if (p_data->role_res.hci_status != HCI_SUCCESS)
+            {
+                p_scb->role &= ~BTA_AV_ROLE_START_INT;
+                bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
+                /* start failed because of role switch. */
+                start.chnl   = p_scb->chnl;
+                start.status = BTA_AV_FAIL_ROLE;
+                start.hndl   = p_scb->hndl;
+                start.initiator = initiator;
+                (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
+            }
+            else
+            {
+                bta_av_start_ok(p_scb, p_data);
+            }
+        }
+        else if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_START)
+            p_scb->wait |= BTA_AV_WAIT_ROLE_SW_FAILED;
+    }
+    else if (p_scb->q_tag == BTA_AV_Q_TAG_OPEN)
+    {
+        if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_OPEN)
+        {
+            p_scb->role &= ~BTA_AV_ROLE_START_INT;
+            p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
+
+            if (p_data->role_res.hci_status != HCI_SUCCESS)
+            {
+                /* Open failed because of role switch. */
+                bdcpy(av_open.bd_addr, p_scb->peer_addr);
+                av_open.chnl   = p_scb->chnl;
+                av_open.hndl   = p_scb->hndl;
+                start.status = BTA_AV_FAIL_ROLE;
+                if(p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC )
+                    av_open.sep = AVDT_TSEP_SNK;
+                else if(p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK )
+                    av_open.sep = AVDT_TSEP_SRC;
+                (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV *)&av_open);
+            }
+            else
+            {
+                /* Continue av open process */
+                p_scb->q_info.open.switch_res = BTA_AV_RS_DONE;
+                bta_av_do_disc_a2d (p_scb, (tBTA_AV_DATA *)&(p_scb->q_info.open));
+            }
+        }
+        else
+        {
+            APPL_TRACE_WARNING ("Unexpected role switch event: q_tag = %d wait = %d", p_scb->q_tag, p_scb->wait);
+        }
+    }
+
+    APPL_TRACE_DEBUG("wait:x%x, role:x%x", p_scb->wait, p_scb->role);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_delay_co
+**
+** Description      Call the delay call-out function to report the delay report
+**                  from SNK
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_delay_co (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    p_scb->p_cos->delay(p_scb->hndl, p_data->str_msg.msg.delay_rpt_cmd.delay);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_do_disc_a2d
+**
+** Description      Do service discovery for A2DP.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_do_disc_a2d (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    BOOLEAN     ok_continue = FALSE;
+    tA2D_SDP_DB_PARAMS  db_params;
+    UINT16              attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
+                                       ATTR_ID_PROTOCOL_DESC_LIST,
+                                       ATTR_ID_BT_PROFILE_DESC_LIST};
+    UINT16 sdp_uuid = 0; /* UUID for which SDP has to be done */
+
+    APPL_TRACE_DEBUG("bta_av_do_disc_a2d use_rc: %d rs:%d, oc:%d",
+        p_data->api_open.use_rc, p_data->api_open.switch_res, bta_av_cb.audio_open_cnt);
+
+    memcpy (&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
+
+    switch(p_data->api_open.switch_res)
+    {
+    case BTA_AV_RS_NONE:
+        if (bta_av_switch_if_needed(p_scb) || !bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
+        {
+            /* waiting for role switch result. save the api to control block */
+            memcpy(&p_scb->q_info.open, &p_data->api_open, sizeof(tBTA_AV_API_OPEN));
+            p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
+            p_scb->q_tag = BTA_AV_Q_TAG_OPEN;
+        }
+        else
+        {
+            ok_continue = TRUE;
+        }
+        break;
+
+    case BTA_AV_RS_FAIL:
+        /* report a new failure event  */
+        p_scb->open_status = BTA_AV_FAIL_ROLE;
+        bta_av_ssm_execute(p_scb, BTA_AV_SDP_DISC_FAIL_EVT, NULL);
+        break;
+
+    case BTA_AV_RS_OK:
+        p_data = (tBTA_AV_DATA *)&p_scb->q_info.open;
+        /* continue to open if link role is ok */
+        if (bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
+        {
+            ok_continue = TRUE;
+        }
+        else
+        {
+            p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
+        }
+        break;
+
+    case BTA_AV_RS_DONE:
+        ok_continue = TRUE;
+        break;
+    }
+
+    APPL_TRACE_DEBUG("ok_continue: %d wait:x%x, q_tag: %d", ok_continue, p_scb->wait, p_scb->q_tag);
+    if (!ok_continue)
+        return;
+
+    /* clear the role switch bits */
+    p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
+
+    if (p_scb->wait & BTA_AV_WAIT_CHECK_RC)
+    {
+        p_scb->wait &= ~BTA_AV_WAIT_CHECK_RC;
+        bta_sys_start_timer(&p_scb->timer, BTA_AV_AVRC_TIMER_EVT, BTA_AV_RC_DISC_TIME_VAL);
+    }
+
+    if (bta_av_cb.features & BTA_AV_FEAT_MASTER)
+    {
+        L2CA_SetDesireRole(L2CAP_ROLE_DISALLOW_SWITCH);
+
+        if (bta_av_cb.audio_open_cnt == 1)
+        {
+            /* there's already an A2DP connection. do not allow switch */
+            bta_sys_clear_default_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH);
+        }
+    }
+    /* store peer addr other parameters */
+    bta_av_save_addr(p_scb, p_data->api_open.bd_addr);
+    p_scb->sec_mask = p_data->api_open.sec_mask;
+    p_scb->use_rc = p_data->api_open.use_rc;
+
+    bta_sys_app_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
+
+    /* allocate discovery database */
+    if (p_scb->p_disc_db == NULL)
+    {
+        p_scb->p_disc_db = (tSDP_DISCOVERY_DB *) GKI_getbuf(BTA_AV_DISC_BUF_SIZE);
+    }
+
+    /* only one A2D find service is active at a time */
+    bta_av_cb.handle = p_scb->hndl;
+
+    if(p_scb->p_disc_db)
+    {
+        /* set up parameters */
+        db_params.db_len = BTA_AV_DISC_BUF_SIZE;
+        db_params.num_attr = 3;
+        db_params.p_db = p_scb->p_disc_db;
+        db_params.p_attrs = attr_list;
+        p_scb->uuid_int = p_data->api_open.uuid;
+        if (p_scb->uuid_int == UUID_SERVCLASS_AUDIO_SINK)
+            sdp_uuid = UUID_SERVCLASS_AUDIO_SOURCE;
+        else if (p_scb->uuid_int == UUID_SERVCLASS_AUDIO_SOURCE)
+            sdp_uuid = UUID_SERVCLASS_AUDIO_SINK;
+
+        APPL_TRACE_DEBUG("uuid_int 0x%x, Doing SDP For 0x%x", p_scb->uuid_int, sdp_uuid);
+        if(A2D_FindService(sdp_uuid, p_scb->peer_addr, &db_params,
+                        bta_av_a2d_sdp_cback) == A2D_SUCCESS)
+        {
+            return;
+        }
+    }
+
+    /* when the code reaches here, either the DB is NULL
+     * or A2D_FindService is not successful */
+    bta_av_a2d_sdp_cback(FALSE, NULL);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_cleanup
+**
+** Description      cleanup AV stream control block.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_cleanup(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_CONN_CHG msg;
+    int             xx;
+    UINT8           role = BTA_AV_ROLE_AD_INT;
+    UNUSED(p_data);
+
+    APPL_TRACE_DEBUG("bta_av_cleanup");
+
+    /* free any buffers */
+    utl_freebuf((void **) &p_scb->p_cap);
+    utl_freebuf((void **) &p_scb->p_disc_db);
+    p_scb->avdt_version = 0;
+
+    /* initialize some control block variables */
+    p_scb->open_status = BTA_AV_SUCCESS;
+
+    /* if de-registering shut everything down */
+    msg.hdr.layer_specific  = p_scb->hndl;
+    p_scb->started  = FALSE;
+    p_scb->cong = FALSE;
+    p_scb->role = role;
+    p_scb->cur_psc_mask = 0;
+    p_scb->wait = 0;
+    p_scb->num_disc_snks = 0;
+    bta_sys_stop_timer(&p_scb->timer);
+    if (p_scb->deregistring)
+    {
+        /* remove stream */
+        for(xx=0; xx<BTA_AV_MAX_SEPS; xx++)
+        {
+            if(p_scb->seps[xx].av_handle)
+                AVDT_RemoveStream(p_scb->seps[xx].av_handle);
+            p_scb->seps[xx].av_handle = 0;
+        }
+
+        bta_av_dereg_comp((tBTA_AV_DATA *) &msg);
+    }
+    else
+    {
+        /* report stream closed to main SM */
+        msg.is_up = FALSE;
+        bdcpy(msg.peer_addr, p_scb->peer_addr);
+        bta_av_conn_chg((tBTA_AV_DATA *) &msg);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_free_sdb
+**
+** Description      Free service discovery db buffer.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_free_sdb(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UNUSED(p_data);
+    utl_freebuf((void **) &p_scb->p_disc_db);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_config_ind
+**
+** Description      Handle a stream configuration indication from the peer.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_config_ind (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_CI_SETCONFIG setconfig;
+    tAVDT_SEP_INFO       *p_info;
+    tAVDT_CFG            *p_evt_cfg = &p_data->str_msg.cfg;
+    UINT8   psc_mask = (p_evt_cfg->psc_mask | p_scb->cfg.psc_mask);
+    UINT8 local_sep;    /* sep type of local handle on which connection was received */
+    tBTA_AV_STR_MSG  *p_msg = (tBTA_AV_STR_MSG *)p_data;
+    UNUSED(p_data);
+
+    local_sep = bta_av_get_scb_sep_type(p_scb, p_msg->handle);
+    p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
+    memcpy(p_scb->cfg.codec_info, p_evt_cfg->codec_info, AVDT_CODEC_SIZE);
+    p_scb->codec_type = p_evt_cfg->codec_info[BTA_AV_CODEC_TYPE_IDX];
+    bta_av_save_addr(p_scb, p_data->str_msg.bd_addr);
+
+    /* Clear collision mask */
+    p_scb->coll_mask = 0;
+    bta_sys_stop_timer(&bta_av_cb.acp_sig_tmr);
+
+    /* if no codec parameters in configuration, fail */
+    if ((p_evt_cfg->num_codec == 0) ||
+    /* or the peer requests for a service we do not support */
+        ((psc_mask != p_scb->cfg.psc_mask) &&
+        (psc_mask != (p_scb->cfg.psc_mask&~AVDT_PSC_DELAY_RPT))) )
+    {
+        setconfig.hndl      = p_scb->hndl; /* we may not need this */
+        setconfig.err_code  = AVDT_ERR_UNSUP_CFG;
+        bta_av_ssm_execute(p_scb, BTA_AV_CI_SETCONFIG_FAIL_EVT, (tBTA_AV_DATA *) &setconfig);
+    }
+    else
+    {
+        p_info = &p_scb->sep_info[0];
+        p_info->in_use = 0;
+        p_info->media_type = p_scb->media_type;
+        p_info->seid = p_data->str_msg.msg.config_ind.int_seid;
+
+        /* Sep type of Peer will be oppsite role to our local sep */
+        if (local_sep == AVDT_TSEP_SRC)
+            p_info->tsep = AVDT_TSEP_SNK;
+        else if (local_sep == AVDT_TSEP_SNK)
+            p_info->tsep = AVDT_TSEP_SRC;
+
+        p_scb->role      |= BTA_AV_ROLE_AD_ACP;
+        p_scb->cur_psc_mask = p_evt_cfg->psc_mask;
+        if (bta_av_cb.features & BTA_AV_FEAT_RCTG)
+            p_scb->use_rc = TRUE;
+        else
+            p_scb->use_rc = FALSE;
+
+        p_scb->num_seps  = 1;
+        p_scb->sep_info_idx = 0;
+        APPL_TRACE_DEBUG("bta_av_config_ind: SEID: %d use_rc: %d cur_psc_mask:0x%x", p_info->seid, p_scb->use_rc, p_scb->cur_psc_mask);
+        /*  in case of A2DP SINK this is the first time peer data is being sent to co functions */
+        if (local_sep == AVDT_TSEP_SNK)
+        {
+            p_scb->p_cos->setcfg(p_scb->hndl, p_scb->codec_type,
+                             p_evt_cfg->codec_info,
+                             p_info->seid,
+                             p_scb->peer_addr,
+                             p_evt_cfg->num_protect,
+                             p_evt_cfg->protect_info,
+                             AVDT_TSEP_SNK,
+                             p_msg->handle);
+        }
+        else
+        {
+            p_scb->p_cos->setcfg(p_scb->hndl, p_scb->codec_type,
+                             p_evt_cfg->codec_info,
+                             p_info->seid,
+                             p_scb->peer_addr,
+                             p_evt_cfg->num_protect,
+                             p_evt_cfg->protect_info,
+                             AVDT_TSEP_SRC,
+                             p_msg->handle);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_disconnect_req
+**
+** Description      Disconnect AVDTP connection.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_disconnect_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_RCB *p_rcb;
+    UNUSED(p_data);
+
+    APPL_TRACE_DEBUG("bta_av_disconnect_req conn_lcb: 0x%x", bta_av_cb.conn_lcb);
+
+    bta_sys_stop_timer(&bta_av_cb.sig_tmr);
+    bta_sys_stop_timer(&p_scb->timer);
+    if(bta_av_cb.conn_lcb)
+    {
+        p_rcb = bta_av_get_rcb_by_shdl((UINT8)(p_scb->hdi + 1));
+        if (p_rcb)
+            bta_av_del_rc(p_rcb);
+        AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
+    }
+    else
+    {
+        bta_av_ssm_execute(p_scb, BTA_AV_AVDT_DISCONNECT_EVT, NULL);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_security_req
+**
+** Description      Send an AVDTP security request.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_security_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
+    {
+        AVDT_SecurityReq(p_scb->avdt_handle, p_data->api_protect_req.p_data,
+                         p_data->api_protect_req.len);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_security_rsp
+**
+** Description      Send an AVDTP security response.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_security_rsp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
+    {
+        AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, p_data->api_protect_rsp.error_code,
+                         p_data->api_protect_rsp.p_data, p_data->api_protect_rsp.len);
+    }
+    else
+    {
+        AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC,
+                     NULL, 0);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_setconfig_rsp
+**
+** Description      setconfig is OK
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_setconfig_rsp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UINT8   num = p_data->ci_setconfig.num_seid + 1;
+    UINT8   avdt_handle = p_data->ci_setconfig.avdt_handle;
+    UINT8   *p_seid = p_data->ci_setconfig.p_seid;
+    int     i;
+    UINT8   local_sep;
+
+    /* we like this codec_type. find the sep_idx */
+    local_sep = bta_av_get_scb_sep_type(p_scb,avdt_handle);
+    bta_av_adjust_seps_idx(p_scb, avdt_handle);
+    APPL_TRACE_DEBUG("bta_av_setconfig_rsp: sep_idx: %d cur_psc_mask:0x%x", p_scb->sep_idx, p_scb->cur_psc_mask);
+
+    if ((AVDT_TSEP_SNK == local_sep) && (p_data->ci_setconfig.err_code == AVDT_SUCCESS) &&
+                                     (p_scb->seps[p_scb->sep_idx].p_app_data_cback != NULL))
+        p_scb->seps[p_scb->sep_idx].p_app_data_cback(BTA_AV_MEDIA_SINK_CFG_EVT,
+                                              (tBTA_AV_MEDIA*)p_scb->cfg.codec_info);
+
+
+    AVDT_ConfigRsp(p_scb->avdt_handle, p_scb->avdt_label, p_data->ci_setconfig.err_code,
+                   p_data->ci_setconfig.category);
+
+    bta_sys_stop_timer(&bta_av_cb.sig_tmr);
+
+    if(p_data->ci_setconfig.err_code == AVDT_SUCCESS)
+    {
+        p_scb->wait = BTA_AV_WAIT_ACP_CAPS_ON;
+        if(p_data->ci_setconfig.recfg_needed)
+            p_scb->role |= BTA_AV_ROLE_SUSPEND_OPT;
+        APPL_TRACE_DEBUG("bta_av_setconfig_rsp recfg_needed:%d role:x%x num:%d",
+            p_data->ci_setconfig.recfg_needed, p_scb->role, num);
+        /* callout module tells BTA the number of "good" SEPs and their SEIDs.
+         * getcap on these SEID */
+        p_scb->num_seps = num;
+
+        if (p_scb->cur_psc_mask & AVDT_PSC_DELAY_RPT)
+            p_scb->avdt_version = AVDT_VERSION_SYNC;
+
+
+        if (p_scb->codec_type == BTA_AV_CODEC_SBC || num > 1)
+        {
+            /* if SBC is used by the SNK as INT, discover req is not sent in bta_av_config_ind.
+                       * call disc_res now */
+           /* this is called in A2DP SRC path only, In case of SINK we don't need it  */
+            if (local_sep == AVDT_TSEP_SRC)
+                p_scb->p_cos->disc_res(p_scb->hndl, num, num, 0, p_scb->peer_addr,
+                                                      UUID_SERVCLASS_AUDIO_SOURCE);
+        }
+        else
+        {
+            /* we do not know the peer device and it is using non-SBC codec
+             * we need to know all the SEPs on SNK */
+            bta_av_discover_req(p_scb, NULL);
+            return;
+        }
+
+        for (i = 1; i < num; i++)
+        {
+            APPL_TRACE_DEBUG("sep_info[%d] SEID: %d", i, p_seid[i-1]);
+            /* initialize the sep_info[] to get capabilities */
+            p_scb->sep_info[i].in_use = FALSE;
+            p_scb->sep_info[i].tsep = AVDT_TSEP_SNK;
+            p_scb->sep_info[i].media_type = p_scb->media_type;
+            p_scb->sep_info[i].seid = p_seid[i-1];
+        }
+
+        /* only in case of local sep as SRC we need to look for other SEPs, In case of SINK we don't */
+        if (local_sep == AVDT_TSEP_SRC)
+        {
+            /* Make sure UUID has been initialized... */
+            if (p_scb->uuid_int == 0)
+                p_scb->uuid_int = p_scb->open_api.uuid;
+            bta_av_next_getcap(p_scb, p_data);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_str_opened
+**
+** Description      Stream opened OK (incoming/outgoing).
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_str_opened (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_CONN_CHG msg;
+    tBTA_AV_OPEN    open;
+    UINT8 *p;
+    UINT16 mtu;
+
+    msg.hdr.layer_specific = p_scb->hndl;
+    msg.is_up = TRUE;
+    bdcpy(msg.peer_addr, p_scb->peer_addr);
+    p_scb->l2c_cid      = AVDT_GetL2CapChannel(p_scb->avdt_handle);
+    bta_av_conn_chg((tBTA_AV_DATA *) &msg);
+    /* set the congestion flag, so AV would not send media packets by accident */
+    p_scb->cong = TRUE;
+
+
+    p_scb->stream_mtu = p_data->str_msg.msg.open_ind.peer_mtu - AVDT_MEDIA_HDR_SIZE;
+    mtu = bta_av_chk_mtu(p_scb, p_scb->stream_mtu);
+    APPL_TRACE_DEBUG("bta_av_str_opened l2c_cid: 0x%x stream_mtu: %d mtu: %d",
+        p_scb->l2c_cid, p_scb->stream_mtu, mtu);
+    if(mtu == 0 || mtu > p_scb->stream_mtu)
+        mtu = p_scb->stream_mtu;
+
+    /* Set the media channel as medium priority */
+    L2CA_SetTxPriority(p_scb->l2c_cid, L2CAP_CHNL_PRIORITY_MEDIUM);
+    L2CA_SetChnlFlushability (p_scb->l2c_cid, TRUE);
+
+    bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
+    memset(&p_scb->q_info, 0, sizeof(tBTA_AV_Q_INFO));
+
+    p_scb->l2c_bufs = 0;
+    p_scb->p_cos->open(p_scb->hndl,
+        p_scb->codec_type, p_scb->cfg.codec_info, mtu);
+
+    {
+        /* TODO check if other audio channel is open.
+         * If yes, check if reconfig is needed
+         * Rigt now we do not do this kind of checking.
+         * BTA-AV is INT for 2nd audio connection.
+         * The application needs to make sure the current codec_info is proper.
+         * If one audio connection is open and another SNK attempts to connect to AV,
+         * the connection will be rejected.
+         */
+        /* check if other audio channel is started. If yes, start */
+        bdcpy(open.bd_addr, p_scb->peer_addr);
+        open.chnl   = p_scb->chnl;
+        open.hndl   = p_scb->hndl;
+        open.status = BTA_AV_SUCCESS;
+        open.starting = bta_av_chk_start(p_scb);
+        open.edr    = 0;
+        if( NULL != (p = BTM_ReadRemoteFeatures(p_scb->peer_addr)))
+        {
+            if(HCI_EDR_ACL_2MPS_SUPPORTED(p))
+                open.edr |= BTA_AV_EDR_2MBPS;
+            if(HCI_EDR_ACL_3MPS_SUPPORTED(p))
+                open.edr |= BTA_AV_EDR_3MBPS;
+        }
+#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
+        bta_ar_avdt_conn(BTA_ID_AV, open.bd_addr);
+#endif
+        if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC )
+            open.sep = AVDT_TSEP_SNK;
+        else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK )
+            open.sep = AVDT_TSEP_SRC;
+
+        (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV *) &open);
+        if(open.starting)
+        {
+            bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
+        }
+    }
+
+#if 0 /* TODO: implement the property enable/disable */   
+    // This code is used to pass PTS TC for AVDTP ABORT
+    char value[PROPERTY_VALUE_MAX] = {0};
+    if ((property_get("bluetooth.pts.force_a2dp_abort", value, "false"))
+        && (!strcmp(value, "true")))
+    {
+        APPL_TRACE_ERROR ("%s: Calling AVDT_AbortReq", __func__);
+        AVDT_AbortReq(p_scb->avdt_handle);
+    }
+#endif /* #if 0*/
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_security_ind
+**
+** Description      Handle an AVDTP security indication.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_security_ind (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_PROTECT_REQ protect_req;
+
+    p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
+
+    if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
+    {
+        protect_req.chnl    = p_scb->chnl;
+        protect_req.hndl    = p_scb->hndl;
+        /*
+        APPL_TRACE_EVENT("sec ind handle: x%x", protect_req.hndl);
+        */
+        protect_req.p_data  = p_data->str_msg.msg.security_ind.p_data;
+        protect_req.len     = p_data->str_msg.msg.security_ind.len;
+
+        (*bta_av_cb.p_cback)(BTA_AV_PROTECT_REQ_EVT, (tBTA_AV *) &protect_req);
+    }
+    /* app doesn't support security indication; respond with failure */
+    else
+    {
+        AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC, NULL, 0);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_security_cfm
+**
+** Description      Handle an AVDTP security confirm.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_security_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_PROTECT_RSP protect_rsp;
+
+    if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
+    {
+        protect_rsp.chnl    = p_scb->chnl;
+        protect_rsp.hndl    = p_scb->hndl;
+        protect_rsp.p_data  = p_data->str_msg.msg.security_cfm.p_data;
+        protect_rsp.len     = p_data->str_msg.msg.security_cfm.len;
+        protect_rsp.err_code= p_data->str_msg.msg.hdr.err_code;
+
+        (*bta_av_cb.p_cback)(BTA_AV_PROTECT_RSP_EVT, (tBTA_AV *) &protect_rsp);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_do_close
+**
+** Description      Close stream.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_do_close (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UNUSED(p_data);
+
+    /* stop stream if started */
+    if (p_scb->co_started)
+    {
+        bta_av_str_stopped(p_scb, NULL);
+    }
+    bta_sys_stop_timer(&bta_av_cb.sig_tmr);
+
+    /* close stream */
+    p_scb->started = FALSE;
+
+    /* drop the buffers queued in L2CAP */
+    L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
+
+    AVDT_CloseReq(p_scb->avdt_handle);
+    /* just in case that the link is congested, link is flow controled by peer or
+     * for whatever reason the the close request can not be sent in time.
+     * when this timer expires, AVDT_DisconnectReq will be called to disconnect the link
+     */
+    bta_sys_start_timer(&p_scb->timer,
+                        (UINT16)BTA_AV_API_CLOSE_EVT,
+                        BTA_AV_CLOSE_REQ_TIME_VAL);
+
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_connect_req
+**
+** Description      Connect AVDTP connection.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_connect_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UNUSED(p_data);
+
+    utl_freebuf((void **) &p_scb->p_disc_db);
+
+    if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
+    {
+        /* SNK initiated L2C connection while SRC was doing SDP.    */
+        /* Wait until timeout to check if SNK starts signalling.    */
+        APPL_TRACE_EVENT("bta_av_connect_req: coll_mask = 0x%2X", p_scb->coll_mask);
+        return;
+    }
+
+    AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_sdp_failed
+**
+** Description      Service discovery failed.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_sdp_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    if (!p_scb->open_status)
+        p_scb->open_status = BTA_AV_FAIL_SDP;
+
+    utl_freebuf((void **) &p_scb->p_disc_db);
+    bta_av_str_closed(p_scb, p_data);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_disc_results
+**
+** Description      Handle the AVDTP discover results.  Search through the
+**                  results and find the first available stream, and get
+**                  its capabilities.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_disc_results (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UINT8 num_snks = 0, num_srcs =0, i;
+    /* our uuid in case we initiate connection */
+    UINT16 uuid_int = p_scb->uuid_int;
+
+    APPL_TRACE_DEBUG(" initiator UUID 0x%x", uuid_int);
+    /* store number of stream endpoints returned */
+    p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
+
+    for (i = 0; i < p_scb->num_seps; i++)
+    {
+        /* steam not in use, is a sink, and is audio */
+        if ((p_scb->sep_info[i].in_use == FALSE) &&
+            (p_scb->sep_info[i].media_type == p_scb->media_type))
+        {
+            if((p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
+               (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE))
+                num_snks++;
+
+            if((p_scb->sep_info[i].tsep == AVDT_TSEP_SRC) &&
+               (uuid_int == UUID_SERVCLASS_AUDIO_SINK))
+                num_srcs++;
+
+        }
+    }
+
+    p_scb->p_cos->disc_res(p_scb->hndl, p_scb->num_seps, num_snks, num_srcs, p_scb->peer_addr,
+                                                                                    uuid_int);
+    p_scb->num_disc_snks = num_snks;
+    p_scb->num_disc_srcs = num_srcs;
+
+    /* if we got any */
+    if (p_scb->num_seps > 0)
+    {
+        /* initialize index into discovery results */
+        p_scb->sep_info_idx = 0;
+
+        /* get the capabilities of the first available stream */
+        bta_av_next_getcap(p_scb, p_data);
+    }
+    /* else we got discover response but with no streams; we're done */
+    else
+    {
+        bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_disc_res_as_acp
+**
+** Description      Handle the AVDTP discover results.  Search through the
+**                  results and find the first available stream, and get
+**                  its capabilities.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_disc_res_as_acp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UINT8 num_snks = 0, i;
+
+    /* store number of stream endpoints returned */
+    p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
+
+
+
+    for (i = 0; i < p_scb->num_seps; i++)
+    {
+        /* steam is a sink, and is audio */
+        if ((p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
+            (p_scb->sep_info[i].media_type == p_scb->media_type))
+        {
+            p_scb->sep_info[i].in_use = FALSE;
+            num_snks++;
+        }
+    }
+    p_scb->p_cos->disc_res(p_scb->hndl, p_scb->num_seps, num_snks, 0, p_scb->peer_addr,
+                                                          UUID_SERVCLASS_AUDIO_SOURCE);
+    p_scb->num_disc_snks = num_snks;
+    p_scb->num_disc_srcs = 0;
+
+    /* if we got any */
+    if (p_scb->num_seps > 0)
+    {
+        /* initialize index into discovery results */
+        p_scb->sep_info_idx = 0;
+
+        /* get the capabilities of the first available stream */
+        bta_av_next_getcap(p_scb, p_data);
+    }
+    /* else we got discover response but with no streams; we're done */
+    else
+    {
+        bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_save_caps
+**
+** Description      report the SNK SEP capabilities to application
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_save_caps(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tAVDT_CFG   cfg;
+    tAVDT_SEP_INFO  *p_info = &p_scb->sep_info[p_scb->sep_info_idx];
+    UINT8       old_wait = p_scb->wait;
+    BOOLEAN     getcap_done = FALSE;
+
+    APPL_TRACE_DEBUG("bta_av_save_caps num_seps:%d sep_info_idx:%d wait:x%x",
+        p_scb->num_seps, p_scb->sep_info_idx, p_scb->wait);
+    memcpy(&cfg, p_scb->p_cap, sizeof(tAVDT_CFG));
+    /* let application know the capability of the SNK */
+    p_scb->p_cos->getcfg(p_scb->hndl, cfg.codec_info[BTA_AV_CODEC_TYPE_IDX],
+        cfg.codec_info, &p_scb->sep_info_idx, p_info->seid,
+        &cfg.num_protect, cfg.protect_info);
+
+    p_scb->sep_info_idx++;
+    if(p_scb->num_seps > p_scb->sep_info_idx)
+    {
+        /* Some devices have seps at the end of the discover list, which is not */
+        /* matching media type(video not audio).                                */
+        /* In this case, we are done with getcap without sending another        */
+        /* request to AVDT.                                                     */
+        if (!bta_av_next_getcap(p_scb, p_data))
+            getcap_done = TRUE;
+    }
+    else
+        getcap_done = TRUE;
+
+    if (getcap_done)
+    {
+        /* we are done getting capabilities. restore the p_cb->sep_info_idx */
+        p_scb->sep_info_idx = 0;
+        p_scb->wait &= ~(BTA_AV_WAIT_ACP_CAPS_ON|BTA_AV_WAIT_ACP_CAPS_STARTED);
+        if (old_wait & BTA_AV_WAIT_ACP_CAPS_STARTED)
+        {
+            bta_av_start_ok (p_scb, NULL);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_set_use_rc
+**
+** Description      set to use AVRC for this stream control block.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_set_use_rc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UNUSED(p_data);
+
+    p_scb->use_rc = TRUE;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_cco_close
+**
+** Description      call close call-out function.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_cco_close (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UINT16 mtu;
+    UNUSED(p_data);
+
+    mtu = bta_av_chk_mtu(p_scb, BTA_AV_MAX_A2DP_MTU);
+
+    p_scb->p_cos->close(p_scb->hndl, p_scb->codec_type, mtu);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_open_failed
+**
+** Description      Failed to open an AVDT stream
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_open_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+
+    BOOLEAN is_av_opened = FALSE;
+    tBTA_AV_SCB * p_opened_scb = NULL;
+    UINT8 idx;
+    tBTA_AV_OPEN    open;
+
+    APPL_TRACE_DEBUG("bta_av_open_failed");
+    p_scb->open_status = BTA_AV_FAIL_STREAM;
+    bta_av_cco_close(p_scb, p_data);
+
+    /* check whether there is already an opened audio or video connection with the same device */
+    for (idx = 0; (idx < BTA_AV_NUM_STRS) && (is_av_opened == FALSE); idx++ )
+    {
+        p_opened_scb = bta_av_cb.p_scb[idx];
+        if (p_opened_scb && (p_opened_scb->state == BTA_AV_OPEN_SST) && (!bdcmp(p_opened_scb->peer_addr,p_scb->peer_addr )) )
+            is_av_opened = TRUE;
+
+    }
+
+    /* if there is already an active AV connnection with the same bd_addr,
+       don't send disconnect req, just report the open event with BTA_AV_FAIL_GET_CAP status */
+    if (is_av_opened == TRUE)
+    {
+        bdcpy(open.bd_addr, p_scb->peer_addr);
+        open.chnl   = p_scb->chnl;
+        open.hndl   = p_scb->hndl;
+        open.status = BTA_AV_FAIL_GET_CAP;
+        open.starting = bta_av_chk_start(p_scb);
+        open.edr    = 0;
+        /* set the state back to initial state */
+        bta_av_set_scb_sst_init(p_scb);
+
+        if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC )
+            open.sep = AVDT_TSEP_SNK;
+        else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK )
+            open.sep = AVDT_TSEP_SRC;
+
+        (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV *) &open);
+
+    }
+    else
+    {
+        AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
+    }
+}
+
+
+/*******************************************************************************
+**
+** Function         bta_av_getcap_results
+**
+** Description      Handle the AVDTP get capabilities results.  Check the codec
+**                  type and see if it matches ours.  If it does not, get the
+**                  capabilities of the next stream, if any.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_getcap_results (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tAVDT_CFG   cfg;
+    UINT8       media_type;
+    tAVDT_SEP_INFO  *p_info = &p_scb->sep_info[p_scb->sep_info_idx];
+    UINT16 uuid_int; /* UUID for which connection was initiatied */
+
+    memcpy(&cfg, &p_scb->cfg, sizeof(tAVDT_CFG));
+    cfg.num_codec = 1;
+    cfg.num_protect = p_scb->p_cap->num_protect;
+    memcpy(cfg.codec_info, p_scb->p_cap->codec_info, AVDT_CODEC_SIZE);
+    memcpy(cfg.protect_info, p_scb->p_cap->protect_info, AVDT_PROTECT_SIZE);
+    media_type = p_scb->p_cap->codec_info[BTA_AV_MEDIA_TYPE_IDX] >> 4;
+
+    APPL_TRACE_DEBUG("num_codec %d", p_scb->p_cap->num_codec);
+    APPL_TRACE_DEBUG("media type x%x, x%x", media_type, p_scb->media_type);
+#if AVDT_MULTIPLEXING == TRUE
+    APPL_TRACE_DEBUG("mux x%x, x%x", cfg.mux_mask, p_scb->p_cap->mux_mask);
+#endif
+
+    /* if codec present and we get a codec configuration */
+    if ((p_scb->p_cap->num_codec != 0) &&
+        (media_type == p_scb->media_type) &&
+        (p_scb->p_cos->getcfg(p_scb->hndl, p_scb->p_cap->codec_info[BTA_AV_CODEC_TYPE_IDX],
+            cfg.codec_info, &p_scb->sep_info_idx, p_info->seid,
+            &cfg.num_protect, cfg.protect_info) == 0))
+    {
+#if AVDT_MULTIPLEXING == TRUE
+        cfg.mux_mask &= p_scb->p_cap->mux_mask;
+        APPL_TRACE_DEBUG("mux_mask used x%x", cfg.mux_mask);
+#endif
+        /* save copy of codec type and configuration */
+        p_scb->codec_type = cfg.codec_info[BTA_AV_CODEC_TYPE_IDX];
+        memcpy(&p_scb->cfg, &cfg, sizeof(tAVDT_CFG));
+
+        uuid_int = p_scb->uuid_int;
+        APPL_TRACE_DEBUG(" initiator UUID = 0x%x ", uuid_int);
+        if (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE)
+            bta_av_adjust_seps_idx(p_scb, bta_av_get_scb_handle(p_scb, AVDT_TSEP_SRC));
+        else if (uuid_int == UUID_SERVCLASS_AUDIO_SINK)
+            bta_av_adjust_seps_idx(p_scb, bta_av_get_scb_handle(p_scb, AVDT_TSEP_SNK));
+
+        /* use only the services peer supports */
+        cfg.psc_mask &= p_scb->p_cap->psc_mask;
+        p_scb->cur_psc_mask = cfg.psc_mask;
+
+        if ((uuid_int == UUID_SERVCLASS_AUDIO_SINK) &&
+            (p_scb->seps[p_scb->sep_idx].p_app_data_cback != NULL))
+        {
+            APPL_TRACE_DEBUG(" Configure Deoder for Sink Connection ");
+            p_scb->seps[p_scb->sep_idx].p_app_data_cback(BTA_AV_MEDIA_SINK_CFG_EVT,
+                     (tBTA_AV_MEDIA*)p_scb->cfg.codec_info);
+        }
+
+        /* open the stream */
+        AVDT_OpenReq(p_scb->seps[p_scb->sep_idx].av_handle, p_scb->peer_addr,
+                     p_scb->sep_info[p_scb->sep_info_idx].seid, &cfg);
+
+        if (!bta_av_is_rcfg_sst(p_scb))
+        {
+            /* free capabilities buffer */
+            utl_freebuf((void **) &p_scb->p_cap);
+        }
+    }
+    else
+    {
+        /* try the next stream, if any */
+        p_scb->sep_info_idx++;
+        bta_av_next_getcap(p_scb, p_data);
+    }
+
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_setconfig_rej
+**
+** Description      Send AVDTP set config reject.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_setconfig_rej (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_REJECT reject;
+    UINT8   avdt_handle = p_data->ci_setconfig.avdt_handle;
+
+    bta_av_adjust_seps_idx(p_scb, avdt_handle);
+    APPL_TRACE_DEBUG("bta_av_setconfig_rej: sep_idx: %d",p_scb->sep_idx);
+    AVDT_ConfigRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_UNSUP_CFG, 0);
+
+    bdcpy(reject.bd_addr, p_data->str_msg.bd_addr);
+    reject.hndl = p_scb->hndl;
+    (*bta_av_cb.p_cback)(BTA_AV_REJECT_EVT, (tBTA_AV *) &reject);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_discover_req
+**
+** Description      Send an AVDTP discover request to the peer.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_discover_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UNUSED(p_data);
+
+    /* send avdtp discover request */
+
+    AVDT_DiscoverReq(p_scb->peer_addr, p_scb->sep_info, BTA_AV_NUM_SEPS, bta_av_dt_cback[p_scb->hdi]);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_conn_failed
+**
+** Description      AVDTP connection failed.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_conn_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    p_scb->open_status = BTA_AV_FAIL_STREAM;
+    bta_av_str_closed(p_scb, p_data);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_do_start
+**
+** Description      Start stream.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_do_start (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UINT8 policy = HCI_ENABLE_SNIFF_MODE;
+    UINT8       cur_role;
+
+    APPL_TRACE_DEBUG("bta_av_do_start sco_occupied:%d, role:x%x, started:%d", bta_av_cb.sco_occupied, p_scb->role, p_scb->started);
+    if (bta_av_cb.sco_occupied)
+    {
+        bta_av_start_failed(p_scb, p_data);
+        return;
+    }
+
+    /* disallow role switch during streaming, only if we are the master role
+     * i.e. allow role switch, if we are slave.
+     * It would not hurt us, if the peer device wants us to be master */
+    if ((BTM_GetRole (p_scb->peer_addr, &cur_role) == BTM_SUCCESS) &&
+        (cur_role == BTM_ROLE_MASTER) )
+    {
+        policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
+    }
+
+    bta_sys_clear_policy(BTA_ID_AV, policy, p_scb->peer_addr);
+
+    if ((p_scb->started == FALSE) && ((p_scb->role & BTA_AV_ROLE_START_INT) == 0))
+    {
+        p_scb->role |= BTA_AV_ROLE_START_INT;
+        bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
+
+        AVDT_StartReq(&p_scb->avdt_handle, 1);
+    }
+    else if (p_scb->started)
+    {
+        p_scb->role |= BTA_AV_ROLE_START_INT;
+        if ( p_scb->wait == 0 ) {
+            if (p_scb->role & BTA_AV_ROLE_SUSPEND) {
+                notify_start_failed(p_scb);
+            } else {
+                bta_av_start_ok(p_scb, NULL);
+            }
+        }
+    }
+    APPL_TRACE_DEBUG("started %d role:x%x", p_scb->started, p_scb->role);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_str_stopped
+**
+** Description      Stream stopped.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_str_stopped (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_SUSPEND suspend_rsp;
+    UINT8   start = p_scb->started;
+    BOOLEAN sus_evt = TRUE;
+    BT_HDR  *p_buf;
+    UINT8 policy = HCI_ENABLE_SNIFF_MODE;
+
+    APPL_TRACE_ERROR("bta_av_str_stopped:audio_open_cnt=%d, p_data %x",
+            bta_av_cb.audio_open_cnt, p_data);
+
+    bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
+    if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
+        policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
+    bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
+
+    if (p_scb->co_started)
+    {
+        bta_av_stream_chg(p_scb, FALSE);
+        p_scb->co_started = FALSE;
+
+        p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
+        L2CA_SetFlushTimeout(p_scb->peer_addr, L2CAP_DEFAULT_FLUSH_TO);
+    }
+
+    /* if q_info.a2d_list is not empty, drop it now */
+    if (BTA_AV_CHNL_AUDIO == p_scb->chnl) {
+        while (!list_is_empty(p_scb->a2d_list))
+        {
+            p_buf = (BT_HDR *)list_front(p_scb->a2d_list);
+            list_remove(p_scb->a2d_list, p_buf);
+            GKI_freebuf(p_buf);
+        }
+
+    /* drop the audio buffers queued in L2CAP */
+        if (p_data && p_data->api_stop.flush)
+            L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
+    }
+
+    suspend_rsp.chnl = p_scb->chnl;
+    suspend_rsp.hndl = p_scb->hndl;
+
+    if (p_data && p_data->api_stop.suspend)
+    {
+        APPL_TRACE_DEBUG("suspending: %d, sup:%d", start, p_scb->suspend_sup);
+        if ((start)  && (p_scb->suspend_sup))
+        {
+            sus_evt = FALSE;
+            p_scb->l2c_bufs = 0;
+            AVDT_SuspendReq(&p_scb->avdt_handle, 1);
+        }
+
+        /* send SUSPEND_EVT event only if not in reconfiguring state and sus_evt is TRUE*/
+        if ((sus_evt)&&(p_scb->state != BTA_AV_RCFG_SST))
+        {
+            suspend_rsp.status = BTA_AV_SUCCESS;
+            suspend_rsp.initiator = TRUE;
+            (*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, (tBTA_AV *) &suspend_rsp);
+        }
+    }
+    else
+    {
+        suspend_rsp.status = BTA_AV_SUCCESS;
+        suspend_rsp.initiator = TRUE;
+        APPL_TRACE_EVENT("bta_av_str_stopped status %d", suspend_rsp.status);
+
+        /* send STOP_EVT event only if not in reconfiguring state */
+        if (p_scb->state != BTA_AV_RCFG_SST)
+        {
+            (*bta_av_cb.p_cback)(BTA_AV_STOP_EVT, (tBTA_AV *) &suspend_rsp);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_reconfig
+**
+** Description      process the reconfigure request.
+**                  save the parameter in control block and
+**                  suspend, reconfigure or close the stream
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_reconfig (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tAVDT_CFG   *p_cfg;
+    tBTA_AV_API_STOP    stop;
+    tBTA_AV_RECONFIG    evt;
+    tBTA_AV_API_RCFG    *p_rcfg = &p_data->api_reconfig;
+
+    APPL_TRACE_DEBUG("bta_av_reconfig r:%d, s:%d idx: %d (o:%d)",
+        p_scb->recfg_sup, p_scb->suspend_sup,
+        p_scb->rcfg_idx, p_scb->sep_info_idx);
+
+    p_scb->num_recfg = 0;
+    /* store the new configuration in control block */
+    if (p_scb->p_cap == NULL)
+    {
+        p_scb->p_cap = (tAVDT_CFG *) GKI_getbuf(sizeof(tAVDT_CFG));
+    }
+    if((p_cfg = p_scb->p_cap) == NULL)
+    {
+        /* report failure */
+        evt.status = BTA_AV_FAIL_RESOURCES;
+        evt.chnl   = p_scb->chnl;
+        evt.hndl   = p_scb->hndl;
+        (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
+
+        /* this event is not possible in this state.
+         * use it to bring the SSM back to open state */
+        bta_av_ssm_execute(p_scb, BTA_AV_SDP_DISC_OK_EVT, NULL);
+        return;
+    }
+
+    /*if(bta_av_cb.features & BTA_AV_FEAT_RCCT)*/
+        bta_sys_stop_timer(&p_scb->timer);
+
+    memcpy(p_cfg, &p_scb->cfg, sizeof(tAVDT_CFG));
+    p_cfg->num_protect = p_rcfg->num_protect;
+    memcpy(p_cfg->codec_info, p_rcfg->codec_info, AVDT_CODEC_SIZE);
+    memcpy(p_cfg->protect_info, p_rcfg->p_protect_info, p_rcfg->num_protect);
+    p_scb->rcfg_idx = p_rcfg->sep_info_idx;
+    p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
+
+    /* if the requested index differs from the current one, we can only close/open */
+    if ((p_scb->rcfg_idx == p_scb->sep_info_idx) &&
+        (p_rcfg->suspend)&& (p_scb->recfg_sup) && (p_scb->suspend_sup))
+    {
+        if(p_scb->started)
+        {
+            stop.flush   = FALSE;
+            stop.suspend = TRUE;
+            bta_av_str_stopped(p_scb, (tBTA_AV_DATA *)&stop);
+        }
+        else
+        {
+            APPL_TRACE_DEBUG("Reconfig");
+            AVDT_ReconfigReq(p_scb->avdt_handle, p_scb->p_cap);
+            p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
+        }
+    }
+    else
+    {
+        /* close the stream */
+        APPL_TRACE_DEBUG("close/open num_protect: %d", p_cfg->num_protect);
+        if(p_scb->started)
+        {
+            bta_av_str_stopped(p_scb, NULL);
+            p_scb->started = FALSE;
+
+            /* drop the buffers queued in L2CAP */
+            L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
+
+            AVDT_CloseReq(p_scb->avdt_handle);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_data_path
+**
+** Description      Handle stream data path.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_data_path (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    BT_HDR  *p_buf = NULL;
+    UINT32  data_len;
+    UINT32  timestamp;
+    BOOLEAN new_buf = FALSE;
+    UINT8   m_pt = 0x60 | p_scb->codec_type;
+    tAVDT_DATA_OPT_MASK     opt;
+    UNUSED(p_data);
+
+    if (p_scb->cong)
+    {
+        return;
+    }
+
+    /*
+    APPL_TRACE_ERROR("q: %d", p_scb->l2c_bufs);
+    */
+    //Always get the current number of bufs que'd up
+    p_scb->l2c_bufs = (UINT8)L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_GET);
+
+    if (!list_is_empty(p_scb->a2d_list)) {
+        p_buf = (BT_HDR *)list_front(p_scb->a2d_list);
+        list_remove(p_scb->a2d_list, p_buf);
+         /* use q_info.a2d data, read the timestamp */
+        timestamp = *(UINT32 *)(p_buf + 1);
+    }
+    else
+    {
+        new_buf = TRUE;
+        /* a2d_list empty, call co_data, dup data to other channels */
+        p_buf = (BT_HDR *)p_scb->p_cos->data(p_scb->codec_type, &data_len,
+                                         &timestamp);
+
+        if (p_buf)
+        {
+            /* use the offset area for the time stamp */
+            *(UINT32 *)(p_buf + 1) = timestamp;
+
+            /* dup the data to other channels */
+            bta_av_dup_audio_buf(p_scb, p_buf);
+        }
+    }
+
+    if(p_buf)
+    {
+        if(p_scb->l2c_bufs < (BTA_AV_QUEUE_DATA_CHK_NUM))
+        {
+            /* there's a buffer, just queue it to L2CAP */
+            /*  There's no need to increment it here, it is always read from L2CAP see above */
+            /* p_scb->l2c_bufs++; */
+            /*
+            APPL_TRACE_ERROR("qw: %d", p_scb->l2c_bufs);
+            */
+
+            /* opt is a bit mask, it could have several options set */
+            opt = AVDT_DATA_OPT_NONE;
+            if (p_scb->no_rtp_hdr)
+            {
+                opt |= AVDT_DATA_OPT_NO_RTP;
+            }
+
+            AVDT_WriteReqOpt(p_scb->avdt_handle, p_buf, timestamp, m_pt, opt);
+            p_scb->cong = TRUE;
+        }
+        else
+        {
+            /* there's a buffer, but L2CAP does not seem to be moving data */
+            if(new_buf)
+            {
+                /* just got this buffer from co_data,
+                 * put it in queue */
+                list_append(p_scb->a2d_list, p_buf);
+            }
+            else
+            {
+                /* just dequeue it from the a2d_list */
+                if (list_length(p_scb->a2d_list) < 3) {
+                    /* put it back to the queue */
+                    list_prepend(p_scb->a2d_list, p_buf);
+                }
+                else
+                {
+                    /* too many buffers in a2d_list, drop it. */
+                    bta_av_co_audio_drop(p_scb->hndl);
+                    GKI_freebuf(p_buf);
+                }
+            }
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_start_ok
+**
+** Description      Stream started.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_start_ok (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_START   start;
+    tBTA_AV_API_STOP stop;
+    BOOLEAN         initiator = FALSE;
+    BOOLEAN         suspend = FALSE;
+    UINT16          flush_to;
+    UINT8           new_role = p_scb->role;
+    BT_HDR          hdr;
+    UINT8           policy = HCI_ENABLE_SNIFF_MODE;
+    UINT8           cur_role;
+
+    APPL_TRACE_DEBUG("bta_av_start_ok wait:x%x, role:x%x", p_scb->wait, p_scb->role);
+
+    p_scb->started = TRUE;
+    if (p_scb->sco_suspend)
+    {
+        p_scb->sco_suspend = FALSE;
+    }
+
+    if (new_role & BTA_AV_ROLE_START_INT)
+        initiator = TRUE;
+
+    /* for A2DP SINK we do not send get_caps */
+    if ((p_scb->avdt_handle == p_scb->seps[p_scb->sep_idx].av_handle)
+         &&(p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK))
+    {
+        p_scb->wait &= ~(BTA_AV_WAIT_ACP_CAPS_ON);
+        APPL_TRACE_DEBUG(" Local SEP type is SNK  new wait is 0x%x",p_scb->wait);
+    }
+    if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_FAILED)
+    {
+        /* role switch has failed */
+        p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_FAILED;
+        p_data = (tBTA_AV_DATA *)&hdr;
+        hdr.offset = BTA_AV_RS_FAIL;
+    }
+    APPL_TRACE_DEBUG("wait:x%x", p_scb->wait);
+
+    if (p_data && (p_data->hdr.offset != BTA_AV_RS_NONE))
+    {
+        p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
+        if (p_data->hdr.offset == BTA_AV_RS_FAIL)
+        {
+            bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
+            start.chnl   = p_scb->chnl;
+            start.status = BTA_AV_FAIL_ROLE;
+            start.hndl   = p_scb->hndl;
+            start.initiator = initiator;
+            (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
+            return;
+        }
+    }
+
+    if (!bta_av_link_role_ok(p_scb, A2D_SET_ONE_BIT))
+        p_scb->q_tag = BTA_AV_Q_TAG_START;
+    else
+    {
+        /* The wait flag may be set here while we are already master on the link */
+        /* this could happen if a role switch complete event occurred during reconfig */
+        /* if we are now master on the link, there is no need to wait for the role switch, */
+        /* complete anymore so we can clear the wait for role switch flag */
+        p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
+    }
+
+    if (p_scb->wait & (BTA_AV_WAIT_ROLE_SW_RES_OPEN|BTA_AV_WAIT_ROLE_SW_RES_START))
+    {
+        p_scb->wait |= BTA_AV_WAIT_ROLE_SW_STARTED;
+        p_scb->q_tag = BTA_AV_Q_TAG_START;
+    }
+
+    if (p_scb->wait)
+    {
+        APPL_TRACE_ERROR("wait:x%x q_tag:%d- not started", p_scb->wait, p_scb->q_tag);
+        /* Clear first bit of p_scb->wait and not to return from this point else
+         * HAL layer gets blocked. And if there is delay in Get Capability response as
+         * first bit of p_scb->wait is cleared hence it ensures bt_av_start_ok is not called
+         * again from bta_av_save_caps.
+        */
+        p_scb->wait &= ~BTA_AV_WAIT_ACP_CAPS_ON;
+    }
+
+    /* tell role manager to check M/S role */
+    bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
+
+    bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
+
+    if(p_scb->media_type == AVDT_MEDIA_AUDIO)
+    {
+        /* in normal logic, conns should be bta_av_cb.audio_count - 1,
+         * However, bta_av_stream_chg is not called to increase bta_av_cb.audio_count yet.
+         * If the code were to be re-arranged for some reasons, this number may need to be changed
+         */
+        p_scb->co_started = bta_av_cb.audio_open_cnt;
+        flush_to = p_bta_av_cfg->p_audio_flush_to[p_scb->co_started - 1];
+    }
+    else
+    {
+        flush_to = p_bta_av_cfg->video_flush_to;
+    }
+    L2CA_SetFlushTimeout(p_scb->peer_addr, flush_to );
+
+    /* clear the congestion flag */
+    p_scb->cong = FALSE;
+
+    if (new_role & BTA_AV_ROLE_START_INT)
+    {
+        new_role &= ~BTA_AV_ROLE_START_INT;
+    }
+    else if ((new_role & BTA_AV_ROLE_AD_ACP) && (new_role & BTA_AV_ROLE_SUSPEND_OPT))
+    {
+        suspend = TRUE;
+    }
+
+    if (!suspend)
+    {
+        p_scb->q_tag = BTA_AV_Q_TAG_STREAM;
+        bta_av_stream_chg(p_scb, TRUE);
+    }
+
+    {
+        /* If sink starts stream, disable sniff mode here */
+        if (!initiator)
+        {
+             /* If souce is the master role, disable role switch during streaming.
+             * Otherwise allow role switch, if source is slave.
+             * Because it would not hurt source, if the peer device wants source to be master */
+            if ((BTM_GetRole (p_scb->peer_addr, &cur_role) == BTM_SUCCESS) &&
+                (cur_role == BTM_ROLE_MASTER) )
+            {
+                policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
+            }
+
+            bta_sys_clear_policy(BTA_ID_AV, policy, p_scb->peer_addr);
+        }
+
+        p_scb->role = new_role;
+        p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
+        p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
+
+        p_scb->no_rtp_hdr = FALSE;
+        p_scb->p_cos->start(p_scb->hndl, p_scb->codec_type, p_scb->cfg.codec_info, &p_scb->no_rtp_hdr);
+        p_scb->co_started = TRUE;
+
+        APPL_TRACE_DEBUG("bta_av_start_ok suspending: %d, role:x%x, init %d",
+            suspend, p_scb->role, initiator);
+
+        start.suspending = suspend;
+        start.initiator = initiator;
+        start.chnl   = p_scb->chnl;
+        start.status = BTA_AV_SUCCESS;
+        start.hndl   = p_scb->hndl;
+        (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
+
+        if(suspend)
+        {
+            p_scb->role |= BTA_AV_ROLE_SUSPEND;
+            p_scb->cong = TRUE;  /* do not allow the media data to go through */
+            /* do not duplicate the media packets to this channel */
+            p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
+            p_scb->co_started = FALSE;
+            stop.flush   = FALSE;
+            stop.suspend = TRUE;
+            bta_av_ssm_execute(p_scb, BTA_AV_AP_STOP_EVT, (tBTA_AV_DATA *)&stop);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_start_failed
+**
+** Description      Stream start failed.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_start_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UNUSED(p_data);
+
+    if(p_scb->started == FALSE && p_scb->co_started == FALSE)
+    {
+        bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
+        notify_start_failed(p_scb);
+    }
+
+    bta_sys_set_policy(BTA_ID_AV, (HCI_ENABLE_SNIFF_MODE|HCI_ENABLE_MASTER_SLAVE_SWITCH), p_scb->peer_addr);
+    p_scb->sco_suspend = FALSE;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_str_closed
+**
+** Description      Stream closed.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_str_closed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV     data;
+    tBTA_AV_EVT event;
+    UINT16      mtu;
+    UINT8 policy = HCI_ENABLE_SNIFF_MODE;
+
+    if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
+        policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
+    bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
+    if (bta_av_cb.audio_open_cnt <= 1)
+    {
+        /* last connection - restore the allow switch flag */
+        L2CA_SetDesireRole(L2CAP_ROLE_ALLOW_SWITCH);
+    }
+
+    if (p_scb->open_status)
+    {
+        /* must be failure when opening the stream */
+        bdcpy(data.open.bd_addr, p_scb->peer_addr);
+        data.open.status = p_scb->open_status;
+        data.open.chnl   = p_scb->chnl;
+        data.open.hndl   = p_scb->hndl;
+
+        if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC )
+            data.open.sep = AVDT_TSEP_SNK;
+        else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK )
+            data.open.sep = AVDT_TSEP_SRC;
+
+        event = BTA_AV_OPEN_EVT;
+        p_scb->open_status = BTA_AV_SUCCESS;
+
+        bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
+        bta_av_cleanup(p_scb, p_data);
+        (*bta_av_cb.p_cback)(event, &data);
+    }
+    else
+    {
+        /* do stop if we were started */
+        if (p_scb->co_started)
+        {
+            bta_av_str_stopped(p_scb, NULL);
+        }
+
+        /* Update common mtu shared by remaining connectons */
+        mtu = bta_av_chk_mtu(p_scb, BTA_AV_MAX_A2DP_MTU);
+
+        {
+            p_scb->p_cos->close(p_scb->hndl, p_scb->codec_type, mtu);
+            data.close.chnl = p_scb->chnl;
+            data.close.hndl = p_scb->hndl;
+            event = BTA_AV_CLOSE_EVT;
+
+            bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
+            bta_av_cleanup(p_scb, p_data);
+            (*bta_av_cb.p_cback)(event, &data);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_clr_cong
+**
+** Description      Clear stream congestion flag.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_clr_cong (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UNUSED(p_data);
+
+    if(p_scb->co_started)
+        p_scb->cong = FALSE;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_suspend_cfm
+**
+** Description      process the suspend response
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_suspend_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_SUSPEND suspend_rsp;
+    UINT8           err_code = p_data->str_msg.msg.hdr.err_code;
+    UINT8 policy = HCI_ENABLE_SNIFF_MODE;
+
+    APPL_TRACE_DEBUG ("bta_av_suspend_cfm:audio_open_cnt = %d, err_code = %d",
+        bta_av_cb.audio_open_cnt, err_code);
+
+    if (p_scb->started == FALSE)
+    {
+        /* handle the condition where there is a collision of SUSPEND req from either side
+        ** Second SUSPEND req could be rejected. Do not treat this as a failure
+        */
+        APPL_TRACE_WARNING("bta_av_suspend_cfm: already suspended, ignore, err_code %d",
+                            err_code);
+        return;
+    }
+
+    suspend_rsp.status = BTA_AV_SUCCESS;
+    if (err_code && (err_code != AVDT_ERR_BAD_STATE))
+    {
+         /* Disable suspend feature only with explicit rejection(not with timeout) */
+        if (err_code != AVDT_ERR_TIMEOUT)
+        {
+            p_scb->suspend_sup = FALSE;
+        }
+        suspend_rsp.status = BTA_AV_FAIL;
+
+        APPL_TRACE_ERROR ("bta_av_suspend_cfm: suspend failed, closing connection");
+
+        /* SUSPEND failed. Close connection. */
+        bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
+    }
+    else
+    {
+        /* only set started to FALSE when suspend is successful */
+        p_scb->started = FALSE;
+    }
+
+    if (p_scb->role & BTA_AV_ROLE_SUSPEND)
+    {
+        p_scb->role &= ~BTA_AV_ROLE_SUSPEND;
+        p_scb->cong = FALSE;
+    }
+
+    bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
+    if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
+        policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
+    bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
+
+    /* in case that we received suspend_ind, we may need to call co_stop here */
+    if(p_scb->co_started)
+    {
+        bta_av_stream_chg(p_scb, FALSE);
+
+        {
+            p_scb->co_started = FALSE;
+            p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
+        }
+        L2CA_SetFlushTimeout(p_scb->peer_addr, L2CAP_DEFAULT_FLUSH_TO);
+    }
+
+    {
+        suspend_rsp.chnl = p_scb->chnl;
+        suspend_rsp.hndl = p_scb->hndl;
+        suspend_rsp.initiator = p_data->str_msg.initiator;
+        (*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, (tBTA_AV *) &suspend_rsp);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rcfg_str_ok
+**
+** Description      report reconfigure successful
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rcfg_str_ok (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_RECONFIG    evt;
+    UNUSED(p_data);
+
+    p_scb->l2c_cid      = AVDT_GetL2CapChannel(p_scb->avdt_handle);
+    APPL_TRACE_DEBUG("bta_av_rcfg_str_ok: l2c_cid: %d", p_scb->l2c_cid);
+
+    /* rc listen */
+    bta_av_st_rc_timer(p_scb, NULL);
+    utl_freebuf((void **)&p_scb->p_cap);
+
+    /* No need to keep the role bits once reconfig is done. */
+    p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
+    p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
+    p_scb->role &= ~BTA_AV_ROLE_START_INT;
+
+    {
+        /* reconfigure success  */
+        evt.status = BTA_AV_SUCCESS;
+        evt.chnl   = p_scb->chnl;
+        evt.hndl   = p_scb->hndl;
+        (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rcfg_failed
+**
+** Description      process reconfigure failed
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rcfg_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_RECONFIG evt;
+
+    APPL_TRACE_DEBUG("bta_av_rcfg_failed num_recfg: %d, conn_lcb:0x%x",
+        p_scb->num_recfg, bta_av_cb.conn_lcb);
+    if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
+    {
+        bta_av_cco_close(p_scb, p_data);
+        /* report failure */
+        evt.status = BTA_AV_FAIL_STREAM;
+        evt.chnl   = p_scb->chnl;
+        evt.hndl   = p_scb->hndl;
+        (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
+        /* go to closing state */
+        bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
+    }
+    else
+    {
+        /* open failed. try again */
+        p_scb->num_recfg++;
+        if(bta_av_cb.conn_lcb)
+        {
+            AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
+        }
+        else
+        {
+            bta_av_connect_req(p_scb, NULL);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rcfg_connect
+**
+** Description      stream closed. reconnect the stream
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rcfg_connect (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UNUSED(p_data);
+
+    p_scb->cong    = FALSE;
+    p_scb->num_recfg++;
+    APPL_TRACE_DEBUG("bta_av_rcfg_connect num_recfg: %d", p_scb->num_recfg);
+    if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
+    {
+        /* let bta_av_rcfg_failed report fail */
+        bta_av_rcfg_failed(p_scb, NULL);
+    }
+    else
+        AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rcfg_discntd
+**
+** Description      AVDT disconnected. reconnect the stream
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rcfg_discntd (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_RECONFIG    evt;
+    UNUSED(p_data);
+
+    APPL_TRACE_DEBUG("bta_av_rcfg_discntd num_recfg: %d", p_scb->num_recfg);
+    p_scb->num_recfg++;
+    if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
+    {
+        /* report failure */
+        evt.status = BTA_AV_FAIL_STREAM;
+        evt.chnl   = p_scb->chnl;
+        evt.hndl   = p_scb->hndl;
+        (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
+        /* report close event & go to init state */
+        bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
+    }
+    else
+        AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_suspend_cont
+**
+** Description      received the suspend response.
+**                  continue to reconfigure the stream
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_suspend_cont (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UINT8       err_code = p_data->str_msg.msg.hdr.err_code;
+    tBTA_AV_RECONFIG    evt;
+
+    p_scb->started = FALSE;
+    p_scb->cong    = FALSE;
+    if (err_code)
+    {
+        if (AVDT_ERR_CONNECT == err_code)
+        {
+            /* report failure */
+            evt.status = BTA_AV_FAIL;
+            (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
+            bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
+        }
+        else
+        {
+            APPL_TRACE_ERROR("suspend rejected, try close");
+             /* Disable suspend feature only with explicit rejection(not with timeout) */
+            if (err_code != AVDT_ERR_TIMEOUT)
+            {
+                p_scb->suspend_sup = FALSE;
+            }
+            /* drop the buffers queued in L2CAP */
+            L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
+
+            AVDT_CloseReq(p_scb->avdt_handle);
+        }
+    }
+    else
+    {
+        APPL_TRACE_DEBUG("bta_av_suspend_cont calling AVDT_ReconfigReq");
+        /* reconfig the stream */
+
+        AVDT_ReconfigReq(p_scb->avdt_handle, p_scb->p_cap);
+        p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rcfg_cfm
+**
+** Description      if reconfigure is successful, report the event
+**                  otherwise, close the stream.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rcfg_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UINT8   err_code = p_data->str_msg.msg.hdr.err_code;
+
+    /*
+    APPL_TRACE_DEBUG("bta_av_rcfg_cfm");
+    */
+    if (err_code)
+    {
+        APPL_TRACE_ERROR("reconfig rejected, try close");
+         /* Disable reconfiguration feature only with explicit rejection(not with timeout) */
+        if (err_code != AVDT_ERR_TIMEOUT)
+        {
+            p_scb->recfg_sup = FALSE;
+        }
+        /* started flag is FALSE when reconfigure command is sent */
+        /* drop the buffers queued in L2CAP */
+        L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
+        AVDT_CloseReq(p_scb->avdt_handle);
+    }
+    else
+    {
+        /* update the codec info after rcfg cfm */
+        memcpy(p_scb->cfg.codec_info,p_data->str_msg.msg.reconfig_cfm.p_cfg->codec_info,AVDT_CODEC_SIZE);
+        /* take the SSM back to OPEN state */
+        bta_av_ssm_execute(p_scb, BTA_AV_STR_OPEN_OK_EVT, NULL);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rcfg_open
+**
+** Description      AVDT is connected. open the stream with the new configuration
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rcfg_open (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UNUSED(p_data);
+
+       APPL_TRACE_DEBUG("bta_av_rcfg_open, num_disc_snks = %d", p_scb->num_disc_snks);
+
+    if (p_scb->num_disc_snks == 0)
+    {
+        /* Need to update call-out module so that it will be ready for discover */
+        p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
+
+        /* send avdtp discover request */
+        AVDT_DiscoverReq(p_scb->peer_addr, p_scb->sep_info, BTA_AV_NUM_SEPS, bta_av_dt_cback[p_scb->hdi]);
+    }
+    else
+    {
+        p_scb->codec_type = p_scb->p_cap->codec_info[BTA_AV_CODEC_TYPE_IDX];
+        memcpy(p_scb->cfg.codec_info, p_scb->p_cap->codec_info, AVDT_CODEC_SIZE);
+        /* we may choose to use a different SEP at reconfig.
+         * adjust the sep_idx now */
+        bta_av_adjust_seps_idx(p_scb, bta_av_get_scb_handle(p_scb, AVDT_TSEP_SRC));
+
+        /* open the stream with the new config */
+        p_scb->sep_info_idx = p_scb->rcfg_idx;
+        AVDT_OpenReq(p_scb->avdt_handle, p_scb->peer_addr,
+                     p_scb->sep_info[p_scb->sep_info_idx].seid, p_scb->p_cap);
+    }
+
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_security_rej
+**
+** Description      Send an AVDTP security reject.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_security_rej (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    UNUSED(p_data);
+
+    AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_BAD_STATE,
+                     NULL, 0);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_chk_2nd_start
+**
+** Description      check if this is 2nd stream and if it needs to be started.
+**                  This function needs to be kept very similar to bta_av_chk_start
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_chk_2nd_start (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_SCB *p_scbi;
+    int i;
+    BOOLEAN new_started = FALSE;
+    UNUSED(p_data);
+
+    if ((p_scb->chnl == BTA_AV_CHNL_AUDIO) && (bta_av_cb.audio_open_cnt >= 2))
+    {
+        /* more than one audio channel is connected */
+        if (!(p_scb->role & BTA_AV_ROLE_SUSPEND_OPT))
+        {
+            /* this channel does not need to be reconfigured.
+             * if there is other channel streaming, start the stream now */
+            for(i=0; i<BTA_AV_NUM_STRS; i++)
+            {
+                p_scbi = bta_av_cb.p_scb[i];
+                if(p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started)
+                {
+                    if (!new_started)
+                    {
+                        /* start the new stream */
+                        new_started = TRUE;
+                        bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
+                    }
+                    /* may need to update the flush timeout of this already started stream */
+                    if (p_scbi->co_started != bta_av_cb.audio_open_cnt)
+                    {
+                        p_scbi->co_started = bta_av_cb.audio_open_cnt;
+                        L2CA_SetFlushTimeout(p_scbi->peer_addr, p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1] );
+                    }
+                }
+            }
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_open_rc
+**
+** Description      Send a message to main SM to open RC channel.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_open_rc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_START   start;
+
+    APPL_TRACE_DEBUG("bta_av_open_rc use_rc: %d, wait: x%x role:x%x", p_scb->use_rc, p_scb->wait, p_scb->role);
+    if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) && (p_scb->q_tag == BTA_AV_Q_TAG_START))
+    {
+        /* waiting for role switch for some reason & the timer expires */
+        if (!bta_av_link_role_ok(p_scb, A2D_SET_ONE_BIT))
+        {
+            APPL_TRACE_ERROR ("failed to start streaming for role management reasons!!");
+            bta_sys_stop_timer(&p_scb->timer);
+            start.chnl   = p_scb->chnl;
+            start.status = BTA_AV_FAIL_ROLE;
+            start.initiator = TRUE;
+            start.hndl   = p_scb->hndl;
+            p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
+            bta_av_cb.rs_idx = 0;
+            (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
+        }
+        else
+        {
+            /* role switch is done. continue to start streaming */
+            bta_av_cb.rs_idx = 0;
+            p_data->hdr.offset = BTA_AV_RS_OK;
+            bta_av_start_ok (p_scb, p_data);
+        }
+        return;
+    }
+
+    if(p_scb->use_rc == TRUE || (p_scb->role & BTA_AV_ROLE_AD_ACP) )
+    {
+        if(bta_av_cb.disc)
+        {
+            /* AVRC discover db is in use */
+            if(p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE)
+            {
+                /* AVRC channel is not connected. delay a little bit */
+                if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0)
+                    bta_sys_start_timer(&p_scb->timer, BTA_AV_AVRC_TIMER_EVT, BTA_AV_RC_DISC_TIME_VAL);
+                else
+                    p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
+            }
+        }
+        else
+        {
+            /* use main SM for AVRC SDP activities */
+            bta_av_rc_disc((UINT8)(p_scb->hdi + 1));
+        }
+    }
+    else
+    {
+        if(BTA_AV_RC_HANDLE_NONE != p_scb->rc_handle)
+        {
+            /* the open API said that this handle does not want a RC connection.
+             * disconnect it now */
+            AVRC_Close(p_scb->rc_handle);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_open_at_inc
+**
+** Description      This function is called if API open is called by application
+**                  while state-machine is at incoming state.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_open_at_inc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_API_OPEN  *p_buf;
+
+    memcpy (&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
+
+    if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
+    {
+        p_scb->coll_mask |= BTA_AV_COLL_API_CALLED;
+
+        /* API open will be handled at timeout if SNK did not start signalling. */
+        /* API open will be ignored if SNK starts signalling.                   */
+    }
+    else
+    {
+        /* SNK did not start signalling, API was called N seconds timeout. */
+        /* We need to switch to INIT state and start opening connection. */
+        p_scb->coll_mask = 0;
+        bta_av_set_scb_sst_init (p_scb);
+
+        if ((p_buf = (tBTA_AV_API_OPEN *) GKI_getbuf(sizeof(tBTA_AV_API_OPEN))) != NULL)
+        {
+            memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
+            bta_sys_sendmsg(p_buf);
+        }
+    }
+}
+
+#endif /* BTA_AV_INCLUDED */
diff --git a/components/bt/bluedroid/bta/av/bta_av_act.c b/components/bt/bluedroid/bta/av/bta_av_act.c
new file mode 100755 (executable)
index 0000000..e98debc
--- /dev/null
@@ -0,0 +1,2067 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2004-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This file contains action functions for advanced audio/video main state
+ *  machine.
+ *
+ ******************************************************************************/
+
+#include "bt_target.h"
+#if defined(BTA_AV_INCLUDED) && (BTA_AV_INCLUDED == TRUE)
+
+#include <string.h>
+#include "bta_av_api.h"
+#include "bta_av_int.h"
+#include "avdt_api.h"
+#include "utl.h"
+#include "l2c_api.h"
+// #include "osi/include/list.h"
+#include "list.h"
+#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
+#include "bta_ar_api.h"
+#endif
+
+#define LOG_TAG "bt_bta_av"
+// #include "osi/include/log.h"
+#include "bt_trace.h"
+
+/*****************************************************************************
+**  Constants
+*****************************************************************************/
+/* the timer in milliseconds to wait for open req after setconfig for incoming connections */
+#ifndef BTA_AV_SIG_TIME_VAL
+#define BTA_AV_SIG_TIME_VAL 8000
+#endif
+
+/* In millisec to wait for signalling from SNK when it is initiated from SNK.   */
+/* If not, we will start signalling from SRC.                                   */
+#ifndef BTA_AV_ACP_SIG_TIME_VAL
+#define BTA_AV_ACP_SIG_TIME_VAL 2000
+#endif
+
+static void bta_av_acp_sig_timer_cback (TIMER_LIST_ENT *p_tle);
+
+/*******************************************************************************
+**
+** Function         bta_av_get_rcb_by_shdl
+**
+** Description      find the RCB associated with the given SCB handle.
+**
+** Returns          tBTA_AV_RCB
+**
+*******************************************************************************/
+tBTA_AV_RCB * bta_av_get_rcb_by_shdl(UINT8 shdl)
+{
+    tBTA_AV_RCB *p_rcb = NULL;
+    int         i;
+
+    for (i=0; i<BTA_AV_NUM_RCB; i++)
+    {
+        if (bta_av_cb.rcb[i].shdl == shdl && bta_av_cb.rcb[i].handle != BTA_AV_RC_HANDLE_NONE)
+        {
+            p_rcb = &bta_av_cb.rcb[i];
+            break;
+        }
+    }
+    return p_rcb;
+}
+#define BTA_AV_STS_NO_RSP       0xFF    /* a number not used by tAVRC_STS */
+
+/*******************************************************************************
+**
+** Function         bta_av_del_rc
+**
+** Description      delete the given AVRC handle.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_del_rc(tBTA_AV_RCB *p_rcb)
+{
+    tBTA_AV_SCB  *p_scb;
+    UINT8        rc_handle;      /* connected AVRCP handle */
+
+    p_scb = NULL;
+    if(p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
+    {
+        if(p_rcb->shdl)
+        {
+            /* Validate array index*/
+            if ((p_rcb->shdl - 1) < BTA_AV_NUM_STRS)
+            {
+                p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
+            }
+            if(p_scb)
+            {
+                APPL_TRACE_DEBUG("bta_av_del_rc shdl:%d, srch:%d rc_handle:%d", p_rcb->shdl,
+                                  p_scb->rc_handle, p_rcb->handle);
+                if(p_scb->rc_handle == p_rcb->handle)
+                    p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
+                /* just in case the RC timer is active
+                if(bta_av_cb.features & BTA_AV_FEAT_RCCT && p_scb->chnl == BTA_AV_CHNL_AUDIO) */
+                    bta_sys_stop_timer(&p_scb->timer);
+            }
+        }
+
+        APPL_TRACE_EVENT("bta_av_del_rc  handle: %d status=0x%x, rc_acp_handle:%d, idx:%d",
+            p_rcb->handle, p_rcb->status, bta_av_cb.rc_acp_handle, bta_av_cb.rc_acp_idx);
+        rc_handle = p_rcb->handle;
+        if(!(p_rcb->status & BTA_AV_RC_CONN_MASK) ||
+            ((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT) )
+        {
+            p_rcb->status = 0;
+            p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
+            p_rcb->shdl = 0;
+            p_rcb->lidx = 0;
+        }
+        /* else ACP && connected. do not clear the handle yet */
+        AVRC_Close(rc_handle);
+        if (rc_handle == bta_av_cb.rc_acp_handle)
+            bta_av_cb.rc_acp_handle = BTA_AV_RC_HANDLE_NONE;
+        APPL_TRACE_EVENT("end del_rc handle: %d status=0x%x, rc_acp_handle:%d, lidx:%d",
+            p_rcb->handle, p_rcb->status, bta_av_cb.rc_acp_handle, p_rcb->lidx);
+    }
+}
+
+
+/*******************************************************************************
+**
+** Function         bta_av_close_all_rc
+**
+** Description      close the all AVRC handle.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_close_all_rc(tBTA_AV_CB *p_cb)
+{
+    int i;
+
+    for(i=0; i<BTA_AV_NUM_RCB; i++)
+    {
+        if ((p_cb->disabling == TRUE) || (bta_av_cb.rcb[i].shdl != 0))
+            bta_av_del_rc(&bta_av_cb.rcb[i]);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_del_sdp_rec
+**
+** Description      delete the given SDP record handle.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_del_sdp_rec(UINT32 *p_sdp_handle)
+{
+    if(*p_sdp_handle != 0)
+    {
+        SDP_DeleteRecord(*p_sdp_handle);
+        *p_sdp_handle = 0;
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_avrc_sdp_cback
+**
+** Description      AVRCP service discovery callback.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_avrc_sdp_cback(UINT16 status)
+{
+    BT_HDR *p_msg;
+    UNUSED(status);
+
+    if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
+    {
+        p_msg->event = BTA_AV_SDP_AVRC_DISC_EVT;
+        bta_sys_sendmsg(p_msg);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_ctrl_cback
+**
+** Description      AVRCP control callback.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_rc_ctrl_cback(UINT8 handle, UINT8 event, UINT16 result, BD_ADDR peer_addr)
+{
+    tBTA_AV_RC_CONN_CHG *p_msg;
+    UINT16 msg_event = 0;
+    UNUSED(result);
+
+#if (defined(BTA_AV_MIN_DEBUG_TRACES) && BTA_AV_MIN_DEBUG_TRACES == TRUE)
+    APPL_TRACE_EVENT("rc_ctrl handle: %d event=0x%x", handle, event);
+#else
+    APPL_TRACE_EVENT("bta_av_rc_ctrl_cback handle: %d event=0x%x", handle, event);
+#endif
+    if (event == AVRC_OPEN_IND_EVT)
+    {
+        /* save handle of opened connection
+        bta_av_cb.rc_handle = handle;*/
+
+        msg_event = BTA_AV_AVRC_OPEN_EVT;
+    }
+    else if (event == AVRC_CLOSE_IND_EVT)
+    {
+        msg_event = BTA_AV_AVRC_CLOSE_EVT;
+    }
+
+    if (msg_event)
+    {
+        if ((p_msg = (tBTA_AV_RC_CONN_CHG *) GKI_getbuf(sizeof(tBTA_AV_RC_CONN_CHG))) != NULL)
+        {
+            p_msg->hdr.event = msg_event;
+            p_msg->handle    = handle;
+            if(peer_addr)
+                bdcpy(p_msg->peer_addr, peer_addr);
+            bta_sys_sendmsg(p_msg);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_msg_cback
+**
+** Description      AVRCP message callback.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_rc_msg_cback(UINT8 handle, UINT8 label, UINT8 opcode, tAVRC_MSG *p_msg)
+{
+    UINT8           *p_data_src = NULL;
+    UINT16          data_len = 0;
+
+    APPL_TRACE_DEBUG("%s handle: %u opcode=0x%x", __func__, handle, opcode);
+
+    /* Determine the size of the buffer we need */
+    if (opcode == AVRC_OP_VENDOR && p_msg->vendor.p_vendor_data != NULL) {
+        p_data_src = p_msg->vendor.p_vendor_data;
+        data_len = (UINT16) p_msg->vendor.vendor_len;
+    } else if (opcode == AVRC_OP_PASS_THRU && p_msg->pass.p_pass_data != NULL) {
+        p_data_src = p_msg->pass.p_pass_data;
+        data_len = (UINT16) p_msg->pass.pass_len;
+    }
+
+    /* Create a copy of the message */
+    tBTA_AV_RC_MSG *p_buf =
+        (tBTA_AV_RC_MSG *)GKI_getbuf((UINT16)(sizeof(tBTA_AV_RC_MSG) + data_len));
+    if (p_buf != NULL) {
+        p_buf->hdr.event = BTA_AV_AVRC_MSG_EVT;
+        p_buf->handle = handle;
+        p_buf->label = label;
+        p_buf->opcode = opcode;
+        memcpy(&p_buf->msg, p_msg, sizeof(tAVRC_MSG));
+        /* Copy the data payload, and set the pointer to it */
+        if (p_data_src != NULL) {
+            UINT8 *p_data_dst = (UINT8 *)(p_buf + 1);
+            memcpy(p_data_dst, p_data_src, data_len);
+            if (opcode == AVRC_OP_VENDOR)
+                p_buf->msg.vendor.p_vendor_data = p_data_dst;
+            else if (opcode == AVRC_OP_PASS_THRU)
+                p_buf->msg.pass.p_pass_data = p_data_dst;
+        }
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_create
+**
+** Description      alloc RCB and call AVRC_Open
+**
+** Returns          the created rc handle
+**
+*******************************************************************************/
+UINT8 bta_av_rc_create(tBTA_AV_CB *p_cb, UINT8 role, UINT8 shdl, UINT8 lidx)
+{
+    tAVRC_CONN_CB ccb;
+    BD_ADDR_PTR   bda = (BD_ADDR_PTR)bd_addr_any;
+    UINT8         status = BTA_AV_RC_ROLE_ACP;
+    tBTA_AV_SCB  *p_scb = p_cb->p_scb[shdl - 1];
+    int i;
+    UINT8   rc_handle;
+    tBTA_AV_RCB *p_rcb;
+
+    if(role == AVCT_INT)
+    {
+        bda = p_scb->peer_addr;
+        status = BTA_AV_RC_ROLE_INT;
+    }
+    else
+    {
+        if ((p_rcb = bta_av_get_rcb_by_shdl(shdl)) != NULL )
+        {
+            APPL_TRACE_ERROR("bta_av_rc_create ACP handle exist for shdl:%d", shdl);
+            return p_rcb->handle;
+        }
+    }
+
+    ccb.p_ctrl_cback = bta_av_rc_ctrl_cback;
+    ccb.p_msg_cback = bta_av_rc_msg_cback;
+    ccb.company_id = p_bta_av_cfg->company_id;
+    ccb.conn = role;
+    /* note: BTA_AV_FEAT_RCTG = AVRC_CT_TARGET, BTA_AV_FEAT_RCCT = AVRC_CT_CONTROL */
+    ccb.control = p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_RCCT | AVRC_CT_PASSIVE);
+
+
+    if (AVRC_Open(&rc_handle, &ccb, bda) != AVRC_SUCCESS)
+        return BTA_AV_RC_HANDLE_NONE;
+
+    i = rc_handle;
+    p_rcb = &p_cb->rcb[i];
+
+    if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
+    {
+        APPL_TRACE_ERROR("bta_av_rc_create found duplicated handle:%d", rc_handle);
+    }
+
+    p_rcb->handle = rc_handle;
+    p_rcb->status = status;
+    p_rcb->shdl = shdl;
+    p_rcb->lidx = lidx;
+    p_rcb->peer_features = 0;
+    if(lidx == (BTA_AV_NUM_LINKS + 1))
+    {
+        /* this LIDX is reserved for the AVRCP ACP connection */
+        p_cb->rc_acp_handle = p_rcb->handle;
+        p_cb->rc_acp_idx = (i + 1);
+        APPL_TRACE_DEBUG("rc_acp_handle:%d idx:%d", p_cb->rc_acp_handle, p_cb->rc_acp_idx);
+    }
+    APPL_TRACE_DEBUG("create %d, role: %d, shdl:%d, rc_handle:%d, lidx:%d, status:0x%x",
+        i, role, shdl, p_rcb->handle, lidx, p_rcb->status);
+
+    return rc_handle;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_valid_group_navi_msg
+**
+** Description      Check if it is Group Navigation Msg for Metadata
+**
+** Returns          BTA_AV_RSP_ACCEPT or BTA_AV_RSP_NOT_IMPL.
+**
+*******************************************************************************/
+static tBTA_AV_CODE bta_av_group_navi_supported(UINT8 len, UINT8 *p_data, BOOLEAN is_inquiry)
+{
+    tBTA_AV_CODE ret=BTA_AV_RSP_NOT_IMPL;
+    UINT8 *p_ptr = p_data;
+    UINT16 u16;
+    UINT32 u32;
+
+    if (p_bta_av_cfg->avrc_group && len == BTA_GROUP_NAVI_MSG_OP_DATA_LEN)
+    {
+        BTA_AV_BE_STREAM_TO_CO_ID(u32, p_ptr);
+        BE_STREAM_TO_UINT16(u16, p_ptr);
+
+        if (u32 == AVRC_CO_METADATA)
+        {
+            if (is_inquiry)
+            {
+                if (u16 <= AVRC_PDU_PREV_GROUP)
+                    ret = BTA_AV_RSP_IMPL_STBL;
+            }
+            else
+            {
+                if (u16 <= AVRC_PDU_PREV_GROUP)
+                    ret = BTA_AV_RSP_ACCEPT;
+                else
+                    ret = BTA_AV_RSP_REJ;
+            }
+        }
+    }
+
+    return ret;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_op_supported
+**
+** Description      Check if remote control operation is supported.
+**
+** Returns          BTA_AV_RSP_ACCEPT of supported, BTA_AV_RSP_NOT_IMPL if not.
+**
+*******************************************************************************/
+static tBTA_AV_CODE bta_av_op_supported(tBTA_AV_RC rc_id, BOOLEAN is_inquiry)
+{
+    tBTA_AV_CODE ret_code = BTA_AV_RSP_NOT_IMPL;
+
+    if (p_bta_av_rc_id)
+    {
+        if (is_inquiry)
+        {
+            if (p_bta_av_rc_id[rc_id >> 4] & (1 << (rc_id & 0x0F)))
+            {
+                ret_code = BTA_AV_RSP_IMPL_STBL;
+            }
+        }
+        else
+        {
+            if (p_bta_av_rc_id[rc_id >> 4] & (1 << (rc_id & 0x0F)))
+            {
+                ret_code = BTA_AV_RSP_ACCEPT;
+            }
+            else if ((p_bta_av_cfg->rc_pass_rsp == BTA_AV_RSP_INTERIM) && p_bta_av_rc_id_ac)
+            {
+                if (p_bta_av_rc_id_ac[rc_id >> 4] & (1 << (rc_id & 0x0F)))
+                {
+                    ret_code = BTA_AV_RSP_INTERIM;
+                }
+            }
+        }
+
+    }
+    return ret_code;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_find_lcb
+**
+** Description      Given BD_addr, find the associated LCB.
+**
+** Returns          NULL, if not found.
+**
+*******************************************************************************/
+tBTA_AV_LCB * bta_av_find_lcb(BD_ADDR addr, UINT8 op)
+{
+    tBTA_AV_CB   *p_cb = &bta_av_cb;
+    int     xx;
+    UINT8   mask;
+    tBTA_AV_LCB *p_lcb = NULL;
+
+    for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
+    {
+        mask = 1 << xx; /* the used mask for this lcb */
+        if((mask & p_cb->conn_lcb) && 0 ==( bdcmp(p_cb->lcb[xx].addr, addr)))
+        {
+            p_lcb = &p_cb->lcb[xx];
+            if(op == BTA_AV_LCB_FREE)
+            {
+                p_cb->conn_lcb &= ~mask; /* clear the connect mask */
+                APPL_TRACE_DEBUG("conn_lcb: 0x%x", p_cb->conn_lcb);
+            }
+            break;
+        }
+    }
+    return p_lcb;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_opened
+**
+** Description      Set AVRCP state to opened.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rc_opened(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_RC_OPEN rc_open;
+    tBTA_AV_SCB     *p_scb;
+    int         i;
+    UINT8       shdl = 0;
+    tBTA_AV_LCB *p_lcb;
+    tBTA_AV_RCB *p_rcb;
+    UINT8       tmp;
+    UINT8       disc = 0;
+
+    /* find the SCB & stop the timer */
+    for(i=0; i<BTA_AV_NUM_STRS; i++)
+    {
+        p_scb = p_cb->p_scb[i];
+        if(p_scb && bdcmp(p_scb->peer_addr, p_data->rc_conn_chg.peer_addr) == 0)
+        {
+            p_scb->rc_handle = p_data->rc_conn_chg.handle;
+            APPL_TRACE_DEBUG("bta_av_rc_opened shdl:%d, srch %d", i + 1, p_scb->rc_handle);
+            shdl = i+1;
+            LOG_INFO("%s allow incoming AVRCP connections:%d", __func__, p_scb->use_rc);
+            bta_sys_stop_timer(&p_scb->timer);
+            disc = p_scb->hndl;
+            break;
+        }
+    }
+
+    i = p_data->rc_conn_chg.handle;
+    if (p_cb->rcb[i].handle == BTA_AV_RC_HANDLE_NONE)
+    {
+        APPL_TRACE_ERROR("not a valid handle:%d any more", i);
+        return;
+    }
+
+
+    if (p_cb->rcb[i].lidx == (BTA_AV_NUM_LINKS + 1) && shdl != 0)
+    {
+        /* rc is opened on the RC only ACP channel, but is for a specific
+         * SCB -> need to switch RCBs */
+        p_rcb = bta_av_get_rcb_by_shdl(shdl);
+        if (p_rcb)
+        {
+            p_rcb->shdl = p_cb->rcb[i].shdl;
+            tmp         = p_rcb->lidx;
+            p_rcb->lidx = p_cb->rcb[i].lidx;
+            p_cb->rcb[i].lidx = tmp;
+            p_cb->rc_acp_handle = p_rcb->handle;
+            p_cb->rc_acp_idx = (p_rcb - p_cb->rcb) + 1;
+            APPL_TRACE_DEBUG("switching RCB rc_acp_handle:%d idx:%d",
+                               p_cb->rc_acp_handle, p_cb->rc_acp_idx);
+        }
+    }
+
+    p_cb->rcb[i].shdl = shdl;
+    rc_open.rc_handle = i;
+    APPL_TRACE_ERROR("bta_av_rc_opened rcb[%d] shdl:%d lidx:%d/%d",
+            i, shdl, p_cb->rcb[i].lidx, p_cb->lcb[BTA_AV_NUM_LINKS].lidx);
+    p_cb->rcb[i].status |= BTA_AV_RC_CONN_MASK;
+
+    if(!shdl && 0 == p_cb->lcb[BTA_AV_NUM_LINKS].lidx)
+    {
+        /* no associated SCB -> connected to an RC only device
+         * update the index to the extra LCB */
+        p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
+        bdcpy(p_lcb->addr, p_data->rc_conn_chg.peer_addr);
+        APPL_TRACE_DEBUG("rc_only bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
+                      p_lcb->addr[0], p_lcb->addr[1],
+                      p_lcb->addr[2], p_lcb->addr[3],
+                      p_lcb->addr[4], p_lcb->addr[5]);
+        p_lcb->lidx = BTA_AV_NUM_LINKS + 1;
+            p_cb->rcb[i].lidx = p_lcb->lidx;
+        p_lcb->conn_msk = 1;
+        APPL_TRACE_ERROR("rcb[%d].lidx=%d, lcb.conn_msk=x%x",
+            i, p_cb->rcb[i].lidx, p_lcb->conn_msk);
+        disc = p_data->rc_conn_chg.handle|BTA_AV_CHNL_MSK;
+    }
+
+    bdcpy(rc_open.peer_addr, p_data->rc_conn_chg.peer_addr);
+    rc_open.peer_features = p_cb->rcb[i].peer_features;
+    rc_open.status = BTA_AV_SUCCESS;
+    APPL_TRACE_DEBUG("local features:x%x peer_features:x%x", p_cb->features,
+                      rc_open.peer_features);
+    if(rc_open.peer_features == 0)
+    {
+        /* we have not done SDP on peer RC capabilities.
+         * peer must have initiated the RC connection */
+        rc_open.peer_features = BTA_AV_FEAT_RCCT;
+        bta_av_rc_disc(disc);
+    }
+    (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, (tBTA_AV *) &rc_open);
+
+}
+
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_remote_cmd
+**
+** Description      Send an AVRCP remote control command.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rc_remote_cmd(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_RCB    *p_rcb;
+    if (p_cb->features & BTA_AV_FEAT_RCCT)
+    {
+        if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
+        {
+            p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
+            if(p_rcb->status & BTA_AV_RC_CONN_MASK)
+            {
+                AVRC_PassCmd(p_rcb->handle, p_data->api_remote_cmd.label,
+                     &p_data->api_remote_cmd.msg);
+            }
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_vendor_cmd
+**
+** Description      Send an AVRCP vendor specific command.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rc_vendor_cmd(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_RCB    *p_rcb;
+    if ( (p_cb->features & (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR)) ==
+         (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR))
+    {
+        if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
+        {
+            p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
+            AVRC_VendorCmd(p_rcb->handle, p_data->api_vendor.label, &p_data->api_vendor.msg);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_vendor_rsp
+**
+** Description      Send an AVRCP vendor specific response.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rc_vendor_rsp(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_RCB    *p_rcb;
+    if ( (p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR)) ==
+         (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR))
+    {
+        if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
+        {
+            p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
+            AVRC_VendorRsp(p_rcb->handle, p_data->api_vendor.label, &p_data->api_vendor.msg);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_meta_rsp
+**
+** Description      Send an AVRCP metadata/advanced control command/response.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rc_meta_rsp(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_RCB *p_rcb;
+    BOOLEAN         do_free = TRUE;
+
+    if ((p_cb->features & BTA_AV_FEAT_METADATA) && (p_data->hdr.layer_specific < BTA_AV_NUM_RCB))
+    {
+        if ((p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCTG)) ||
+            (!p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCCT)) )
+        {
+            p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
+            if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE) {
+                AVRC_MsgReq(p_rcb->handle, p_data->api_meta_rsp.label,
+                            p_data->api_meta_rsp.rsp_code,
+                            p_data->api_meta_rsp.p_pkt);
+                do_free = FALSE;
+            }
+        }
+    }
+
+    if (do_free)
+        GKI_freebuf (p_data->api_meta_rsp.p_pkt);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_free_rsp
+**
+** Description      free an AVRCP metadata command buffer.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rc_free_rsp (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
+{
+    UNUSED(p_cb);
+
+    GKI_freebuf (p_data->api_meta_rsp.p_pkt);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_meta_req
+**
+** Description      Send an AVRCP metadata command.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rc_free_msg (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
+{
+    UNUSED(p_cb);
+    UNUSED(p_data);
+}
+
+
+
+/*******************************************************************************
+**
+** Function         bta_av_chk_notif_evt_id
+**
+** Description      make sure the requested player id is valid.
+**
+** Returns          BTA_AV_STS_NO_RSP, if no error
+**
+*******************************************************************************/
+static tAVRC_STS bta_av_chk_notif_evt_id(tAVRC_MSG_VENDOR *p_vendor)
+{
+    tAVRC_STS   status = BTA_AV_STS_NO_RSP;
+    UINT8       xx;
+    UINT16      u16;
+    UINT8       *p = p_vendor->p_vendor_data + 2;
+
+    BE_STREAM_TO_UINT16 (u16, p);
+    /* double check the fixed length */
+    if ((u16 != 5) || (p_vendor->vendor_len != 9))
+    {
+        status = AVRC_STS_INTERNAL_ERR;
+    }
+    else
+    {
+        /* make sure the player_id is valid */
+        for (xx=0; xx<p_bta_av_cfg->num_evt_ids; xx++)
+        {
+            if (*p == p_bta_av_cfg->p_meta_evt_ids[xx])
+            {
+                break;
+            }
+        }
+        if (xx == p_bta_av_cfg->num_evt_ids)
+        {
+            status = AVRC_STS_BAD_PARAM;
+        }
+    }
+
+    return status;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_proc_meta_cmd
+**
+** Description      Process an AVRCP metadata command from the peer.
+**
+** Returns          TRUE to respond immediately
+**
+*******************************************************************************/
+tBTA_AV_EVT bta_av_proc_meta_cmd(tAVRC_RESPONSE  *p_rc_rsp, tBTA_AV_RC_MSG *p_msg, UINT8 *p_ctype)
+{
+    tBTA_AV_EVT evt = BTA_AV_META_MSG_EVT;
+    UINT8       u8, pdu, *p;
+    UINT16      u16;
+    tAVRC_MSG_VENDOR    *p_vendor = &p_msg->msg.vendor;
+
+#if (AVRC_METADATA_INCLUDED == TRUE)
+
+    pdu = *(p_vendor->p_vendor_data);
+    p_rc_rsp->pdu = pdu;
+    *p_ctype = AVRC_RSP_REJ;
+    /* Metadata messages only use PANEL sub-unit type */
+    if (p_vendor->hdr.subunit_type != AVRC_SUB_PANEL)
+    {
+        APPL_TRACE_DEBUG("SUBUNIT must be PANEL");
+        /* reject it */
+        evt=0;
+        p_vendor->hdr.ctype = BTA_AV_RSP_NOT_IMPL;
+        AVRC_VendorRsp(p_msg->handle, p_msg->label, &p_msg->msg.vendor);
+    }
+    else if (!AVRC_IsValidAvcType(pdu, p_vendor->hdr.ctype) )
+    {
+        APPL_TRACE_DEBUG("Invalid pdu/ctype: 0x%x, %d", pdu, p_vendor->hdr.ctype);
+        /* reject invalid message without reporting to app */
+        evt = 0;
+        p_rc_rsp->rsp.status = AVRC_STS_BAD_CMD;
+    }
+    else
+    {
+        switch (pdu)
+        {
+        case AVRC_PDU_GET_CAPABILITIES:
+            /* process GetCapabilities command without reporting the event to app */
+            evt = 0;
+            u8 = *(p_vendor->p_vendor_data + 4);
+            p = p_vendor->p_vendor_data + 2;
+            p_rc_rsp->get_caps.capability_id = u8;
+            BE_STREAM_TO_UINT16 (u16, p);
+            if ((u16 != 1) || (p_vendor->vendor_len != 5))
+            {
+                p_rc_rsp->get_caps.status = AVRC_STS_INTERNAL_ERR;
+            }
+            else
+            {
+                p_rc_rsp->get_caps.status = AVRC_STS_NO_ERROR;
+                if (u8 == AVRC_CAP_COMPANY_ID)
+                {
+                    *p_ctype = AVRC_RSP_IMPL_STBL;
+                    p_rc_rsp->get_caps.count = p_bta_av_cfg->num_co_ids;
+                    memcpy(p_rc_rsp->get_caps.param.company_id, p_bta_av_cfg->p_meta_co_ids,
+                           (p_bta_av_cfg->num_co_ids << 2));
+                }
+                else if (u8 == AVRC_CAP_EVENTS_SUPPORTED)
+                {
+                    *p_ctype = AVRC_RSP_IMPL_STBL;
+                    p_rc_rsp->get_caps.count = p_bta_av_cfg->num_evt_ids;
+                    memcpy(p_rc_rsp->get_caps.param.event_id, p_bta_av_cfg->p_meta_evt_ids,
+                           p_bta_av_cfg->num_evt_ids);
+                }
+                else
+                {
+                    APPL_TRACE_DEBUG("Invalid capability ID: 0x%x", u8);
+                    /* reject - unknown capability ID */
+                    p_rc_rsp->get_caps.status = AVRC_STS_BAD_PARAM;
+                }
+            }
+            break;
+
+
+        case AVRC_PDU_REGISTER_NOTIFICATION:
+            /* make sure the event_id is implemented */
+            p_rc_rsp->rsp.status = bta_av_chk_notif_evt_id (p_vendor);
+            if (p_rc_rsp->rsp.status != BTA_AV_STS_NO_RSP)
+                evt = 0;
+            break;
+
+        }
+    }
+#else
+    APPL_TRACE_DEBUG("AVRCP 1.3 Metadata not supporteed. Reject command.");
+    /* reject invalid message without reporting to app */
+    evt = 0;
+    p_rc_rsp->rsp.status = AVRC_STS_BAD_CMD;
+#endif
+
+    return evt;
+}
+
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_msg
+**
+** Description      Process an AVRCP message from the peer.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rc_msg(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_EVT evt = 0;
+    tBTA_AV     av;
+    BT_HDR      *p_pkt = NULL;
+    tAVRC_MSG_VENDOR    *p_vendor = &p_data->rc_msg.msg.vendor;
+    BOOLEAN is_inquiry = ((p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_SPEC_INQ) || p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_GEN_INQ);
+#if (AVRC_METADATA_INCLUDED == TRUE)
+    UINT8       ctype = 0;
+    tAVRC_RESPONSE  rc_rsp;
+
+    rc_rsp.rsp.status = BTA_AV_STS_NO_RSP;
+#endif
+
+    if (p_data->rc_msg.opcode == AVRC_OP_PASS_THRU)
+    {
+    /* if this is a pass thru command */
+        if ((p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_CTRL) ||
+            (p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_SPEC_INQ) ||
+            (p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_GEN_INQ)
+            )
+        {
+        /* check if operation is supported */
+            if (p_data->rc_msg.msg.pass.op_id == AVRC_ID_VENDOR)
+            {
+                p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_NOT_IMPL;
+#if (AVRC_METADATA_INCLUDED == TRUE)
+                if (p_cb->features & BTA_AV_FEAT_METADATA)
+                    p_data->rc_msg.msg.hdr.ctype =
+                        bta_av_group_navi_supported(p_data->rc_msg.msg.pass.pass_len,
+                        p_data->rc_msg.msg.pass.p_pass_data, is_inquiry);
+#endif
+            }
+            else
+            {
+                p_data->rc_msg.msg.hdr.ctype = bta_av_op_supported(p_data->rc_msg.msg.pass.op_id, is_inquiry);
+            }
+
+            APPL_TRACE_DEBUG("ctype %d",p_data->rc_msg.msg.hdr.ctype)
+
+            /* send response */
+            if (p_data->rc_msg.msg.hdr.ctype != BTA_AV_RSP_INTERIM)
+                AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.pass);
+
+            /* set up for callback if supported */
+            if (p_data->rc_msg.msg.hdr.ctype == BTA_AV_RSP_ACCEPT || p_data->rc_msg.msg.hdr.ctype == BTA_AV_RSP_INTERIM)
+            {
+                evt = BTA_AV_REMOTE_CMD_EVT;
+                av.remote_cmd.rc_id = p_data->rc_msg.msg.pass.op_id;
+                av.remote_cmd.key_state = p_data->rc_msg.msg.pass.state;
+                av.remote_cmd.p_data = p_data->rc_msg.msg.pass.p_pass_data;
+                av.remote_cmd.len = p_data->rc_msg.msg.pass.pass_len;
+                memcpy(&av.remote_cmd.hdr, &p_data->rc_msg.msg.hdr, sizeof (tAVRC_HDR));
+                av.remote_cmd.label = p_data->rc_msg.label;
+            }
+        }
+        /* else if this is a pass thru response */
+        else if (p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_ACCEPT)
+        {
+            /* set up for callback */
+            evt = BTA_AV_REMOTE_RSP_EVT;
+            av.remote_rsp.rc_id = p_data->rc_msg.msg.pass.op_id;
+            av.remote_rsp.key_state = p_data->rc_msg.msg.pass.state;
+            av.remote_rsp.rsp_code = p_data->rc_msg.msg.hdr.ctype;
+            av.remote_rsp.label = p_data->rc_msg.label;
+        }
+        /* must be a bad ctype -> reject*/
+        else
+        {
+            p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_REJ;
+            AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.pass);
+        }
+    }
+    /* else if this is a vendor specific command or response */
+    else if (p_data->rc_msg.opcode == AVRC_OP_VENDOR)
+    {
+        /* set up for callback */
+        av.vendor_cmd.code = p_data->rc_msg.msg.hdr.ctype;
+        av.vendor_cmd.company_id = p_vendor->company_id;
+        av.vendor_cmd.label = p_data->rc_msg.label;
+        av.vendor_cmd.p_data = p_vendor->p_vendor_data;
+        av.vendor_cmd.len = p_vendor->vendor_len;
+
+        /* if configured to support vendor specific and it's a command */
+        if ((p_cb->features & BTA_AV_FEAT_VENDOR)  &&
+            p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ)
+        {
+#if (AVRC_METADATA_INCLUDED == TRUE)
+            if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
+               (p_vendor->company_id == AVRC_CO_METADATA))
+            {
+                av.meta_msg.p_msg = &p_data->rc_msg.msg;
+                evt = bta_av_proc_meta_cmd (&rc_rsp, &p_data->rc_msg, &ctype);
+            }
+            else
+#endif
+                evt = BTA_AV_VENDOR_CMD_EVT;
+        }
+        /* else if configured to support vendor specific and it's a response */
+        else if ((p_cb->features & BTA_AV_FEAT_VENDOR) &&
+                 p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_ACCEPT)
+        {
+#if (AVRC_METADATA_INCLUDED == TRUE)
+            if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
+               (p_vendor->company_id == AVRC_CO_METADATA))
+            {
+                av.meta_msg.p_msg = &p_data->rc_msg.msg;
+                evt = BTA_AV_META_MSG_EVT;
+            }
+            else
+#endif
+                evt = BTA_AV_VENDOR_RSP_EVT;
+
+        }
+        /* else if not configured to support vendor specific and it's a command */
+        else if (!(p_cb->features & BTA_AV_FEAT_VENDOR)  &&
+            p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ)
+        {
+           if(p_data->rc_msg.msg.vendor.p_vendor_data[0] == AVRC_PDU_INVALID)
+           {
+           /* reject it */
+              p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_REJ;
+              p_data->rc_msg.msg.vendor.p_vendor_data[4] = AVRC_STS_BAD_CMD;
+           }
+           else
+              p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_NOT_IMPL;
+           AVRC_VendorRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.vendor);
+        }
+    }
+#if (AVRC_METADATA_INCLUDED == TRUE)
+    if (evt == 0 && rc_rsp.rsp.status != BTA_AV_STS_NO_RSP)
+    {
+        if (!p_pkt)
+        {
+            rc_rsp.rsp.opcode = p_data->rc_msg.opcode;
+            AVRC_BldResponse (0, &rc_rsp, &p_pkt);
+        }
+        if (p_pkt)
+            AVRC_MsgReq (p_data->rc_msg.handle, p_data->rc_msg.label, ctype, p_pkt);
+    }
+#endif
+
+    /* call callback */
+    if (evt != 0)
+    {
+        av.remote_cmd.rc_handle = p_data->rc_msg.handle;
+        (*p_cb->p_cback)(evt, &av);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_close
+**
+** Description      close the specified AVRC handle.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rc_close (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
+{
+    UINT16 handle = p_data->hdr.layer_specific;
+    tBTA_AV_SCB  *p_scb;
+    tBTA_AV_RCB *p_rcb;
+
+    if(handle < BTA_AV_NUM_RCB)
+    {
+        p_rcb = &p_cb->rcb[handle];
+
+        APPL_TRACE_DEBUG("bta_av_rc_close handle: %d, status=0x%x", p_rcb->handle, p_rcb->status);
+        if(p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
+        {
+            if(p_rcb->shdl)
+            {
+                p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
+                if(p_scb)
+                {
+                    /* just in case the RC timer is active
+                    if(bta_av_cb.features & BTA_AV_FEAT_RCCT &&
+                       p_scb->chnl == BTA_AV_CHNL_AUDIO) */
+                        bta_sys_stop_timer(&p_scb->timer);
+                }
+            }
+
+            AVRC_Close(p_rcb->handle);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_get_shdl
+**
+** Returns          The index to p_scb[]
+**
+*******************************************************************************/
+static UINT8 bta_av_get_shdl(tBTA_AV_SCB *p_scb)
+{
+    int     i;
+    UINT8   shdl = 0;
+    /* find the SCB & stop the timer */
+    for(i=0; i<BTA_AV_NUM_STRS; i++)
+    {
+        if(p_scb == bta_av_cb.p_scb[i])
+        {
+            shdl = i+1;
+            break;
+        }
+    }
+    return shdl;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_stream_chg
+**
+** Description      audio streaming status changed.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_stream_chg(tBTA_AV_SCB *p_scb, BOOLEAN started)
+{
+    UINT8   started_msk;
+    int     i;
+    UINT8   *p_streams;
+    BOOLEAN no_streams = FALSE;
+    tBTA_AV_SCB *p_scbi;
+
+    started_msk = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
+    APPL_TRACE_DEBUG ("bta_av_stream_chg started:%d started_msk:x%x chnl:x%x", started,
+                                                  started_msk, p_scb->chnl);
+    if (BTA_AV_CHNL_AUDIO == p_scb->chnl)
+        p_streams = &bta_av_cb.audio_streams;
+    else
+        p_streams = &bta_av_cb.video_streams;
+
+    if (started)
+    {
+        /* Let L2CAP know this channel is processed with high priority */
+        L2CA_SetAclPriority(p_scb->peer_addr, L2CAP_PRIORITY_HIGH);
+        (*p_streams) |= started_msk;
+    }
+    else
+    {
+        (*p_streams) &= ~started_msk;
+    }
+
+    if (!started)
+    {
+        i=0;
+        if (BTA_AV_CHNL_AUDIO == p_scb->chnl)
+        {
+            if (bta_av_cb.video_streams == 0)
+                no_streams = TRUE;
+        }
+        else
+        {
+            no_streams = TRUE;
+            if ( bta_av_cb.audio_streams )
+            {
+                for (; i<BTA_AV_NUM_STRS; i++)
+                {
+                    p_scbi = bta_av_cb.p_scb[i];
+                    /* scb is used and started */
+                    if ( p_scbi && (bta_av_cb.audio_streams & BTA_AV_HNDL_TO_MSK(i))
+                        && bdcmp(p_scbi->peer_addr, p_scb->peer_addr) == 0)
+                    {
+                        no_streams = FALSE;
+                        break;
+                    }
+                }
+
+            }
+        }
+
+        APPL_TRACE_DEBUG ("no_streams:%d i:%d, audio_streams:x%x, video_streams:x%x", no_streams, i,
+                           bta_av_cb.audio_streams, bta_av_cb.video_streams);
+        if (no_streams)
+        {
+            /* Let L2CAP know this channel is processed with low priority */
+            L2CA_SetAclPriority(p_scb->peer_addr, L2CAP_PRIORITY_NORMAL);
+        }
+    }
+}
+
+
+/*******************************************************************************
+**
+** Function         bta_av_conn_chg
+**
+** Description      connetion status changed.
+**                  Open an AVRCP acceptor channel, if new conn.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_conn_chg(tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_CB   *p_cb = &bta_av_cb;
+    tBTA_AV_SCB     *p_scb = NULL;
+    tBTA_AV_SCB     *p_scbi;
+    UINT8   mask;
+    UINT8   conn_msk;
+    UINT8   old_msk;
+    int i;
+    int index = (p_data->hdr.layer_specific & BTA_AV_HNDL_MSK) - 1;
+    tBTA_AV_LCB *p_lcb;
+    tBTA_AV_LCB *p_lcb_rc;
+    tBTA_AV_RCB *p_rcb, *p_rcb2;
+    BOOLEAN     chk_restore = FALSE;
+
+    /* Validate array index*/
+    if (index < BTA_AV_NUM_STRS)
+    {
+        p_scb = p_cb->p_scb[index];
+    }
+    mask = BTA_AV_HNDL_TO_MSK(index);
+    p_lcb = bta_av_find_lcb(p_data->conn_chg.peer_addr, BTA_AV_LCB_FIND);
+    conn_msk = 1 << (index + 1);
+    if(p_data->conn_chg.is_up)
+    {
+        /* set the conned mask for this channel */
+        if(p_scb)
+        {
+            if(p_lcb)
+            {
+                p_lcb->conn_msk |= conn_msk;
+                for (i=0; i<BTA_AV_NUM_RCB; i++)
+                {
+                    if (bta_av_cb.rcb[i].lidx == p_lcb->lidx)
+                    {
+                        bta_av_cb.rcb[i].shdl = index + 1;
+                        APPL_TRACE_DEBUG("conn_chg up[%d]: %d, status=0x%x, shdl:%d, lidx:%d", i,
+                                          bta_av_cb.rcb[i].handle, bta_av_cb.rcb[i].status,
+                                          bta_av_cb.rcb[i].shdl, bta_av_cb.rcb[i].lidx);
+                        break;
+                    }
+                }
+            }
+            if (p_scb->chnl == BTA_AV_CHNL_AUDIO)
+            {
+                old_msk = p_cb->conn_audio;
+                p_cb->conn_audio |= mask;
+            }
+            else
+            {
+                old_msk = p_cb->conn_video;
+                p_cb->conn_video |= mask;
+            }
+
+            if ((old_msk & mask) == 0)
+            {
+                /* increase the audio open count, if not set yet */
+                bta_av_cb.audio_open_cnt++;
+            }
+
+
+            APPL_TRACE_DEBUG("rc_acp_handle:%d rc_acp_idx:%d", p_cb->rc_acp_handle, p_cb->rc_acp_idx);
+            /* check if the AVRCP ACP channel is already connected */
+            if(p_lcb && p_cb->rc_acp_handle != BTA_AV_RC_HANDLE_NONE && p_cb->rc_acp_idx)
+            {
+                p_lcb_rc = &p_cb->lcb[BTA_AV_NUM_LINKS];
+                APPL_TRACE_DEBUG("rc_acp is connected && conn_chg on same addr p_lcb_rc->conn_msk:x%x",
+                                  p_lcb_rc->conn_msk);
+                /* check if the RC is connected to the scb addr */
+                APPL_TRACE_DEBUG ("p_lcb_rc->addr: %02x:%02x:%02x:%02x:%02x:%02x",
+                       p_lcb_rc->addr[0], p_lcb_rc->addr[1], p_lcb_rc->addr[2], p_lcb_rc->addr[3],
+                       p_lcb_rc->addr[4], p_lcb_rc->addr[5]);
+                APPL_TRACE_DEBUG ("conn_chg.peer_addr: %02x:%02x:%02x:%02x:%02x:%02x",
+                       p_data->conn_chg.peer_addr[0], p_data->conn_chg.peer_addr[1],
+                       p_data->conn_chg.peer_addr[2],
+                       p_data->conn_chg.peer_addr[3], p_data->conn_chg.peer_addr[4],
+                       p_data->conn_chg.peer_addr[5]);
+                if (p_lcb_rc->conn_msk && bdcmp(p_lcb_rc->addr, p_data->conn_chg.peer_addr) == 0)
+                {
+                    /* AVRCP is already connected.
+                     * need to update the association betwen SCB and RCB */
+                    p_lcb_rc->conn_msk = 0; /* indicate RC ONLY is not connected */
+                    p_lcb_rc->lidx = 0;
+                    p_scb->rc_handle = p_cb->rc_acp_handle;
+                    p_rcb = &p_cb->rcb[p_cb->rc_acp_idx - 1];
+                    p_rcb->shdl = bta_av_get_shdl(p_scb);
+                    APPL_TRACE_DEBUG("update rc_acp shdl:%d/%d srch:%d", index + 1, p_rcb->shdl,
+                                      p_scb->rc_handle );
+
+                    p_rcb2 = bta_av_get_rcb_by_shdl(p_rcb->shdl);
+                    if (p_rcb2)
+                    {
+                        /* found the RCB that was created to associated with this SCB */
+                        p_cb->rc_acp_handle = p_rcb2->handle;
+                        p_cb->rc_acp_idx = (p_rcb2 - p_cb->rcb) + 1;
+                        APPL_TRACE_DEBUG("new rc_acp_handle:%d, idx:%d", p_cb->rc_acp_handle,
+                                           p_cb->rc_acp_idx);
+                        p_rcb2->lidx = (BTA_AV_NUM_LINKS + 1);
+                        APPL_TRACE_DEBUG("rc2 handle:%d lidx:%d/%d",p_rcb2->handle, p_rcb2->lidx,
+                                          p_cb->lcb[p_rcb2->lidx-1].lidx);
+                    }
+                    p_rcb->lidx = p_lcb->lidx;
+                    APPL_TRACE_DEBUG("rc handle:%d lidx:%d/%d",p_rcb->handle, p_rcb->lidx,
+                                      p_cb->lcb[p_rcb->lidx-1].lidx);
+                }
+            }
+        }
+    }
+    else
+    {
+        if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt)
+        {
+            /* this channel is still marked as open. decrease the count */
+            bta_av_cb.audio_open_cnt--;
+        }
+
+        /* clear the conned mask for this channel */
+        p_cb->conn_audio &= ~mask;
+        p_cb->conn_video &= ~mask;
+        if(p_scb)
+        {
+            /* the stream is closed.
+             * clear the peer address, so it would not mess up the AVRCP for the next round of operation */
+            bdcpy(p_scb->peer_addr, bd_addr_null);
+            if(p_scb->chnl == BTA_AV_CHNL_AUDIO)
+            {
+                if(p_lcb)
+                {
+                    p_lcb->conn_msk &= ~conn_msk;
+                }
+                /* audio channel is down. make sure the INT channel is down */
+                /* just in case the RC timer is active
+                if(p_cb->features & BTA_AV_FEAT_RCCT) */
+                {
+                    bta_sys_stop_timer(&p_scb->timer);
+                }
+                /* one audio channel goes down. check if we need to restore high priority */
+                chk_restore = TRUE;
+            }
+        }
+
+        APPL_TRACE_DEBUG("bta_av_conn_chg shdl:%d", index + 1);
+        for (i=0; i<BTA_AV_NUM_RCB; i++)
+        {
+            APPL_TRACE_DEBUG("conn_chg dn[%d]: %d, status=0x%x, shdl:%d, lidx:%d", i,
+                              bta_av_cb.rcb[i].handle, bta_av_cb.rcb[i].status,
+                              bta_av_cb.rcb[i].shdl, bta_av_cb.rcb[i].lidx);
+            if(bta_av_cb.rcb[i].shdl == index + 1)
+            {
+                bta_av_del_rc(&bta_av_cb.rcb[i]);
+                break;
+            }
+        }
+
+        if(p_cb->conn_audio == 0 && p_cb->conn_video == 0)
+        {
+            /* if both channels are not connected,
+             * close all RC channels */
+            bta_av_close_all_rc(p_cb);
+        }
+
+        /* if the AVRCP is no longer listening, create the listening channel */
+        if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE && bta_av_cb.features & BTA_AV_FEAT_RCTG)
+            bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
+    }
+
+    APPL_TRACE_DEBUG("bta_av_conn_chg audio:%x video:%x up:%d conn_msk:0x%x chk_restore:%d audio_open_cnt:%d",
+        p_cb->conn_audio, p_cb->conn_video, p_data->conn_chg.is_up, conn_msk, chk_restore, p_cb->audio_open_cnt);
+
+    if (chk_restore)
+    {
+        if (p_cb->audio_open_cnt == 1)
+        {
+            /* one audio channel goes down and there's one audio channel remains open.
+             * restore the switch role in default link policy */
+            bta_sys_set_default_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH);
+            /* allow role switch, if this is the last connection */
+            bta_av_restore_switch();
+        }
+        if (p_cb->audio_open_cnt)
+        {
+            /* adjust flush timeout settings to longer period */
+            for (i=0; i<BTA_AV_NUM_STRS; i++)
+            {
+                p_scbi = bta_av_cb.p_scb[i];
+                if (p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started)
+                {
+                    /* may need to update the flush timeout of this already started stream */
+                    if (p_scbi->co_started != bta_av_cb.audio_open_cnt)
+                    {
+                        p_scbi->co_started = bta_av_cb.audio_open_cnt;
+                        L2CA_SetFlushTimeout(p_scbi->peer_addr, p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1] );
+                    }
+                }
+            }
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_disable
+**
+** Description      disable AV.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_disable(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
+{
+    BT_HDR  hdr;
+    UINT16  xx;
+    UNUSED(p_data);
+
+    p_cb->disabling = TRUE;
+
+    bta_av_close_all_rc(p_cb);
+
+    utl_freebuf((void **) &p_cb->p_disc_db);
+
+    /* disable audio/video - de-register all channels,
+     * expect BTA_AV_DEREG_COMP_EVT when deregister is complete */
+    for(xx=0; xx<BTA_AV_NUM_STRS; xx++)
+    {
+        hdr.layer_specific = xx + 1;
+        bta_av_api_deregister((tBTA_AV_DATA *)&hdr);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_api_disconnect
+**
+** Description      .
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_api_disconnect(tBTA_AV_DATA *p_data)
+{
+    AVDT_DisconnectReq(p_data->api_discnt.bd_addr, bta_av_conn_cback);
+    bta_sys_stop_timer(&bta_av_cb.sig_tmr);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_sig_chg
+**
+** Description      process AVDT signal channel up/down.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_sig_chg(tBTA_AV_DATA *p_data)
+{
+    UINT16 event = p_data->str_msg.hdr.layer_specific;
+    tBTA_AV_CB   *p_cb = &bta_av_cb;
+    int     xx;
+    UINT8   mask;
+    tBTA_AV_LCB *p_lcb = NULL;
+
+    APPL_TRACE_DEBUG("bta_av_sig_chg event: %d", event);
+    if(event == AVDT_CONNECT_IND_EVT)
+    {
+        p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FIND);
+        if(!p_lcb)
+        {
+            /* if the address does not have an LCB yet, alloc one */
+            for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
+            {
+                mask = 1 << xx;
+                APPL_TRACE_DEBUG("conn_lcb: 0x%x", p_cb->conn_lcb);
+
+                /* look for a p_lcb with its p_scb registered */
+                if((!(mask & p_cb->conn_lcb)) && (p_cb->p_scb[xx] != NULL))
+                {
+                    p_lcb = &p_cb->lcb[xx];
+                    p_lcb->lidx = xx + 1;
+                    bdcpy(p_lcb->addr, p_data->str_msg.bd_addr);
+                    p_lcb->conn_msk = 0; /* clear the connect mask */
+                    /* start listening when the signal channel is open */
+                    if (p_cb->features & BTA_AV_FEAT_RCTG)
+                    {
+                        bta_av_rc_create(p_cb, AVCT_ACP, 0, p_lcb->lidx);
+                    }
+                    /* this entry is not used yet. */
+                    p_cb->conn_lcb |= mask;     /* mark it as used */
+                    APPL_TRACE_DEBUG("start sig timer %d", p_data->hdr.offset);
+                    if (p_data->hdr.offset == AVDT_ACP)
+                    {
+                        APPL_TRACE_DEBUG("Incoming L2CAP acquired, set state as incoming", NULL);
+                        bdcpy(p_cb->p_scb[xx]->peer_addr, p_data->str_msg.bd_addr);
+                        p_cb->p_scb[xx]->use_rc = TRUE;     /* allowing RC for incoming connection */
+                        bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_ACP_CONNECT_EVT, p_data);
+
+                        /* The Pending Event should be sent as soon as the L2CAP signalling channel
+                         * is set up, which is NOW. Earlier this was done only after
+                         * BTA_AV_SIG_TIME_VAL milliseconds.
+                         * The following function shall send the event and start the recurring timer
+                         */
+                        bta_av_sig_timer(NULL);
+
+                        /* Possible collision : need to avoid outgoing processing while the timer is running */
+                        p_cb->p_scb[xx]->coll_mask = BTA_AV_COLL_INC_TMR;
+
+                        p_cb->acp_sig_tmr.param = (UINT32)xx;
+                        p_cb->acp_sig_tmr.p_cback = (TIMER_CBACK*)&bta_av_acp_sig_timer_cback;
+                        bta_sys_start_timer(&p_cb->acp_sig_tmr, 0, BTA_AV_ACP_SIG_TIME_VAL);
+                    }
+                    break;
+                }
+            }
+
+            /* check if we found something */
+            if (xx == BTA_AV_NUM_LINKS)
+            {
+                /* We do not have scb for this avdt connection.     */
+                /* Silently close the connection.                   */
+                APPL_TRACE_ERROR("av scb not available for avdt connection");
+                AVDT_DisconnectReq (p_data->str_msg.bd_addr, NULL);
+                return;
+            }
+        }
+    }
+#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
+    else if (event == BTA_AR_AVDT_CONN_EVT)
+    {
+        bta_sys_stop_timer(&bta_av_cb.sig_tmr);
+    }
+#endif
+    else
+    {
+        /* disconnected. */
+        p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FREE);
+        if(p_lcb && p_lcb->conn_msk)
+        {
+            APPL_TRACE_DEBUG("conn_msk: 0x%x", p_lcb->conn_msk);
+            /* clean up ssm  */
+            for(xx=0; xx < BTA_AV_NUM_STRS; xx++)
+            {
+                mask = 1 << (xx + 1);
+                if ((mask & p_lcb->conn_msk) && (p_cb->p_scb[xx]) &&
+                    (bdcmp(p_cb->p_scb[xx]->peer_addr, p_data->str_msg.bd_addr) == 0))
+                {
+                    bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_AVDT_DISCONNECT_EVT, NULL);
+                }
+            }
+        }
+    }
+    APPL_TRACE_DEBUG("conn_lcb: 0x%x", p_cb->conn_lcb);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_sig_timer
+**
+** Description      process the signal channel timer. This timer is started
+**                  when the AVDTP signal channel is connected. If no profile
+**                  is connected, the timer goes off every BTA_AV_SIG_TIME_VAL
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_sig_timer(tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_CB   *p_cb = &bta_av_cb;
+    int     xx;
+    UINT8   mask;
+    tBTA_AV_LCB *p_lcb = NULL;
+    tBTA_AV_PEND pend;
+    UNUSED(p_data);
+
+    APPL_TRACE_DEBUG("bta_av_sig_timer");
+    for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
+    {
+        mask = 1 << xx;
+        if(mask & p_cb->conn_lcb)
+        {
+            /* this entry is used. check if it is connected */
+            p_lcb = &p_cb->lcb[xx];
+            if(!p_lcb->conn_msk)
+            {
+                bta_sys_start_timer(&p_cb->sig_tmr, BTA_AV_SIG_TIMER_EVT, BTA_AV_SIG_TIME_VAL);
+                bdcpy(pend.bd_addr, p_lcb->addr);
+                (*p_cb->p_cback)(BTA_AV_PENDING_EVT, (tBTA_AV *) &pend);
+            }
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_acp_sig_timer_cback
+**
+** Description      Process the timeout when SRC is accepting connection
+**                  and SNK did not start signalling.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_acp_sig_timer_cback (TIMER_LIST_ENT *p_tle)
+{
+    UINT8   inx = (UINT8)p_tle->param;
+    tBTA_AV_CB  *p_cb = &bta_av_cb;
+    tBTA_AV_SCB *p_scb = NULL;
+    tBTA_AV_API_OPEN  *p_buf;
+    if (inx < BTA_AV_NUM_STRS)
+    {
+        p_scb = p_cb->p_scb[inx];
+    }
+    if (p_scb)
+    {
+        APPL_TRACE_DEBUG("bta_av_acp_sig_timer_cback, coll_mask = 0x%02X", p_scb->coll_mask);
+
+        if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
+        {
+            p_scb->coll_mask &= ~BTA_AV_COLL_INC_TMR;
+
+            if (bta_av_is_scb_opening(p_scb))
+            {
+                if (p_scb->p_disc_db)
+                {
+                    /* We are still doing SDP. Run the timer again. */
+                    p_scb->coll_mask |= BTA_AV_COLL_INC_TMR;
+
+                    p_cb->acp_sig_tmr.param = (UINT32)inx;
+                    p_cb->acp_sig_tmr.p_cback = (TIMER_CBACK *)&bta_av_acp_sig_timer_cback;
+                    bta_sys_start_timer(&p_cb->acp_sig_tmr, 0, BTA_AV_ACP_SIG_TIME_VAL);
+                }
+                else
+                {
+                    /* SNK did not start signalling, resume signalling process. */
+                    bta_av_discover_req (p_scb, NULL);
+                }
+            }
+            else if (bta_av_is_scb_incoming(p_scb))
+            {
+                /* Stay in incoming state if SNK does not start signalling */
+
+                /* API open was called right after SNK opened L2C connection. */
+                if (p_scb->coll_mask & BTA_AV_COLL_API_CALLED)
+                {
+                    p_scb->coll_mask &= ~BTA_AV_COLL_API_CALLED;
+
+                    /* BTA_AV_API_OPEN_EVT */
+                    if ((p_buf = (tBTA_AV_API_OPEN *) GKI_getbuf(sizeof(tBTA_AV_API_OPEN))) != NULL)
+                    {
+                        memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
+                        bta_sys_sendmsg(p_buf);
+                    }
+                }
+            }
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_check_peer_features
+**
+** Description      check supported features on the peer device from the SDP record
+**                  and return the feature mask
+**
+** Returns          tBTA_AV_FEAT peer device feature mask
+**
+*******************************************************************************/
+tBTA_AV_FEAT bta_av_check_peer_features (UINT16 service_uuid)
+{
+    tBTA_AV_FEAT peer_features = 0;
+    tBTA_AV_CB   *p_cb = &bta_av_cb;
+    tSDP_DISC_REC       *p_rec = NULL;
+    tSDP_DISC_ATTR      *p_attr;
+    UINT16              peer_rc_version=0;
+    UINT16              categories = 0;
+
+    APPL_TRACE_DEBUG("bta_av_check_peer_features service_uuid:x%x", service_uuid);
+    /* loop through all records we found */
+    while (TRUE)
+    {
+        /* get next record; if none found, we're done */
+        if ((p_rec = SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, p_rec)) == NULL)
+        {
+            break;
+        }
+
+        if (( SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_CLASS_ID_LIST)) != NULL)
+        {
+            /* find peer features */
+            if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REMOTE_CONTROL, NULL))
+            {
+                peer_features |= BTA_AV_FEAT_RCCT;
+            }
+            if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REM_CTRL_TARGET, NULL))
+            {
+                peer_features |= BTA_AV_FEAT_RCTG;
+            }
+        }
+
+        if (( SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST)) != NULL)
+        {
+            /* get profile version (if failure, version parameter is not updated) */
+            SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL, &peer_rc_version);
+            APPL_TRACE_DEBUG("peer_rc_version 0x%x", peer_rc_version);
+
+            if (peer_rc_version >= AVRC_REV_1_3)
+                peer_features |= (BTA_AV_FEAT_VENDOR | BTA_AV_FEAT_METADATA);
+
+            if (peer_rc_version >= AVRC_REV_1_4)
+            {
+                peer_features |= (BTA_AV_FEAT_ADV_CTRL);
+                /* get supported categories */
+                if ((p_attr = SDP_FindAttributeInRec(p_rec,
+                                ATTR_ID_SUPPORTED_FEATURES)) != NULL)
+                {
+                    categories = p_attr->attr_value.v.u16;
+                    if (categories & AVRC_SUPF_CT_BROWSE)
+                        peer_features |= (BTA_AV_FEAT_BROWSE);
+                }
+            }
+        }
+    }
+    APPL_TRACE_DEBUG("peer_features:x%x", peer_features);
+    return peer_features;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_disc_done
+**
+** Description      Handle AVRCP service discovery results.  If matching
+**                  service found, open AVRCP connection.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rc_disc_done(tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_CB   *p_cb = &bta_av_cb;
+    tBTA_AV_SCB  *p_scb = NULL;
+    tBTA_AV_LCB  *p_lcb;
+    tBTA_AV_RC_OPEN rc_open;
+    tBTA_AV_RC_FEAT rc_feat;
+    UINT8               rc_handle;
+    tBTA_AV_FEAT        peer_features;  /* peer features mask */
+    UNUSED(p_data);
+
+    APPL_TRACE_DEBUG("bta_av_rc_disc_done disc:x%x", p_cb->disc);
+    if (!p_cb->disc)
+    {
+        return;
+    }
+
+    if ((p_cb->disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK)
+    {
+        /* this is the rc handle/index to tBTA_AV_RCB */
+        rc_handle = p_cb->disc & (~BTA_AV_CHNL_MSK);
+    }
+    else
+    {
+        /* Validate array index*/
+        if (((p_cb->disc & BTA_AV_HNDL_MSK) - 1) < BTA_AV_NUM_STRS)
+        {
+            p_scb = p_cb->p_scb[(p_cb->disc & BTA_AV_HNDL_MSK) - 1];
+        }
+        if (p_scb)
+            rc_handle = p_scb->rc_handle;
+        else
+        {
+            p_cb->disc = 0;
+            return;
+        }
+    }
+
+    APPL_TRACE_DEBUG("rc_handle %d", rc_handle);
+    /* check peer version and whether support CT and TG role */
+    peer_features = bta_av_check_peer_features (UUID_SERVCLASS_AV_REMOTE_CONTROL);
+    if ((p_cb->features & BTA_AV_FEAT_ADV_CTRL) && ((peer_features&BTA_AV_FEAT_ADV_CTRL) == 0))
+    {
+        /* if we support advance control and peer does not, check their support on TG role
+         * some implementation uses 1.3 on CT ans 1.4 on TG */
+        peer_features |= bta_av_check_peer_features (UUID_SERVCLASS_AV_REM_CTRL_TARGET);
+    }
+
+    p_cb->disc = 0;
+    utl_freebuf((void **) &p_cb->p_disc_db);
+
+    APPL_TRACE_DEBUG("peer_features 0x%x, features 0x%x", peer_features, p_cb->features);
+
+    /* if we have no rc connection */
+    if (rc_handle == BTA_AV_RC_HANDLE_NONE)
+    {
+        if (p_scb)
+        {
+            /* if peer remote control service matches ours and USE_RC is TRUE */
+            if ((((p_cb->features & BTA_AV_FEAT_RCCT) && (peer_features & BTA_AV_FEAT_RCTG)) ||
+                 ((p_cb->features & BTA_AV_FEAT_RCTG) && (peer_features & BTA_AV_FEAT_RCCT))) )
+            {
+                p_lcb = bta_av_find_lcb(p_scb->peer_addr, BTA_AV_LCB_FIND);
+                if(p_lcb)
+                {
+                    rc_handle = bta_av_rc_create(p_cb, AVCT_INT, (UINT8)(p_scb->hdi + 1), p_lcb->lidx);
+                    p_cb->rcb[rc_handle].peer_features = peer_features;
+                }
+#if (BT_USE_TRACES == TRUE || BT_TRACE_APPL == TRUE)
+                else
+                {
+                    APPL_TRACE_ERROR("can not find LCB!!");
+                }
+#endif
+            }
+            else if(p_scb->use_rc)
+            {
+                /* can not find AVRC on peer device. report failure */
+                p_scb->use_rc = FALSE;
+                bdcpy(rc_open.peer_addr, p_scb->peer_addr);
+                rc_open.peer_features = 0;
+                rc_open.status = BTA_AV_FAIL_SDP;
+                (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, (tBTA_AV *) &rc_open);
+            }
+        }
+    }
+    else
+    {
+        p_cb->rcb[rc_handle].peer_features = peer_features;
+        rc_feat.rc_handle =  rc_handle;
+        rc_feat.peer_features = peer_features;
+        (*p_cb->p_cback)(BTA_AV_RC_FEAT_EVT, (tBTA_AV *) &rc_feat);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_closed
+**
+** Description      Set AVRCP state to closed.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rc_closed(tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_CB   *p_cb = &bta_av_cb;
+    tBTA_AV_RC_CLOSE rc_close;
+    tBTA_AV_RC_CONN_CHG *p_msg = (tBTA_AV_RC_CONN_CHG *)p_data;
+    tBTA_AV_RCB    *p_rcb;
+    tBTA_AV_SCB    *p_scb;
+    int i;
+    BOOLEAN conn = FALSE;
+    tBTA_AV_LCB *p_lcb;
+
+    rc_close.rc_handle = BTA_AV_RC_HANDLE_NONE;
+    p_scb = NULL;
+    APPL_TRACE_DEBUG("bta_av_rc_closed rc_handle:%d", p_msg->handle);
+    for(i=0; i<BTA_AV_NUM_RCB; i++)
+    {
+        p_rcb = &p_cb->rcb[i];
+        APPL_TRACE_DEBUG("bta_av_rc_closed rcb[%d] rc_handle:%d, status=0x%x", i, p_rcb->handle, p_rcb->status);
+        if(p_rcb->handle == p_msg->handle)
+        {
+            rc_close.rc_handle = i;
+            p_rcb->status &= ~BTA_AV_RC_CONN_MASK;
+            p_rcb->peer_features = 0;
+            APPL_TRACE_DEBUG("       shdl:%d, lidx:%d", p_rcb->shdl, p_rcb->lidx);
+            if(p_rcb->shdl)
+            {
+                if ((p_rcb->shdl - 1) < BTA_AV_NUM_STRS)
+                {
+                    p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
+                }
+                if(p_scb)
+                {
+                    bdcpy(rc_close.peer_addr, p_scb->peer_addr);
+                    if(p_scb->rc_handle == p_rcb->handle)
+                        p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
+                    APPL_TRACE_DEBUG("shdl:%d, srch:%d", p_rcb->shdl, p_scb->rc_handle);
+                }
+                p_rcb->shdl = 0;
+            }
+            else if(p_rcb->lidx == (BTA_AV_NUM_LINKS + 1) )
+            {
+                /* if the RCB uses the extra LCB, use the addr for event and clean it */
+                p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
+                bdcpy(rc_close.peer_addr, p_msg->peer_addr);
+                APPL_TRACE_DEBUG("rc_only closed bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
+                              p_msg->peer_addr[0], p_msg->peer_addr[1],
+                              p_msg->peer_addr[2], p_msg->peer_addr[3],
+                              p_msg->peer_addr[4], p_msg->peer_addr[5]);
+                p_lcb->conn_msk = 0;
+                p_lcb->lidx = 0;
+            }
+            p_rcb->lidx = 0;
+
+            if((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT)
+            {
+                /* AVCT CCB is deallocated */
+                p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
+                p_rcb->status = 0;
+            }
+            else
+            {
+                /* AVCT CCB is still there. dealloc */
+                bta_av_del_rc(p_rcb);
+
+                /* if the AVRCP is no longer listening, create the listening channel */
+                if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE && bta_av_cb.features & BTA_AV_FEAT_RCTG)
+                    bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
+            }
+        }
+        else if((p_rcb->handle != BTA_AV_RC_HANDLE_NONE) && (p_rcb->status & BTA_AV_RC_CONN_MASK))
+        {
+            /* at least one channel is still connected */
+            conn = TRUE;
+        }
+    }
+
+    if(!conn)
+    {
+        /* no AVRC channels are connected, go back to INIT state */
+        bta_av_sm_execute(p_cb, BTA_AV_AVRC_NONE_EVT, NULL);
+    }
+
+    if (rc_close.rc_handle == BTA_AV_RC_HANDLE_NONE)
+    {
+        rc_close.rc_handle = p_msg->handle;
+        bdcpy(rc_close.peer_addr, p_msg->peer_addr);
+    }
+    (*p_cb->p_cback)(BTA_AV_RC_CLOSE_EVT, (tBTA_AV *) &rc_close);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rc_disc
+**
+** Description      start AVRC SDP discovery.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_rc_disc(UINT8 disc)
+{
+    tBTA_AV_CB   *p_cb = &bta_av_cb;
+    tAVRC_SDP_DB_PARAMS db_params;
+      UINT16              attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
+                                       ATTR_ID_BT_PROFILE_DESC_LIST,
+                                       ATTR_ID_SUPPORTED_FEATURES};
+    UINT8       hdi;
+    tBTA_AV_SCB *p_scb;
+    UINT8       *p_addr = NULL;
+    UINT8       rc_handle;
+
+    APPL_TRACE_DEBUG("bta_av_rc_disc 0x%x, %d", disc, bta_av_cb.disc);
+    if ((bta_av_cb.disc != 0) || (disc == 0))
+        return;
+
+    if ((disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK)
+    {
+        /* this is the rc handle/index to tBTA_AV_RCB */
+        rc_handle = disc & (~BTA_AV_CHNL_MSK);
+        if (p_cb->rcb[rc_handle].lidx)
+        {
+            p_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx-1].addr;
+        }
+    }
+    else
+    {
+        hdi = (disc & BTA_AV_HNDL_MSK) - 1;
+        p_scb = p_cb->p_scb[hdi];
+
+        if (p_scb)
+        {
+            APPL_TRACE_DEBUG("rc_handle %d", p_scb->rc_handle);
+            p_addr = p_scb->peer_addr;
+        }
+    }
+
+    if (p_addr)
+    {
+        /* allocate discovery database */
+        if (p_cb->p_disc_db == NULL)
+        {
+            p_cb->p_disc_db = (tSDP_DISCOVERY_DB *) GKI_getbuf(BTA_AV_DISC_BUF_SIZE);
+        }
+
+        if (p_cb->p_disc_db)
+        {
+            /* set up parameters */
+            db_params.db_len = BTA_AV_DISC_BUF_SIZE;
+            db_params.num_attr = 3;
+            db_params.p_db = p_cb->p_disc_db;
+            db_params.p_attrs = attr_list;
+
+            /* searching for UUID_SERVCLASS_AV_REMOTE_CONTROL gets both TG and CT */
+            if (AVRC_FindService(UUID_SERVCLASS_AV_REMOTE_CONTROL, p_addr, &db_params,
+                            bta_av_avrc_sdp_cback) == AVRC_SUCCESS)
+            {
+                p_cb->disc = disc;
+                APPL_TRACE_DEBUG("disc %d", p_cb->disc);
+            }
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_dereg_comp
+**
+** Description      deregister complete. free the stream control block.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_dereg_comp(tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_CB   *p_cb = &bta_av_cb;
+    tBTA_AV_SCB  *p_scb;
+    tBTA_UTL_COD    cod;
+    UINT8   mask;
+    BT_HDR  *p_buf;
+
+    /* find the stream control block */
+    p_scb = bta_av_hndl_to_scb(p_data->hdr.layer_specific);
+
+    if(p_scb)
+    {
+        APPL_TRACE_DEBUG("deregistered %d(h%d)", p_scb->chnl, p_scb->hndl);
+        mask = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
+        if(p_scb->chnl == BTA_AV_CHNL_AUDIO)
+        {
+            p_cb->reg_audio  &= ~mask;
+            if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt)
+            {
+                /* this channel is still marked as open. decrease the count */
+                bta_av_cb.audio_open_cnt--;
+            }
+            p_cb->conn_audio &= ~mask;
+
+            if (p_scb->q_tag == BTA_AV_Q_TAG_STREAM && p_scb->a2d_list) {
+                /* make sure no buffers are in a2d_list */
+                while (!list_is_empty(p_scb->a2d_list)) {
+                    p_buf = (BT_HDR*)list_front(p_scb->a2d_list);
+                    list_remove(p_scb->a2d_list, p_buf);
+                    GKI_freebuf(p_buf);
+                }
+            }
+
+            /* remove the A2DP SDP record, if no more audio stream is left */
+            if(!p_cb->reg_audio)
+            {
+#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
+                bta_ar_dereg_avrc (UUID_SERVCLASS_AV_REMOTE_CONTROL, BTA_ID_AV);
+#endif
+                bta_av_del_sdp_rec(&p_cb->sdp_a2d_handle);
+                bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
+
+#if (BTA_AV_SINK_INCLUDED == TRUE)
+                bta_av_del_sdp_rec(&p_cb->sdp_a2d_snk_handle);
+                bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SINK);
+#endif
+            }
+        }
+        else
+        {
+            p_cb->reg_video  &= ~mask;
+            /* make sure that this channel is not connected */
+            p_cb->conn_video &= ~mask;
+            /* remove the VDP SDP record, (only one video stream at most) */
+            bta_av_del_sdp_rec(&p_cb->sdp_vdp_handle);
+            bta_sys_remove_uuid(UUID_SERVCLASS_VIDEO_SOURCE);
+        }
+
+        /* make sure that the timer is not active */
+        bta_sys_stop_timer(&p_scb->timer);
+        utl_freebuf((void **)&p_cb->p_scb[p_scb->hdi]);
+    }
+
+    APPL_TRACE_DEBUG("audio 0x%x, video: 0x%x, disable:%d",
+        p_cb->reg_audio, p_cb->reg_video, p_cb->disabling);
+    /* if no stream control block is active */
+    if((p_cb->reg_audio + p_cb->reg_video) == 0)
+    {
+#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
+        /* deregister from AVDT */
+        bta_ar_dereg_avdt(BTA_ID_AV);
+
+        /* deregister from AVCT */
+        bta_ar_dereg_avrc (UUID_SERVCLASS_AV_REM_CTRL_TARGET, BTA_ID_AV);
+        bta_ar_dereg_avct(BTA_ID_AV);
+#endif
+
+        if(p_cb->disabling)
+        {
+            p_cb->disabling     = FALSE;
+            bta_av_cb.features  = 0;
+        }
+
+        /* Clear the Capturing service class bit */
+        cod.service = BTM_COD_SERVICE_CAPTURING;
+        utl_set_device_class(&cod, BTA_UTL_CLR_COD_SERVICE_CLASS);
+    }
+}
+#endif /* BTA_AV_INCLUDED */
diff --git a/components/bt/bluedroid/bta/av/bta_av_api.c b/components/bt/bluedroid/bta/av/bta_av_api.c
new file mode 100755 (executable)
index 0000000..ecc05e1
--- /dev/null
@@ -0,0 +1,607 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2011-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This is the implementation of the API for the advanced audio/video (AV)
+ *  subsystem of BTA, Broadcom's Bluetooth application layer for mobile
+ *  phones.
+ *
+ ******************************************************************************/
+
+#include "bt_target.h"
+#if defined(BTA_AV_INCLUDED) && (BTA_AV_INCLUDED == TRUE)
+
+#include "bta_api.h"
+#include "bta_sys.h"
+#include "bta_av_api.h"
+#include "bta_av_int.h"
+#include "gki.h"
+#include <string.h>
+
+/*****************************************************************************
+**  Constants
+*****************************************************************************/
+
+static const tBTA_SYS_REG bta_av_reg =
+{
+    bta_av_hdl_event,
+    BTA_AvDisable
+};
+
+/*******************************************************************************
+**
+** Function         BTA_AvEnable
+**
+** Description      Enable the advanced audio/video service. When the enable
+**                  operation is complete the callback function will be
+**                  called with a BTA_AV_ENABLE_EVT. This function must
+**                  be called before other function in the AV API are
+**                  called.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvEnable(tBTA_SEC sec_mask, tBTA_AV_FEAT features, tBTA_AV_CBACK *p_cback)
+{
+    tBTA_AV_API_ENABLE  *p_buf;
+
+    /* register with BTA system manager */
+    bta_sys_register(BTA_ID_AV, &bta_av_reg);
+
+    if ((p_buf = (tBTA_AV_API_ENABLE *) GKI_getbuf(sizeof(tBTA_AV_API_ENABLE))) != NULL)
+    {
+        p_buf->hdr.event = BTA_AV_API_ENABLE_EVT;
+        p_buf->p_cback  = p_cback;
+        p_buf->features = features;
+        p_buf->sec_mask = sec_mask;
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvDisable
+**
+** Description      Disable the advanced audio/video service.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvDisable(void)
+{
+    BT_HDR  *p_buf;
+
+    bta_sys_deregister(BTA_ID_AV);
+    if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
+    {
+        p_buf->event = BTA_AV_API_DISABLE_EVT;
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvRegister
+**
+** Description      Register the audio or video service to stack. When the
+**                  operation is complete the callback function will be
+**                  called with a BTA_AV_REGISTER_EVT. This function must
+**                  be called before AVDT stream is open.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvRegister(tBTA_AV_CHNL chnl, const char *p_service_name, UINT8 app_id, tBTA_AV_DATA_CBACK  *p_data_cback)
+{
+    tBTA_AV_API_REG  *p_buf;
+
+
+    if ((p_buf = (tBTA_AV_API_REG *) GKI_getbuf(sizeof(tBTA_AV_API_REG))) != NULL)
+    {
+        p_buf->hdr.layer_specific   = chnl;
+        p_buf->hdr.event = BTA_AV_API_REGISTER_EVT;
+        if(p_service_name)
+        {
+            BCM_STRNCPY_S(p_buf->p_service_name, sizeof(p_buf->p_service_name), p_service_name, BTA_SERVICE_NAME_LEN);
+            p_buf->p_service_name[BTA_SERVICE_NAME_LEN-1] = 0;
+        }
+        else
+        {
+            p_buf->p_service_name[0] = 0;
+        }
+        p_buf->app_id = app_id;
+        p_buf->p_app_data_cback = p_data_cback;
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvDeregister
+**
+** Description      Deregister the audio or video service
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvDeregister(tBTA_AV_HNDL hndl)
+{
+    BT_HDR  *p_buf;
+
+    if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
+    {
+        p_buf->layer_specific   = hndl;
+        p_buf->event = BTA_AV_API_DEREGISTER_EVT;
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvOpen
+**
+** Description      Opens an advanced audio/video connection to a peer device.
+**                  When connection is open callback function is called
+**                  with a BTA_AV_OPEN_EVT.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvOpen(BD_ADDR bd_addr, tBTA_AV_HNDL handle, BOOLEAN use_rc, tBTA_SEC sec_mask,
+                                                                             UINT16 uuid)
+{
+    tBTA_AV_API_OPEN  *p_buf;
+
+    if ((p_buf = (tBTA_AV_API_OPEN *) GKI_getbuf(sizeof(tBTA_AV_API_OPEN))) != NULL)
+    {
+        p_buf->hdr.event = BTA_AV_API_OPEN_EVT;
+        p_buf->hdr.layer_specific   = handle;
+        bdcpy(p_buf->bd_addr, bd_addr);
+        p_buf->use_rc = use_rc;
+        p_buf->sec_mask = sec_mask;
+        p_buf->switch_res = BTA_AV_RS_NONE;
+        p_buf->uuid = uuid;
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvClose
+**
+** Description      Close the current streams.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvClose(tBTA_AV_HNDL handle)
+{
+    BT_HDR  *p_buf;
+
+    if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
+    {
+        p_buf->event = BTA_AV_API_CLOSE_EVT;
+        p_buf->layer_specific   = handle;
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvDisconnect
+**
+** Description      Close the connection to the address.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvDisconnect(BD_ADDR bd_addr)
+{
+    tBTA_AV_API_DISCNT  *p_buf;
+
+    if ((p_buf = (tBTA_AV_API_DISCNT *) GKI_getbuf(sizeof(tBTA_AV_API_DISCNT))) != NULL)
+    {
+        p_buf->hdr.event = BTA_AV_API_DISCONNECT_EVT;
+        bdcpy(p_buf->bd_addr, bd_addr);
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvStart
+**
+** Description      Start audio/video stream data transfer.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvStart(void)
+{
+    BT_HDR  *p_buf;
+
+    if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
+    {
+        p_buf->event = BTA_AV_API_START_EVT;
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvEnable_Sink
+**
+** Description      Enable/Disable A2DP Sink..
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvEnable_Sink(int enable)
+{
+#if (BTA_AV_SINK_INCLUDED == TRUE)
+    BT_HDR  *p_buf;
+    if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
+    {
+        p_buf->event = BTA_AV_API_SINK_ENABLE_EVT;
+        p_buf->layer_specific = enable;
+        bta_sys_sendmsg(p_buf);
+    }
+#else
+    return;
+#endif
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvStop
+**
+** Description      Stop audio/video stream data transfer.
+**                  If suspend is TRUE, this function sends AVDT suspend signal
+**                  to the connected peer(s).
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvStop(BOOLEAN suspend)
+{
+    tBTA_AV_API_STOP  *p_buf;
+
+    if ((p_buf = (tBTA_AV_API_STOP *) GKI_getbuf(sizeof(tBTA_AV_API_STOP))) != NULL)
+    {
+        p_buf->hdr.event = BTA_AV_API_STOP_EVT;
+        p_buf->flush   = TRUE;
+        p_buf->suspend = suspend;
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvReconfig
+**
+** Description      Reconfigure the audio/video stream.
+**                  If suspend is TRUE, this function tries the suspend/reconfigure
+**                  procedure first.
+**                  If suspend is FALSE or when suspend/reconfigure fails,
+**                  this function closes and re-opens the AVDT connection.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvReconfig(tBTA_AV_HNDL hndl, BOOLEAN suspend, UINT8 sep_info_idx,
+                    UINT8 *p_codec_info, UINT8 num_protect, UINT8 *p_protect_info)
+{
+    tBTA_AV_API_RCFG  *p_buf;
+
+    if ((p_buf = (tBTA_AV_API_RCFG *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_API_RCFG) + num_protect))) != NULL)
+    {
+        p_buf->hdr.layer_specific   = hndl;
+        p_buf->hdr.event    = BTA_AV_API_RECONFIG_EVT;
+        p_buf->num_protect  = num_protect;
+        p_buf->suspend      = suspend;
+        p_buf->sep_info_idx = sep_info_idx;
+        p_buf->p_protect_info = (UINT8 *)(p_buf + 1);
+        memcpy(p_buf->codec_info, p_codec_info, AVDT_CODEC_SIZE);
+        memcpy(p_buf->p_protect_info, p_protect_info, num_protect);
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvProtectReq
+**
+** Description      Send a content protection request.  This function can only
+**                  be used if AV is enabled with feature BTA_AV_FEAT_PROTECT.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvProtectReq(tBTA_AV_HNDL hndl, UINT8 *p_data, UINT16 len)
+{
+    tBTA_AV_API_PROTECT_REQ  *p_buf;
+
+    if ((p_buf = (tBTA_AV_API_PROTECT_REQ *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_API_PROTECT_REQ) + len))) != NULL)
+    {
+        p_buf->hdr.layer_specific   = hndl;
+        p_buf->hdr.event = BTA_AV_API_PROTECT_REQ_EVT;
+        p_buf->len       = len;
+        if (p_data == NULL)
+        {
+            p_buf->p_data = NULL;
+        }
+        else
+        {
+            p_buf->p_data = (UINT8 *) (p_buf + 1);
+            memcpy(p_buf->p_data, p_data, len);
+        }
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvProtectRsp
+**
+** Description      Send a content protection response.  This function must
+**                  be called if a BTA_AV_PROTECT_REQ_EVT is received.
+**                  This function can only be used if AV is enabled with
+**                  feature BTA_AV_FEAT_PROTECT.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvProtectRsp(tBTA_AV_HNDL hndl, UINT8 error_code, UINT8 *p_data, UINT16 len)
+{
+    tBTA_AV_API_PROTECT_RSP  *p_buf;
+
+    if ((p_buf = (tBTA_AV_API_PROTECT_RSP *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_API_PROTECT_RSP) + len))) != NULL)
+    {
+        p_buf->hdr.layer_specific   = hndl;
+        p_buf->hdr.event    = BTA_AV_API_PROTECT_RSP_EVT;
+        p_buf->len          = len;
+        p_buf->error_code   = error_code;
+        if (p_data == NULL)
+        {
+            p_buf->p_data = NULL;
+        }
+        else
+        {
+            p_buf->p_data = (UINT8 *) (p_buf + 1);
+            memcpy(p_buf->p_data, p_data, len);
+        }
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvRemoteCmd
+**
+** Description      Send a remote control command.  This function can only
+**                  be used if AV is enabled with feature BTA_AV_FEAT_RCCT.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvRemoteCmd(UINT8 rc_handle, UINT8 label, tBTA_AV_RC rc_id, tBTA_AV_STATE key_state)
+{
+    tBTA_AV_API_REMOTE_CMD  *p_buf;
+
+    if ((p_buf = (tBTA_AV_API_REMOTE_CMD *) GKI_getbuf(sizeof(tBTA_AV_API_REMOTE_CMD))) != NULL)
+    {
+        p_buf->hdr.event = BTA_AV_API_REMOTE_CMD_EVT;
+        p_buf->hdr.layer_specific   = rc_handle;
+        p_buf->msg.op_id = rc_id;
+        p_buf->msg.state = key_state;
+        p_buf->msg.p_pass_data = NULL;
+        p_buf->msg.pass_len = 0;
+        p_buf->label = label;
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvVendorCmd
+**
+** Description      Send a vendor dependent remote control command.  This
+**                  function can only be used if AV is enabled with feature
+**                  BTA_AV_FEAT_VENDOR.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvVendorCmd(UINT8 rc_handle, UINT8 label, tBTA_AV_CODE cmd_code, UINT8 *p_data, UINT16 len)
+{
+    tBTA_AV_API_VENDOR  *p_buf;
+
+    if ((p_buf = (tBTA_AV_API_VENDOR *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_API_VENDOR) + len))) != NULL)
+    {
+        p_buf->hdr.event = BTA_AV_API_VENDOR_CMD_EVT;
+        p_buf->hdr.layer_specific   = rc_handle;
+        p_buf->msg.hdr.ctype = cmd_code;
+        p_buf->msg.hdr.subunit_type = AVRC_SUB_PANEL;
+        p_buf->msg.hdr.subunit_id = 0;
+        p_buf->msg.company_id = p_bta_av_cfg->company_id;
+        p_buf->label = label;
+        p_buf->msg.vendor_len = len;
+        if (p_data == NULL)
+        {
+            p_buf->msg.p_vendor_data = NULL;
+        }
+        else
+        {
+            p_buf->msg.p_vendor_data = (UINT8 *) (p_buf + 1);
+            memcpy(p_buf->msg.p_vendor_data, p_data, len);
+        }
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvVendorRsp
+**
+** Description      Send a vendor dependent remote control response.
+**                  This function must be called if a BTA_AV_VENDOR_CMD_EVT
+**                  is received. This function can only be used if AV is
+**                  enabled with feature BTA_AV_FEAT_VENDOR.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvVendorRsp(UINT8 rc_handle, UINT8 label, tBTA_AV_CODE rsp_code, UINT8 *p_data, UINT16 len, UINT32 company_id)
+{
+    tBTA_AV_API_VENDOR  *p_buf;
+
+    if ((p_buf = (tBTA_AV_API_VENDOR *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_API_VENDOR) + len))) != NULL)
+    {
+        p_buf->hdr.event = BTA_AV_API_VENDOR_RSP_EVT;
+        p_buf->hdr.layer_specific   = rc_handle;
+        p_buf->msg.hdr.ctype = rsp_code;
+        p_buf->msg.hdr.subunit_type = AVRC_SUB_PANEL;
+        p_buf->msg.hdr.subunit_id = 0;
+        if(company_id)
+            p_buf->msg.company_id = company_id;
+        else
+            p_buf->msg.company_id = p_bta_av_cfg->company_id;
+        p_buf->label = label;
+        p_buf->msg.vendor_len = len;
+        if (p_data == NULL)
+        {
+            p_buf->msg.p_vendor_data = NULL;
+        }
+        else
+        {
+            p_buf->msg.p_vendor_data = (UINT8 *) (p_buf + 1);
+            memcpy(p_buf->msg.p_vendor_data, p_data, len);
+        }
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvOpenRc
+**
+** Description      Open an AVRCP connection toward the device with the
+**                  specified handle
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvOpenRc(tBTA_AV_HNDL handle)
+{
+    tBTA_AV_API_OPEN_RC  *p_buf;
+
+    if ((p_buf = (tBTA_AV_API_OPEN_RC *) GKI_getbuf(sizeof(tBTA_AV_API_OPEN_RC))) != NULL)
+    {
+        p_buf->hdr.event = BTA_AV_API_RC_OPEN_EVT;
+        p_buf->hdr.layer_specific   = handle;
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvCloseRc
+**
+** Description      Close an AVRCP connection
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvCloseRc(UINT8 rc_handle)
+{
+    tBTA_AV_API_CLOSE_RC  *p_buf;
+
+    if ((p_buf = (tBTA_AV_API_CLOSE_RC *) GKI_getbuf(sizeof(tBTA_AV_API_CLOSE_RC))) != NULL)
+    {
+        p_buf->hdr.event = BTA_AV_API_RC_CLOSE_EVT;
+        p_buf->hdr.layer_specific   = rc_handle;
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvMetaRsp
+**
+** Description      Send a Metadata/Advanced Control response. The message contained
+**                  in p_pkt can be composed with AVRC utility functions.
+**                  This function can only be used if AV is enabled with feature
+**                  BTA_AV_FEAT_METADATA.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvMetaRsp(UINT8 rc_handle, UINT8 label, tBTA_AV_CODE rsp_code,
+                               BT_HDR *p_pkt)
+{
+    tBTA_AV_API_META_RSP  *p_buf;
+
+    if ((p_buf = (tBTA_AV_API_META_RSP *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_API_META_RSP)))) != NULL)
+    {
+        p_buf->hdr.event = BTA_AV_API_META_RSP_EVT;
+        p_buf->hdr.layer_specific   = rc_handle;
+        p_buf->rsp_code = rsp_code;
+        p_buf->p_pkt = p_pkt;
+        p_buf->is_rsp = TRUE;
+        p_buf->label = label;
+
+        bta_sys_sendmsg(p_buf);
+    } else if (p_pkt) {
+        GKI_freebuf(p_pkt);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         BTA_AvMetaCmd
+**
+** Description      Send a Metadata/Advanced Control command. The message contained
+**                  in p_pkt can be composed with AVRC utility functions.
+**                  This function can only be used if AV is enabled with feature
+**                  BTA_AV_FEAT_METADATA.
+**                  This message is sent only when the peer supports the TG role.
+*8                  The only command makes sense right now is the absolute volume command.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvMetaCmd(UINT8 rc_handle, UINT8 label, tBTA_AV_CMD cmd_code, BT_HDR *p_pkt)
+{
+    tBTA_AV_API_META_RSP  *p_buf;
+
+    if ((p_buf = (tBTA_AV_API_META_RSP *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_API_META_RSP)))) != NULL)
+    {
+        p_buf->hdr.event = BTA_AV_API_META_RSP_EVT;
+        p_buf->hdr.layer_specific   = rc_handle;
+        p_buf->p_pkt = p_pkt;
+        p_buf->rsp_code = cmd_code;
+        p_buf->is_rsp = FALSE;
+        p_buf->label = label;
+
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+#endif /* BTA_AV_INCLUDED */
diff --git a/components/bt/bluedroid/bta/av/bta_av_cfg.c b/components/bt/bluedroid/bta/av/bta_av_cfg.c
new file mode 100755 (executable)
index 0000000..a7db8ff
--- /dev/null
@@ -0,0 +1,207 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2005-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This file contains compile-time configurable constants for advanced
+ *  audio/video
+ *
+ ******************************************************************************/
+
+#include <stddef.h>
+
+#include "bt_target.h"
+#include "gki.h"
+#include "bta_api.h"
+#include "bta_av_int.h"
+
+#ifndef BTA_AV_RC_PASS_RSP_CODE
+#define BTA_AV_RC_PASS_RSP_CODE     BTA_AV_RSP_NOT_IMPL
+#endif
+
+const UINT32  bta_av_meta_caps_co_ids[] = {
+    AVRC_CO_METADATA,
+    AVRC_CO_BROADCOM
+};
+
+/* AVRCP cupported categories */
+#define BTA_AV_RC_SUPF_CT       (AVRC_SUPF_CT_CAT2)
+
+/* Added to modify
+**     1. flush timeout
+**     2. Remove Group navigation support in SupportedFeatures
+**     3. GetCapabilities supported event_ids list
+**     4. GetCapabilities supported event_ids count
+*/
+/* Flushing partial avdtp packets can cause some headsets to disconnect the link
+   if receiving partial a2dp frames */
+const UINT16  bta_av_audio_flush_to[] = {
+     0, /* 1 stream */
+     0, /* 2 streams */
+     0, /* 3 streams */
+     0, /* 4 streams */
+     0  /* 5 streams */
+};     /* AVDTP audio transport channel flush timeout */
+
+/* Note: Android doesnt support AVRC_SUPF_TG_GROUP_NAVI  */
+/* Note: if AVRC_SUPF_TG_GROUP_NAVI is set, bta_av_cfg.avrc_group should be TRUE */
+#if AVRC_METADATA_INCLUDED == TRUE
+#define BTA_AV_RC_SUPF_TG       (AVRC_SUPF_TG_CAT1) /* TODO: | AVRC_SUPF_TG_APP_SETTINGS) */
+#else
+#define BTA_AV_RC_SUPF_TG       (AVRC_SUPF_TG_CAT1)
+#endif
+
+/*
+ * If the number of event IDs is changed in this array, BTA_AV_ NUM_RC_EVT_IDS   also needs to be changed.
+ */
+const UINT8  bta_av_meta_caps_evt_ids[] = {
+    AVRC_EVT_PLAY_STATUS_CHANGE,
+    AVRC_EVT_TRACK_CHANGE,
+    AVRC_EVT_PLAY_POS_CHANGED,
+    /* TODO: Add support for these events
+    AVRC_EVT_APP_SETTING_CHANGE,
+    */
+};
+#ifndef BTA_AV_NUM_RC_EVT_IDS
+#define BTA_AV_NUM_RC_EVT_IDS   (sizeof(bta_av_meta_caps_evt_ids) / sizeof(bta_av_meta_caps_evt_ids[0]))
+#endif /* BTA_AV_NUM_RC_EVT_IDS */
+
+/* the MTU for the AVRCP browsing channel */
+#ifndef BTA_AV_MAX_RC_BR_MTU
+#define BTA_AV_MAX_RC_BR_MTU      1008
+#endif
+
+const tBTA_AV_CFG bta_av_cfg =
+{
+    AVRC_CO_BROADCOM,       /* AVRCP Company ID */
+#if AVRC_METADATA_INCLUDED == TRUE
+    512,                    /* AVRCP MTU at L2CAP for control channel */
+#else
+    48,                     /* AVRCP MTU at L2CAP for control channel */
+#endif
+    BTA_AV_MAX_RC_BR_MTU,   /* AVRCP MTU at L2CAP for browsing channel */
+    BTA_AV_RC_SUPF_CT,      /* AVRCP controller categories */
+    BTA_AV_RC_SUPF_TG,      /* AVRCP target categories */
+    672,                    /* AVDTP signaling channel MTU at L2CAP */
+    BTA_AV_MAX_A2DP_MTU,    /* AVDTP audio transport channel MTU at L2CAP */
+    bta_av_audio_flush_to,  /* AVDTP audio transport channel flush timeout */
+    6,                      /* AVDTP audio channel max data queue size */
+    BTA_AV_MAX_VDP_MTU,     /* AVDTP video transport channel MTU at L2CAP */
+    600,                    /* AVDTP video transport channel flush timeout */
+    FALSE,                   /* TRUE, to accept AVRC 1.3 group nevigation command */
+    2,                      /* company id count in p_meta_co_ids */
+    BTA_AV_NUM_RC_EVT_IDS, /* event id count in p_meta_evt_ids */
+    BTA_AV_RC_PASS_RSP_CODE,/* the default response code for pass through commands */
+    bta_av_meta_caps_co_ids,/* the metadata Get Capabilities response for company id */
+    bta_av_meta_caps_evt_ids,/* the the metadata Get Capabilities response for event id */
+    NULL,                   /* the action function table for VDP stream */
+    NULL,                   /* action function to register VDP */
+    {0},                    /* Default AVRCP controller name */
+    {0},                    /* Default AVRCP target name */
+};
+
+tBTA_AV_CFG *p_bta_av_cfg = (tBTA_AV_CFG *) &bta_av_cfg;
+
+const UINT16 bta_av_rc_id[] =
+{
+    0x0000, /* bit mask: 0=SELECT, 1=UP, 2=DOWN, 3=LEFT,
+                         4=RIGHT, 5=RIGHT_UP, 6=RIGHT_DOWN, 7=LEFT_UP,
+                         8=LEFT_DOWN, 9=ROOT_MENU, 10=SETUP_MENU, 11=CONT_MENU,
+                         12=FAV_MENU, 13=EXIT */
+
+    0,      /* not used */
+
+    0x0000, /* bit mask: 0=0, 1=1, 2=2, 3=3,
+                         4=4, 5=5, 6=6, 7=7,
+                         8=8, 9=9, 10=DOT, 11=ENTER,
+                         12=CLEAR */
+
+    0x0000, /* bit mask: 0=CHAN_UP, 1=CHAN_DOWN, 2=PREV_CHAN, 3=SOUND_SEL,
+                         4=INPUT_SEL, 5=DISP_INFO, 6=HELP, 7=PAGE_UP,
+                         8=PAGE_DOWN */
+
+#if (BTA_AV_RC_PASS_RSP_CODE == BTA_AV_RSP_INTERIM)
+    /* btui_app provides an example of how to leave the decision of rejecting a command or not
+     * based on which media player is currently addressed (this is only applicable for AVRCP 1.4 or later)
+     * If the decision is per player for a particular rc_id, the related bit is clear (not set) */
+    0x0070, /* bit mask: 0=POWER, 1=VOL_UP, 2=VOL_DOWN, 3=MUTE,
+                         4=PLAY, 5=STOP, 6=PAUSE, 7=RECORD,
+                         8=REWIND, 9=FAST_FOR, 10=EJECT, 11=FORWARD,
+                         12=BACKWARD */
+#else
+#if (defined BTA_AVRCP_FF_RW_SUPPORT) && (BTA_AVRCP_FF_RW_SUPPORT == TRUE)
+    0x1b70, /* bit mask: 0=POWER, 1=VOL_UP, 2=VOL_DOWN, 3=MUTE,
+                         4=PLAY, 5=STOP, 6=PAUSE, 7=RECORD,
+                         8=REWIND, 9=FAST_FOR, 10=EJECT, 11=FORWARD,
+                         12=BACKWARD */
+#else
+    0x1870, /* bit mask: 0=POWER, 1=VOL_UP, 2=VOL_DOWN, 3=MUTE,
+                         4=PLAY, 5=STOP, 6=PAUSE, 7=RECORD,
+                         8=REWIND, 9=FAST_FOR, 10=EJECT, 11=FORWARD,
+                         12=BACKWARD */
+#endif
+#endif
+
+    0x0000, /* bit mask: 0=ANGLE, 1=SUBPICT */
+
+    0,      /* not used */
+
+    0x0000  /* bit mask: 0=not used, 1=F1, 2=F2, 3=F3,
+                         4=F4, 5=F5 */
+};
+
+#if (BTA_AV_RC_PASS_RSP_CODE == BTA_AV_RSP_INTERIM)
+const UINT16 bta_av_rc_id_ac[] =
+{
+    0x0000, /* bit mask: 0=SELECT, 1=UP, 2=DOWN, 3=LEFT,
+                         4=RIGHT, 5=RIGHT_UP, 6=RIGHT_DOWN, 7=LEFT_UP,
+                         8=LEFT_DOWN, 9=ROOT_MENU, 10=SETUP_MENU, 11=CONT_MENU,
+                         12=FAV_MENU, 13=EXIT */
+
+    0,      /* not used */
+
+    0x0000, /* bit mask: 0=0, 1=1, 2=2, 3=3,
+                         4=4, 5=5, 6=6, 7=7,
+                         8=8, 9=9, 10=DOT, 11=ENTER,
+                         12=CLEAR */
+
+    0x0000, /* bit mask: 0=CHAN_UP, 1=CHAN_DOWN, 2=PREV_CHAN, 3=SOUND_SEL,
+                         4=INPUT_SEL, 5=DISP_INFO, 6=HELP, 7=PAGE_UP,
+                         8=PAGE_DOWN */
+
+    /* btui_app provides an example of how to leave the decision of rejecting a command or not
+     * based on which media player is currently addressed (this is only applicable for AVRCP 1.4 or later)
+     * If the decision is per player for a particular rc_id, the related bit is set */
+    0x1800, /* bit mask: 0=POWER, 1=VOL_UP, 2=VOL_DOWN, 3=MUTE,
+                         4=PLAY, 5=STOP, 6=PAUSE, 7=RECORD,
+                         8=REWIND, 9=FAST_FOR, 10=EJECT, 11=FORWARD,
+                         12=BACKWARD */
+
+    0x0000, /* bit mask: 0=ANGLE, 1=SUBPICT */
+
+    0,      /* not used */
+
+    0x0000  /* bit mask: 0=not used, 1=F1, 2=F2, 3=F3,
+                         4=F4, 5=F5 */
+};
+UINT16 *p_bta_av_rc_id_ac = (UINT16 *) bta_av_rc_id_ac;
+#else
+UINT16 *p_bta_av_rc_id_ac = NULL;
+#endif
+
+UINT16 *p_bta_av_rc_id = (UINT16 *) bta_av_rc_id;
diff --git a/components/bt/bluedroid/bta/av/bta_av_ci.c b/components/bt/bluedroid/bta/av/bta_av_ci.c
new file mode 100755 (executable)
index 0000000..2da1d97
--- /dev/null
@@ -0,0 +1,99 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2005-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This is the implementation file for advanced audio/video call-in
+ *  functions.
+ *
+ ******************************************************************************/
+
+#include "bta_api.h"
+#include "bta_sys.h"
+#include "bta_av_int.h"
+#include "bta_av_ci.h"
+
+#include <string.h>
+
+/*******************************************************************************
+**
+** Function         bta_av_ci_src_data_ready
+**
+** Description      This function sends an event to the AV indicating that
+**                  the phone has audio stream data ready to send and AV
+**                  should call bta_av_co_audio_src_data_path() or
+**                  bta_av_co_video_src_data_path().
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_ci_src_data_ready(tBTA_AV_CHNL chnl)
+{
+    BT_HDR  *p_buf;
+
+    if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
+    {
+        p_buf->layer_specific   = chnl;
+        p_buf->event = BTA_AV_CI_SRC_DATA_READY_EVT;
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_ci_setconfig
+**
+** Description      This function must be called in response to function
+**                  bta_av_co_audio_setconfig() or bta_av_co_video_setconfig.
+**                  Parameter err_code is set to an AVDTP status value;
+**                  AVDT_SUCCESS if the codec configuration is ok,
+**                  otherwise error.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_ci_setconfig(tBTA_AV_HNDL hndl, UINT8 err_code, UINT8 category,
+                         UINT8 num_seid, UINT8 *p_seid, BOOLEAN recfg_needed, UINT8 avdt_handle)
+{
+    tBTA_AV_CI_SETCONFIG  *p_buf;
+
+    if ((p_buf = (tBTA_AV_CI_SETCONFIG *) GKI_getbuf(sizeof(tBTA_AV_CI_SETCONFIG))) != NULL)
+    {
+        p_buf->hdr.layer_specific   = hndl;
+        p_buf->hdr.event = (err_code == AVDT_SUCCESS) ?
+                           BTA_AV_CI_SETCONFIG_OK_EVT : BTA_AV_CI_SETCONFIG_FAIL_EVT;
+        p_buf->err_code = err_code;
+        p_buf->category = category;
+        p_buf->recfg_needed = recfg_needed;
+        p_buf->num_seid = num_seid;
+        p_buf->avdt_handle= avdt_handle;
+        if(p_seid && num_seid)
+        {
+            p_buf->p_seid   = (UINT8 *)(p_buf + 1);
+            memcpy(p_buf->p_seid, p_seid, num_seid);
+        }
+        else
+        {
+            p_buf->p_seid   = NULL;
+            p_buf->num_seid = 0;
+        }
+
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
diff --git a/components/bt/bluedroid/bta/av/bta_av_int.h b/components/bt/bluedroid/bta/av/bta_av_int.h
new file mode 100755 (executable)
index 0000000..6764e94
--- /dev/null
@@ -0,0 +1,732 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2004-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This is the private interface file for the BTA advanced audio/video.
+ *
+ ******************************************************************************/
+#ifndef BTA_AV_INT_H
+#define BTA_AV_INT_H
+
+#include "bta_sys.h"
+#include "bta_api.h"
+#include "bta_av_api.h"
+#include "avdt_api.h"
+#include "bta_av_co.h"
+#include "list.h"
+
+#define BTA_AV_DEBUG TRUE
+/*****************************************************************************
+**  Constants
+*****************************************************************************/
+
+enum
+{
+    /* these events are handled by the AV main state machine */
+    BTA_AV_API_DISABLE_EVT = BTA_SYS_EVT_START(BTA_ID_AV),
+    BTA_AV_API_REMOTE_CMD_EVT,
+    BTA_AV_API_VENDOR_CMD_EVT,
+    BTA_AV_API_VENDOR_RSP_EVT,
+    BTA_AV_API_META_RSP_EVT,
+    BTA_AV_API_RC_CLOSE_EVT,
+    BTA_AV_AVRC_OPEN_EVT,
+    BTA_AV_AVRC_MSG_EVT,
+    BTA_AV_AVRC_NONE_EVT,
+
+    /* these events are handled by the AV stream state machine */
+    BTA_AV_API_OPEN_EVT,
+    BTA_AV_API_CLOSE_EVT,
+    BTA_AV_AP_START_EVT,        /* the following 2 events must be in the same order as the *API_*EVT */
+    BTA_AV_AP_STOP_EVT,
+    BTA_AV_API_RECONFIG_EVT,
+    BTA_AV_API_PROTECT_REQ_EVT,
+    BTA_AV_API_PROTECT_RSP_EVT,
+    BTA_AV_API_RC_OPEN_EVT,
+    BTA_AV_SRC_DATA_READY_EVT,
+    BTA_AV_CI_SETCONFIG_OK_EVT,
+    BTA_AV_CI_SETCONFIG_FAIL_EVT,
+    BTA_AV_SDP_DISC_OK_EVT,
+    BTA_AV_SDP_DISC_FAIL_EVT,
+    BTA_AV_STR_DISC_OK_EVT,
+    BTA_AV_STR_DISC_FAIL_EVT,
+    BTA_AV_STR_GETCAP_OK_EVT,
+    BTA_AV_STR_GETCAP_FAIL_EVT,
+    BTA_AV_STR_OPEN_OK_EVT,
+    BTA_AV_STR_OPEN_FAIL_EVT,
+    BTA_AV_STR_START_OK_EVT,
+    BTA_AV_STR_START_FAIL_EVT,
+    BTA_AV_STR_CLOSE_EVT,
+    BTA_AV_STR_CONFIG_IND_EVT,
+    BTA_AV_STR_SECURITY_IND_EVT,
+    BTA_AV_STR_SECURITY_CFM_EVT,
+    BTA_AV_STR_WRITE_CFM_EVT,
+    BTA_AV_STR_SUSPEND_CFM_EVT,
+    BTA_AV_STR_RECONFIG_CFM_EVT,
+    BTA_AV_AVRC_TIMER_EVT,
+    BTA_AV_AVDT_CONNECT_EVT,
+    BTA_AV_AVDT_DISCONNECT_EVT,
+    BTA_AV_ROLE_CHANGE_EVT,
+    BTA_AV_AVDT_DELAY_RPT_EVT,
+    BTA_AV_ACP_CONNECT_EVT,
+
+    /* these events are handled outside of the state machine */
+    BTA_AV_API_ENABLE_EVT,
+    BTA_AV_API_REGISTER_EVT,
+    BTA_AV_API_DEREGISTER_EVT,
+    BTA_AV_API_DISCONNECT_EVT,
+    BTA_AV_CI_SRC_DATA_READY_EVT,
+    BTA_AV_SIG_CHG_EVT,
+    BTA_AV_SIG_TIMER_EVT,
+    BTA_AV_SDP_AVRC_DISC_EVT,
+    BTA_AV_AVRC_CLOSE_EVT,
+    BTA_AV_CONN_CHG_EVT,
+    BTA_AV_DEREG_COMP_EVT,
+#if (BTA_AV_SINK_INCLUDED == TRUE)
+    BTA_AV_API_SINK_ENABLE_EVT,
+#endif
+#if (AVDT_REPORTING == TRUE)
+    BTA_AV_AVDT_RPT_CONN_EVT,
+#endif
+    BTA_AV_API_START_EVT,       /* the following 2 events must be in the same order as the *AP_*EVT */
+    BTA_AV_API_STOP_EVT
+};
+
+/* events for AV control block state machine */
+#define BTA_AV_FIRST_SM_EVT     BTA_AV_API_DISABLE_EVT
+#define BTA_AV_LAST_SM_EVT      BTA_AV_AVRC_NONE_EVT
+
+/* events for AV stream control block state machine */
+#define BTA_AV_FIRST_SSM_EVT    BTA_AV_API_OPEN_EVT
+
+/* events that do not go through state machine */
+#define BTA_AV_FIRST_NSM_EVT    BTA_AV_API_ENABLE_EVT
+#define BTA_AV_LAST_NSM_EVT     BTA_AV_API_STOP_EVT
+
+/* API events passed to both SSMs (by bta_av_api_to_ssm) */
+#define BTA_AV_FIRST_A2S_API_EVT    BTA_AV_API_START_EVT
+#define BTA_AV_FIRST_A2S_SSM_EVT    BTA_AV_AP_START_EVT
+
+#define BTA_AV_LAST_EVT             BTA_AV_API_STOP_EVT
+
+/* maximum number of SEPS in stream discovery results */
+#define BTA_AV_NUM_SEPS         32
+
+/* initialization value for AVRC handle */
+#define BTA_AV_RC_HANDLE_NONE   0xFF
+
+/* size of database for service discovery */
+#define BTA_AV_DISC_BUF_SIZE        1000
+
+/* offset of media type in codec info byte array */
+#define BTA_AV_MEDIA_TYPE_IDX       1
+
+/* maximum length of AVDTP security data */
+#define BTA_AV_SECURITY_MAX_LEN     400
+
+/* check number of buffers queued at L2CAP when this amount of buffers are queued to L2CAP */
+#define BTA_AV_QUEUE_DATA_CHK_NUM   L2CAP_HIGH_PRI_MIN_XMIT_QUOTA
+
+/* the number of ACL links with AVDT */
+#define BTA_AV_NUM_LINKS            AVDT_NUM_LINKS
+
+#define BTA_AV_CO_ID_TO_BE_STREAM(p, u32) {*(p)++ = (UINT8)((u32) >> 16); *(p)++ = (UINT8)((u32) >> 8); *(p)++ = (UINT8)(u32); }
+#define BTA_AV_BE_STREAM_TO_CO_ID(u32, p) {u32 = (((UINT32)(*((p) + 2))) + (((UINT32)(*((p) + 1))) << 8) + (((UINT32)(*(p))) << 16)); (p) += 3;}
+
+/* these bits are defined for bta_av_cb.multi_av */
+#define BTA_AV_MULTI_AV_SUPPORTED   0x01
+#define BTA_AV_MULTI_AV_IN_USE      0x02
+
+
+/*****************************************************************************
+**  Data types
+*****************************************************************************/
+
+/* function types for call-out functions */
+typedef BOOLEAN (*tBTA_AV_CO_INIT) (UINT8 *p_codec_type, UINT8 *p_codec_info,
+                                   UINT8 *p_num_protect, UINT8 *p_protect_info, UINT8 index);
+typedef void (*tBTA_AV_CO_DISC_RES) (tBTA_AV_HNDL hndl, UINT8 num_seps,
+                                     UINT8 num_snk, UINT8 num_src, BD_ADDR addr, UINT16 uuid_local);
+typedef UINT8 (*tBTA_AV_CO_GETCFG) (tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                     UINT8 *p_codec_info, UINT8 *p_sep_info_idx, UINT8 seid,
+                                     UINT8 *p_num_protect, UINT8 *p_protect_info);
+typedef void (*tBTA_AV_CO_SETCFG) (tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                     UINT8 *p_codec_info, UINT8 seid, BD_ADDR addr,
+                                     UINT8 num_protect, UINT8 *p_protect_info,
+                                     UINT8 t_local_sep, UINT8 avdt_handle);
+typedef void (*tBTA_AV_CO_OPEN) (tBTA_AV_HNDL hndl,
+                                 tBTA_AV_CODEC codec_type, UINT8 *p_codec_info,
+                                   UINT16 mtu);
+typedef void (*tBTA_AV_CO_CLOSE) (tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type, UINT16 mtu);
+typedef void (*tBTA_AV_CO_START) (tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,UINT8 *p_codec_info, BOOLEAN *p_no_rtp_hdr);
+typedef void (*tBTA_AV_CO_STOP) (tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type);
+typedef void * (*tBTA_AV_CO_DATAPATH) (tBTA_AV_CODEC codec_type,
+                                       UINT32 *p_len, UINT32 *p_timestamp);
+typedef void (*tBTA_AV_CO_DELAY) (tBTA_AV_HNDL hndl, UINT16 delay);
+
+/* the call-out functions for one stream */
+typedef struct
+{
+    tBTA_AV_CO_INIT     init;
+    tBTA_AV_CO_DISC_RES disc_res;
+    tBTA_AV_CO_GETCFG   getcfg;
+    tBTA_AV_CO_SETCFG   setcfg;
+    tBTA_AV_CO_OPEN     open;
+    tBTA_AV_CO_CLOSE    close;
+    tBTA_AV_CO_START    start;
+    tBTA_AV_CO_STOP     stop;
+    tBTA_AV_CO_DATAPATH data;
+    tBTA_AV_CO_DELAY    delay;
+} tBTA_AV_CO_FUNCTS;
+
+/* data type for BTA_AV_API_ENABLE_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    tBTA_AV_CBACK       *p_cback;
+    tBTA_AV_FEAT        features;
+    tBTA_SEC            sec_mask;
+} tBTA_AV_API_ENABLE;
+
+/* data type for BTA_AV_API_REG_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    char                p_service_name[BTA_SERVICE_NAME_LEN+1];
+    UINT8               app_id;
+    tBTA_AV_DATA_CBACK       *p_app_data_cback;
+} tBTA_AV_API_REG;
+
+
+enum
+{
+    BTA_AV_RS_NONE,     /* straight API call */
+    BTA_AV_RS_OK,       /* the role switch result - successful */
+    BTA_AV_RS_FAIL,     /* the role switch result - failed */
+    BTA_AV_RS_DONE      /* the role switch is done - continue */
+};
+typedef UINT8 tBTA_AV_RS_RES;
+/* data type for BTA_AV_API_OPEN_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    BD_ADDR             bd_addr;
+    BOOLEAN             use_rc;
+    tBTA_SEC            sec_mask;
+    tBTA_AV_RS_RES      switch_res;
+    UINT16              uuid;  /* uuid of initiator */
+} tBTA_AV_API_OPEN;
+
+/* data type for BTA_AV_API_STOP_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    BOOLEAN             suspend;
+    BOOLEAN             flush;
+} tBTA_AV_API_STOP;
+
+/* data type for BTA_AV_API_DISCONNECT_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    BD_ADDR             bd_addr;
+} tBTA_AV_API_DISCNT;
+
+/* data type for BTA_AV_API_PROTECT_REQ_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    UINT8               *p_data;
+    UINT16              len;
+} tBTA_AV_API_PROTECT_REQ;
+
+/* data type for BTA_AV_API_PROTECT_RSP_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    UINT8               *p_data;
+    UINT16              len;
+    UINT8               error_code;
+} tBTA_AV_API_PROTECT_RSP;
+
+/* data type for BTA_AV_API_REMOTE_CMD_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    tAVRC_MSG_PASS      msg;
+    UINT8               label;
+} tBTA_AV_API_REMOTE_CMD;
+
+/* data type for BTA_AV_API_VENDOR_CMD_EVT and RSP */
+typedef struct
+{
+    BT_HDR              hdr;
+    tAVRC_MSG_VENDOR    msg;
+    UINT8               label;
+} tBTA_AV_API_VENDOR;
+
+/* data type for BTA_AV_API_RC_OPEN_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+} tBTA_AV_API_OPEN_RC;
+
+/* data type for BTA_AV_API_RC_CLOSE_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+} tBTA_AV_API_CLOSE_RC;
+
+/* data type for BTA_AV_API_META_RSP_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    BOOLEAN             is_rsp;
+    UINT8               label;
+    tBTA_AV_CODE        rsp_code;
+    BT_HDR              *p_pkt;
+} tBTA_AV_API_META_RSP;
+
+
+/* data type for BTA_AV_API_RECONFIG_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    UINT8               codec_info[AVDT_CODEC_SIZE];    /* codec configuration */
+    UINT8               *p_protect_info;
+    UINT8               num_protect;
+    BOOLEAN             suspend;
+    UINT8               sep_info_idx;
+} tBTA_AV_API_RCFG;
+
+/* data type for BTA_AV_CI_SETCONFIG_OK_EVT and BTA_AV_CI_SETCONFIG_FAIL_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    tBTA_AV_HNDL        hndl;
+    UINT8               err_code;
+    UINT8               category;
+    UINT8               num_seid;
+    UINT8               *p_seid;
+    BOOLEAN             recfg_needed;
+    UINT8               avdt_handle;  /* local sep type for which this stream will be set up */
+} tBTA_AV_CI_SETCONFIG;
+
+/* data type for all stream events from AVDTP */
+typedef struct {
+    BT_HDR              hdr;
+    tAVDT_CFG           cfg;        /* configuration/capabilities parameters */
+    tAVDT_CTRL          msg;        /* AVDTP callback message parameters */
+    BD_ADDR             bd_addr;    /* bd address */
+    UINT8               handle;
+    UINT8               avdt_event;
+    BOOLEAN             initiator; /* TRUE, if local device initiates the SUSPEND */
+} tBTA_AV_STR_MSG;
+
+/* data type for BTA_AV_AVRC_MSG_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    tAVRC_MSG           msg;
+    UINT8               handle;
+    UINT8               label;
+    UINT8               opcode;
+} tBTA_AV_RC_MSG;
+
+/* data type for BTA_AV_AVRC_OPEN_EVT, BTA_AV_AVRC_CLOSE_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    BD_ADDR             peer_addr;
+    UINT8               handle;
+} tBTA_AV_RC_CONN_CHG;
+
+/* data type for BTA_AV_CONN_CHG_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    BD_ADDR             peer_addr;
+    BOOLEAN             is_up;
+} tBTA_AV_CONN_CHG;
+
+/* data type for BTA_AV_ROLE_CHANGE_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    UINT8               new_role;
+    UINT8               hci_status;
+} tBTA_AV_ROLE_RES;
+
+/* data type for BTA_AV_SDP_DISC_OK_EVT */
+typedef struct
+{
+    BT_HDR              hdr;
+    UINT16              avdt_version;   /* AVDTP protocol version */
+} tBTA_AV_SDP_RES;
+
+/* type for SEP control block */
+typedef struct
+{
+    UINT8               av_handle;         /* AVDTP handle */
+    tBTA_AV_CODEC       codec_type;        /* codec type */
+    UINT8               tsep;              /* SEP type of local SEP */
+    tBTA_AV_DATA_CBACK  *p_app_data_cback; /* Application callback for media packets */
+} tBTA_AV_SEP;
+
+
+/* initiator/acceptor role for adaption */
+#define BTA_AV_ROLE_AD_INT          0x00       /* initiator */
+#define BTA_AV_ROLE_AD_ACP          0x01       /* acceptor */
+
+/* initiator/acceptor signaling roles */
+#define BTA_AV_ROLE_START_ACP       0x00
+#define BTA_AV_ROLE_START_INT       0x10    /* do not change this value */
+
+#define BTA_AV_ROLE_SUSPEND         0x20    /* suspending on start */
+#define BTA_AV_ROLE_SUSPEND_OPT     0x40    /* Suspend on Start option is set */
+
+/* union of all event datatypes */
+typedef union
+{
+    BT_HDR                  hdr;
+    tBTA_AV_API_ENABLE      api_enable;
+    tBTA_AV_API_REG         api_reg;
+    tBTA_AV_API_OPEN        api_open;
+    tBTA_AV_API_STOP        api_stop;
+    tBTA_AV_API_DISCNT      api_discnt;
+    tBTA_AV_API_PROTECT_REQ api_protect_req;
+    tBTA_AV_API_PROTECT_RSP api_protect_rsp;
+    tBTA_AV_API_REMOTE_CMD  api_remote_cmd;
+    tBTA_AV_API_VENDOR      api_vendor;
+    tBTA_AV_API_RCFG        api_reconfig;
+    tBTA_AV_CI_SETCONFIG    ci_setconfig;
+    tBTA_AV_STR_MSG         str_msg;
+    tBTA_AV_RC_MSG          rc_msg;
+    tBTA_AV_RC_CONN_CHG     rc_conn_chg;
+    tBTA_AV_CONN_CHG        conn_chg;
+    tBTA_AV_ROLE_RES        role_res;
+    tBTA_AV_SDP_RES         sdp_res;
+    tBTA_AV_API_META_RSP    api_meta_rsp;
+} tBTA_AV_DATA;
+
+typedef void (tBTA_AV_VDP_DATA_ACT)(void *p_scb);
+
+typedef struct
+{
+    tBTA_AV_VDP_DATA_ACT    *p_act;
+    UINT8                   *p_frame;
+    UINT16                  buf_size;
+    UINT32                  len;
+    UINT32                  offset;
+    UINT32                  timestamp;
+} tBTA_AV_VF_INFO;
+
+typedef union
+{
+    tBTA_AV_VF_INFO     vdp;            /* used for video channels only */
+    tBTA_AV_API_OPEN    open;           /* used only before open and role switch
+                                           is needed on another AV channel */
+} tBTA_AV_Q_INFO;
+
+#define BTA_AV_Q_TAG_OPEN               0x01 /* after API_OPEN, before STR_OPENED */
+#define BTA_AV_Q_TAG_START              0x02 /* before start sending media packets */
+#define BTA_AV_Q_TAG_STREAM             0x03 /* during streaming */
+
+#define BTA_AV_WAIT_ACP_CAPS_ON         0x01 /* retriving the peer capabilities */
+#define BTA_AV_WAIT_ACP_CAPS_STARTED    0x02 /* started while retriving peer capabilities */
+#define BTA_AV_WAIT_ROLE_SW_RES_OPEN    0x04 /* waiting for role switch result after API_OPEN, before STR_OPENED */
+#define BTA_AV_WAIT_ROLE_SW_RES_START   0x08 /* waiting for role switch result before streaming */
+#define BTA_AV_WAIT_ROLE_SW_STARTED     0x10 /* started while waiting for role switch result */
+#define BTA_AV_WAIT_ROLE_SW_RETRY       0x20 /* set when retry on timeout */
+#define BTA_AV_WAIT_CHECK_RC            0x40 /* set when the timer is used by role switch */
+#define BTA_AV_WAIT_ROLE_SW_FAILED      0x80 /* role switch failed */
+
+#define BTA_AV_WAIT_ROLE_SW_BITS        (BTA_AV_WAIT_ROLE_SW_RES_OPEN|BTA_AV_WAIT_ROLE_SW_RES_START|BTA_AV_WAIT_ROLE_SW_STARTED|BTA_AV_WAIT_ROLE_SW_RETRY)
+
+/* Bitmap for collision, coll_mask */
+#define BTA_AV_COLL_INC_TMR             0x01 /* Timer is running for incoming L2C connection */
+#define BTA_AV_COLL_API_CALLED          0x02 /* API open was called while incoming timer is running */
+
+/* type for AV stream control block */
+typedef struct
+{
+    const tBTA_AV_ACT   *p_act_tbl;     /* the action table for stream state machine */
+    const tBTA_AV_CO_FUNCTS *p_cos;     /* the associated callout functions */
+    tSDP_DISCOVERY_DB   *p_disc_db;     /* pointer to discovery database */
+    tBTA_AV_SEP         seps[BTA_AV_MAX_SEPS];
+    tAVDT_CFG           *p_cap;         /* buffer used for get capabilities */
+    list_t              *a2d_list;      /* used for audio channels only */
+    tBTA_AV_Q_INFO      q_info;
+    tAVDT_SEP_INFO      sep_info[BTA_AV_NUM_SEPS];      /* stream discovery results */
+    tAVDT_CFG           cfg;            /* local SEP configuration */
+    TIMER_LIST_ENT      timer;          /* delay timer for AVRC CT */
+    BD_ADDR             peer_addr;      /* peer BD address */
+    UINT16              l2c_cid;        /* L2CAP channel ID */
+    UINT16              stream_mtu;     /* MTU of stream */
+    UINT16              avdt_version;   /* the avdt version of peer device */
+    tBTA_SEC            sec_mask;       /* security mask */
+    tBTA_AV_CODEC       codec_type;     /* codec type */
+    UINT8               media_type;     /* Media type */
+    BOOLEAN             cong;           /* TRUE if AVDTP congested */
+    tBTA_AV_STATUS      open_status;    /* open failure status */
+    tBTA_AV_CHNL        chnl;           /* the channel: audio/video */
+    tBTA_AV_HNDL        hndl;           /* the handle: ((hdi + 1)|chnl) */
+    UINT16              cur_psc_mask;   /* Protocol service capabilities mask for current connection */
+    UINT8               avdt_handle;    /* AVDTP handle */
+    UINT8               hdi;            /* the index to SCB[] */
+    UINT8               num_seps;       /* number of seps returned by stream discovery */
+    UINT8               num_disc_snks;  /* number of discovered snks */
+    UINT8               num_disc_srcs;  /* number of discovered srcs */
+    UINT8               sep_info_idx;   /* current index into sep_info */
+    UINT8               sep_idx;        /* current index into local seps[] */
+    UINT8               rcfg_idx;       /* reconfig requested index into sep_info */
+    UINT8               state;          /* state machine state */
+    UINT8               avdt_label;     /* AVDTP label */
+    UINT8               app_id;         /* application id */
+    UINT8               num_recfg;      /* number of reconfigure sent */
+    UINT8               role;
+    UINT8               l2c_bufs;       /* the number of buffers queued to L2CAP */
+    UINT8               rc_handle;      /* connected AVRCP handle */
+    BOOLEAN             use_rc;         /* TRUE if AVRCP is allowed */
+    BOOLEAN             started;        /* TRUE if stream started */
+    UINT8               co_started;     /* non-zero, if stream started from call-out perspective */
+    BOOLEAN             recfg_sup;      /* TRUE if the first attempt to reconfigure the stream was successfull, else False if command fails */
+    BOOLEAN             suspend_sup;    /* TRUE if Suspend stream is supported, else FALSE if suspend command fails */
+    BOOLEAN             deregistring;   /* TRUE if deregistering */
+    BOOLEAN             sco_suspend;    /* TRUE if SUSPEND is issued automatically for SCO */
+    UINT8               coll_mask;      /* Mask to check incoming and outgoing collision */
+    tBTA_AV_API_OPEN    open_api;       /* Saved OPEN api message */
+    UINT8               wait;           /* set 0x1, when getting Caps as ACP, set 0x2, when started */
+    UINT8               q_tag;          /* identify the associated q_info union member */
+    BOOLEAN             no_rtp_hdr;     /* TRUE if add no RTP header*/
+    UINT16              uuid_int;       /*intended UUID of Initiator to connect to */
+} tBTA_AV_SCB;
+
+#define BTA_AV_RC_ROLE_MASK     0x10
+#define BTA_AV_RC_ROLE_INT      0x00
+#define BTA_AV_RC_ROLE_ACP      0x10
+
+#define BTA_AV_RC_CONN_MASK     0x20
+
+/* type for AV RCP control block */
+/* index to this control block is the rc handle */
+typedef struct
+{
+    UINT8   status;
+    UINT8   handle;
+    UINT8   shdl;   /* stream handle (hdi + 1) */
+    UINT8   lidx;   /* (index+1) to LCB */
+    tBTA_AV_FEAT        peer_features;  /* peer features mask */
+} tBTA_AV_RCB;
+#define BTA_AV_NUM_RCB      (BTA_AV_NUM_STRS  + 2)
+
+enum
+{
+    BTA_AV_LCB_FREE,
+    BTA_AV_LCB_FIND
+};
+
+/* type for AV ACL Link control block */
+typedef struct
+{
+    BD_ADDR             addr;           /* peer BD address */
+    UINT8               conn_msk;       /* handle mask of connected stream handle */
+    UINT8               lidx;           /* index + 1 */
+} tBTA_AV_LCB;
+
+/* type for stream state machine action functions */
+typedef void (*tBTA_AV_SACT)(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+
+
+/* type for AV control block */
+typedef struct
+{
+    tBTA_AV_SCB         *p_scb[BTA_AV_NUM_STRS];    /* stream control block */
+    tSDP_DISCOVERY_DB   *p_disc_db;     /* pointer to discovery database */
+    tBTA_AV_CBACK       *p_cback;       /* application callback function */
+    tBTA_AV_RCB         rcb[BTA_AV_NUM_RCB];  /* RCB control block */
+    tBTA_AV_LCB         lcb[BTA_AV_NUM_LINKS+1];  /* link control block */
+    TIMER_LIST_ENT      sig_tmr;        /* link timer */
+    TIMER_LIST_ENT      acp_sig_tmr;    /* timer to monitor signalling when accepting */
+    UINT32              sdp_a2d_handle; /* SDP record handle for audio src */
+#if (BTA_AV_SINK_INCLUDED == TRUE)
+    UINT32              sdp_a2d_snk_handle; /* SDP record handle for audio snk */
+#endif
+    UINT32              sdp_vdp_handle; /* SDP record handle for video src */
+    tBTA_AV_FEAT        features;       /* features mask */
+    tBTA_SEC            sec_mask;       /* security mask */
+    tBTA_AV_HNDL        handle;         /* the handle for SDP activity */
+    BOOLEAN             disabling;      /* TRUE if api disabled called */
+    UINT8               disc;           /* (hdi+1) or (rc_handle|BTA_AV_CHNL_MSK) if p_disc_db is in use */
+    UINT8               state;          /* state machine state */
+    UINT8               conn_rc;        /* handle mask of connected RCP channels */
+    UINT8               conn_audio;     /* handle mask of connected audio channels */
+    UINT8               conn_video;     /* handle mask of connected video channels */
+    UINT8               conn_lcb;       /* index mask of used LCBs */
+    UINT8               audio_open_cnt; /* number of connected audio channels */
+    UINT8               reg_audio;      /* handle mask of registered audio channels */
+    UINT8               reg_video;      /* handle mask of registered video channels */
+    UINT8               rc_acp_handle;
+    UINT8               rc_acp_idx;     /* (index + 1) to RCB */
+    UINT8               rs_idx;         /* (index + 1) to SCB for the one waiting for RS on open */
+    BOOLEAN             sco_occupied;   /* TRUE if SCO is being used or call is in progress */
+    UINT8               audio_streams;  /* handle mask of streaming audio channels */
+    UINT8               video_streams;  /* handle mask of streaming video channels */
+} tBTA_AV_CB;
+
+
+
+/*****************************************************************************
+**  Global data
+*****************************************************************************/
+
+/* control block declaration */
+#if BTA_DYNAMIC_MEMORY == FALSE
+extern tBTA_AV_CB bta_av_cb;
+#else
+extern tBTA_AV_CB *bta_av_cb_ptr;
+#define bta_av_cb (*bta_av_cb_ptr)
+#endif
+
+/* config struct */
+extern tBTA_AV_CFG *p_bta_av_cfg;
+
+/* rc id config struct */
+extern UINT16 *p_bta_av_rc_id;
+extern UINT16 *p_bta_av_rc_id_ac;
+
+extern const tBTA_AV_SACT bta_av_a2d_action[];
+extern const tBTA_AV_CO_FUNCTS bta_av_a2d_cos;
+extern const tBTA_AV_SACT bta_av_vdp_action[];
+extern tAVDT_CTRL_CBACK * const bta_av_dt_cback[];
+extern void bta_av_stream_data_cback(UINT8 handle, BT_HDR *p_pkt, UINT32 time_stamp, UINT8 m_pt);
+
+/*****************************************************************************
+**  Function prototypes
+*****************************************************************************/
+/* utility functions */
+extern tBTA_AV_SCB *bta_av_hndl_to_scb(UINT16 handle);
+extern BOOLEAN bta_av_chk_start(tBTA_AV_SCB *p_scb);
+extern void bta_av_restore_switch (void);
+extern UINT16 bta_av_chk_mtu(tBTA_AV_SCB *p_scb, UINT16 mtu);
+extern void bta_av_conn_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
+extern UINT8 bta_av_rc_create(tBTA_AV_CB *p_cb, UINT8 role, UINT8 shdl, UINT8 lidx);
+extern void bta_av_stream_chg(tBTA_AV_SCB *p_scb, BOOLEAN started);
+extern BOOLEAN bta_av_is_scb_opening (tBTA_AV_SCB *p_scb);
+extern BOOLEAN bta_av_is_scb_incoming (tBTA_AV_SCB *p_scb);
+extern void bta_av_set_scb_sst_init (tBTA_AV_SCB *p_scb);
+extern BOOLEAN bta_av_is_scb_init (tBTA_AV_SCB *p_scb);
+extern void bta_av_set_scb_sst_incoming (tBTA_AV_SCB *p_scb);
+extern tBTA_AV_LCB * bta_av_find_lcb(BD_ADDR addr, UINT8 op);
+
+
+/* main functions */
+extern void bta_av_api_deregister(tBTA_AV_DATA *p_data);
+extern void bta_av_dup_audio_buf(tBTA_AV_SCB *p_scb, BT_HDR *p_buf);
+extern void bta_av_sm_execute(tBTA_AV_CB *p_cb, UINT16 event, tBTA_AV_DATA *p_data);
+extern void bta_av_ssm_execute(tBTA_AV_SCB *p_scb, UINT16 event, tBTA_AV_DATA *p_data);
+extern BOOLEAN bta_av_hdl_event(BT_HDR *p_msg);
+#if (defined(BTA_AV_DEBUG) && BTA_AV_DEBUG == TRUE)
+extern char *bta_av_evt_code(UINT16 evt_code);
+#endif
+extern BOOLEAN bta_av_switch_if_needed(tBTA_AV_SCB *p_scb);
+extern BOOLEAN bta_av_link_role_ok(tBTA_AV_SCB *p_scb, UINT8 bits);
+extern BOOLEAN bta_av_is_rcfg_sst(tBTA_AV_SCB *p_scb);
+
+/* nsm action functions */
+extern void bta_av_api_disconnect(tBTA_AV_DATA *p_data);
+extern void bta_av_sig_chg(tBTA_AV_DATA *p_data);
+extern void bta_av_sig_timer(tBTA_AV_DATA *p_data);
+extern void bta_av_rc_disc_done(tBTA_AV_DATA *p_data);
+extern void bta_av_rc_closed(tBTA_AV_DATA *p_data);
+extern void bta_av_rc_disc(UINT8 disc);
+extern void bta_av_conn_chg(tBTA_AV_DATA *p_data);
+extern void bta_av_dereg_comp(tBTA_AV_DATA *p_data);
+
+/* sm action functions */
+extern void bta_av_disable (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data);
+extern void bta_av_rc_opened (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data);
+extern void bta_av_rc_remote_cmd (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data);
+extern void bta_av_rc_vendor_cmd (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data);
+extern void bta_av_rc_vendor_rsp (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data);
+extern void bta_av_rc_msg (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data);
+extern void bta_av_rc_close (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data);
+extern void bta_av_rc_meta_rsp (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data);
+extern void bta_av_rc_free_rsp (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data);
+extern void bta_av_rc_free_msg (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data);
+
+extern tBTA_AV_RCB * bta_av_get_rcb_by_shdl(UINT8 shdl);
+extern void bta_av_del_rc(tBTA_AV_RCB *p_rcb);
+
+/* ssm action functions */
+extern void bta_av_do_disc_a2d (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_cleanup (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_free_sdb (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_config_ind (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_disconnect_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_security_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_security_rsp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_setconfig_rsp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_str_opened (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_security_ind (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_security_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_do_close (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_connect_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_sdp_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_disc_results (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_disc_res_as_acp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_open_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_getcap_results (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_setconfig_rej (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_discover_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_conn_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_do_start (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_str_stopped (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_reconfig (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_data_path (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_start_ok (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_start_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_str_closed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_clr_cong (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_suspend_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_rcfg_str_ok (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_rcfg_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_rcfg_connect (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_rcfg_discntd (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_suspend_cont (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_rcfg_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_rcfg_open (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_security_rej (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_open_rc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_chk_2nd_start (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_save_caps (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_rej_conn (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_rej_conn (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_set_use_rc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_cco_close (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_switch_role (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_role_res (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_delay_co (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_open_at_inc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+
+/* ssm action functions - vdp specific */
+extern void bta_av_do_disc_vdp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_vdp_str_opened (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
+extern void bta_av_reg_vdp (tAVDT_CS *p_cs, char *p_service_name, void *p_data);
+
+#endif /* BTA_AV_INT_H */
diff --git a/components/bt/bluedroid/bta/av/bta_av_main.c b/components/bt/bluedroid/bta/av/bta_av_main.c
new file mode 100755 (executable)
index 0000000..8c99986
--- /dev/null
@@ -0,0 +1,1441 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2004-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This is the main implementation file for the BTA advanced audio/video.
+ *
+ ******************************************************************************/
+
+// #include <assert.h>
+#include <string.h>
+
+#include "bt_target.h"
+// #include "osi/include/log.h"
+#include "bt_trace.h"
+
+#if defined(BTA_AV_INCLUDED) && (BTA_AV_INCLUDED == TRUE)
+#include "bta_av_int.h"
+#include "utl.h"
+#include "l2c_api.h"
+#include "l2cdefs.h"
+#include "bta_av_co.h"
+#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
+#include "bta_ar_api.h"
+#endif
+
+/*****************************************************************************
+** Constants and types
+*****************************************************************************/
+
+/* AVDTP protocol timeout values */
+#define BTIF_AVK_SERVICE_NAME "Advanced Audio Sink"
+
+#ifndef BTA_AV_RET_TOUT
+#define BTA_AV_RET_TOUT     4
+#endif
+
+#ifndef BTA_AV_SIG_TOUT
+#define BTA_AV_SIG_TOUT     4
+#endif
+
+#ifndef BTA_AV_IDLE_TOUT
+#define BTA_AV_IDLE_TOUT    10
+#endif
+
+/* the delay time in milliseconds to retry role switch */
+#ifndef BTA_AV_RS_TIME_VAL
+#define BTA_AV_RS_TIME_VAL     1000
+#endif
+
+/* state machine states */
+enum
+{
+    BTA_AV_INIT_ST,
+    BTA_AV_OPEN_ST
+};
+
+/* state machine action enumeration list */
+enum
+{
+    BTA_AV_DISABLE,
+    BTA_AV_RC_OPENED,
+    BTA_AV_RC_REMOTE_CMD,
+    BTA_AV_RC_VENDOR_CMD,
+    BTA_AV_RC_VENDOR_RSP,
+    BTA_AV_RC_FREE_RSP,
+    BTA_AV_RC_FREE_MSG,
+    BTA_AV_RC_META_RSP,
+    BTA_AV_RC_MSG,
+    BTA_AV_RC_CLOSE,
+    BTA_AV_NUM_ACTIONS
+};
+
+#define BTA_AV_IGNORE       BTA_AV_NUM_ACTIONS
+
+/* type for action functions */
+typedef void (*tBTA_AV_ACTION)(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data);
+
+/* action functions */
+const tBTA_AV_ACTION bta_av_action[] =
+{
+    bta_av_disable,
+    bta_av_rc_opened,
+    bta_av_rc_remote_cmd,
+    bta_av_rc_vendor_cmd,
+    bta_av_rc_vendor_rsp,
+    bta_av_rc_free_rsp,
+    bta_av_rc_free_msg,
+    bta_av_rc_meta_rsp,
+    bta_av_rc_msg,
+    bta_av_rc_close,
+    NULL
+};
+
+/* state table information */
+#define BTA_AV_ACTION_COL           0       /* position of actions */
+#define BTA_AV_NEXT_STATE           1       /* position of next state */
+#define BTA_AV_NUM_COLS             2       /* number of columns in state tables */
+
+/* state table for init state */
+static const UINT8 bta_av_st_init[][BTA_AV_NUM_COLS] =
+{
+/* Event                     Action 1               Next state */
+/* API_DISABLE_EVT */       {BTA_AV_DISABLE,        BTA_AV_INIT_ST },
+/* API_REMOTE_CMD_EVT */    {BTA_AV_IGNORE,         BTA_AV_INIT_ST },
+/* API_VENDOR_CMD_EVT */    {BTA_AV_IGNORE,         BTA_AV_INIT_ST },
+/* API_VENDOR_RSP_EVT */    {BTA_AV_IGNORE,         BTA_AV_INIT_ST },
+/* API_META_RSP_EVT */      {BTA_AV_RC_FREE_RSP,    BTA_AV_INIT_ST },
+/* API_RC_CLOSE_EVT */      {BTA_AV_IGNORE,         BTA_AV_INIT_ST },
+/* AVRC_OPEN_EVT */         {BTA_AV_RC_OPENED,      BTA_AV_OPEN_ST },
+/* AVRC_MSG_EVT */          {BTA_AV_RC_FREE_MSG,    BTA_AV_INIT_ST },
+/* AVRC_NONE_EVT */         {BTA_AV_IGNORE,         BTA_AV_INIT_ST },
+};
+
+/* state table for open state */
+static const UINT8 bta_av_st_open[][BTA_AV_NUM_COLS] =
+{
+/* Event                     Action 1               Next state */
+/* API_DISABLE_EVT */       {BTA_AV_DISABLE,        BTA_AV_INIT_ST },
+/* API_REMOTE_CMD_EVT */    {BTA_AV_RC_REMOTE_CMD,  BTA_AV_OPEN_ST },
+/* API_VENDOR_CMD_EVT */    {BTA_AV_RC_VENDOR_CMD,  BTA_AV_OPEN_ST },
+/* API_VENDOR_RSP_EVT */    {BTA_AV_RC_VENDOR_RSP,  BTA_AV_OPEN_ST },
+/* API_META_RSP_EVT */      {BTA_AV_RC_META_RSP,    BTA_AV_OPEN_ST },
+/* API_RC_CLOSE_EVT */      {BTA_AV_RC_CLOSE,       BTA_AV_OPEN_ST },
+/* AVRC_OPEN_EVT */         {BTA_AV_RC_OPENED,      BTA_AV_OPEN_ST },
+/* AVRC_MSG_EVT */          {BTA_AV_RC_MSG,         BTA_AV_OPEN_ST },
+/* AVRC_NONE_EVT */         {BTA_AV_IGNORE,         BTA_AV_INIT_ST },
+};
+
+/* type for state table */
+typedef const UINT8 (*tBTA_AV_ST_TBL)[BTA_AV_NUM_COLS];
+
+/* state table */
+static const tBTA_AV_ST_TBL bta_av_st_tbl[] =
+{
+    bta_av_st_init,
+    bta_av_st_open
+};
+
+typedef void (*tBTA_AV_NSM_ACT)(tBTA_AV_DATA *p_data);
+static void bta_av_api_enable(tBTA_AV_DATA *p_data);
+static void bta_av_api_register(tBTA_AV_DATA *p_data);
+#if (BTA_AV_SINK_INCLUDED == TRUE)
+static void bta_av_api_sink_enable(tBTA_AV_DATA *p_data);
+#endif
+static void bta_av_ci_data(tBTA_AV_DATA *p_data);
+#if (AVDT_REPORTING == TRUE)
+static void bta_av_rpc_conn(tBTA_AV_DATA *p_data);
+#endif
+static void bta_av_api_to_ssm(tBTA_AV_DATA *p_data);
+
+static void bta_av_sco_chg_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8
+                                 app_id, BD_ADDR peer_addr);
+static void bta_av_sys_rs_cback (tBTA_SYS_CONN_STATUS status,UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
+
+
+/* action functions */
+const tBTA_AV_NSM_ACT bta_av_nsm_act[] =
+{
+    bta_av_api_enable,      /* BTA_AV_API_ENABLE_EVT */
+    bta_av_api_register,    /* BTA_AV_API_REGISTER_EVT */
+    bta_av_api_deregister,  /* BTA_AV_API_DEREGISTER_EVT */
+    bta_av_api_disconnect,  /* BTA_AV_API_DISCONNECT_EVT */
+    bta_av_ci_data,         /* BTA_AV_CI_SRC_DATA_READY_EVT */
+    bta_av_sig_chg,         /* BTA_AV_SIG_CHG_EVT */
+    bta_av_sig_timer,       /* BTA_AV_SIG_TIMER_EVT */
+    bta_av_rc_disc_done,    /* BTA_AV_SDP_AVRC_DISC_EVT */
+    bta_av_rc_closed,       /* BTA_AV_AVRC_CLOSE_EVT */
+    bta_av_conn_chg,        /* BTA_AV_CONN_CHG_EVT */
+    bta_av_dereg_comp,      /* BTA_AV_DEREG_COMP_EVT */
+#if (BTA_AV_SINK_INCLUDED == TRUE)
+    bta_av_api_sink_enable, /* BTA_AV_API_SINK_ENABLE_EVT */
+#endif
+#if (AVDT_REPORTING == TRUE)
+    bta_av_rpc_conn,        /* BTA_AV_AVDT_RPT_CONN_EVT */
+#endif
+    bta_av_api_to_ssm,      /* BTA_AV_API_START_EVT */
+    bta_av_api_to_ssm,      /* BTA_AV_API_STOP_EVT */
+};
+
+/*****************************************************************************
+** Global data
+*****************************************************************************/
+
+/* AV control block */
+#if BTA_DYNAMIC_MEMORY == FALSE
+tBTA_AV_CB  bta_av_cb;
+#endif
+
+#if (defined(BTA_AV_DEBUG) && BTA_AV_DEBUG == TRUE)
+static char *bta_av_st_code(UINT8 state);
+#endif
+
+/*******************************************************************************
+**
+** Function         bta_av_timer_cback
+**
+** Description      forward the event to stream state machine
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_timer_cback(void *p_tle)
+{
+    BT_HDR          *p_buf;
+    TIMER_LIST_ENT  *p = (TIMER_LIST_ENT *)p_tle;
+    int xx;
+    tBTA_AV_SCB *p_scb = NULL;
+
+    /* find the SCB that has the timer */
+    for(xx=0; xx<BTA_AV_NUM_STRS; xx++)
+    {
+        if(bta_av_cb.p_scb[xx] && &(bta_av_cb.p_scb[xx]->timer)== p)
+        {
+            p_scb = bta_av_cb.p_scb[xx];
+            break;
+        }
+    }
+
+    if (p_scb && (p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
+    {
+        /* send the event through the audio state machine.
+         * only when the audio SM is open, the main SM opens the RC connection as INT */
+        p_buf->event = p->event;
+        p_buf->layer_specific = p_scb->hndl;
+        bta_sys_sendmsg(p_buf);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_api_enable
+**
+** Description      Handle an API enable event.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_api_enable(tBTA_AV_DATA *p_data)
+{
+    int i;
+    tBTA_AV_ENABLE      enable;
+
+    /* initialize control block */
+    memset(&bta_av_cb, 0, sizeof(tBTA_AV_CB));
+
+    for(i=0; i<BTA_AV_NUM_RCB; i++)
+        bta_av_cb.rcb[i].handle = BTA_AV_RC_HANDLE_NONE;
+
+    bta_av_cb.rc_acp_handle = BTA_AV_RC_HANDLE_NONE;
+
+    /* store parameters */
+    bta_av_cb.p_cback  = p_data->api_enable.p_cback;
+    bta_av_cb.features = p_data->api_enable.features;
+    bta_av_cb.sec_mask = p_data->api_enable.sec_mask;
+
+    enable.features = bta_av_cb.features;
+
+    /* Register for SCO change event */
+    if (!(bta_av_cb.features & BTA_AV_FEAT_NO_SCO_SSPD))
+    {
+        bta_sys_sco_register(bta_av_sco_chg_cback);
+    }
+
+    /* call callback with enable event */
+    (*bta_av_cb.p_cback)(BTA_AV_ENABLE_EVT, (tBTA_AV *)&enable);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_addr_to_scb
+**
+** Description      find the stream control block by the peer addr
+**
+** Returns          void
+**
+*******************************************************************************/
+static tBTA_AV_SCB * bta_av_addr_to_scb(BD_ADDR bd_addr)
+{
+    tBTA_AV_SCB * p_scb = NULL;
+    int         xx;
+
+    for(xx=0; xx<BTA_AV_NUM_STRS; xx++)
+    {
+        if(bta_av_cb.p_scb[xx])
+        {
+            if(!bdcmp(bd_addr, bta_av_cb.p_scb[xx]->peer_addr))
+            {
+                p_scb = bta_av_cb.p_scb[xx];
+                break;
+            }
+        }
+    }
+    return p_scb;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_hndl_to_scb
+**
+** Description      find the stream control block by the handle
+**
+** Returns          void
+**
+*******************************************************************************/
+tBTA_AV_SCB * bta_av_hndl_to_scb(UINT16 handle)
+{
+    tBTA_AV_HNDL hndl = (tBTA_AV_HNDL)handle;
+    tBTA_AV_SCB * p_scb = NULL;
+    UINT8 idx = (hndl & BTA_AV_HNDL_MSK);
+
+    if(idx && (idx <= BTA_AV_NUM_STRS) )
+    {
+        p_scb = bta_av_cb.p_scb[idx-1];
+    }
+    return p_scb;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_alloc_scb
+**
+** Description      allocate stream control block,
+**                  register the service to stack
+**                  create SDP record
+**
+** Returns          void
+**
+*******************************************************************************/
+static tBTA_AV_SCB * bta_av_alloc_scb(tBTA_AV_CHNL chnl)
+{
+    tBTA_AV_SCB *p_ret = NULL;
+    int         xx;
+    tBTA_AV_STATUS sts = BTA_AV_SUCCESS;
+
+    if(chnl == BTA_AV_CHNL_VIDEO)
+    {
+        if(p_bta_av_cfg->p_act_tbl == NULL || p_bta_av_cfg->p_reg == NULL)
+        {
+            APPL_TRACE_ERROR("Video streaming not supported");
+            sts = BTA_AV_FAIL;
+        }
+        else
+        {
+            /* allow only one Video channel */
+            if(bta_av_cb.reg_video)
+            {
+                APPL_TRACE_ERROR("Already registered");
+                sts = BTA_AV_FAIL;
+            }
+        }
+    }
+    else if(chnl != BTA_AV_CHNL_AUDIO)
+    {
+        APPL_TRACE_ERROR("bad channel: %d", chnl);
+        sts = BTA_AV_FAIL;
+    }
+
+    if(sts == BTA_AV_SUCCESS)
+    {
+        for(xx=0; xx<BTA_AV_NUM_STRS; xx++)
+        {
+            if(bta_av_cb.p_scb[xx] == NULL)
+            {
+                /* found an empty spot */
+                p_ret = (tBTA_AV_SCB *)GKI_getbuf(sizeof(tBTA_AV_SCB));
+                if(p_ret)
+                {
+                    memset(p_ret, 0, sizeof(tBTA_AV_SCB));
+                    p_ret->rc_handle = BTA_AV_RC_HANDLE_NONE;
+                    p_ret->chnl = chnl;
+                    p_ret->hndl = (tBTA_AV_HNDL)((xx + 1) | chnl);
+                    p_ret->hdi  = xx;
+                    p_ret->a2d_list = list_new(NULL);
+                    bta_av_cb.p_scb[xx] = p_ret;
+                }
+                break;
+            }
+        }
+    }
+    return p_ret;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_free_scb
+**
+** Description      free stream control block,
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_free_scb(tBTA_AV_SCB *p_scb)
+{
+    // NOTE(google) This free currently is not called
+    assert(p_scb != NULL);
+
+    list_free(p_scb->a2d_list);
+    GKI_freebuf(p_scb);
+}
+
+/*******************************************************************************
+*******************************************************************************/
+void bta_av_conn_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
+{
+    tBTA_AV_STR_MSG     *p_msg;
+    UINT16  evt = 0;
+    tBTA_AV_SCB *p_scb = NULL;
+    UNUSED(handle);
+
+#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
+    if (event == BTA_AR_AVDT_CONN_EVT ||
+        event == AVDT_CONNECT_IND_EVT || event == AVDT_DISCONNECT_IND_EVT)
+#else
+    if (event == AVDT_CONNECT_IND_EVT || event == AVDT_DISCONNECT_IND_EVT)
+#endif
+    {
+        evt = BTA_AV_SIG_CHG_EVT;
+        if(AVDT_DISCONNECT_IND_EVT == event)
+            p_scb = bta_av_addr_to_scb(bd_addr);
+#if (defined(BTA_AV_DEBUG) && BTA_AV_DEBUG == TRUE)
+        else if (AVDT_CONNECT_IND_EVT == event)
+        {
+            APPL_TRACE_DEBUG("CONN_IND is ACP:%d", p_data->hdr.err_param);
+        }
+#endif
+
+        if (/*((p_scb && (p_scb->role & BTA_AV_ROLE_AD_ACP)) ||
+
+            //(AVDT_CONNECT_IND_EVT == event && AVDT_ACP == p_data->hdr.err_param))
+
+            (AVDT_CONNECT_IND_EVT == event))&& */
+            (p_msg = (tBTA_AV_STR_MSG *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_STR_MSG)))) != NULL)
+        {
+            p_msg->hdr.event = evt;
+            p_msg->hdr.layer_specific = event;
+            p_msg->hdr.offset = p_data->hdr.err_param;
+            bdcpy(p_msg->bd_addr, bd_addr);
+#if (defined(BTA_AV_DEBUG) && BTA_AV_DEBUG == TRUE)
+            if(p_scb)
+            {
+                APPL_TRACE_DEBUG("scb hndl x%x, role x%x", p_scb->hndl, p_scb->role);
+            }
+#endif
+            APPL_TRACE_DEBUG("conn_cback bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
+                          bd_addr[0], bd_addr[1],
+                          bd_addr[2], bd_addr[3],
+                          bd_addr[4], bd_addr[5]);
+            bta_sys_sendmsg(p_msg);
+        }
+    }
+
+}
+
+#if AVDT_REPORTING == TRUE
+/*******************************************************************************
+**
+** Function         bta_av_a2dp_report_cback
+**
+** Description      A2DP report callback.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_a2dp_report_cback(UINT8 handle, AVDT_REPORT_TYPE type,
+                                    tAVDT_REPORT_DATA *p_data)
+{
+    UNUSED(handle);
+    UNUSED(type);
+    UNUSED(p_data);
+    /* Do not need to handle report data for now.
+     * This empty function is here for conformance reasons. */
+}
+#endif
+
+#if (BTA_AV_SINK_INCLUDED == TRUE)
+/*******************************************************************************
+**
+** Function         bta_av_api_sink_enable
+**
+** Description      activate, deactive A2DP Sink,
+**
+** Returns          void
+**
+*******************************************************************************/
+
+static void bta_av_api_sink_enable(tBTA_AV_DATA *p_data)
+{
+    UINT16 activate_sink = 0;
+    activate_sink = p_data->hdr.layer_specific;
+    APPL_TRACE_DEBUG("bta_av_api_sink_enable %d ", activate_sink)
+    char p_service_name[BTA_SERVICE_NAME_LEN+1];
+    BCM_STRNCPY_S(p_service_name, sizeof(p_service_name),
+            BTIF_AVK_SERVICE_NAME, BTA_SERVICE_NAME_LEN);
+
+    if(activate_sink)
+    {
+        AVDT_SINK_Activate();
+        if (bta_av_cb.sdp_a2d_snk_handle == 0)
+        {
+            bta_av_cb.sdp_a2d_snk_handle = SDP_CreateRecord();
+            A2D_AddRecord(UUID_SERVCLASS_AUDIO_SINK, p_service_name, NULL,
+                          A2D_SUPF_PLAYER, bta_av_cb.sdp_a2d_snk_handle);
+            bta_sys_add_uuid(UUID_SERVCLASS_AUDIO_SINK);
+        }
+    }
+    else
+    {
+        AVDT_SINK_Deactivate();
+        if (bta_av_cb.sdp_a2d_snk_handle != 0)
+        {
+            SDP_DeleteRecord(bta_av_cb.sdp_a2d_snk_handle);
+            bta_av_cb.sdp_a2d_snk_handle = 0;
+            bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SINK);
+        }
+    }
+}
+#endif
+/*******************************************************************************
+**
+** Function         bta_av_api_register
+**
+** Description      allocate stream control block,
+**                  register the service to stack
+**                  create SDP record
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_api_register(tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_REGISTER    registr;
+    tBTA_AV_SCB         *p_scb;    /* stream control block */
+    tAVDT_REG       reg;
+    tAVDT_CS        cs;
+    char            *p_service_name;
+    tBTA_AV_CODEC   codec_type;
+    tBTA_UTL_COD    cod;
+    UINT8           index = 0;
+    char p_avk_service_name[BTA_SERVICE_NAME_LEN+1];
+    BCM_STRNCPY_S(p_avk_service_name, sizeof(p_avk_service_name), BTIF_AVK_SERVICE_NAME, BTA_SERVICE_NAME_LEN);
+
+    memset(&cs,0,sizeof(tAVDT_CS));
+
+    registr.status = BTA_AV_FAIL_RESOURCES;
+    registr.app_id = p_data->api_reg.app_id;
+    registr.chnl   = (tBTA_AV_CHNL)p_data->hdr.layer_specific;
+    do
+    {
+        p_scb = bta_av_alloc_scb(registr.chnl);
+        if(p_scb == NULL)
+        {
+            APPL_TRACE_ERROR("failed to alloc SCB");
+            break;
+        }
+
+        registr.hndl    = p_scb->hndl;
+        p_scb->app_id   = registr.app_id;
+
+        /* initialize the stream control block */
+        p_scb->timer.p_cback = (TIMER_CBACK*)&bta_av_timer_cback;
+        registr.status = BTA_AV_SUCCESS;
+
+        if((bta_av_cb.reg_audio + bta_av_cb.reg_video) == 0)
+        {
+            /* the first channel registered. register to AVDTP */
+            reg.ctrl_mtu = p_bta_av_cfg->sig_mtu;
+            reg.ret_tout = BTA_AV_RET_TOUT;
+            reg.sig_tout = BTA_AV_SIG_TOUT;
+            reg.idle_tout = BTA_AV_IDLE_TOUT;
+            reg.sec_mask = bta_av_cb.sec_mask;
+#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
+            bta_ar_reg_avdt(&reg, bta_av_conn_cback, BTA_ID_AV);
+#endif
+            bta_sys_role_chg_register(&bta_av_sys_rs_cback);
+
+            /* create remote control TG service if required */
+            if (bta_av_cb.features & (BTA_AV_FEAT_RCTG))
+            {
+                /* register with no authorization; let AVDTP use authorization instead */
+#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
+#if (BTA_AV_WITH_AVCTP_AUTHORIZATION == TRUE)
+                bta_ar_reg_avct(p_bta_av_cfg->avrc_mtu, p_bta_av_cfg->avrc_br_mtu,
+                                bta_av_cb.sec_mask, BTA_ID_AV);
+#else
+                bta_ar_reg_avct(p_bta_av_cfg->avrc_mtu, p_bta_av_cfg->avrc_br_mtu,
+                                (UINT8)(bta_av_cb.sec_mask & (~BTA_SEC_AUTHORIZE)), BTA_ID_AV);
+#endif
+
+                bta_ar_reg_avrc(UUID_SERVCLASS_AV_REM_CTRL_TARGET, "AV Remote Control Target", NULL,
+                                p_bta_av_cfg->avrc_tg_cat, BTA_ID_AV);
+#endif
+            }
+
+            /* Set the Capturing service class bit */
+#if (BTA_AV_SINK_INCLUDED == TRUE)
+            cod.service = BTM_COD_SERVICE_CAPTURING | BTM_COD_SERVICE_RENDERING;
+#else
+            cod.service = BTM_COD_SERVICE_CAPTURING;
+#endif
+            utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS);
+        } /* if 1st channel */
+
+        /* get stream configuration and create stream */
+        /* memset(&cs.cfg,0,sizeof(tAVDT_CFG)); */
+        cs.cfg.num_codec = 1;
+        cs.tsep = AVDT_TSEP_SRC;
+
+        /*
+         * memset of cs takes care setting call back pointers to null.
+        cs.p_data_cback = NULL;
+        cs.p_report_cback = NULL;
+        */
+        cs.nsc_mask = AVDT_NSC_RECONFIG |
+              ((bta_av_cb.features & BTA_AV_FEAT_PROTECT) ? 0 : AVDT_NSC_SECURITY);
+        APPL_TRACE_DEBUG("nsc_mask: 0x%x", cs.nsc_mask);
+
+        if (p_data->api_reg.p_service_name[0] == 0)
+        {
+            p_service_name = NULL;
+        }
+        else
+        {
+            p_service_name = p_data->api_reg.p_service_name;
+        }
+
+        p_scb->suspend_sup  = TRUE;
+        p_scb->recfg_sup    = TRUE;
+
+        cs.p_ctrl_cback  = bta_av_dt_cback[p_scb->hdi];
+        if(registr.chnl == BTA_AV_CHNL_AUDIO)
+        {
+            /* set up the audio stream control block */
+            p_scb->p_act_tbl = (const tBTA_AV_ACT *)bta_av_a2d_action;
+            p_scb->p_cos     = &bta_av_a2d_cos;
+            p_scb->media_type= AVDT_MEDIA_AUDIO;
+            cs.cfg.psc_mask  = AVDT_PSC_TRANS;
+            cs.media_type    = AVDT_MEDIA_AUDIO;
+            cs.mtu           = p_bta_av_cfg->audio_mtu;
+            cs.flush_to      = L2CAP_DEFAULT_FLUSH_TO;
+#if AVDT_REPORTING == TRUE
+            if(bta_av_cb.features & BTA_AV_FEAT_REPORT)
+            {
+                cs.cfg.psc_mask |= AVDT_PSC_REPORT;
+                cs.p_report_cback = bta_av_a2dp_report_cback;
+#if AVDT_MULTIPLEXING == TRUE
+                cs.cfg.mux_tsid_report = 2;
+#endif
+            }
+#endif
+            if(bta_av_cb.features & BTA_AV_FEAT_DELAY_RPT)
+                cs.cfg.psc_mask |= AVDT_PSC_DELAY_RPT;
+
+            /* keep the configuration in the stream control block */
+            memcpy(&p_scb->cfg, &cs.cfg, sizeof(tAVDT_CFG));
+            while(index < BTA_AV_MAX_SEPS &&
+                (*bta_av_a2d_cos.init)(&codec_type, cs.cfg.codec_info,
+                &cs.cfg.num_protect, cs.cfg.protect_info, index) == TRUE)
+            {
+
+#if (BTA_AV_SINK_INCLUDED == TRUE)
+            if(index == 1)
+            {
+                cs.tsep = AVDT_TSEP_SNK;
+                cs.p_data_cback = bta_av_stream_data_cback;
+            }
+                APPL_TRACE_DEBUG(" SEP Type = %d",cs.tsep);
+#endif
+                if(AVDT_CreateStream(&p_scb->seps[index].av_handle, &cs) == AVDT_SUCCESS)
+                {
+                    p_scb->seps[index].codec_type = codec_type;
+
+#if (BTA_AV_SINK_INCLUDED == TRUE)
+                    p_scb->seps[index].tsep = cs.tsep;
+                    if(cs.tsep == AVDT_TSEP_SNK)
+                        p_scb->seps[index].p_app_data_cback = p_data->api_reg.p_app_data_cback;
+                    else
+                        p_scb->seps[index].p_app_data_cback = NULL; /* In case of A2DP SOURCE we don't need a callback to handle media packets */
+#endif
+
+                    APPL_TRACE_DEBUG("audio[%d] av_handle: %d codec_type: %d",
+                        index, p_scb->seps[index].av_handle, p_scb->seps[index].codec_type);
+                    index++;
+                }
+                else
+                    break;
+            }
+
+            if(!bta_av_cb.reg_audio)
+            {
+                /* create the SDP records on the 1st audio channel */
+                bta_av_cb.sdp_a2d_handle = SDP_CreateRecord();
+                A2D_AddRecord(UUID_SERVCLASS_AUDIO_SOURCE, p_service_name, NULL,
+                                  A2D_SUPF_PLAYER, bta_av_cb.sdp_a2d_handle);
+                bta_sys_add_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
+
+#if (BTA_AV_SINK_INCLUDED == TRUE)
+                bta_av_cb.sdp_a2d_snk_handle = SDP_CreateRecord();
+                A2D_AddRecord(UUID_SERVCLASS_AUDIO_SINK, p_avk_service_name, NULL,
+                                  A2D_SUPF_PLAYER, bta_av_cb.sdp_a2d_snk_handle);
+                bta_sys_add_uuid(UUID_SERVCLASS_AUDIO_SINK);
+#endif
+                /* start listening when A2DP is registered */
+                if (bta_av_cb.features & BTA_AV_FEAT_RCTG)
+                    bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
+
+                /* if the AV and AVK are both supported, it cannot support the CT role */
+                if (bta_av_cb.features & (BTA_AV_FEAT_RCCT))
+                {
+                    /* if TG is not supported, we need to register to AVCT now */
+                    if ((bta_av_cb.features & (BTA_AV_FEAT_RCTG)) == 0)
+                    {
+#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
+#if (BTA_AV_WITH_AVCTP_AUTHORIZATION == TRUE)
+                        bta_ar_reg_avct(p_bta_av_cfg->avrc_mtu, p_bta_av_cfg->avrc_br_mtu,
+                                        bta_av_cb.sec_mask, BTA_ID_AV);
+#else
+                        bta_ar_reg_avct(p_bta_av_cfg->avrc_mtu, p_bta_av_cfg->avrc_br_mtu,
+                                        (UINT8)(bta_av_cb.sec_mask & (~BTA_SEC_AUTHORIZE)), BTA_ID_AV);
+#endif
+#endif
+                    }
+#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
+                    /* create an SDP record as AVRC CT. */
+                    bta_ar_reg_avrc(UUID_SERVCLASS_AV_REMOTE_CONTROL, NULL, NULL,
+                           p_bta_av_cfg->avrc_ct_cat, BTA_ID_AV);
+#endif
+                }
+            }
+            bta_av_cb.reg_audio |= BTA_AV_HNDL_TO_MSK(p_scb->hdi);
+            APPL_TRACE_DEBUG("reg_audio: 0x%x",bta_av_cb.reg_audio);
+        }
+        else
+        {
+            bta_av_cb.reg_video = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
+            bta_av_cb.sdp_vdp_handle = SDP_CreateRecord();
+            /* register the video channel */
+            /* no need to verify the function pointer here. it's verified prior */
+            (*p_bta_av_cfg->p_reg)(&cs, p_service_name, p_scb);
+        }
+    } while (0);
+
+    /* call callback with register event */
+    (*bta_av_cb.p_cback)(BTA_AV_REGISTER_EVT, (tBTA_AV *)&registr);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_api_deregister
+**
+** Description      de-register a channel
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_api_deregister(tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_SCB *p_scb = bta_av_hndl_to_scb(p_data->hdr.layer_specific);
+
+    if(p_scb)
+    {
+        p_scb->deregistring = TRUE;
+        bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, p_data);
+    }
+    else
+    {
+        bta_av_dereg_comp(p_data);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_ci_data
+**
+** Description      forward the BTA_AV_CI_SRC_DATA_READY_EVT to stream state machine
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_ci_data(tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_SCB *p_scb;
+    int     i;
+    UINT8   chnl = (UINT8)p_data->hdr.layer_specific;
+
+    for( i=0; i < BTA_AV_NUM_STRS; i++ )
+    {
+        p_scb = bta_av_cb.p_scb[i];
+
+        if(p_scb && p_scb->chnl == chnl)
+        {
+            bta_av_ssm_execute(p_scb, BTA_AV_SRC_DATA_READY_EVT, p_data);
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_rpc_conn
+**
+** Description      report report channel open
+**
+** Returns          void
+**
+*******************************************************************************/
+#if (AVDT_REPORTING == TRUE)
+static void bta_av_rpc_conn(tBTA_AV_DATA *p_data)
+{
+    UNUSED(p_data);
+}
+#endif
+
+/*******************************************************************************
+**
+** Function         bta_av_api_to_ssm
+**
+** Description      forward the API request to stream state machine
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_api_to_ssm(tBTA_AV_DATA *p_data)
+{
+    int xx;
+    UINT16 event = p_data->hdr.event - BTA_AV_FIRST_A2S_API_EVT + BTA_AV_FIRST_A2S_SSM_EVT;
+
+    for(xx=0; xx<BTA_AV_NUM_STRS; xx++)
+    {
+        bta_av_ssm_execute(bta_av_cb.p_scb[xx], event, p_data);
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_chk_start
+**
+** Description      if this is audio channel, check if more than one audio
+**                  channel is connected & already started.
+**
+** Returns          TRUE, if need api_start
+**
+*******************************************************************************/
+BOOLEAN bta_av_chk_start(tBTA_AV_SCB *p_scb)
+{
+    BOOLEAN start = FALSE;
+    tBTA_AV_SCB *p_scbi;
+    int i;
+
+    if(p_scb->chnl == BTA_AV_CHNL_AUDIO)
+    {
+        if ((bta_av_cb.audio_open_cnt >= 2) &&
+            ((0 == (p_scb->role & BTA_AV_ROLE_AD_ACP)) ||       /* Outgoing connection or   */
+             (bta_av_cb.features & BTA_AV_FEAT_ACP_START)))     /* auto-starting option     */
+        {
+            /* more than one audio channel is connected */
+            /* if this is the 2nd stream as ACP, give INT a chance to issue the START command */
+            for(i=0; i<BTA_AV_NUM_STRS; i++)
+            {
+                p_scbi = bta_av_cb.p_scb[i];
+                if(p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started)
+                {
+                    start = TRUE;
+                    /* may need to update the flush timeout of this already started stream */
+                    if(p_scbi->co_started != bta_av_cb.audio_open_cnt)
+                    {
+                        p_scbi->co_started = bta_av_cb.audio_open_cnt;
+                        L2CA_SetFlushTimeout(p_scbi->peer_addr, p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1] );
+                    }
+                }
+            }
+        }
+    }
+    return start;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_restore_switch
+**
+** Description      assume that the caller of this function already makes
+**                  sure that there's only one ACL connection left
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_restore_switch (void)
+{
+    tBTA_AV_CB   *p_cb = &bta_av_cb;
+    int     i;
+    UINT8   mask;
+
+    APPL_TRACE_DEBUG("reg_audio: 0x%x",bta_av_cb.reg_audio);
+    for(i=0; i<BTA_AV_NUM_STRS; i++)
+    {
+        mask = BTA_AV_HNDL_TO_MSK(i);
+        if (p_cb->conn_audio == mask)
+        {
+            if (p_cb->p_scb[i])
+            {
+                bta_sys_set_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH, p_cb->p_scb[i]->peer_addr);
+            }
+            break;
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_sys_rs_cback
+**
+** Description      Receives the role change event from dm
+**
+** Returns          (BTA_SYS_ROLE_CHANGE, new_role, hci_status, p_bda)
+**
+*******************************************************************************/
+static void bta_av_sys_rs_cback (tBTA_SYS_CONN_STATUS status,UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
+{
+    int         i;
+    tBTA_AV_SCB *p_scb = NULL;
+    tBTA_AV_ROLE_RES  *p_buf;
+    UINT8       cur_role;
+    UINT8       peer_idx = 0;
+    UNUSED(status);
+
+    APPL_TRACE_DEBUG("bta_av_sys_rs_cback: %d", bta_av_cb.rs_idx);
+    for(i=0; i<BTA_AV_NUM_STRS; i++)
+    {
+        /* loop through all the SCBs to find matching peer addresses and report the role change event */
+        /* note that more than one SCB (a2dp & vdp) maybe waiting for this event */
+        p_scb = bta_av_cb.p_scb[i];
+        if (p_scb && (bdcmp (peer_addr, p_scb->peer_addr) == 0) &&
+            (p_buf = (tBTA_AV_ROLE_RES *) GKI_getbuf(sizeof(tBTA_AV_ROLE_RES))) != NULL)
+        {
+            APPL_TRACE_DEBUG("new_role:%d, hci_status:x%x hndl: x%x", id, app_id, p_scb->hndl);
+            /*
+            if ((id != BTM_ROLE_MASTER) && (app_id != HCI_SUCCESS))
+            {
+                bta_sys_set_policy(BTA_ID_AV, (HCI_ENABLE_MASTER_SLAVE_SWITCH|HCI_ENABLE_SNIFF_MODE), p_scb->peer_addr);
+            }
+            */
+            p_buf->hdr.event = BTA_AV_ROLE_CHANGE_EVT;
+            p_buf->hdr.layer_specific = p_scb->hndl;
+            p_buf->new_role = id;
+            p_buf->hci_status = app_id;
+            bta_sys_sendmsg(p_buf);
+
+            peer_idx = p_scb->hdi + 1;  /* Handle index for the peer_addr */
+        }
+    }
+
+    /* restore role switch policy, if role switch failed */
+    if ((HCI_SUCCESS != app_id) &&
+        (BTM_GetRole (peer_addr, &cur_role) == BTM_SUCCESS) &&
+        (cur_role == BTM_ROLE_SLAVE) )
+    {
+        bta_sys_set_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH, peer_addr);
+    }
+
+    /* if BTA_AvOpen() was called for other device, which caused the role switch of the peer_addr,  */
+    /* we need to continue opening process for the BTA_AvOpen().                                    */
+    if ((bta_av_cb.rs_idx != 0) && (bta_av_cb.rs_idx != peer_idx))
+    {
+        if ((bta_av_cb.rs_idx -1) < BTA_AV_NUM_STRS)
+        {
+            p_scb = bta_av_cb.p_scb[bta_av_cb.rs_idx - 1];
+        }
+        if (p_scb && p_scb->q_tag == BTA_AV_Q_TAG_OPEN)
+        {
+            APPL_TRACE_DEBUG ("bta_av_sys_rs_cback: rs_idx(%d), hndl:x%x q_tag: %d",
+                bta_av_cb.rs_idx, p_scb->hndl, p_scb->q_tag);
+
+            if(HCI_SUCCESS == app_id || HCI_ERR_NO_CONNECTION == app_id)
+                p_scb->q_info.open.switch_res = BTA_AV_RS_OK;
+            else
+                p_scb->q_info.open.switch_res = BTA_AV_RS_FAIL;
+
+            /* Continue av open process */
+            bta_av_do_disc_a2d (p_scb, (tBTA_AV_DATA *)&(p_scb->q_info.open));
+        }
+
+        bta_av_cb.rs_idx = 0;
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_sco_chg_cback
+**
+** Description      receive & process the SCO connection up/down event from sys.
+**                  call setup also triggers this callback, to suspend av before sco
+**                  activity happens, or to resume av once call ends.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_av_sco_chg_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8
+                                 app_id, BD_ADDR peer_addr)
+{
+    tBTA_AV_SCB *p_scb;
+    int     i;
+    tBTA_AV_API_STOP stop;
+    UNUSED(app_id);
+    UNUSED(peer_addr);
+
+    APPL_TRACE_DEBUG("bta_av_sco_chg_cback:%d status:%d", id, status);
+    if(id)
+    {
+        bta_av_cb.sco_occupied = TRUE;
+
+        /* either BTA_SYS_SCO_OPEN or BTA_SYS_SCO_CLOSE with remaining active SCO */
+        for(i=0; i<BTA_AV_NUM_STRS; i++)
+        {
+            p_scb = bta_av_cb.p_scb[i];
+
+            if( p_scb && p_scb->co_started && (p_scb->sco_suspend == FALSE))
+            {
+                APPL_TRACE_DEBUG("suspending scb:%d", i);
+                /* scb is used and started, not suspended automatically */
+                p_scb->sco_suspend = TRUE;
+                stop.flush   = FALSE;
+                stop.suspend = TRUE;
+                bta_av_ssm_execute(p_scb, BTA_AV_AP_STOP_EVT, (tBTA_AV_DATA *)&stop);
+            }
+        }
+    }
+    else
+    {
+        bta_av_cb.sco_occupied = FALSE;
+
+        for(i=0; i<BTA_AV_NUM_STRS; i++)
+        {
+            p_scb = bta_av_cb.p_scb[i];
+
+            if( p_scb && p_scb->sco_suspend ) /* scb is used and suspended for SCO */
+            {
+                APPL_TRACE_DEBUG("starting scb:%d", i);
+                bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
+            }
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_switch_if_needed
+**
+** Description      This function checks if there is another existing AV
+**                  channel that is local as slave role.
+**                  If so, role switch and remove it from link policy.
+**
+** Returns          TRUE, if role switch is done
+**
+*******************************************************************************/
+BOOLEAN bta_av_switch_if_needed(tBTA_AV_SCB *p_scb)
+{
+    UINT8 role;
+    BOOLEAN needed = FALSE;
+    tBTA_AV_SCB *p_scbi;
+    int i;
+    UINT8       mask;
+
+    for(i=0; i<BTA_AV_NUM_STRS; i++)
+    {
+        mask = BTA_AV_HNDL_TO_MSK(i);
+        p_scbi = bta_av_cb.p_scb[i];
+        if( p_scbi && (p_scb->hdi != i) && /* not the original channel */
+            ((bta_av_cb.conn_audio & mask) ||/* connected audio */
+            (bta_av_cb.conn_video & mask)) ) /* connected video */
+        {
+            BTM_GetRole(p_scbi->peer_addr, &role);
+            /* this channel is open - clear the role switch link policy for this link */
+            if(BTM_ROLE_MASTER != role)
+            {
+                if (bta_av_cb.features & BTA_AV_FEAT_MASTER)
+                    bta_sys_clear_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH, p_scbi->peer_addr);
+                if (BTM_CMD_STARTED != BTM_SwitchRole(p_scbi->peer_addr, BTM_ROLE_MASTER, NULL))
+                {
+                    /* can not switch role on SCBI
+                     * start the timer on SCB - because this function is ONLY called when SCB gets API_OPEN */
+                    bta_sys_start_timer(&p_scb->timer, BTA_AV_AVRC_TIMER_EVT, BTA_AV_RS_TIME_VAL);
+                }
+                needed = TRUE;
+                /* mark the original channel as waiting for RS result */
+                bta_av_cb.rs_idx = p_scb->hdi + 1;
+                break;
+            }
+        }
+    }
+    return needed;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_link_role_ok
+**
+** Description      This function checks if the SCB has existing ACL connection
+**                  If so, check if the link role fits the requirements.
+**
+** Returns          TRUE, if role is ok
+**
+*******************************************************************************/
+BOOLEAN bta_av_link_role_ok(tBTA_AV_SCB *p_scb, UINT8 bits)
+{
+    UINT8 role;
+    BOOLEAN is_ok = TRUE;
+
+    if (BTM_GetRole(p_scb->peer_addr, &role) == BTM_SUCCESS)
+    {
+        LOG_INFO("%s hndl:x%x role:%d conn_audio:x%x bits:%d features:x%x",
+                __func__, p_scb->hndl, role, bta_av_cb.conn_audio, bits,
+                bta_av_cb.features);
+        if (BTM_ROLE_MASTER != role && (A2D_BitsSet(bta_av_cb.conn_audio) > bits || (bta_av_cb.features & BTA_AV_FEAT_MASTER)))
+        {
+            if (bta_av_cb.features & BTA_AV_FEAT_MASTER)
+                bta_sys_clear_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH, p_scb->peer_addr);
+
+            if (BTM_CMD_STARTED != BTM_SwitchRole(p_scb->peer_addr, BTM_ROLE_MASTER, NULL))
+            {
+                /* can not switch role on SCB - start the timer on SCB */
+            }
+            is_ok = FALSE;
+            p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_START;
+
+        }
+    }
+
+    return is_ok;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_chk_mtu
+**
+** Description      if this is audio channel, check if more than one audio
+**                  channel is connected.
+**
+** Returns          The smallest mtu of the connected audio channels
+**
+*******************************************************************************/
+UINT16 bta_av_chk_mtu(tBTA_AV_SCB *p_scb, UINT16 mtu)
+{
+    UINT16 ret_mtu = BTA_AV_MAX_A2DP_MTU;
+    tBTA_AV_SCB *p_scbi;
+    int i;
+    UINT8   mask;
+    UNUSED(mtu);
+
+    /* TODO_MV mess with the mtu according to the number of EDR/non-EDR headsets */
+    if(p_scb->chnl == BTA_AV_CHNL_AUDIO)
+    {
+        if(bta_av_cb.audio_open_cnt >= 2)
+        {
+            /* more than one audio channel is connected */
+            for(i=0; i<BTA_AV_NUM_STRS; i++)
+            {
+                p_scbi = bta_av_cb.p_scb[i];
+                if((p_scb != p_scbi) && p_scbi && (p_scbi->chnl == BTA_AV_CHNL_AUDIO) )
+                {
+                    mask = BTA_AV_HNDL_TO_MSK(i);
+                    APPL_TRACE_DEBUG("[%d] mtu: %d, mask:0x%x",
+                        i, p_scbi->stream_mtu, mask);
+                    if(bta_av_cb.conn_audio & mask)
+                    {
+                        if(ret_mtu > p_scbi->stream_mtu)
+                            ret_mtu = p_scbi->stream_mtu;
+                    }
+                }
+            }
+        }
+        APPL_TRACE_DEBUG("bta_av_chk_mtu audio count:%d, conn_audio:0x%x, ret:%d",
+            bta_av_cb.audio_open_cnt, bta_av_cb.conn_audio, ret_mtu);
+    }
+    return ret_mtu;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_dup_audio_buf
+**
+** Description      dup the audio data to the q_info.a2d of other audio channels
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_dup_audio_buf(tBTA_AV_SCB *p_scb, BT_HDR *p_buf)
+{
+    tBTA_AV_SCB *p_scbi;
+    int     i;
+    UINT16  size, copy_size;
+    BT_HDR *p_new;
+
+    if(!p_buf)
+        return;
+
+    if(bta_av_cb.audio_open_cnt >= 2)
+    {
+        size = GKI_get_buf_size(p_buf);
+        copy_size = BT_HDR_SIZE + p_buf->len + p_buf->offset;
+        /* more than one audio channel is connected */
+        for(i=0; i<BTA_AV_NUM_STRS; i++)
+        {
+            p_scbi = bta_av_cb.p_scb[i];
+            if( (p_scb->hdi != i) && /* not the original channel */
+                (bta_av_cb.conn_audio & BTA_AV_HNDL_TO_MSK(i)) && /* connected audio */
+                p_scbi && p_scbi->co_started ) /* scb is used and started */
+            {
+                /* enqueue the data only when the stream is started */
+                p_new = (BT_HDR *)GKI_getbuf(size);
+                if(p_new)
+                {
+                    memcpy(p_new, p_buf, copy_size);
+                    list_append(p_scbi->a2d_list, p_new);
+                    if (list_length(p_scbi->a2d_list) >  p_bta_av_cfg->audio_mqs) {
+                        // Drop the oldest packet
+                        bta_av_co_audio_drop(p_scbi->hndl);
+                        BT_HDR *p_buf = list_front(p_scbi->a2d_list);
+                        list_remove(p_scbi->a2d_list, p_buf);
+                        GKI_freebuf(p_buf);
+                    }
+                }
+            }
+        }
+    }
+
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_sm_execute
+**
+** Description      State machine event handling function for AV
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_sm_execute(tBTA_AV_CB *p_cb, UINT16 event, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_ST_TBL      state_table;
+    UINT8               action;
+
+#if (defined(BTA_AV_DEBUG) && BTA_AV_DEBUG == TRUE)
+    APPL_TRACE_EVENT("AV event=0x%x(%s) state=%d(%s)",
+        event, bta_av_evt_code(event), p_cb->state, bta_av_st_code(p_cb->state));
+#else
+    APPL_TRACE_EVENT("AV event=0x%x state=%d", event, p_cb->state);
+#endif
+
+    /* look up the state table for the current state */
+    state_table = bta_av_st_tbl[p_cb->state];
+
+    event &= 0x00FF;
+
+    /* set next state */
+    p_cb->state = state_table[event][BTA_AV_NEXT_STATE];
+    APPL_TRACE_EVENT("next state=%d", p_cb->state);
+
+    /* execute action functions */
+    if ((action = state_table[event][BTA_AV_ACTION_COL]) != BTA_AV_IGNORE)
+    {
+        (*bta_av_action[action])(p_cb, p_data);
+    }
+}
+
+
+/*******************************************************************************
+**
+** Function         bta_av_hdl_event
+**
+** Description      Advanced audio/video main event handling function.
+**
+**
+** Returns          BOOLEAN
+**
+*******************************************************************************/
+BOOLEAN bta_av_hdl_event(BT_HDR *p_msg)
+{
+    UINT16 event = p_msg->event;
+    UINT16 first_event = BTA_AV_FIRST_NSM_EVT;
+
+    if (event > BTA_AV_LAST_EVT)
+    {
+        return TRUE; /* to free p_msg */
+    }
+
+    if(event >= first_event)
+    {
+#if (defined(BTA_AV_DEBUG) && BTA_AV_DEBUG == TRUE)
+        APPL_TRACE_VERBOSE("AV nsm event=0x%x(%s)", event, bta_av_evt_code(event));
+#else
+        APPL_TRACE_VERBOSE("AV nsm event=0x%x", event);
+#endif
+        /* non state machine events */
+        (*bta_av_nsm_act[event - BTA_AV_FIRST_NSM_EVT]) ((tBTA_AV_DATA *) p_msg);
+    }
+    else if (event >= BTA_AV_FIRST_SM_EVT && event <= BTA_AV_LAST_SM_EVT)
+    {
+#if (defined(BTA_AV_DEBUG) && BTA_AV_DEBUG == TRUE)
+        APPL_TRACE_VERBOSE("AV sm event=0x%x(%s)", event, bta_av_evt_code(event));
+#else
+        APPL_TRACE_VERBOSE("AV sm event=0x%x", event);
+#endif
+        /* state machine events */
+        bta_av_sm_execute(&bta_av_cb, p_msg->event, (tBTA_AV_DATA *) p_msg);
+    }
+    else
+    {
+        APPL_TRACE_VERBOSE("handle=0x%x", p_msg->layer_specific);
+        /* stream state machine events */
+        bta_av_ssm_execute( bta_av_hndl_to_scb(p_msg->layer_specific),
+                                p_msg->event, (tBTA_AV_DATA *) p_msg);
+    }
+    return TRUE;
+}
+
+
+/*****************************************************************************
+**  Debug Functions
+*****************************************************************************/
+#if (defined(BTA_AV_DEBUG) && BTA_AV_DEBUG == TRUE)
+/*******************************************************************************
+**
+** Function         bta_av_st_code
+**
+** Description
+**
+** Returns          char *
+**
+*******************************************************************************/
+static char *bta_av_st_code(UINT8 state)
+{
+    switch(state)
+    {
+    case BTA_AV_INIT_ST: return "INIT";
+    case BTA_AV_OPEN_ST: return "OPEN";
+    default:             return "unknown";
+    }
+}
+/*******************************************************************************
+**
+** Function         bta_av_evt_code
+**
+** Description
+**
+** Returns          char *
+**
+*******************************************************************************/
+char *bta_av_evt_code(UINT16 evt_code)
+{
+    switch(evt_code)
+    {
+    case BTA_AV_API_DISABLE_EVT: return "API_DISABLE";
+    case BTA_AV_API_REMOTE_CMD_EVT: return "API_REMOTE_CMD";
+    case BTA_AV_API_VENDOR_CMD_EVT: return "API_VENDOR_CMD";
+    case BTA_AV_API_VENDOR_RSP_EVT: return "API_VENDOR_RSP";
+    case BTA_AV_API_META_RSP_EVT: return "API_META_RSP_EVT";
+    case BTA_AV_API_RC_CLOSE_EVT: return "API_RC_CLOSE";
+    case BTA_AV_AVRC_OPEN_EVT: return "AVRC_OPEN";
+    case BTA_AV_AVRC_MSG_EVT: return "AVRC_MSG";
+    case BTA_AV_AVRC_NONE_EVT: return "AVRC_NONE";
+
+    case BTA_AV_API_OPEN_EVT: return "API_OPEN";
+    case BTA_AV_API_CLOSE_EVT: return "API_CLOSE";
+    case BTA_AV_AP_START_EVT: return "AP_START";
+    case BTA_AV_AP_STOP_EVT: return "AP_STOP";
+    case BTA_AV_API_RECONFIG_EVT: return "API_RECONFIG";
+    case BTA_AV_API_PROTECT_REQ_EVT: return "API_PROTECT_REQ";
+    case BTA_AV_API_PROTECT_RSP_EVT: return "API_PROTECT_RSP";
+    case BTA_AV_API_RC_OPEN_EVT: return "API_RC_OPEN";
+    case BTA_AV_SRC_DATA_READY_EVT: return "SRC_DATA_READY";
+    case BTA_AV_CI_SETCONFIG_OK_EVT: return "CI_SETCONFIG_OK";
+    case BTA_AV_CI_SETCONFIG_FAIL_EVT: return "CI_SETCONFIG_FAIL";
+    case BTA_AV_SDP_DISC_OK_EVT: return "SDP_DISC_OK";
+    case BTA_AV_SDP_DISC_FAIL_EVT: return "SDP_DISC_FAIL";
+    case BTA_AV_STR_DISC_OK_EVT: return "STR_DISC_OK";
+    case BTA_AV_STR_DISC_FAIL_EVT: return "STR_DISC_FAIL";
+    case BTA_AV_STR_GETCAP_OK_EVT: return "STR_GETCAP_OK";
+    case BTA_AV_STR_GETCAP_FAIL_EVT: return "STR_GETCAP_FAIL";
+    case BTA_AV_STR_OPEN_OK_EVT: return "STR_OPEN_OK";
+    case BTA_AV_STR_OPEN_FAIL_EVT: return "STR_OPEN_FAIL";
+    case BTA_AV_STR_START_OK_EVT: return "STR_START_OK";
+    case BTA_AV_STR_START_FAIL_EVT: return "STR_START_FAIL";
+    case BTA_AV_STR_CLOSE_EVT: return "STR_CLOSE";
+    case BTA_AV_STR_CONFIG_IND_EVT: return "STR_CONFIG_IND";
+    case BTA_AV_STR_SECURITY_IND_EVT: return "STR_SECURITY_IND";
+    case BTA_AV_STR_SECURITY_CFM_EVT: return "STR_SECURITY_CFM";
+    case BTA_AV_STR_WRITE_CFM_EVT: return "STR_WRITE_CFM";
+    case BTA_AV_STR_SUSPEND_CFM_EVT: return "STR_SUSPEND_CFM";
+    case BTA_AV_STR_RECONFIG_CFM_EVT: return "STR_RECONFIG_CFM";
+    case BTA_AV_AVRC_TIMER_EVT: return "AVRC_TIMER";
+    case BTA_AV_AVDT_CONNECT_EVT: return "AVDT_CONNECT";
+    case BTA_AV_AVDT_DISCONNECT_EVT: return "AVDT_DISCONNECT";
+    case BTA_AV_ROLE_CHANGE_EVT: return "ROLE_CHANGE";
+    case BTA_AV_AVDT_DELAY_RPT_EVT: return "AVDT_DELAY_RPT";
+    case BTA_AV_ACP_CONNECT_EVT: return "ACP_CONNECT";
+
+    case BTA_AV_API_ENABLE_EVT: return "API_ENABLE";
+    case BTA_AV_API_REGISTER_EVT: return "API_REG";
+    case BTA_AV_API_DEREGISTER_EVT: return "API_DEREG";
+    case BTA_AV_API_DISCONNECT_EVT: return "API_DISCNT";
+    case BTA_AV_CI_SRC_DATA_READY_EVT: return "CI_DATA_READY";
+    case BTA_AV_SIG_CHG_EVT: return "SIG_CHG";
+    case BTA_AV_SIG_TIMER_EVT: return "SIG_TMR";
+    case BTA_AV_SDP_AVRC_DISC_EVT: return "SDP_AVRC_DISC";
+    case BTA_AV_AVRC_CLOSE_EVT: return "AVRC_CLOSE";
+    case BTA_AV_CONN_CHG_EVT: return "CONN_CHG";
+    case BTA_AV_DEREG_COMP_EVT: return "DEREG_COMP";
+#if (BTA_AV_SINK_INCLUDED == TRUE)
+    case BTA_AV_API_SINK_ENABLE_EVT: return "SINK_ENABLE";
+#endif
+#if (AVDT_REPORTING == TRUE)
+    case BTA_AV_AVDT_RPT_CONN_EVT: return "RPT_CONN";
+#endif
+    case BTA_AV_API_START_EVT: return "API_START";
+    case BTA_AV_API_STOP_EVT: return "API_STOP";
+    default:             return "unknown";
+    }
+}
+#endif
+
+#endif /* BTA_AV_INCLUDED */
diff --git a/components/bt/bluedroid/bta/av/bta_av_sbc.c b/components/bt/bluedroid/bta/av/bta_av_sbc.c
new file mode 100755 (executable)
index 0000000..4d539cf
--- /dev/null
@@ -0,0 +1,666 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2004-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This module contains utility functions for dealing with SBC data frames
+ *  and codec capabilities.
+ *
+ ******************************************************************************/
+
+#include "a2d_api.h"
+#include "a2d_sbc.h"
+#include "bta_av_sbc.h"
+#include "utl.h"
+#include "bt_utils.h"
+
+typedef int (tBTA_AV_SBC_ACT)(void *p_src, void *p_dst,
+                               UINT32 src_samples, UINT32 dst_samples,
+                               UINT32 *p_ret);
+
+typedef struct
+{
+    INT32               cur_pos;    /* current position */
+    UINT32              src_sps;    /* samples per second (source audio data) */
+    UINT32              dst_sps;    /* samples per second (converted audio data) */
+    tBTA_AV_SBC_ACT     *p_act;     /* the action function to do the conversion */
+    UINT16              bits;       /* number of bits per pcm sample */
+    UINT16              n_channels; /* number of channels (i.e. mono(1), stereo(2)...) */
+    INT16               worker1;
+    INT16               worker2;
+    UINT8               div;
+} tBTA_AV_SBC_UPS_CB;
+
+tBTA_AV_SBC_UPS_CB bta_av_sbc_ups_cb;
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_init_up_sample
+**
+** Description      initialize the up sample
+**
+**                  src_sps: samples per second (source audio data)
+**                  dst_sps: samples per second (converted audio data)
+**                  bits: number of bits per pcm sample
+**                  n_channels: number of channels (i.e. mono(1), stereo(2)...)
+**
+** Returns          none
+**
+*******************************************************************************/
+void bta_av_sbc_init_up_sample (UINT32 src_sps, UINT32 dst_sps, UINT16 bits, UINT16 n_channels)
+{
+    bta_av_sbc_ups_cb.cur_pos   = -1;
+    bta_av_sbc_ups_cb.src_sps   = src_sps;
+    bta_av_sbc_ups_cb.dst_sps   = dst_sps;
+    bta_av_sbc_ups_cb.bits      = bits;
+    bta_av_sbc_ups_cb.n_channels= n_channels;
+
+    if(n_channels == 1)
+    {
+        /* mono */
+        if(bits == 8)
+        {
+            bta_av_sbc_ups_cb.p_act = bta_av_sbc_up_sample_8m;
+            bta_av_sbc_ups_cb.div   = 1;
+        }
+        else
+        {
+            bta_av_sbc_ups_cb.p_act = bta_av_sbc_up_sample_16m;
+            bta_av_sbc_ups_cb.div   = 2;
+        }
+    }
+    else
+    {
+        /* stereo */
+        if(bits == 8)
+        {
+            bta_av_sbc_ups_cb.p_act = bta_av_sbc_up_sample_8s;
+            bta_av_sbc_ups_cb.div   = 2;
+        }
+        else
+        {
+            bta_av_sbc_ups_cb.p_act = bta_av_sbc_up_sample_16s;
+            bta_av_sbc_ups_cb.div   = 4;
+        }
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_up_sample
+**
+** Description      Given the source (p_src) audio data and
+**                  source speed (src_sps, samples per second),
+**                  This function converts it to audio data in the desired format
+**
+**                  p_src: the data buffer that holds the source audio data
+**                  p_dst: the data buffer to hold the converted audio data
+**                  src_samples: The number of source samples (number of bytes)
+**                  dst_samples: The size of p_dst (number of bytes)
+**
+** Note:            An AE reported an issue with this function.
+**                  When called with bta_av_sbc_up_sample(src, uint8_array_dst..)
+**                  the byte before uint8_array_dst may get overwritten.
+**                  Using uint16_array_dst avoids the problem.
+**                  This issue is related to endian-ness and is hard to resolve
+**                  in a generic manner.
+** **************** Please use uint16 array as dst.
+**
+** Returns          The number of bytes used in p_dst
+**                  The number of bytes used in p_src (in *p_ret)
+**
+*******************************************************************************/
+int bta_av_sbc_up_sample (void *p_src, void *p_dst,
+                         UINT32 src_samples, UINT32 dst_samples,
+                         UINT32 *p_ret)
+{
+    UINT32 src;
+    UINT32 dst;
+
+    if(bta_av_sbc_ups_cb.p_act)
+    {
+        src = src_samples/bta_av_sbc_ups_cb.div;
+        dst = dst_samples/bta_av_sbc_ups_cb.div;
+        return (*bta_av_sbc_ups_cb.p_act)(p_src, p_dst, src, dst, p_ret);
+    }
+    else
+    {
+        *p_ret = 0;
+        return 0;
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_up_sample_16s (16bits-stereo)
+**
+** Description      Given the source (p_src) audio data and
+**                  source speed (src_sps, samples per second),
+**                  This function converts it to audio data in the desired format
+**
+**                  p_src: the data buffer that holds the source audio data
+**                  p_dst: the data buffer to hold the converted audio data
+**                  src_samples: The number of source samples (in uint of 4 bytes)
+**                  dst_samples: The size of p_dst (in uint of 4 bytes)
+**
+** Returns          The number of bytes used in p_dst
+**                  The number of bytes used in p_src (in *p_ret)
+**
+*******************************************************************************/
+int bta_av_sbc_up_sample_16s (void *p_src, void *p_dst,
+                         UINT32 src_samples, UINT32 dst_samples,
+                         UINT32 *p_ret)
+{
+    INT16   *p_src_tmp = (INT16 *)p_src;
+    INT16   *p_dst_tmp = (INT16 *)p_dst;
+    INT16   *p_worker1 = &bta_av_sbc_ups_cb.worker1;
+    INT16   *p_worker2 = &bta_av_sbc_ups_cb.worker2;
+    UINT32  src_sps = bta_av_sbc_ups_cb.src_sps;
+    UINT32  dst_sps = bta_av_sbc_ups_cb.dst_sps;
+
+    while (bta_av_sbc_ups_cb.cur_pos > 0 && dst_samples)
+    {
+        *p_dst_tmp++    = *p_worker1;
+        *p_dst_tmp++    = *p_worker2;
+
+        bta_av_sbc_ups_cb.cur_pos -= src_sps;
+        dst_samples--;
+    }
+
+    bta_av_sbc_ups_cb.cur_pos = dst_sps;
+
+    while (src_samples-- && dst_samples)
+    {
+        *p_worker1 = *p_src_tmp++;
+        *p_worker2 = *p_src_tmp++;
+
+        do
+        {
+            *p_dst_tmp++    = *p_worker1;
+            *p_dst_tmp++    = *p_worker2;
+
+            bta_av_sbc_ups_cb.cur_pos -= src_sps;
+            dst_samples--;
+        } while (bta_av_sbc_ups_cb.cur_pos > 0 && dst_samples);
+
+        bta_av_sbc_ups_cb.cur_pos += dst_sps;
+    }
+
+    if (bta_av_sbc_ups_cb.cur_pos == (INT32)dst_sps)
+        bta_av_sbc_ups_cb.cur_pos = 0;
+
+    *p_ret = ((char *)p_src_tmp - (char *)p_src);
+    return ((char *)p_dst_tmp - (char *)p_dst);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_up_sample_16m (16bits-mono)
+**
+** Description      Given the source (p_src) audio data and
+**                  source speed (src_sps, samples per second),
+**                  This function converts it to audio data in the desired format
+**
+**                  p_src: the data buffer that holds the source audio data
+**                  p_dst: the data buffer to hold the converted audio data
+**                  src_samples: The number of source samples (in uint of 2 bytes)
+**                  dst_samples: The size of p_dst (in uint of 2 bytes)
+**
+** Returns          The number of bytes used in p_dst
+**                  The number of bytes used in p_src (in *p_ret)
+**
+*******************************************************************************/
+int bta_av_sbc_up_sample_16m (void *p_src, void *p_dst,
+                              UINT32 src_samples, UINT32 dst_samples,
+                              UINT32 *p_ret)
+{
+    INT16   *p_src_tmp = (INT16 *)p_src;
+    INT16   *p_dst_tmp = (INT16 *)p_dst;
+    INT16   *p_worker = &bta_av_sbc_ups_cb.worker1;
+    UINT32  src_sps = bta_av_sbc_ups_cb.src_sps;
+    UINT32  dst_sps = bta_av_sbc_ups_cb.dst_sps;
+
+    while (bta_av_sbc_ups_cb.cur_pos > 0 && dst_samples)
+    {
+        *p_dst_tmp++ = *p_worker;
+        *p_dst_tmp++ = *p_worker;
+
+        bta_av_sbc_ups_cb.cur_pos -= src_sps;
+        dst_samples--;
+        dst_samples--;
+    }
+
+
+    bta_av_sbc_ups_cb.cur_pos = dst_sps;
+
+    while (src_samples-- && dst_samples)
+    {
+        *p_worker = *p_src_tmp++;
+
+        do
+        {
+            *p_dst_tmp++ = *p_worker;
+            *p_dst_tmp++ = *p_worker;
+
+            bta_av_sbc_ups_cb.cur_pos -= src_sps;
+            dst_samples--;
+            dst_samples--;
+
+        } while (bta_av_sbc_ups_cb.cur_pos > 0 && dst_samples);
+
+        bta_av_sbc_ups_cb.cur_pos += dst_sps;
+    }
+
+    if (bta_av_sbc_ups_cb.cur_pos == (INT32)dst_sps)
+        bta_av_sbc_ups_cb.cur_pos = 0;
+
+    *p_ret = ((char *)p_src_tmp - (char *)p_src);
+    return ((char *)p_dst_tmp - (char *)p_dst);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_up_sample_8s (8bits-stereo)
+**
+** Description      Given the source (p_src) audio data and
+**                  source speed (src_sps, samples per second),
+**                  This function converts it to audio data in the desired format
+**
+**                  p_src: the data buffer that holds the source audio data
+**                  p_dst: the data buffer to hold the converted audio data
+**                  src_samples: The number of source samples (in uint of 2 bytes)
+**                  dst_samples: The size of p_dst (in uint of 2 bytes)
+**
+** Returns          The number of bytes used in p_dst
+**                  The number of bytes used in p_src (in *p_ret)
+**
+*******************************************************************************/
+int bta_av_sbc_up_sample_8s (void *p_src, void *p_dst,
+                             UINT32 src_samples, UINT32 dst_samples,
+                             UINT32 *p_ret)
+{
+    UINT8   *p_src_tmp = (UINT8 *)p_src;
+    INT16   *p_dst_tmp = (INT16 *)p_dst;
+    INT16   *p_worker1 = &bta_av_sbc_ups_cb.worker1;
+    INT16   *p_worker2 = &bta_av_sbc_ups_cb.worker2;
+    UINT32  src_sps = bta_av_sbc_ups_cb.src_sps;
+    UINT32  dst_sps = bta_av_sbc_ups_cb.dst_sps;
+
+    while (bta_av_sbc_ups_cb.cur_pos > 0 && dst_samples)
+    {
+        *p_dst_tmp++    = *p_worker1;
+        *p_dst_tmp++    = *p_worker2;
+
+        bta_av_sbc_ups_cb.cur_pos -= src_sps;
+        dst_samples--;
+        dst_samples--;
+    }
+
+    bta_av_sbc_ups_cb.cur_pos = dst_sps;
+
+    while (src_samples -- && dst_samples)
+    {
+        *p_worker1 = *(UINT8 *)p_src_tmp++;
+        *p_worker1 -= 0x80;
+        *p_worker1 <<= 8;
+        *p_worker2 = *(UINT8 *)p_src_tmp++;
+        *p_worker2 -= 0x80;
+        *p_worker2 <<= 8;
+
+        do
+        {
+            *p_dst_tmp++    = *p_worker1;
+            *p_dst_tmp++    = *p_worker2;
+
+            bta_av_sbc_ups_cb.cur_pos -= src_sps;
+            dst_samples--;
+            dst_samples--;
+        } while (bta_av_sbc_ups_cb.cur_pos > 0 && dst_samples);
+
+        bta_av_sbc_ups_cb.cur_pos += dst_sps;
+    }
+
+    if (bta_av_sbc_ups_cb.cur_pos == (INT32)dst_sps)
+        bta_av_sbc_ups_cb.cur_pos = 0;
+
+    *p_ret = ((char *)p_src_tmp - (char *)p_src);
+    return ((char *)p_dst_tmp - (char *)p_dst);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_up_sample_8m (8bits-mono)
+**
+** Description      Given the source (p_src) audio data and
+**                  source speed (src_sps, samples per second),
+**                  This function converts it to audio data in the desired format
+**
+**                  p_src: the data buffer that holds the source audio data
+**                  p_dst: the data buffer to hold the converted audio data
+**                  src_samples: The number of source samples (number of bytes)
+**                  dst_samples: The size of p_dst (number of bytes)
+**
+** Returns          The number of bytes used in p_dst
+**                  The number of bytes used in p_src (in *p_ret)
+**
+*******************************************************************************/
+int bta_av_sbc_up_sample_8m (void *p_src, void *p_dst,
+                             UINT32 src_samples, UINT32 dst_samples,
+                             UINT32 *p_ret)
+{
+    UINT8   *p_src_tmp = (UINT8 *)p_src;
+    INT16   *p_dst_tmp = (INT16 *)p_dst;
+    INT16   *p_worker = &bta_av_sbc_ups_cb.worker1;
+    UINT32  src_sps = bta_av_sbc_ups_cb.src_sps;
+    UINT32  dst_sps = bta_av_sbc_ups_cb.dst_sps;
+
+    while (bta_av_sbc_ups_cb.cur_pos > 0 && dst_samples)
+    {
+        *p_dst_tmp++ = *p_worker;
+        *p_dst_tmp++ = *p_worker;
+
+        bta_av_sbc_ups_cb.cur_pos -= src_sps;
+        dst_samples -= 4;
+    }
+
+
+    bta_av_sbc_ups_cb.cur_pos = dst_sps;
+
+    while (src_samples-- && dst_samples)
+    {
+        *p_worker = *(UINT8 *)p_src_tmp++;
+        *p_worker -= 0x80;
+        *p_worker <<= 8;
+
+        do
+        {
+            *p_dst_tmp++ = *p_worker;
+            *p_dst_tmp++ = *p_worker;
+
+            bta_av_sbc_ups_cb.cur_pos -= src_sps;
+            dst_samples -= 4;
+
+        } while (bta_av_sbc_ups_cb.cur_pos > 0 && dst_samples);
+
+        bta_av_sbc_ups_cb.cur_pos += dst_sps;
+    }
+
+    if (bta_av_sbc_ups_cb.cur_pos == (INT32)dst_sps)
+        bta_av_sbc_ups_cb.cur_pos = 0;
+
+    *p_ret = ((char *)p_src_tmp - (char *)p_src);
+    return ((char *)p_dst_tmp - (char *)p_dst);
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_cfg_for_cap
+**
+** Description      Determine the preferred SBC codec configuration for the
+**                  given codec capabilities.  The function is passed the
+**                  preferred codec configuration and the peer codec
+**                  capabilities for the stream.  The function attempts to
+**                  match the preferred capabilities with the configuration
+**                  as best it can.  The resulting codec configuration is
+**                  returned in the same memory used for the capabilities.
+**
+** Returns          0 if ok, nonzero if error.
+**                  Codec configuration in p_cap.
+**
+*******************************************************************************/
+UINT8 bta_av_sbc_cfg_for_cap(UINT8 *p_peer, tA2D_SBC_CIE *p_cap, tA2D_SBC_CIE *p_pref)
+{
+    UINT8           status = A2D_SUCCESS;
+    tA2D_SBC_CIE    peer_cie;
+    UNUSED(p_cap);
+
+    /* parse peer capabilities */
+    if ((status = A2D_ParsSbcInfo(&peer_cie, p_peer, TRUE)) != 0)
+    {
+        return status;
+    }
+
+    /* Check if the peer supports our channel mode */
+    if (peer_cie.ch_mode & p_pref->ch_mode)
+    {
+        peer_cie.ch_mode = p_pref->ch_mode;
+    }
+    else
+    {
+        APPL_TRACE_ERROR("bta_av_sbc_cfg_for_cap: ch_mode(0x%02X) not supported", p_pref->ch_mode);
+        return A2D_FAIL;
+    }
+
+    /* Check if the peer supports our sampling freq */
+    if (peer_cie.samp_freq & p_pref->samp_freq)
+    {
+        peer_cie.samp_freq = p_pref->samp_freq;
+    }
+    else
+    {
+        APPL_TRACE_ERROR("bta_av_sbc_cfg_for_cap: samp_freq(0x%02X) not supported", p_pref->samp_freq);
+        return A2D_FAIL;
+    }
+
+    /* Check if the peer supports our block len */
+    if (peer_cie.block_len & p_pref->block_len)
+    {
+        peer_cie.block_len = p_pref->block_len;
+    }
+    else
+    {
+        APPL_TRACE_ERROR("bta_av_sbc_cfg_for_cap: block_len(0x%02X) not supported", p_pref->block_len);
+        return A2D_FAIL;
+    }
+
+    /* Check if the peer supports our num subbands */
+    if (peer_cie.num_subbands & p_pref->num_subbands)
+    {
+        peer_cie.num_subbands = p_pref->num_subbands;
+    }
+    else
+    {
+        APPL_TRACE_ERROR("bta_av_sbc_cfg_for_cap: num_subbands(0x%02X) not supported", p_pref->num_subbands);
+        return A2D_FAIL;
+    }
+
+    /* Check if the peer supports our alloc method */
+    if (peer_cie.alloc_mthd & p_pref->alloc_mthd)
+    {
+        peer_cie.alloc_mthd = p_pref->alloc_mthd;
+    }
+    else
+    {
+        APPL_TRACE_ERROR("bta_av_sbc_cfg_for_cap: alloc_mthd(0x%02X) not supported", p_pref->alloc_mthd);
+        return A2D_FAIL;
+    }
+
+    /* max bitpool */
+    if (p_pref->max_bitpool != 0 && p_pref->max_bitpool < peer_cie.max_bitpool)
+    {
+        peer_cie.max_bitpool = p_pref->max_bitpool;
+    }
+
+    /* min bitpool */
+    if (p_pref->min_bitpool != 0 && p_pref->min_bitpool > peer_cie.min_bitpool)
+    {
+        peer_cie.min_bitpool = p_pref->min_bitpool;
+    }
+
+    if (status == A2D_SUCCESS)
+    {
+        /* build configuration */
+        A2D_BldSbcInfo(A2D_MEDIA_TYPE_AUDIO, &peer_cie, p_peer);
+    }
+    return status;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_cfg_matches_cap
+**
+** Description      This function checks whether an SBC codec configuration
+**                  matched with capabilities. Here we check subset.
+**
+** Returns          0 if ok, nonzero if error.
+**
+*******************************************************************************/
+UINT8 bta_av_sbc_cfg_matches_cap(UINT8 *p_cfg, tA2D_SBC_CIE *p_cap)
+{
+    UINT8           status = 0;
+    tA2D_SBC_CIE    cfg_cie;
+
+    /* parse configuration */
+    if ((status = A2D_ParsSbcInfo(&cfg_cie, p_cfg, TRUE)) != 0)
+    {
+        APPL_TRACE_ERROR(" bta_av_sbc_cfg_matches_cap Parsing Failed %d", status);
+        return status;
+    }
+
+    /* verify that each parameter is in range */
+
+    APPL_TRACE_DEBUG(" FREQ peer: 0%x, capability  0%x", cfg_cie.samp_freq, p_cap->samp_freq);
+    APPL_TRACE_DEBUG(" CH_MODE peer: 0%x, capability  0%x", cfg_cie.ch_mode, p_cap->ch_mode);
+    APPL_TRACE_DEBUG(" BLOCK_LEN peer: 0%x, capability  0%x", cfg_cie.block_len, p_cap->block_len);
+    APPL_TRACE_DEBUG(" SUB_BAND peer: 0%x, capability  0%x", cfg_cie.num_subbands, p_cap->num_subbands);
+    APPL_TRACE_DEBUG(" ALLOC_MTHD peer: 0%x, capability  0%x", cfg_cie.alloc_mthd, p_cap->alloc_mthd);
+    APPL_TRACE_DEBUG(" MAX_BitPool peer: 0%x, capability  0%x", cfg_cie.max_bitpool, p_cap->max_bitpool);
+    APPL_TRACE_DEBUG(" Min_bitpool peer: 0%x, capability  0%x", cfg_cie.min_bitpool, p_cap->min_bitpool);
+
+    /* sampling frequency */
+    if ((cfg_cie.samp_freq & p_cap->samp_freq) == 0)
+    {
+        status = A2D_NS_SAMP_FREQ;
+    }
+    /* channel mode */
+    else if ((cfg_cie.ch_mode & p_cap->ch_mode) == 0)
+    {
+        status = A2D_NS_CH_MODE;
+    }
+    /* block length */
+    else if ((cfg_cie.block_len & p_cap->block_len) == 0)
+    {
+        status = A2D_BAD_BLOCK_LEN;
+    }
+    /* subbands */
+    else if ((cfg_cie.num_subbands & p_cap->num_subbands) == 0)
+    {
+        status = A2D_NS_SUBBANDS;
+    }
+    /* allocation method */
+    else if ((cfg_cie.alloc_mthd & p_cap->alloc_mthd) == 0)
+    {
+        status = A2D_NS_ALLOC_MTHD;
+    }
+    /* max bitpool */
+    else if (cfg_cie.max_bitpool > p_cap->max_bitpool)
+    {
+        status = A2D_NS_MAX_BITPOOL;
+    }
+    /* min bitpool */
+    else if (cfg_cie.min_bitpool < p_cap->min_bitpool)
+    {
+        status = A2D_NS_MIN_BITPOOL;
+    }
+
+    return status;
+}
+
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_cfg_in_cap
+**
+** Description      This function checks whether an SBC codec configuration
+**                  is allowable for the given codec capabilities.
+**
+** Returns          0 if ok, nonzero if error.
+**
+*******************************************************************************/
+UINT8 bta_av_sbc_cfg_in_cap(UINT8 *p_cfg, tA2D_SBC_CIE *p_cap)
+{
+    UINT8           status = 0;
+    tA2D_SBC_CIE    cfg_cie;
+
+    /* parse configuration */
+    if ((status = A2D_ParsSbcInfo(&cfg_cie, p_cfg, FALSE)) != 0)
+    {
+        return status;
+    }
+
+    /* verify that each parameter is in range */
+
+
+    /* sampling frequency */
+    if ((cfg_cie.samp_freq & p_cap->samp_freq) == 0)
+    {
+        status = A2D_NS_SAMP_FREQ;
+    }
+    /* channel mode */
+    else if ((cfg_cie.ch_mode & p_cap->ch_mode) == 0)
+    {
+        status = A2D_NS_CH_MODE;
+    }
+    /* block length */
+    else if ((cfg_cie.block_len & p_cap->block_len) == 0)
+    {
+        status = A2D_BAD_BLOCK_LEN;
+    }
+    /* subbands */
+    else if ((cfg_cie.num_subbands & p_cap->num_subbands) == 0)
+    {
+        status = A2D_NS_SUBBANDS;
+    }
+    /* allocation method */
+    else if ((cfg_cie.alloc_mthd & p_cap->alloc_mthd) == 0)
+    {
+        status = A2D_NS_ALLOC_MTHD;
+    }
+    /* max bitpool */
+    else if (cfg_cie.max_bitpool > p_cap->max_bitpool)
+    {
+        status = A2D_NS_MAX_BITPOOL;
+    }
+    /* min bitpool */
+    else if (cfg_cie.min_bitpool < p_cap->min_bitpool)
+    {
+        status = A2D_NS_MIN_BITPOOL;
+    }
+
+    return status;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_bld_hdr
+**
+** Description      This function builds the packet header for MPF1.
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_sbc_bld_hdr(BT_HDR *p_buf, UINT16 fr_per_pkt)
+{
+    UINT8   *p;
+
+    p_buf->offset -= BTA_AV_SBC_HDR_SIZE;
+    p = (UINT8 *) (p_buf + 1) + p_buf->offset;
+    p_buf->len += BTA_AV_SBC_HDR_SIZE;
+    A2D_BldSbcMplHdr(p, FALSE, FALSE, FALSE, (UINT8) fr_per_pkt);
+}
+
diff --git a/components/bt/bluedroid/bta/av/bta_av_ssm.c b/components/bt/bluedroid/bta/av/bta_av_ssm.c
new file mode 100755 (executable)
index 0000000..64ee42a
--- /dev/null
@@ -0,0 +1,599 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2004-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This is the stream state machine for the BTA advanced audio/video.
+ *
+ ******************************************************************************/
+
+#include "bt_target.h"
+#if defined(BTA_AV_INCLUDED) && (BTA_AV_INCLUDED == TRUE)
+
+#include <string.h>
+#include "bta_av_co.h"
+#include "bta_av_int.h"
+
+/*****************************************************************************
+** Constants and types
+*****************************************************************************/
+
+/* state machine states */
+enum
+{
+    BTA_AV_INIT_SST,
+    BTA_AV_INCOMING_SST,
+    BTA_AV_OPENING_SST,
+    BTA_AV_OPEN_SST,
+    BTA_AV_RCFG_SST,
+    BTA_AV_CLOSING_SST
+};
+
+
+/* state machine action enumeration list */
+enum
+{
+    BTA_AV_DO_DISC,
+    BTA_AV_CLEANUP,
+    BTA_AV_FREE_SDB,
+    BTA_AV_CONFIG_IND,
+    BTA_AV_DISCONNECT_REQ,
+    BTA_AV_SECURITY_REQ,
+    BTA_AV_SECURITY_RSP,
+    BTA_AV_SETCONFIG_RSP,
+    BTA_AV_ST_RC_TIMER,
+    BTA_AV_STR_OPENED,
+    BTA_AV_SECURITY_IND,
+    BTA_AV_SECURITY_CFM,
+    BTA_AV_DO_CLOSE,
+    BTA_AV_CONNECT_REQ,
+    BTA_AV_SDP_FAILED,
+    BTA_AV_DISC_RESULTS,
+    BTA_AV_DISC_RES_AS_ACP,
+    BTA_AV_OPEN_FAILED,
+    BTA_AV_GETCAP_RESULTS,
+    BTA_AV_SETCONFIG_REJ,
+    BTA_AV_DISCOVER_REQ,
+    BTA_AV_CONN_FAILED,
+    BTA_AV_DO_START,
+    BTA_AV_STR_STOPPED,
+    BTA_AV_RECONFIG,
+    BTA_AV_DATA_PATH,
+    BTA_AV_START_OK,
+    BTA_AV_START_FAILED,
+    BTA_AV_STR_CLOSED,
+    BTA_AV_CLR_CONG,
+    BTA_AV_SUSPEND_CFM,
+    BTA_AV_RCFG_STR_OK,
+    BTA_AV_RCFG_FAILED,
+    BTA_AV_RCFG_CONNECT,
+    BTA_AV_RCFG_DISCNTD,
+    BTA_AV_SUSPEND_CONT,
+    BTA_AV_RCFG_CFM,
+    BTA_AV_RCFG_OPEN,
+    BTA_AV_SECURITY_REJ,
+    BTA_AV_OPEN_RC,
+    BTA_AV_CHK_2ND_START,
+    BTA_AV_SAVE_CAPS,
+    BTA_AV_SET_USE_RC,
+    BTA_AV_CCO_CLOSE,
+    BTA_AV_SWITCH_ROLE,
+    BTA_AV_ROLE_RES,
+    BTA_AV_DELAY_CO,
+    BTA_AV_OPEN_AT_INC,
+    BTA_AV_NUM_SACTIONS
+};
+
+#define BTA_AV_SIGNORE       BTA_AV_NUM_SACTIONS
+
+
+/* state table information */
+/* #define BTA_AV_SACTION_COL           0       position of actions */
+#define BTA_AV_SACTIONS              2       /* number of actions */
+#define BTA_AV_SNEXT_STATE           2       /* position of next state */
+#define BTA_AV_NUM_COLS              3       /* number of columns in state tables */
+
+/* state table for init state */
+static const UINT8 bta_av_sst_init[][BTA_AV_NUM_COLS] =
+{
+/* Event                     Action 1               Action 2               Next state */
+/* AP_OPEN_EVT */           {BTA_AV_DO_DISC,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* AP_CLOSE_EVT */          {BTA_AV_CLEANUP,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* AP_START_EVT */          {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* AP_STOP_EVT */           {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* API_RECONFIG_EVT */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* API_PROTECT_REQ_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* API_PROTECT_RSP_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* API_RC_OPEN_EVT  */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* SRC_DATA_READY_EVT */    {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* CI_SETCONFIG_OK_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* CI_SETCONFIG_FAIL_EVT */ {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* SDP_DISC_OK_EVT */       {BTA_AV_FREE_SDB,       BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* SDP_DISC_FAIL_EVT */     {BTA_AV_FREE_SDB,       BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_DISC_OK_EVT */       {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_DISC_FAIL_EVT */     {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_GETCAP_OK_EVT */     {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_GETCAP_FAIL_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_OPEN_OK_EVT */       {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_OPEN_FAIL_EVT */     {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_START_OK_EVT */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_START_FAIL_EVT */    {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_CLOSE_EVT */         {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_CONFIG_IND_EVT */    {BTA_AV_SETCONFIG_REJ,  BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_SECURITY_IND_EVT */  {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_SECURITY_CFM_EVT */  {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_WRITE_CFM_EVT */     {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_SUSPEND_CFM_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_RECONFIG_CFM_EVT */  {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* AVRC_TIMER_EVT */        {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* AVDT_CONNECT_EVT */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* AVDT_DISCONNECT_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* ROLE_CHANGE_EVT*/        {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* AVDT_DELAY_RPT_EVT */    {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* ACP_CONNECT_EVT */       {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST }
+};
+
+/* state table for incoming state */
+static const UINT8 bta_av_sst_incoming[][BTA_AV_NUM_COLS] =
+{
+/* Event                     Action 1               Action 2               Next state */
+/* AP_OPEN_EVT */           {BTA_AV_OPEN_AT_INC,    BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* AP_CLOSE_EVT */          {BTA_AV_CCO_CLOSE,      BTA_AV_DISCONNECT_REQ, BTA_AV_CLOSING_SST },
+/* AP_START_EVT */          {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* AP_STOP_EVT */           {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* API_RECONFIG_EVT */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* API_PROTECT_REQ_EVT */   {BTA_AV_SECURITY_REQ,   BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* API_PROTECT_RSP_EVT */   {BTA_AV_SECURITY_RSP,   BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* API_RC_OPEN_EVT  */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* SRC_DATA_READY_EVT */    {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* CI_SETCONFIG_OK_EVT */   {BTA_AV_SETCONFIG_RSP,  BTA_AV_ST_RC_TIMER,    BTA_AV_INCOMING_SST },
+/* CI_SETCONFIG_FAIL_EVT */ {BTA_AV_SETCONFIG_REJ,  BTA_AV_CLEANUP,        BTA_AV_INIT_SST },
+/* SDP_DISC_OK_EVT */       {BTA_AV_FREE_SDB,       BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* SDP_DISC_FAIL_EVT */     {BTA_AV_FREE_SDB,       BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* STR_DISC_OK_EVT */       {BTA_AV_DISC_RES_AS_ACP,BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* STR_DISC_FAIL_EVT */     {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* STR_GETCAP_OK_EVT */     {BTA_AV_SAVE_CAPS,      BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* STR_GETCAP_FAIL_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* STR_OPEN_OK_EVT */       {BTA_AV_STR_OPENED,     BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_OPEN_FAIL_EVT */     {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* STR_START_OK_EVT */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* STR_START_FAIL_EVT */    {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* STR_CLOSE_EVT */         {BTA_AV_CCO_CLOSE,      BTA_AV_CLEANUP,        BTA_AV_INIT_SST },
+/* STR_CONFIG_IND_EVT */    {BTA_AV_CONFIG_IND,     BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* STR_SECURITY_IND_EVT */  {BTA_AV_SECURITY_IND,   BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* STR_SECURITY_CFM_EVT */  {BTA_AV_SECURITY_CFM,   BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* STR_WRITE_CFM_EVT */     {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* STR_SUSPEND_CFM_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* STR_RECONFIG_CFM_EVT */  {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* AVRC_TIMER_EVT */        {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* AVDT_CONNECT_EVT */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* AVDT_DISCONNECT_EVT */   {BTA_AV_CCO_CLOSE,      BTA_AV_CLEANUP,        BTA_AV_INIT_SST },
+/* ROLE_CHANGE_EVT*/        {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* AVDT_DELAY_RPT_EVT */    {BTA_AV_DELAY_CO,       BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* ACP_CONNECT_EVT */       {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST }
+};
+
+/* state table for opening state */
+static const UINT8 bta_av_sst_opening[][BTA_AV_NUM_COLS] =
+{
+/* Event                     Action 1               Action 2               Next state */
+/* AP_OPEN_EVT */           {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* AP_CLOSE_EVT */          {BTA_AV_DO_CLOSE,       BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* AP_START_EVT */          {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* AP_STOP_EVT */           {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* API_RECONFIG_EVT */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* API_PROTECT_REQ_EVT */   {BTA_AV_SECURITY_REQ,   BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* API_PROTECT_RSP_EVT */   {BTA_AV_SECURITY_RSP,   BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* API_RC_OPEN_EVT  */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* SRC_DATA_READY_EVT */    {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* CI_SETCONFIG_OK_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* CI_SETCONFIG_FAIL_EVT */ {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* SDP_DISC_OK_EVT */       {BTA_AV_CONNECT_REQ,    BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* SDP_DISC_FAIL_EVT */     {BTA_AV_CONNECT_REQ,    BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* STR_DISC_OK_EVT */       {BTA_AV_DISC_RESULTS,   BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* STR_DISC_FAIL_EVT */     {BTA_AV_OPEN_FAILED,    BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_GETCAP_OK_EVT */     {BTA_AV_GETCAP_RESULTS, BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* STR_GETCAP_FAIL_EVT */   {BTA_AV_OPEN_FAILED,    BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_OPEN_OK_EVT */       {BTA_AV_ST_RC_TIMER,    BTA_AV_STR_OPENED,     BTA_AV_OPEN_SST },
+/* STR_OPEN_FAIL_EVT */     {BTA_AV_OPEN_FAILED,    BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_START_OK_EVT */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* STR_START_FAIL_EVT */    {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* STR_CLOSE_EVT */         {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* STR_CONFIG_IND_EVT */    {BTA_AV_CONFIG_IND,     BTA_AV_SIGNORE,        BTA_AV_INCOMING_SST },
+/* STR_SECURITY_IND_EVT */  {BTA_AV_SECURITY_IND,   BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* STR_SECURITY_CFM_EVT */  {BTA_AV_SECURITY_CFM,   BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* STR_WRITE_CFM_EVT */     {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* STR_SUSPEND_CFM_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* STR_RECONFIG_CFM_EVT */  {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* AVRC_TIMER_EVT */        {BTA_AV_SWITCH_ROLE,    BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* AVDT_CONNECT_EVT */      {BTA_AV_DISCOVER_REQ,   BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* AVDT_DISCONNECT_EVT */   {BTA_AV_CONN_FAILED,    BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* ROLE_CHANGE_EVT*/        {BTA_AV_ROLE_RES,       BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* AVDT_DELAY_RPT_EVT */    {BTA_AV_DELAY_CO,       BTA_AV_SIGNORE,        BTA_AV_OPENING_SST },
+/* ACP_CONNECT_EVT */       {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPENING_SST }
+};
+
+/* state table for open state */
+static const UINT8 bta_av_sst_open[][BTA_AV_NUM_COLS] =
+{
+/* Event                     Action 1               Action 2               Next state */
+/* AP_OPEN_EVT */           {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* AP_CLOSE_EVT */          {BTA_AV_DO_CLOSE,       BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* AP_START_EVT */          {BTA_AV_DO_START,       BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* AP_STOP_EVT */           {BTA_AV_STR_STOPPED,    BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* API_RECONFIG_EVT */      {BTA_AV_RECONFIG,       BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* API_PROTECT_REQ_EVT */   {BTA_AV_SECURITY_REQ,   BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* API_PROTECT_RSP_EVT */   {BTA_AV_SECURITY_RSP,   BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* API_RC_OPEN_EVT  */      {BTA_AV_SET_USE_RC,     BTA_AV_OPEN_RC,        BTA_AV_OPEN_SST },
+/* SRC_DATA_READY_EVT */    {BTA_AV_DATA_PATH,      BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* CI_SETCONFIG_OK_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* CI_SETCONFIG_FAIL_EVT */ {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* SDP_DISC_OK_EVT */       {BTA_AV_FREE_SDB,       BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* SDP_DISC_FAIL_EVT */     {BTA_AV_FREE_SDB,       BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_DISC_OK_EVT */       {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_DISC_FAIL_EVT */     {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_GETCAP_OK_EVT */     {BTA_AV_SAVE_CAPS,      BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_GETCAP_FAIL_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_OPEN_OK_EVT */       {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_OPEN_FAIL_EVT */     {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_START_OK_EVT */      {BTA_AV_START_OK,       BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_START_FAIL_EVT */    {BTA_AV_START_FAILED,   BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_CLOSE_EVT */         {BTA_AV_STR_CLOSED,     BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_CONFIG_IND_EVT */    {BTA_AV_SETCONFIG_REJ,  BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_SECURITY_IND_EVT */  {BTA_AV_SECURITY_IND,   BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_SECURITY_CFM_EVT */  {BTA_AV_SECURITY_CFM,   BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_WRITE_CFM_EVT */     {BTA_AV_CLR_CONG,       BTA_AV_DATA_PATH,      BTA_AV_OPEN_SST },
+/* STR_SUSPEND_CFM_EVT */   {BTA_AV_SUSPEND_CFM,    BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_RECONFIG_CFM_EVT */  {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* AVRC_TIMER_EVT */        {BTA_AV_OPEN_RC,        BTA_AV_CHK_2ND_START,  BTA_AV_OPEN_SST },
+/* AVDT_CONNECT_EVT */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* AVDT_DISCONNECT_EVT */   {BTA_AV_STR_CLOSED,     BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* ROLE_CHANGE_EVT*/        {BTA_AV_ROLE_RES,       BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* AVDT_DELAY_RPT_EVT */    {BTA_AV_DELAY_CO,       BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* ACP_CONNECT_EVT */       {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_OPEN_SST }
+};
+
+/* state table for reconfig state */
+static const UINT8 bta_av_sst_rcfg[][BTA_AV_NUM_COLS] =
+{
+/* Event                     Action 1               Action 2               Next state */
+/* AP_OPEN_EVT */           {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* AP_CLOSE_EVT */          {BTA_AV_DISCONNECT_REQ, BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* AP_START_EVT */          {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* AP_STOP_EVT */           {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* API_RECONFIG_EVT */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* API_PROTECT_REQ_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* API_PROTECT_RSP_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* API_RC_OPEN_EVT  */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* SRC_DATA_READY_EVT */    {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* CI_SETCONFIG_OK_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* CI_SETCONFIG_FAIL_EVT */ {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* SDP_DISC_OK_EVT */       {BTA_AV_FREE_SDB,       BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* SDP_DISC_FAIL_EVT */     {BTA_AV_FREE_SDB,       BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* STR_DISC_OK_EVT */       {BTA_AV_DISC_RESULTS,   BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* STR_DISC_FAIL_EVT */     {BTA_AV_STR_CLOSED,     BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_GETCAP_OK_EVT */     {BTA_AV_GETCAP_RESULTS, BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* STR_GETCAP_FAIL_EVT */   {BTA_AV_STR_CLOSED,     BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_OPEN_OK_EVT */       {BTA_AV_RCFG_STR_OK,    BTA_AV_SIGNORE,        BTA_AV_OPEN_SST },
+/* STR_OPEN_FAIL_EVT */     {BTA_AV_RCFG_FAILED,    BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* STR_START_OK_EVT */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* STR_START_FAIL_EVT */    {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* STR_CLOSE_EVT */         {BTA_AV_RCFG_CONNECT,   BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* STR_CONFIG_IND_EVT */    {BTA_AV_SETCONFIG_REJ,  BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* STR_SECURITY_IND_EVT */  {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* STR_SECURITY_CFM_EVT */  {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* STR_WRITE_CFM_EVT */     {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* STR_SUSPEND_CFM_EVT */   {BTA_AV_SUSPEND_CONT,   BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* STR_RECONFIG_CFM_EVT */  {BTA_AV_RCFG_CFM,       BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* AVRC_TIMER_EVT */        {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* AVDT_CONNECT_EVT */      {BTA_AV_RCFG_OPEN,      BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* AVDT_DISCONNECT_EVT */   {BTA_AV_RCFG_DISCNTD,   BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* ROLE_CHANGE_EVT*/        {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* AVDT_DELAY_RPT_EVT */    {BTA_AV_DELAY_CO,       BTA_AV_SIGNORE,        BTA_AV_RCFG_SST },
+/* ACP_CONNECT_EVT */       {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_RCFG_SST }
+};
+
+/* state table for closing state */
+static const UINT8 bta_av_sst_closing[][BTA_AV_NUM_COLS] =
+{
+/* Event                     Action 1               Action 2               Next state */
+/* AP_OPEN_EVT */           {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* AP_CLOSE_EVT */          {BTA_AV_DISCONNECT_REQ, BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* AP_START_EVT */          {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* AP_STOP_EVT */           {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* API_RECONFIG_EVT */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* API_PROTECT_REQ_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* API_PROTECT_RSP_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* API_RC_OPEN_EVT  */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* SRC_DATA_READY_EVT */    {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* CI_SETCONFIG_OK_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* CI_SETCONFIG_FAIL_EVT */ {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* SDP_DISC_OK_EVT */       {BTA_AV_SDP_FAILED,     BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* SDP_DISC_FAIL_EVT */     {BTA_AV_SDP_FAILED,     BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* STR_DISC_OK_EVT */       {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_DISC_FAIL_EVT */     {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_GETCAP_OK_EVT */     {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_GETCAP_FAIL_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_OPEN_OK_EVT */       {BTA_AV_DO_CLOSE,       BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_OPEN_FAIL_EVT */     {BTA_AV_DISCONNECT_REQ, BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_START_OK_EVT */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_START_FAIL_EVT */    {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_CLOSE_EVT */         {BTA_AV_DISCONNECT_REQ, BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_CONFIG_IND_EVT */    {BTA_AV_SETCONFIG_REJ,  BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_SECURITY_IND_EVT */  {BTA_AV_SECURITY_REJ,   BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_SECURITY_CFM_EVT */  {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_WRITE_CFM_EVT */     {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_SUSPEND_CFM_EVT */   {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* STR_RECONFIG_CFM_EVT */  {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* AVRC_TIMER_EVT */        {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* AVDT_CONNECT_EVT */      {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* AVDT_DISCONNECT_EVT */   {BTA_AV_STR_CLOSED,     BTA_AV_SIGNORE,        BTA_AV_INIT_SST },
+/* ROLE_CHANGE_EVT*/        {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* AVDT_DELAY_RPT_EVT */    {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST },
+/* ACP_CONNECT_EVT */       {BTA_AV_SIGNORE,        BTA_AV_SIGNORE,        BTA_AV_CLOSING_SST }
+};
+
+/* type for state table */
+typedef const UINT8 (*tBTA_AV_SST_TBL)[BTA_AV_NUM_COLS];
+
+/* state table */
+static const tBTA_AV_SST_TBL bta_av_sst_tbl[] =
+{
+    bta_av_sst_init,
+    bta_av_sst_incoming,
+    bta_av_sst_opening,
+    bta_av_sst_open,
+    bta_av_sst_rcfg,
+    bta_av_sst_closing
+};
+
+
+
+#if (defined(BTA_AV_DEBUG) && BTA_AV_DEBUG == TRUE)
+static char *bta_av_sst_code(UINT8 state);
+#endif
+
+/*******************************************************************************
+**
+** Function         bta_av_is_rcfg_sst
+**
+** Description      Check if stream state machine is in reconfig state.
+**
+**
+** Returns          TRUE if stream state machine is in reconfig state.
+**
+*******************************************************************************/
+BOOLEAN bta_av_is_rcfg_sst (tBTA_AV_SCB *p_scb)
+{
+    BOOLEAN is_rcfg_sst = FALSE;
+
+    if (p_scb != NULL)
+    {
+        if (p_scb->state == BTA_AV_RCFG_SST)
+            is_rcfg_sst = TRUE;
+    }
+
+    return is_rcfg_sst;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_ssm_execute
+**
+** Description      Stream state machine event handling function for AV
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+void bta_av_ssm_execute(tBTA_AV_SCB *p_scb, UINT16 event, tBTA_AV_DATA *p_data)
+{
+    tBTA_AV_SST_TBL     state_table;
+    UINT8               action;
+    int                 i, xx;
+
+    if(p_scb == NULL)
+    {
+        /* this stream is not registered */
+        APPL_TRACE_EVENT("AV channel not registered");
+        return;
+    }
+
+    /* In case incoming connection is for VDP, we need to swap scb.        */
+    /* When ACP_CONNECT_EVT was received, we put first available scb to    */
+    /* to Incoming state. Later, when STR_CONFIG_IND_EVT is coming, we     */
+    /* know if it is A2DP or VDP.                                          */
+    if ((p_scb->state == BTA_AV_INIT_SST) && (event == BTA_AV_STR_CONFIG_IND_EVT))
+    {
+        for (xx = 0; xx < BTA_AV_NUM_STRS; xx++)
+        {
+            if (bta_av_cb.p_scb[xx])
+            {
+                if (bta_av_cb.p_scb[xx]->state == BTA_AV_INCOMING_SST)
+                {
+                    bta_av_cb.p_scb[xx]->state = BTA_AV_INIT_SST;
+                    bta_av_cb.p_scb[xx]->coll_mask = 0;
+                    p_scb->state = BTA_AV_INCOMING_SST;
+                    break;
+                }
+            }
+        }
+    }
+
+#if (defined(BTA_AV_DEBUG) && BTA_AV_DEBUG == TRUE)
+    APPL_TRACE_VERBOSE("AV Sevent(0x%x)=0x%x(%s) state=%d(%s)",
+        p_scb->hndl, event, bta_av_evt_code(event), p_scb->state, bta_av_sst_code(p_scb->state));
+#else
+    APPL_TRACE_VERBOSE("AV Sevent=0x%x state=%d", event, p_scb->state);
+#endif
+
+    /* look up the state table for the current state */
+    state_table = bta_av_sst_tbl[p_scb->state];
+
+    event -= BTA_AV_FIRST_SSM_EVT;
+
+    /* set next state */
+    p_scb->state = state_table[event][BTA_AV_SNEXT_STATE];
+
+    /* execute action functions */
+    for(i=0; i< BTA_AV_SACTIONS; i++)
+    {
+        if ((action = state_table[event][i]) != BTA_AV_SIGNORE)
+        {
+            (*p_scb->p_act_tbl[action])(p_scb, p_data);
+        }
+        else
+            break;
+    }
+
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_is_scb_opening
+**
+** Description      Returns TRUE is scb is in opening state.
+**
+**
+** Returns          TRUE if scb is in opening state.
+**
+*******************************************************************************/
+BOOLEAN bta_av_is_scb_opening (tBTA_AV_SCB *p_scb)
+{
+    BOOLEAN is_opening = FALSE;
+
+    if (p_scb)
+    {
+        if (p_scb->state == BTA_AV_OPENING_SST)
+            is_opening = TRUE;
+    }
+
+    return is_opening;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_is_scb_incoming
+**
+** Description      Returns TRUE is scb is in incoming state.
+**
+**
+** Returns          TRUE if scb is in incoming state.
+**
+*******************************************************************************/
+BOOLEAN bta_av_is_scb_incoming (tBTA_AV_SCB *p_scb)
+{
+    BOOLEAN is_incoming = FALSE;
+
+    if (p_scb)
+    {
+        if (p_scb->state == BTA_AV_INCOMING_SST)
+            is_incoming = TRUE;
+    }
+
+    return is_incoming;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_set_scb_sst_init
+**
+** Description      Set SST state to INIT.
+**                  Use this function to change SST outside of state machine.
+**
+** Returns          None
+**
+*******************************************************************************/
+void bta_av_set_scb_sst_init (tBTA_AV_SCB *p_scb)
+{
+    if (p_scb)
+    {
+        p_scb->state = BTA_AV_INIT_SST;
+    }
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_is_scb_init
+**
+** Description      Returns TRUE is scb is in init state.
+**
+**
+** Returns          TRUE if scb is in incoming state.
+**
+*******************************************************************************/
+BOOLEAN bta_av_is_scb_init (tBTA_AV_SCB *p_scb)
+{
+    BOOLEAN is_init = FALSE;
+
+    if (p_scb)
+    {
+        if (p_scb->state == BTA_AV_INIT_SST)
+            is_init = TRUE;
+    }
+
+    return is_init;
+}
+
+/*******************************************************************************
+**
+** Function         bta_av_set_scb_sst_incoming
+**
+** Description      Set SST state to incoming.
+**                  Use this function to change SST outside of state machine.
+**
+** Returns          None
+**
+*******************************************************************************/
+void bta_av_set_scb_sst_incoming (tBTA_AV_SCB *p_scb)
+{
+    if (p_scb)
+    {
+        p_scb->state = BTA_AV_INCOMING_SST;
+    }
+}
+
+/*****************************************************************************
+**  Debug Functions
+*****************************************************************************/
+#if (defined(BTA_AV_DEBUG) && BTA_AV_DEBUG == TRUE)
+/*******************************************************************************
+**
+** Function         bta_av_sst_code
+**
+** Description
+**
+** Returns          char *
+**
+*******************************************************************************/
+static char *bta_av_sst_code(UINT8 state)
+{
+    switch(state)
+    {
+    case BTA_AV_INIT_SST: return "INIT";
+    case BTA_AV_INCOMING_SST: return "INCOMING";
+    case BTA_AV_OPENING_SST: return "OPENING";
+    case BTA_AV_OPEN_SST: return "OPEN";
+    case BTA_AV_RCFG_SST: return "RCFG";
+    case BTA_AV_CLOSING_SST: return "CLOSING";
+    default:             return "unknown";
+    }
+}
+
+#endif
+#endif /* BTA_AV_INCLUDED */
diff --git a/components/bt/bluedroid/bta/include/bta_ar_api.h b/components/bt/bluedroid/bta/include/bta_ar_api.h
new file mode 100755 (executable)
index 0000000..b451cb4
--- /dev/null
@@ -0,0 +1,140 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2004-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This is the public interface file for the simulatenous advanced
+ *  audio/video streaming (AV) source and sink of BTA, Broadcom's Bluetooth
+ *  application layer for mobile phones.
+ *
+ ******************************************************************************/
+#ifndef BTA_AR_API_H
+#define BTA_AR_API_H
+
+#include "avdt_api.h"
+#include "avct_api.h"
+#include "avrc_api.h"
+#include "sdp_api.h"
+#include "bta_av_api.h"
+#include "bta_sys.h"
+
+/*****************************************************************************
+**  Constants and data types
+*****************************************************************************/
+/* This event signal to AR user that other profile is connected */
+#define BTA_AR_AVDT_CONN_EVT    (AVDT_MAX_EVT + 1)
+
+/*******************************************************************************
+**
+** Function         bta_ar_init
+**
+** Description      This function is called from bta_sys_init().
+**                  to initialize the control block
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_ar_init(void);
+
+/*******************************************************************************
+**
+** Function         bta_ar_reg_avdt
+**
+** Description      This function is called to register to AVDTP.
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_ar_reg_avdt(tAVDT_REG *p_reg, tAVDT_CTRL_CBACK *p_cback, tBTA_SYS_ID sys_id);
+
+/*******************************************************************************
+**
+** Function         bta_ar_dereg_avdt
+**
+** Description      This function is called to de-register from AVDTP.
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_ar_dereg_avdt(tBTA_SYS_ID sys_id);
+
+/*******************************************************************************
+**
+** Function         bta_ar_avdt_conn
+**
+** Description      This function is called to let ar know that some AVDTP profile
+**                  is connected for this sys_id.
+**                  If the other sys modules started a timer for PENDING_EVT,
+**                  the timer can be stopped now.
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_ar_avdt_conn(tBTA_SYS_ID sys_id, BD_ADDR bd_addr);
+
+/*******************************************************************************
+**
+** Function         bta_ar_reg_avct
+**
+** Description      This function is called to register to AVCTP.
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_ar_reg_avct(UINT16 mtu, UINT16 mtu_br, UINT8 sec_mask, tBTA_SYS_ID sys_id);
+
+/*******************************************************************************
+**
+** Function         bta_ar_dereg_avct
+**
+** Description      This function is called to deregister from AVCTP.
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_ar_dereg_avct(tBTA_SYS_ID sys_id);
+
+/******************************************************************************
+**
+** Function         bta_ar_reg_avrc
+**
+** Description      This function is called to register an SDP record for AVRCP.
+**
+** Returns          void
+**
+******************************************************************************/
+extern void bta_ar_reg_avrc(UINT16 service_uuid, char *p_service_name,
+                                                        char *p_provider_name, UINT16 categories, tBTA_SYS_ID sys_id);
+
+/******************************************************************************
+**
+** Function         bta_ar_dereg_avrc
+**
+** Description      This function is called to de-register/delete an SDP record for AVRCP.
+**
+** Returns          void
+**
+******************************************************************************/
+extern void bta_ar_dereg_avrc(UINT16 service_uuid, tBTA_SYS_ID sys_id);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BTA_AR_API_H */
diff --git a/components/bt/bluedroid/bta/include/bta_av_api.h b/components/bt/bluedroid/bta/include/bta_av_api.h
new file mode 100755 (executable)
index 0000000..e0e356f
--- /dev/null
@@ -0,0 +1,791 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2004-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This is the public interface file for the advanced audio/video streaming
+ *  (AV) subsystem of BTA, Broadcom's Bluetooth application layer for mobile
+ *  phones.
+ *
+ ******************************************************************************/
+#ifndef BTA_AV_API_H
+#define BTA_AV_API_H
+
+#include "avrc_api.h"
+#include "avdt_api.h"
+#include "a2d_api.h"
+#include "bta_api.h"
+
+/*****************************************************************************
+**  Constants and data types
+*****************************************************************************/
+/* Set to TRUE if seperate authorization prompt desired for AVCTP besides A2DP authorization */
+/* Typically FALSE when AVRCP is used in conjunction with A2DP */
+#ifndef BTA_AV_WITH_AVCTP_AUTHORIZATION
+#define BTA_AV_WITH_AVCTP_AUTHORIZATION     FALSE
+#endif
+
+/* AV status values */
+#define BTA_AV_SUCCESS          0       /* successful operation */
+#define BTA_AV_FAIL             1       /* generic failure */
+#define BTA_AV_FAIL_SDP         2       /* service not found */
+#define BTA_AV_FAIL_STREAM      3       /* stream connection failed */
+#define BTA_AV_FAIL_RESOURCES   4       /* no resources */
+#define BTA_AV_FAIL_ROLE        5       /* failed due to role management related issues */
+#define BTA_AV_FAIL_GET_CAP     6       /* get capability failed due to no SEP availale on the peer  */
+
+typedef UINT8 tBTA_AV_STATUS;
+
+/* AV features masks */
+#define BTA_AV_FEAT_RCTG        0x0001  /* remote control target */
+#define BTA_AV_FEAT_RCCT        0x0002  /* remote control controller */
+#define BTA_AV_FEAT_PROTECT     0x0004  /* streaming media contect protection */
+#define BTA_AV_FEAT_VENDOR      0x0008  /* remote control vendor dependent commands */
+#define BTA_AV_FEAT_REPORT      0x0020  /* use reporting service for VDP */
+#define BTA_AV_FEAT_METADATA    0x0040  /* remote control Metadata Transfer command/response */
+#define BTA_AV_FEAT_MULTI_AV    0x0080  /* use multi-av, if controller supports it */
+#define BTA_AV_FEAT_BROWSE      0x0010  /* use browsing channel */
+#define BTA_AV_FEAT_MASTER      0x0100  /* stream only as master role */
+#define BTA_AV_FEAT_ADV_CTRL    0x0200  /* remote control Advanced Control command/response */
+#define BTA_AV_FEAT_DELAY_RPT   0x0400  /* allow delay reporting */
+#define BTA_AV_FEAT_ACP_START   0x0800  /* start stream when 2nd SNK was accepted   */
+
+/* Internal features */
+#define BTA_AV_FEAT_NO_SCO_SSPD 0x8000  /* Do not suspend av streaming as to AG events(SCO or Call) */
+
+typedef UINT16 tBTA_AV_FEAT;
+
+/* AV channel values */
+#define BTA_AV_CHNL_MSK             0xC0
+#define BTA_AV_CHNL_AUDIO           0x40  /* audio channel */
+#define BTA_AV_CHNL_VIDEO           0x80  /* video channel */
+typedef UINT8 tBTA_AV_CHNL;
+
+
+#define BTA_AV_HNDL_MSK             0x3F
+typedef UINT8 tBTA_AV_HNDL;
+/* handle index to mask */
+#define BTA_AV_HNDL_TO_MSK(h)       ((UINT8)(1 << (h)))
+
+/* tBTA_AV_HNDL to mask */
+#define BTA_AV_HNDL_TYPE_TO_MSK(h)  ((UINT8)(1 << (h&BTA_AV_HNDL_MSK)))
+
+/* offset of codec type in codec info byte array */
+#define BTA_AV_CODEC_TYPE_IDX       AVDT_CODEC_TYPE_INDEX   /* 2 */
+
+
+
+/* maximum number of streams created: 1 for audio, 1 for video */
+#ifndef BTA_AV_NUM_STRS
+#define BTA_AV_NUM_STRS         2
+#endif
+
+#ifndef BTA_AV_MAX_SEPS
+#define BTA_AV_MAX_SEPS         2
+#endif
+
+#ifndef BTA_AV_MAX_A2DP_MTU
+ /*#define BTA_AV_MAX_A2DP_MTU     668 //224 (DM5) * 3 - 4(L2CAP header) */
+#define BTA_AV_MAX_A2DP_MTU     1008
+#endif
+
+#ifndef BTA_AV_MAX_VDP_MTU
+#define BTA_AV_MAX_VDP_MTU      1008
+#endif
+
+
+/* codec type */
+#define BTA_AV_CODEC_SBC        A2D_MEDIA_CT_SBC        /* SBC media codec type */
+#define BTA_AV_CODEC_M12        A2D_MEDIA_CT_M12        /* MPEG-1, 2 Audio media codec type */
+#define BTA_AV_CODEC_M24        A2D_MEDIA_CT_M24        /* MPEG-2, 4 AAC media codec type */
+#define BTA_AV_CODEC_ATRAC      A2D_MEDIA_CT_ATRAC      /* ATRAC family media codec type */
+#define BTA_AV_CODEC_H263_P0    VDP_MEDIA_CT_H263_P0    /* H.263 baseline (profile 0) */
+#define BTA_AV_CODEC_MPEG4      VDP_MEDIA_CT_MPEG4      /* MPEG-4 Visual Simple Profile */
+#define BTA_AV_CODEC_H263_P3    VDP_MEDIA_CT_H263_P3    /* H.263 profile 3 */
+#define BTA_AV_CODEC_H263_P8    VDP_MEDIA_CT_H263_P8    /* H.263 profile 8 */
+#define BTA_AV_CODEC_VEND       VDP_MEDIA_CT_VEND       /* Non-VDP */
+
+typedef UINT8 tBTA_AV_CODEC;
+
+/* Company ID in BT assigned numbers */
+#define BTA_AV_BT_VENDOR_ID     VDP_BT_VENDOR_ID        /* Broadcom Corporation */
+
+/* vendor specific codec ID */
+#define BTA_AV_CODEC_ID_H264    VDP_CODEC_ID_H264       /* Non-VDP codec ID - H.264 */
+#define BTA_AV_CODEC_ID_IMG     VDP_CODEC_ID_IMG        /* Non-VDP codec ID - images/slideshow */
+
+/* operation id list for BTA_AvRemoteCmd */
+#define BTA_AV_RC_SELECT        AVRC_ID_SELECT      /* select */
+#define BTA_AV_RC_UP            AVRC_ID_UP          /* up */
+#define BTA_AV_RC_DOWN          AVRC_ID_DOWN        /* down */
+#define BTA_AV_RC_LEFT          AVRC_ID_LEFT        /* left */
+#define BTA_AV_RC_RIGHT         AVRC_ID_RIGHT       /* right */
+#define BTA_AV_RC_RIGHT_UP      AVRC_ID_RIGHT_UP    /* right-up */
+#define BTA_AV_RC_RIGHT_DOWN    AVRC_ID_RIGHT_DOWN  /* right-down */
+#define BTA_AV_RC_LEFT_UP       AVRC_ID_LEFT_UP     /* left-up */
+#define BTA_AV_RC_LEFT_DOWN     AVRC_ID_LEFT_DOWN   /* left-down */
+#define BTA_AV_RC_ROOT_MENU     AVRC_ID_ROOT_MENU   /* root menu */
+#define BTA_AV_RC_SETUP_MENU    AVRC_ID_SETUP_MENU  /* setup menu */
+#define BTA_AV_RC_CONT_MENU     AVRC_ID_CONT_MENU   /* contents menu */
+#define BTA_AV_RC_FAV_MENU      AVRC_ID_FAV_MENU    /* favorite menu */
+#define BTA_AV_RC_EXIT          AVRC_ID_EXIT        /* exit */
+#define BTA_AV_RC_0             AVRC_ID_0           /* 0 */
+#define BTA_AV_RC_1             AVRC_ID_1           /* 1 */
+#define BTA_AV_RC_2             AVRC_ID_2           /* 2 */
+#define BTA_AV_RC_3             AVRC_ID_3           /* 3 */
+#define BTA_AV_RC_4             AVRC_ID_4           /* 4 */
+#define BTA_AV_RC_5             AVRC_ID_5           /* 5 */
+#define BTA_AV_RC_6             AVRC_ID_6           /* 6 */
+#define BTA_AV_RC_7             AVRC_ID_7           /* 7 */
+#define BTA_AV_RC_8             AVRC_ID_8           /* 8 */
+#define BTA_AV_RC_9             AVRC_ID_9           /* 9 */
+#define BTA_AV_RC_DOT           AVRC_ID_DOT         /* dot */
+#define BTA_AV_RC_ENTER         AVRC_ID_ENTER       /* enter */
+#define BTA_AV_RC_CLEAR         AVRC_ID_CLEAR       /* clear */
+#define BTA_AV_RC_CHAN_UP       AVRC_ID_CHAN_UP     /* channel up */
+#define BTA_AV_RC_CHAN_DOWN     AVRC_ID_CHAN_DOWN   /* channel down */
+#define BTA_AV_RC_PREV_CHAN     AVRC_ID_PREV_CHAN   /* previous channel */
+#define BTA_AV_RC_SOUND_SEL     AVRC_ID_SOUND_SEL   /* sound select */
+#define BTA_AV_RC_INPUT_SEL     AVRC_ID_INPUT_SEL   /* input select */
+#define BTA_AV_RC_DISP_INFO     AVRC_ID_DISP_INFO   /* display information */
+#define BTA_AV_RC_HELP          AVRC_ID_HELP        /* help */
+#define BTA_AV_RC_PAGE_UP       AVRC_ID_PAGE_UP     /* page up */
+#define BTA_AV_RC_PAGE_DOWN     AVRC_ID_PAGE_DOWN   /* page down */
+#define BTA_AV_RC_POWER         AVRC_ID_POWER       /* power */
+#define BTA_AV_RC_VOL_UP        AVRC_ID_VOL_UP      /* volume up */
+#define BTA_AV_RC_VOL_DOWN      AVRC_ID_VOL_DOWN    /* volume down */
+#define BTA_AV_RC_MUTE          AVRC_ID_MUTE        /* mute */
+#define BTA_AV_RC_PLAY          AVRC_ID_PLAY        /* play */
+#define BTA_AV_RC_STOP          AVRC_ID_STOP        /* stop */
+#define BTA_AV_RC_PAUSE         AVRC_ID_PAUSE       /* pause */
+#define BTA_AV_RC_RECORD        AVRC_ID_RECORD      /* record */
+#define BTA_AV_RC_REWIND        AVRC_ID_REWIND      /* rewind */
+#define BTA_AV_RC_FAST_FOR      AVRC_ID_FAST_FOR    /* fast forward */
+#define BTA_AV_RC_EJECT         AVRC_ID_EJECT       /* eject */
+#define BTA_AV_RC_FORWARD       AVRC_ID_FORWARD     /* forward */
+#define BTA_AV_RC_BACKWARD      AVRC_ID_BACKWARD    /* backward */
+#define BTA_AV_RC_ANGLE         AVRC_ID_ANGLE       /* angle */
+#define BTA_AV_RC_SUBPICT       AVRC_ID_SUBPICT     /* subpicture */
+#define BTA_AV_RC_F1            AVRC_ID_F1          /* F1 */
+#define BTA_AV_RC_F2            AVRC_ID_F2          /* F2 */
+#define BTA_AV_RC_F3            AVRC_ID_F3          /* F3 */
+#define BTA_AV_RC_F4            AVRC_ID_F4          /* F4 */
+#define BTA_AV_RC_F5            AVRC_ID_F5          /* F5 */
+#define BTA_AV_VENDOR           AVRC_ID_VENDOR      /* vendor unique */
+
+typedef UINT8 tBTA_AV_RC;
+
+/* state flag for pass through command */
+#define BTA_AV_STATE_PRESS      AVRC_STATE_PRESS    /* key pressed */
+#define BTA_AV_STATE_RELEASE    AVRC_STATE_RELEASE  /* key released */
+
+typedef UINT8 tBTA_AV_STATE;
+
+/* command codes for BTA_AvVendorCmd */
+#define BTA_AV_CMD_CTRL         AVRC_CMD_CTRL
+#define BTA_AV_CMD_STATUS       AVRC_CMD_STATUS
+#define BTA_AV_CMD_SPEC_INQ     AVRC_CMD_SPEC_INQ
+#define BTA_AV_CMD_NOTIF        AVRC_CMD_NOTIF
+#define BTA_AV_CMD_GEN_INQ      AVRC_CMD_GEN_INQ
+
+typedef UINT8 tBTA_AV_CMD;
+
+/* response codes for BTA_AvVendorRsp */
+#define BTA_AV_RSP_NOT_IMPL     AVRC_RSP_NOT_IMPL
+#define BTA_AV_RSP_ACCEPT       AVRC_RSP_ACCEPT
+#define BTA_AV_RSP_REJ          AVRC_RSP_REJ
+#define BTA_AV_RSP_IN_TRANS     AVRC_RSP_IN_TRANS
+#define BTA_AV_RSP_IMPL_STBL    AVRC_RSP_IMPL_STBL
+#define BTA_AV_RSP_CHANGED      AVRC_RSP_CHANGED
+#define BTA_AV_RSP_INTERIM      AVRC_RSP_INTERIM
+
+typedef UINT8 tBTA_AV_CODE;
+
+/* error codes for BTA_AvProtectRsp */
+#define BTA_AV_ERR_NONE             A2D_SUCCESS         /* Success, no error */
+#define BTA_AV_ERR_BAD_STATE        AVDT_ERR_BAD_STATE  /* Message cannot be processed in this state */
+#define BTA_AV_ERR_RESOURCE         AVDT_ERR_RESOURCE   /* Insufficient resources */
+#define BTA_AV_ERR_BAD_CP_TYPE      A2D_BAD_CP_TYPE     /* The requested Content Protection Type is not supported */
+#define BTA_AV_ERR_BAD_CP_FORMAT    A2D_BAD_CP_FORMAT   /* The format of Content Protection Data is not correct */
+
+typedef UINT8 tBTA_AV_ERR;
+
+
+/* AV callback events */
+#define BTA_AV_ENABLE_EVT       0       /* AV enabled */
+#define BTA_AV_REGISTER_EVT     1       /* registered to AVDT */
+#define BTA_AV_OPEN_EVT         2       /* connection opened */
+#define BTA_AV_CLOSE_EVT        3       /* connection closed */
+#define BTA_AV_START_EVT        4       /* stream data transfer started */
+#define BTA_AV_STOP_EVT         5       /* stream data transfer stopped */
+#define BTA_AV_PROTECT_REQ_EVT  6       /* content protection request */
+#define BTA_AV_PROTECT_RSP_EVT  7       /* content protection response */
+#define BTA_AV_RC_OPEN_EVT      8       /* remote control channel open */
+#define BTA_AV_RC_CLOSE_EVT     9       /* remote control channel closed */
+#define BTA_AV_REMOTE_CMD_EVT   10      /* remote control command */
+#define BTA_AV_REMOTE_RSP_EVT   11      /* remote control response */
+#define BTA_AV_VENDOR_CMD_EVT   12      /* vendor dependent remote control command */
+#define BTA_AV_VENDOR_RSP_EVT   13      /* vendor dependent remote control response */
+#define BTA_AV_RECONFIG_EVT     14      /* reconfigure response */
+#define BTA_AV_SUSPEND_EVT      15      /* suspend response */
+#define BTA_AV_PENDING_EVT      16      /* incoming connection pending:
+                                         * signal channel is open and stream is not open
+                                         * after BTA_AV_SIG_TIME_VAL ms */
+#define BTA_AV_META_MSG_EVT     17      /* metadata messages */
+#define BTA_AV_REJECT_EVT       18      /* incoming connection rejected */
+#define BTA_AV_RC_FEAT_EVT      19      /* remote control channel peer supported features update */
+#define BTA_AV_MEDIA_SINK_CFG_EVT    20      /* command to configure codec */
+#define BTA_AV_MEDIA_DATA_EVT   21      /* sending data to Media Task */
+/* Max BTA event */
+#define BTA_AV_MAX_EVT          22
+
+
+typedef UINT8 tBTA_AV_EVT;
+
+/* Event associated with BTA_AV_ENABLE_EVT */
+typedef struct
+{
+    tBTA_AV_FEAT    features;
+} tBTA_AV_ENABLE;
+
+/* Event associated with BTA_AV_REGISTER_EVT */
+typedef struct
+{
+    tBTA_AV_CHNL    chnl;       /* audio/video */
+    tBTA_AV_HNDL    hndl;       /* Handle associated with the stream. */
+    UINT8           app_id;     /* ID associated with call to BTA_AvRegister() */
+    tBTA_AV_STATUS  status;
+} tBTA_AV_REGISTER;
+
+/* data associated with BTA_AV_OPEN_EVT */
+#define BTA_AV_EDR_2MBPS        0x01
+#define BTA_AV_EDR_3MBPS        0x02
+typedef UINT8 tBTA_AV_EDR;
+
+typedef struct
+{
+    tBTA_AV_CHNL    chnl;
+    tBTA_AV_HNDL    hndl;
+    BD_ADDR         bd_addr;
+    tBTA_AV_STATUS  status;
+    BOOLEAN         starting;
+    tBTA_AV_EDR     edr;        /* 0, if peer device does not support EDR */
+    UINT8           sep;        /*  sep type of peer device */
+} tBTA_AV_OPEN;
+
+/* data associated with BTA_AV_CLOSE_EVT */
+typedef struct
+{
+    tBTA_AV_CHNL    chnl;
+    tBTA_AV_HNDL    hndl;
+} tBTA_AV_CLOSE;
+
+/* data associated with BTA_AV_START_EVT */
+typedef struct
+{
+    tBTA_AV_CHNL    chnl;
+    tBTA_AV_HNDL    hndl;
+    tBTA_AV_STATUS  status;
+    BOOLEAN         initiator; /* TRUE, if local device initiates the START */
+    BOOLEAN         suspending;
+} tBTA_AV_START;
+
+/* data associated with BTA_AV_SUSPEND_EVT */
+typedef struct
+{
+    tBTA_AV_CHNL    chnl;
+    tBTA_AV_HNDL    hndl;
+    BOOLEAN         initiator; /* TRUE, if local device initiates the SUSPEND */
+    tBTA_AV_STATUS  status;
+} tBTA_AV_SUSPEND;
+
+/* data associated with BTA_AV_RECONFIG_EVT */
+typedef struct
+{
+    tBTA_AV_CHNL    chnl;
+    tBTA_AV_HNDL    hndl;
+    tBTA_AV_STATUS  status;
+} tBTA_AV_RECONFIG;
+
+/* data associated with BTA_AV_PROTECT_REQ_EVT */
+typedef struct
+{
+    tBTA_AV_CHNL    chnl;
+    tBTA_AV_HNDL    hndl;
+    UINT8           *p_data;
+    UINT16          len;
+} tBTA_AV_PROTECT_REQ;
+
+/* data associated with BTA_AV_PROTECT_RSP_EVT */
+typedef struct
+{
+    tBTA_AV_CHNL    chnl;
+    tBTA_AV_HNDL    hndl;
+    UINT8           *p_data;
+    UINT16          len;
+    tBTA_AV_ERR     err_code;
+} tBTA_AV_PROTECT_RSP;
+
+/* data associated with BTA_AV_RC_OPEN_EVT */
+typedef struct
+{
+    UINT8           rc_handle;
+    tBTA_AV_FEAT    peer_features;
+    BD_ADDR         peer_addr;
+    tBTA_AV_STATUS  status;
+} tBTA_AV_RC_OPEN;
+
+/* data associated with BTA_AV_RC_CLOSE_EVT */
+typedef struct
+{
+    UINT8           rc_handle;
+    BD_ADDR         peer_addr;
+} tBTA_AV_RC_CLOSE;
+
+/* data associated with BTA_AV_RC_FEAT_EVT */
+typedef struct
+{
+    UINT8           rc_handle;
+    tBTA_AV_FEAT    peer_features;
+} tBTA_AV_RC_FEAT;
+
+/* data associated with BTA_AV_REMOTE_CMD_EVT */
+typedef struct
+{
+    UINT8           rc_handle;
+    tBTA_AV_RC      rc_id;
+    tBTA_AV_STATE   key_state;
+    UINT8           len;
+    UINT8           *p_data;
+    tAVRC_HDR       hdr;    /* Message header. */
+    UINT8           label;
+} tBTA_AV_REMOTE_CMD;
+
+/* data associated with BTA_AV_REMOTE_RSP_EVT */
+typedef struct
+{
+    UINT8           rc_handle;
+    tBTA_AV_RC      rc_id;
+    tBTA_AV_STATE   key_state;
+    UINT8           len;
+    UINT8           *p_data;
+    tBTA_AV_CODE    rsp_code;
+    UINT8           label;
+} tBTA_AV_REMOTE_RSP;
+
+/* data associated with BTA_AV_VENDOR_CMD_EVT, BTA_AV_VENDOR_RSP_EVT */
+typedef struct
+{
+    UINT8           rc_handle;
+    UINT16          len;            /* Max vendor dependent message is 512 */
+    UINT8           label;
+    tBTA_AV_CODE    code;
+    UINT32          company_id;
+    UINT8           *p_data;
+} tBTA_AV_VENDOR;
+
+/* data associated with BTA_AV_META_MSG_EVT */
+typedef struct
+{
+    UINT8           rc_handle;
+    UINT16          len;
+    UINT8           label;
+    tBTA_AV_CODE    code;
+    UINT32          company_id;
+    UINT8           *p_data;
+    tAVRC_MSG       *p_msg;
+} tBTA_AV_META_MSG;
+
+/* data associated with BTA_AV_PENDING_EVT */
+typedef struct
+{
+    BD_ADDR         bd_addr;
+} tBTA_AV_PEND;
+
+/* data associated with BTA_AV_REJECT_EVT */
+typedef struct
+{
+    BD_ADDR         bd_addr;
+    tBTA_AV_HNDL    hndl;       /* Handle associated with the stream that rejected the connection. */
+} tBTA_AV_REJECT;
+
+
+/* union of data associated with AV callback */
+typedef union
+{
+    tBTA_AV_CHNL        chnl;
+    tBTA_AV_ENABLE      enable;
+    tBTA_AV_REGISTER    registr;
+    tBTA_AV_OPEN        open;
+    tBTA_AV_CLOSE       close;
+    tBTA_AV_START       start;
+    tBTA_AV_PROTECT_REQ protect_req;
+    tBTA_AV_PROTECT_RSP protect_rsp;
+    tBTA_AV_RC_OPEN     rc_open;
+    tBTA_AV_RC_CLOSE    rc_close;
+    tBTA_AV_REMOTE_CMD  remote_cmd;
+    tBTA_AV_REMOTE_RSP  remote_rsp;
+    tBTA_AV_VENDOR      vendor_cmd;
+    tBTA_AV_VENDOR      vendor_rsp;
+    tBTA_AV_RECONFIG    reconfig;
+    tBTA_AV_SUSPEND     suspend;
+    tBTA_AV_PEND        pend;
+    tBTA_AV_META_MSG    meta_msg;
+    tBTA_AV_REJECT      reject;
+    tBTA_AV_RC_FEAT     rc_feat;
+} tBTA_AV;
+
+/* union of data associated with AV Media callback */
+typedef union
+{
+    BT_HDR     *p_data;
+    UINT8      *codec_info;
+} tBTA_AV_MEDIA;
+
+
+#define BTA_AVC_PACKET_LEN                  AVRC_PACKET_LEN
+#define BTA_VENDOR_DATA_OFFSET              6
+#define BTA_VENDOR_HEADER_LEN               4
+#define BTA_MAX_VENDOR_DEPENDENT_DATA_LEN   (BTA_AVC_PACKET_LEN-BTA_VENDOR_DATA_OFFSET-BTA_VENDOR_HEADER_LEN)
+#define BTA_GROUP_NAVI_MSG_OP_DATA_LEN      5
+
+#define BTA_ERROR_INVALID_CMD           AVRC_STS_BAD_CMD
+#define BTA_ERROR_INVALID_PARAM         AVRC_STS_BAD_PARAM
+#define BTA_ERROR_BAD_CONTENTS          AVRC_STS_NOT_FOUND
+#define BTA_ERROR_INTERNAL              AVRC_STS_INTERNAL_ERR
+
+#define BTA_AV_META_SINGLE_PACKET       AVRC_PKT_SINGLE
+
+#define BTA_AV_CO_METADATA              AVRC_CO_METADATA
+
+/* AV callback */
+typedef void (tBTA_AV_CBACK)(tBTA_AV_EVT event, tBTA_AV *p_data);
+typedef void (tBTA_AV_DATA_CBACK)(tBTA_AV_EVT event, tBTA_AV_MEDIA *p_data);
+
+/* type for stream state machine action functions */
+typedef void (*tBTA_AV_ACT)(void *p_cb, void *p_data);
+
+/* type for registering VDP */
+typedef void (tBTA_AV_REG) (tAVDT_CS *p_cs, char *p_service_name, void *p_data);
+
+/* AV configuration structure */
+typedef struct
+{
+    UINT32  company_id;         /* AVRCP Company ID */
+    UINT16  avrc_mtu;           /* AVRCP MTU at L2CAP for control channel */
+    UINT16  avrc_br_mtu;        /* AVRCP MTU at L2CAP for browsing channel */
+    UINT16  avrc_ct_cat;        /* AVRCP controller categories */
+    UINT16  avrc_tg_cat;        /* AVRCP target categories */
+    UINT16  sig_mtu;            /* AVDTP signaling channel MTU at L2CAP */
+    UINT16  audio_mtu;          /* AVDTP audio transport channel MTU at L2CAP */
+    const UINT16 *p_audio_flush_to;/* AVDTP audio transport channel flush timeout */
+    UINT16  audio_mqs;          /* AVDTP audio channel max data queue size */
+    UINT16  video_mtu;          /* AVDTP video transport channel MTU at L2CAP */
+    UINT16  video_flush_to;     /* AVDTP video transport channel flush timeout */
+    BOOLEAN avrc_group;         /* TRUE, to accept AVRC 1.3 group nevigation command */
+    UINT8   num_co_ids;         /* company id count in p_meta_co_ids */
+    UINT8   num_evt_ids;        /* event id count in p_meta_evt_ids */
+    tBTA_AV_CODE  rc_pass_rsp;  /* the default response code for pass through commands */
+    const UINT32 *p_meta_co_ids;/* the metadata Get Capabilities response for company id */
+    const UINT8 *p_meta_evt_ids;/* the the metadata Get Capabilities response for event id */
+    const tBTA_AV_ACT *p_act_tbl;/* the action function table for VDP stream */
+    tBTA_AV_REG       *p_reg;   /* action function to register VDP */
+    char              avrc_controller_name[BTA_SERVICE_NAME_LEN]; /* Default AVRCP controller name */
+    char              avrc_target_name[BTA_SERVICE_NAME_LEN];     /* Default AVRCP target name*/
+} tBTA_AV_CFG;
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/*****************************************************************************
+**  External Function Declarations
+*****************************************************************************/
+
+/*******************************************************************************
+**
+** Function         BTA_AvEnable
+**
+** Description      Enable the advanced audio/video service. When the enable
+**                  operation is complete the callback function will be
+**                  called with a BTA_AV_ENABLE_EVT. This function must
+**                  be called before other function in the AV API are
+**                  called.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvEnable(tBTA_SEC sec_mask, tBTA_AV_FEAT features,
+                          tBTA_AV_CBACK *p_cback);
+
+/*******************************************************************************
+**
+** Function         BTA_AvDisable
+**
+** Description      Disable the advanced audio/video service.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvDisable(void);
+
+/*******************************************************************************
+**
+** Function         BTA_AvRegister
+**
+** Description      Register the audio or video service to stack. When the
+**                  operation is complete the callback function will be
+**                  called with a BTA_AV_REGISTER_EVT. This function must
+**                  be called before AVDT stream is open.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvRegister(tBTA_AV_CHNL chnl, const char *p_service_name,
+                            UINT8 app_id, tBTA_AV_DATA_CBACK  *p_data_cback);
+
+/*******************************************************************************
+**
+** Function         BTA_AvDeregister
+**
+** Description      Deregister the audio or video service
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvDeregister(tBTA_AV_HNDL hndl);
+
+/*******************************************************************************
+**
+** Function         BTA_AvOpen
+**
+** Description      Opens an advanced audio/video connection to a peer device.
+**                  When connection is open callback function is called
+**                  with a BTA_AV_OPEN_EVT.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvOpen(BD_ADDR bd_addr, tBTA_AV_HNDL handle,
+                        BOOLEAN use_rc, tBTA_SEC sec_mask, UINT16 uuid);
+
+/*******************************************************************************
+**
+** Function         BTA_AvClose
+**
+** Description      Close the current streams.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvClose(tBTA_AV_HNDL handle);
+
+/*******************************************************************************
+**
+** Function         BTA_AvDisconnect
+**
+** Description      Close the connection to the address.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvDisconnect(BD_ADDR bd_addr);
+
+/*******************************************************************************
+**
+** Function         BTA_AvEnable_Sink
+**
+** Description      Enable/Disable A2DP Sink.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvEnable_Sink(int enable);
+
+/*******************************************************************************
+**
+** Function         BTA_AvStart
+**
+** Description      Start audio/video stream data transfer.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvStart(void);
+
+/*******************************************************************************
+**
+** Function         BTA_AvStop
+**
+** Description      Stop audio/video stream data transfer.
+**                  If suspend is TRUE, this function sends AVDT suspend signal
+**                  to the connected peer(s).
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvStop(BOOLEAN suspend);
+
+/*******************************************************************************
+**
+** Function         BTA_AvReconfig
+**
+** Description      Reconfigure the audio/video stream.
+**                  If suspend is TRUE, this function tries the suspend/reconfigure
+**                  procedure first.
+**                  If suspend is FALSE or when suspend/reconfigure fails,
+**                  this function closes and re-opens the AVDT connection.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvReconfig(tBTA_AV_HNDL hndl, BOOLEAN suspend, UINT8 sep_info_idx,
+                            UINT8 *p_codec_info, UINT8 num_protect, UINT8 *p_protect_info);
+
+/*******************************************************************************
+**
+** Function         BTA_AvProtectReq
+**
+** Description      Send a content protection request.  This function can only
+**                  be used if AV is enabled with feature BTA_AV_FEAT_PROTECT.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvProtectReq(tBTA_AV_HNDL hndl, UINT8 *p_data, UINT16 len);
+
+/*******************************************************************************
+**
+** Function         BTA_AvProtectRsp
+**
+** Description      Send a content protection response.  This function must
+**                  be called if a BTA_AV_PROTECT_REQ_EVT is received.
+**                  This function can only be used if AV is enabled with
+**                  feature BTA_AV_FEAT_PROTECT.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvProtectRsp(tBTA_AV_HNDL hndl, UINT8 error_code, UINT8 *p_data,
+                              UINT16 len);
+
+/*******************************************************************************
+**
+** Function         BTA_AvRemoteCmd
+**
+** Description      Send a remote control command.  This function can only
+**                  be used if AV is enabled with feature BTA_AV_FEAT_RCCT.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvRemoteCmd(UINT8 rc_handle, UINT8 label, tBTA_AV_RC rc_id,
+                             tBTA_AV_STATE key_state);
+
+/*******************************************************************************
+**
+** Function         BTA_AvVendorCmd
+**
+** Description      Send a vendor dependent remote control command.  This
+**                  function can only be used if AV is enabled with feature
+**                  BTA_AV_FEAT_VENDOR.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvVendorCmd(UINT8 rc_handle, UINT8 label, tBTA_AV_CODE cmd_code,
+                             UINT8 *p_data, UINT16 len);
+
+/*******************************************************************************
+**
+** Function         BTA_AvVendorRsp
+**
+** Description      Send a vendor dependent remote control response.
+**                  This function must be called if a BTA_AV_VENDOR_CMD_EVT
+**                  is received. This function can only be used if AV is
+**                  enabled with feature BTA_AV_FEAT_VENDOR.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvVendorRsp(UINT8 rc_handle, UINT8 label, tBTA_AV_CODE rsp_code,
+                             UINT8 *p_data, UINT16 len, UINT32 company_id);
+
+
+/*******************************************************************************
+**
+** Function         BTA_AvOpenRc
+**
+** Description      Open an AVRCP connection toward the device with the
+**                  specified handle
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvOpenRc(tBTA_AV_HNDL handle);
+
+/*******************************************************************************
+**
+** Function         BTA_AvCloseRc
+**
+** Description      Close an AVRCP connection
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvCloseRc(UINT8 rc_handle);
+
+/*******************************************************************************
+**
+** Function         BTA_AvMetaRsp
+**
+** Description      Send a Metadata command/response. The message contained
+**                  in p_pkt can be composed with AVRC utility functions.
+**                  This function can only be used if AV is enabled with feature
+**                  BTA_AV_FEAT_METADATA.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvMetaRsp(UINT8 rc_handle, UINT8 label, tBTA_AV_CODE rsp_code,
+                               BT_HDR *p_pkt);
+
+/*******************************************************************************
+**
+** Function         BTA_AvMetaCmd
+**
+** Description      Send a Metadata/Advanced Control command. The message contained
+**                  in p_pkt can be composed with AVRC utility functions.
+**                  This function can only be used if AV is enabled with feature
+**                  BTA_AV_FEAT_METADATA.
+**                  This message is sent only when the peer supports the TG role.
+*8                  The only command makes sense right now is the absolute volume command.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_AvMetaCmd(UINT8 rc_handle, UINT8 label, tBTA_AV_CMD cmd_code, BT_HDR *p_pkt);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BTA_AV_API_H */
diff --git a/components/bt/bluedroid/bta/include/bta_av_ci.h b/components/bt/bluedroid/bta/include/bta_av_ci.h
new file mode 100755 (executable)
index 0000000..9e2c62c
--- /dev/null
@@ -0,0 +1,73 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2005-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This is the interface file for advanced audio/video call-in functions.
+ *
+ ******************************************************************************/
+#ifndef BTA_AV_CI_H
+#define BTA_AV_CI_H
+
+#include "bta_av_api.h"
+
+/*****************************************************************************
+**  Function Declarations
+*****************************************************************************/
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/*******************************************************************************
+**
+** Function         bta_av_ci_src_data_ready
+**
+** Description      This function sends an event to the AV indicating that
+**                  the phone has audio stream data ready to send and AV
+**                  should call bta_av_co_audio_src_data_path() or
+**                  bta_av_co_video_src_data_path().
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_ci_src_data_ready(tBTA_AV_CHNL chnl);
+
+/*******************************************************************************
+**
+** Function         bta_av_ci_setconfig
+**
+** Description      This function must be called in response to function
+**                  bta_av_co_audio_setconfig() or bta_av_co_video_setconfig.
+**                  Parameter err_code is set to an AVDTP status value;
+**                  AVDT_SUCCESS if the codec configuration is ok,
+**                  otherwise error.
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_ci_setconfig(tBTA_AV_HNDL hndl, UINT8 err_code,
+                                        UINT8 category, UINT8 num_seid, UINT8 *p_seid,
+                                        BOOLEAN recfg_needed, UINT8 avdt_handle);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BTA_AV_CI_H */
diff --git a/components/bt/bluedroid/bta/include/bta_av_co.h b/components/bt/bluedroid/bta/include/bta_av_co.h
new file mode 100755 (executable)
index 0000000..ad8e697
--- /dev/null
@@ -0,0 +1,391 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2003-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This is the interface file for advanced audio/video call-out functions.
+ *
+ ******************************************************************************/
+#ifndef BTA_AV_CO_H
+#define BTA_AV_CO_H
+
+#include "l2c_api.h"
+#include "bta_av_api.h"
+
+/*****************************************************************************
+**  Constants and data types
+*****************************************************************************/
+
+/* TRUE to use SCMS-T content protection */
+#ifndef BTA_AV_CO_CP_SCMS_T
+#define BTA_AV_CO_CP_SCMS_T     FALSE
+#endif
+
+/* the content protection IDs assigned by BT SIG */
+#define BTA_AV_CP_SCMS_T_ID     0x0002
+#define BTA_AV_CP_DTCP_ID       0x0001
+
+#define BTA_AV_CP_LOSC                  2
+#define BTA_AV_CP_INFO_LEN              3
+
+#define BTA_AV_CP_SCMS_COPY_MASK        3
+#define BTA_AV_CP_SCMS_COPY_FREE        2
+#define BTA_AV_CP_SCMS_COPY_ONCE        1
+#define BTA_AV_CP_SCMS_COPY_NEVER       0
+
+#define BTA_AV_CO_DEFAULT_AUDIO_OFFSET      AVDT_MEDIA_OFFSET
+
+enum
+{
+    BTA_AV_CO_ST_INIT,
+    BTA_AV_CO_ST_IN,
+    BTA_AV_CO_ST_OUT,
+    BTA_AV_CO_ST_OPEN,
+    BTA_AV_CO_ST_STREAM
+};
+
+
+/* data type for the Audio Codec Information*/
+typedef struct
+{
+    UINT16  bit_rate;       /* SBC encoder bit rate in kbps */
+    UINT16  bit_rate_busy;  /* SBC encoder bit rate in kbps */
+    UINT16  bit_rate_swampd;/* SBC encoder bit rate in kbps */
+    UINT8   busy_level;     /* Busy level indicating the bit-rate to be used */
+    UINT8   codec_info[AVDT_CODEC_SIZE];
+    UINT8   codec_type;     /* Codec type */
+} tBTA_AV_AUDIO_CODEC_INFO;
+
+/*******************************************************************************
+**
+** Function         bta_av_co_audio_init
+**
+** Description      This callout function is executed by AV when it is
+**                  started by calling BTA_AvEnable().  This function can be
+**                  used by the phone to initialize audio paths or for other
+**                  initialization purposes.
+**
+**
+** Returns          Stream codec and content protection capabilities info.
+**
+*******************************************************************************/
+extern BOOLEAN bta_av_co_audio_init(UINT8 *p_codec_type, UINT8 *p_codec_info,
+                                    UINT8 *p_num_protect, UINT8 *p_protect_info, UINT8 index);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_audio_disc_res
+**
+** Description      This callout function is executed by AV to report the
+**                  number of stream end points (SEP) were found during the
+**                  AVDT stream discovery process.
+**
+**
+** Returns          void.
+**
+*******************************************************************************/
+extern void bta_av_co_audio_disc_res(tBTA_AV_HNDL hndl, UINT8 num_seps,
+                    UINT8 num_snk, UINT8 num_src, BD_ADDR addr, UINT16 uuid_local);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_video_disc_res
+**
+** Description      This callout function is executed by AV to report the
+**                  number of stream end points (SEP) were found during the
+**                  AVDT stream discovery process.
+**
+**
+** Returns          void.
+**
+*******************************************************************************/
+extern void bta_av_co_video_disc_res(tBTA_AV_HNDL hndl, UINT8 num_seps,
+                                     UINT8 num_snk, BD_ADDR addr);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_audio_getconfig
+**
+** Description      This callout function is executed by AV to retrieve the
+**                  desired codec and content protection configuration for the
+**                  audio stream.
+**
+**
+** Returns          Stream codec and content protection configuration info.
+**
+*******************************************************************************/
+extern UINT8 bta_av_co_audio_getconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                       UINT8 *p_codec_info, UINT8 *p_sep_info_idx, UINT8 seid,
+                                       UINT8 *p_num_protect, UINT8 *p_protect_info);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_video_getconfig
+**
+** Description      This callout function is executed by AV to retrieve the
+**                  desired codec and content protection configuration for the
+**                  video stream.
+**
+**
+** Returns          Stream codec and content protection configuration info.
+**
+*******************************************************************************/
+extern UINT8 bta_av_co_video_getconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                       UINT8 *p_codec_info, UINT8 *p_sep_info_idx, UINT8 seid,
+                                       UINT8 *p_num_protect, UINT8 *p_protect_info);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_audio_setconfig
+**
+** Description      This callout function is executed by AV to set the
+**                  codec and content protection configuration of the audio stream.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_audio_setconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                        UINT8 *p_codec_info, UINT8 seid, BD_ADDR addr,
+                                        UINT8 num_protect, UINT8 *p_protect_info,UINT8 t_local_sep, UINT8 avdt_handle);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_video_setconfig
+**
+** Description      This callout function is executed by AV to set the
+**                  codec and content protection configuration of the video stream.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_video_setconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                      UINT8 *p_codec_info, UINT8 seid, BD_ADDR addr,
+                                      UINT8 num_protect, UINT8 *p_protect_info);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_audio_open
+**
+** Description      This function is called by AV when the audio stream connection
+**                  is opened.
+**                  BTA-AV maintains the MTU of A2DP streams.
+**                  If this is the 2nd audio stream, mtu is the smaller of the 2
+**                  streams.
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_audio_open(tBTA_AV_HNDL hndl,
+                                 tBTA_AV_CODEC codec_type, UINT8 *p_codec_info,
+                                 UINT16 mtu);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_video_open
+**
+** Description      This function is called by AV when the video stream connection
+**                  is opened.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_video_open(tBTA_AV_HNDL hndl,
+                                 tBTA_AV_CODEC codec_type, UINT8 *p_codec_info,
+                                 UINT16 mtu);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_audio_close
+**
+** Description      This function is called by AV when the audio stream connection
+**                  is closed.
+**                  BTA-AV maintains the MTU of A2DP streams.
+**                  When one stream is closed and no other audio stream is open,
+**                  mtu is reported as 0.
+**                  Otherwise, the MTU remains open is reported.
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_audio_close(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                  UINT16 mtu);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_video_close
+**
+** Description      This function is called by AV when the video stream connection
+**                  is closed.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_video_close(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                  UINT16 mtu);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_audio_start
+**
+** Description      This function is called by AV when the audio streaming data
+**                  transfer is started.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_audio_start(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                  UINT8 *p_codec_info, BOOLEAN *p_no_rtp_hdr);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_video_start
+**
+** Description      This function is called by AV when the video streaming data
+**                  transfer is started.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_video_start(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                  UINT8 *p_codec_info, BOOLEAN *p_no_rtp_hdr);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_audio_stop
+**
+** Description      This function is called by AV when the audio streaming data
+**                  transfer is stopped.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_audio_stop(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_video_stop
+**
+** Description      This function is called by AV when the video streaming data
+**                  transfer is stopped.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_video_stop(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_audio_src_data_path
+**
+** Description      This function is called to get the next data buffer from
+**                  the audio codec
+**
+** Returns          NULL if data is not ready.
+**                  Otherwise, a GKI buffer (BT_HDR*) containing the audio data.
+**
+*******************************************************************************/
+extern void * bta_av_co_audio_src_data_path(tBTA_AV_CODEC codec_type,
+                                            UINT32 *p_len, UINT32 *p_timestamp);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_video_src_data_path
+**
+** Description      This function is called to get the next data buffer from
+**                  the video codec.
+**
+** Returns          NULL if data is not ready.
+**                  Otherwise, a video data buffer (UINT8*).
+**
+*******************************************************************************/
+extern void * bta_av_co_video_src_data_path(tBTA_AV_CODEC codec_type,
+                                            UINT32 *p_len, UINT32 *p_timestamp);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_audio_drop
+**
+** Description      An Audio packet is dropped. .
+**                  It's very likely that the connected headset with this handle
+**                  is moved far away. The implementation may want to reduce
+**                  the encoder bit rate setting to reduce the packet size.
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_audio_drop(tBTA_AV_HNDL hndl);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_video_report_conn
+**
+** Description      This function is called by AV when the reporting channel is
+**                  opened (open=TRUE) or closed (open=FALSE).
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_video_report_conn (BOOLEAN open, UINT8 avdt_handle);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_video_report_rr
+**
+** Description      This function is called by AV when a Receiver Report is
+**                  received
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_video_report_rr (UINT32 packet_lost);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_audio_delay
+**
+** Description      This function is called by AV when the audio stream connection
+**                  needs to send the initial delay report to the connected SRC.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_audio_delay(tBTA_AV_HNDL hndl, UINT16 delay);
+
+/*******************************************************************************
+**
+** Function         bta_av_co_video_delay
+**
+** Description      This function is called by AV when the video stream connection
+**                  needs to send the initial delay report to the connected SRC.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_co_video_delay(tBTA_AV_HNDL hndl, UINT16 delay);
+
+#endif /* BTA_AV_CO_H */
diff --git a/components/bt/bluedroid/bta/include/bta_av_sbc.h b/components/bt/bluedroid/bta/include/bta_av_sbc.h
new file mode 100755 (executable)
index 0000000..d7cfa89
--- /dev/null
@@ -0,0 +1,219 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2004-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This is the interface to utility functions for dealing with SBC data
+ *  frames and codec capabilities.
+ *
+ ******************************************************************************/
+#ifndef BTA_AV_SBC_H
+#define BTA_AV_SBC_H
+
+/*****************************************************************************
+**  constants
+*****************************************************************************/
+
+/* SBC packet header size */
+#define BTA_AV_SBC_HDR_SIZE         A2D_SBC_MPL_HDR_LEN
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_init_up_sample
+**
+** Description      initialize the up sample
+**
+**                  src_sps: samples per second (source audio data)
+**                  dst_sps: samples per second (converted audio data)
+**                  bits: number of bits per pcm sample
+**                  n_channels: number of channels (i.e. mono(1), stereo(2)...)
+**
+** Returns          none
+**
+*******************************************************************************/
+extern void bta_av_sbc_init_up_sample (UINT32 src_sps, UINT32 dst_sps,
+                                       UINT16 bits, UINT16 n_channels);
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_up_sample
+**
+** Description      Given the source (p_src) audio data and
+**                  source speed (src_sps, samples per second),
+**                  This function converts it to audio data in the desired format
+**
+**                  p_src: the data buffer that holds the source audio data
+**                  p_dst: the data buffer to hold the converted audio data
+**                  src_samples: The number of source samples (number of bytes)
+**                  dst_samples: The size of p_dst (number of bytes)
+**
+** Note:            An AE reported an issue with this function.
+**                  When called with bta_av_sbc_up_sample(src, uint8_array_dst..)
+**                  the byte before uint8_array_dst may get overwritten.
+**                  Using uint16_array_dst avoids the problem.
+**                  This issue is related to endian-ness and is hard to resolve
+**                  in a generic manner.
+** **************** Please use uint16 array as dst.
+**
+** Returns          The number of bytes used in p_dst
+**                  The number of bytes used in p_src (in *p_ret)
+**
+*******************************************************************************/
+extern int bta_av_sbc_up_sample (void *p_src, void *p_dst,
+                                 UINT32 src_samples, UINT32 dst_samples,
+                                 UINT32 *p_ret);
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_up_sample_16s (16bits-stereo)
+**
+** Description      Given the source (p_src) audio data and
+**                  source speed (src_sps, samples per second),
+**                  This function converts it to audio data in the desired format
+**
+**                  p_src: the data buffer that holds the source audio data
+**                  p_dst: the data buffer to hold the converted audio data
+**                  src_samples: The number of source samples (in uint of 4 bytes)
+**                  dst_samples: The size of p_dst (in uint of 4 bytes)
+**
+** Returns          The number of bytes used in p_dst
+**                  The number of bytes used in p_src (in *p_ret)
+**
+*******************************************************************************/
+extern int bta_av_sbc_up_sample_16s (void *p_src, void *p_dst,
+                                 UINT32 src_samples, UINT32 dst_samples,
+                                 UINT32 *p_ret);
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_up_sample_16m (16bits-mono)
+**
+** Description      Given the source (p_src) audio data and
+**                  source speed (src_sps, samples per second),
+**                  This function converts it to audio data in the desired format
+**
+**                  p_src: the data buffer that holds the source audio data
+**                  p_dst: the data buffer to hold the converted audio data
+**                  src_samples: The number of source samples (in uint of 2 bytes)
+**                  dst_samples: The size of p_dst (in uint of 2 bytes)
+**
+** Returns          The number of bytes used in p_dst
+**                  The number of bytes used in p_src (in *p_ret)
+**
+*******************************************************************************/
+extern int bta_av_sbc_up_sample_16m (void *p_src, void *p_dst,
+                                     UINT32 src_samples, UINT32 dst_samples,
+                                     UINT32 *p_ret);
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_up_sample_8s (8bits-stereo)
+**
+** Description      Given the source (p_src) audio data and
+**                  source speed (src_sps, samples per second),
+**                  This function converts it to audio data in the desired format
+**
+**                  p_src: the data buffer that holds the source audio data
+**                  p_dst: the data buffer to hold the converted audio data
+**                  src_samples: The number of source samples (in uint of 2 bytes)
+**                  dst_samples: The size of p_dst (in uint of 2 bytes)
+**
+** Returns          The number of bytes used in p_dst
+**                  The number of bytes used in p_src (in *p_ret)
+**
+*******************************************************************************/
+extern int bta_av_sbc_up_sample_8s (void *p_src, void *p_dst,
+                                 UINT32 src_samples, UINT32 dst_samples,
+                                 UINT32 *p_ret);
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_up_sample_8m (8bits-mono)
+**
+** Description      Given the source (p_src) audio data and
+**                  source speed (src_sps, samples per second),
+**                  This function converts it to audio data in the desired format
+**
+**                  p_src: the data buffer that holds the source audio data
+**                  p_dst: the data buffer to hold the converted audio data
+**                  src_samples: The number of source samples (number of bytes)
+**                  dst_samples: The size of p_dst (number of bytes)
+**
+** Returns          The number of bytes used in p_dst
+**                  The number of bytes used in p_src (in *p_ret)
+**
+*******************************************************************************/
+extern int bta_av_sbc_up_sample_8m (void *p_src, void *p_dst,
+                                     UINT32 src_samples, UINT32 dst_samples,
+                                     UINT32 *p_ret);
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_cfg_for_cap
+**
+** Description      Determine the preferred SBC codec configuration for the
+**                  given codec capabilities.  The function is passed the
+**                  preferred codec configuration and the peer codec
+**                  capabilities for the stream.  The function attempts to
+**                  match the preferred capabilities with the configuration
+**                  as best it can.  The resulting codec configuration is
+**                  returned in the same memory used for the capabilities.
+**
+** Returns          0 if ok, nonzero if error.
+**                  Codec configuration in p_cap.
+**
+*******************************************************************************/
+extern UINT8 bta_av_sbc_cfg_for_cap(UINT8 *p_peer, tA2D_SBC_CIE *p_cap, tA2D_SBC_CIE *p_pref);
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_cfg_in_cap
+**
+** Description      This function checks whether an SBC codec configuration
+**                  is allowable for the given codec capabilities.
+**
+** Returns          0 if ok, nonzero if error.
+**
+*******************************************************************************/
+extern UINT8 bta_av_sbc_cfg_in_cap(UINT8 *p_cfg, tA2D_SBC_CIE *p_cap);
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_cfg_matches_cap
+**
+** Description      This function checks whether an SBC codec configuration
+**                  matched with capabilities. Here we check subset.
+**
+** Returns          0 if ok, nonzero if error.
+**
+*******************************************************************************/
+extern UINT8 bta_av_sbc_cfg_matches_cap(UINT8 *p_cfg, tA2D_SBC_CIE *p_cap);
+
+/*******************************************************************************
+**
+** Function         bta_av_sbc_bld_hdr
+**
+** Description      This function builds the packet header for MPF1.
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void bta_av_sbc_bld_hdr(BT_HDR *p_buf, UINT16 fr_per_pkt);
+
+#endif /* BTA_AV_SBC_H */
+
diff --git a/components/bt/bluedroid/btif/bta_av_co.c b/components/bt/bluedroid/btif/bta_av_co.c
new file mode 100755 (executable)
index 0000000..d35bb64
--- /dev/null
@@ -0,0 +1,1977 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2004-2012 Broadcom Corporation
+ *
+ *  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.
+ *
+ ******************************************************************************/
+
+/******************************************************************************
+ *
+ *  This is the advanced audio/video call-out function implementation for
+ *  BTIF.
+ *
+ ******************************************************************************/
+
+#include "string.h"
+#include "a2d_api.h"
+#include "a2d_sbc.h"
+#include "bta_sys.h"
+#include "bta_av_api.h"
+#include "bta_av_co.h"
+#include "bta_av_ci.h"
+#include "bta_av_sbc.h"
+#if 0 // todo : port the call out functions
+#include "btif_media.h"
+#include "sbc_encoder.h"
+#include "btif_av_co.h"
+#include "btif_util.h"
+
+/*****************************************************************************
+ **  Constants
+ *****************************************************************************/
+
+#define FUNC_TRACE()     APPL_TRACE_DEBUG("%s", __FUNCTION__);
+
+/* Macro to retrieve the number of elements in a statically allocated array */
+#define BTA_AV_CO_NUM_ELEMENTS(__a) (sizeof(__a)/sizeof((__a)[0]))
+
+/* MIN and MAX macros */
+#define BTA_AV_CO_MIN(X,Y) ((X) < (Y) ? (X) : (Y))
+#define BTA_AV_CO_MAX(X,Y) ((X) > (Y) ? (X) : (Y))
+
+/* Macro to convert audio handle to index and vice versa */
+#define BTA_AV_CO_AUDIO_HNDL_TO_INDX(hndl) (((hndl) & (~BTA_AV_CHNL_MSK)) - 1)
+#define BTA_AV_CO_AUDIO_INDX_TO_HNDL(indx) (((indx) + 1) | BTA_AV_CHNL_AUDIO)
+
+
+/* Offsets to access codec information in SBC codec */
+#define BTA_AV_CO_SBC_FREQ_CHAN_OFF    3
+#define BTA_AV_CO_SBC_BLOCK_BAND_OFF   4
+#define BTA_AV_CO_SBC_MIN_BITPOOL_OFF  5
+#define BTA_AV_CO_SBC_MAX_BITPOOL_OFF  6
+
+#define BTA_AV_CO_SBC_MAX_BITPOOL  53
+
+/* SCMS-T protect info */
+const UINT8 bta_av_co_cp_scmst[BTA_AV_CP_INFO_LEN] = "\x02\x02\x00";
+
+/* SBC SRC codec capabilities */
+const tA2D_SBC_CIE bta_av_co_sbc_caps =
+{
+    (A2D_SBC_IE_SAMP_FREQ_44), /* samp_freq */
+    (A2D_SBC_IE_CH_MD_MONO | A2D_SBC_IE_CH_MD_STEREO | A2D_SBC_IE_CH_MD_JOINT | A2D_SBC_IE_CH_MD_DUAL), /* ch_mode */
+    (A2D_SBC_IE_BLOCKS_16 | A2D_SBC_IE_BLOCKS_12 | A2D_SBC_IE_BLOCKS_8 | A2D_SBC_IE_BLOCKS_4), /* block_len */
+    (A2D_SBC_IE_SUBBAND_4 | A2D_SBC_IE_SUBBAND_8), /* num_subbands */
+    (A2D_SBC_IE_ALLOC_MD_L | A2D_SBC_IE_ALLOC_MD_S), /* alloc_mthd */
+    BTA_AV_CO_SBC_MAX_BITPOOL, /* max_bitpool */
+    A2D_SBC_IE_MIN_BITPOOL /* min_bitpool */
+};
+
+/* SBC SINK codec capabilities */
+const tA2D_SBC_CIE bta_av_co_sbc_sink_caps =
+{
+    (A2D_SBC_IE_SAMP_FREQ_48 | A2D_SBC_IE_SAMP_FREQ_44), /* samp_freq */
+    (A2D_SBC_IE_CH_MD_MONO | A2D_SBC_IE_CH_MD_STEREO | A2D_SBC_IE_CH_MD_JOINT | A2D_SBC_IE_CH_MD_DUAL), /* ch_mode */
+    (A2D_SBC_IE_BLOCKS_16 | A2D_SBC_IE_BLOCKS_12 | A2D_SBC_IE_BLOCKS_8 | A2D_SBC_IE_BLOCKS_4), /* block_len */
+    (A2D_SBC_IE_SUBBAND_4 | A2D_SBC_IE_SUBBAND_8), /* num_subbands */
+    (A2D_SBC_IE_ALLOC_MD_L | A2D_SBC_IE_ALLOC_MD_S), /* alloc_mthd */
+    A2D_SBC_IE_MAX_BITPOOL, /* max_bitpool */
+    A2D_SBC_IE_MIN_BITPOOL /* min_bitpool */
+};
+
+#if !defined(BTIF_AV_SBC_DEFAULT_SAMP_FREQ)
+#define BTIF_AV_SBC_DEFAULT_SAMP_FREQ A2D_SBC_IE_SAMP_FREQ_44
+#endif
+
+/* Default SBC codec configuration */
+const tA2D_SBC_CIE btif_av_sbc_default_config =
+{
+    BTIF_AV_SBC_DEFAULT_SAMP_FREQ,   /* samp_freq */
+    A2D_SBC_IE_CH_MD_JOINT,         /* ch_mode */
+    A2D_SBC_IE_BLOCKS_16,           /* block_len */
+    A2D_SBC_IE_SUBBAND_8,           /* num_subbands */
+    A2D_SBC_IE_ALLOC_MD_L,          /* alloc_mthd */
+    BTA_AV_CO_SBC_MAX_BITPOOL,      /* max_bitpool */
+    A2D_SBC_IE_MIN_BITPOOL          /* min_bitpool */
+};
+
+
+/*****************************************************************************
+**  Local data
+*****************************************************************************/
+typedef struct
+{
+    UINT8 sep_info_idx;                 /* local SEP index (in BTA tables) */
+    UINT8 seid;                         /* peer SEP index (in peer tables) */
+    UINT8 codec_type;                   /* peer SEP codec type */
+    UINT8 codec_caps[AVDT_CODEC_SIZE];  /* peer SEP codec capabilities */
+    UINT8 num_protect;                  /* peer SEP number of CP elements */
+    UINT8 protect_info[BTA_AV_CP_INFO_LEN];  /* peer SEP content protection info */
+} tBTA_AV_CO_SINK;
+
+typedef struct
+{
+    BD_ADDR         addr;               /* address of audio/video peer */
+    tBTA_AV_CO_SINK snks[BTIF_SV_AV_AA_SEP_INDEX]; /* array of supported sinks */
+    tBTA_AV_CO_SINK srcs[BTIF_SV_AV_AA_SEP_INDEX]; /* array of supported srcs */
+    UINT8           num_snks;           /* total number of sinks at peer */
+    UINT8           num_srcs;           /* total number of srcs at peer */
+    UINT8           num_seps;           /* total number of seids at peer */
+    UINT8           num_rx_snks;        /* number of received sinks */
+    UINT8           num_rx_srcs;        /* number of received srcs */
+    UINT8           num_sup_snks;       /* number of supported sinks in the snks array */
+    UINT8           num_sup_srcs;       /* number of supported srcs in the srcs array */
+    tBTA_AV_CO_SINK *p_snk;             /* currently selected sink */
+    tBTA_AV_CO_SINK *p_src;             /* currently selected src */
+    UINT8           codec_cfg[AVDT_CODEC_SIZE]; /* current codec configuration */
+    BOOLEAN         cp_active;          /* current CP configuration */
+    BOOLEAN         acp;                /* acceptor */
+    BOOLEAN         recfg_needed;       /* reconfiguration is needed */
+    BOOLEAN         opened;             /* opened */
+    UINT16          mtu;                /* maximum transmit unit size */
+    UINT16          uuid_to_connect;    /* uuid of peer device */
+} tBTA_AV_CO_PEER;
+
+typedef struct
+{
+    BOOLEAN active;
+    UINT8 flag;
+} tBTA_AV_CO_CP;
+
+typedef struct
+{
+    /* Connected peer information */
+    tBTA_AV_CO_PEER peers[BTA_AV_NUM_STRS];
+    /* Current codec configuration - access to this variable must be protected */
+    tBTIF_AV_CODEC_INFO codec_cfg;
+    tBTIF_AV_CODEC_INFO codec_cfg_setconfig; /* remote peer setconfig preference */
+
+    tBTA_AV_CO_CP cp;
+} tBTA_AV_CO_CB;
+
+/* Control block instance */
+static tBTA_AV_CO_CB bta_av_co_cb;
+
+static BOOLEAN bta_av_co_audio_codec_build_config(const UINT8 *p_codec_caps, UINT8 *p_codec_cfg);
+static void bta_av_co_audio_peer_reset_config(tBTA_AV_CO_PEER *p_peer);
+static BOOLEAN bta_av_co_cp_is_scmst(const UINT8 *p_protectinfo);
+static BOOLEAN bta_av_co_audio_sink_has_scmst(const tBTA_AV_CO_SINK *p_sink);
+static BOOLEAN bta_av_co_audio_peer_supports_codec(tBTA_AV_CO_PEER *p_peer, UINT8 *p_snk_index);
+static BOOLEAN bta_av_co_audio_media_supports_config(UINT8 codec_type, const UINT8 *p_codec_cfg);
+static BOOLEAN bta_av_co_audio_sink_supports_config(UINT8 codec_type, const UINT8 *p_codec_cfg);
+static BOOLEAN bta_av_co_audio_peer_src_supports_codec(tBTA_AV_CO_PEER *p_peer, UINT8 *p_src_index);
+
+
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_cp_is_active
+ **
+ ** Description      Get the current configuration of content protection
+ **
+ ** Returns          TRUE if the current streaming has CP, FALSE otherwise
+ **
+ *******************************************************************************/
+BOOLEAN bta_av_co_cp_is_active(void)
+{
+    FUNC_TRACE();
+    return bta_av_co_cb.cp.active;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_cp_get_flag
+ **
+ ** Description      Get content protection flag
+ **                  BTA_AV_CP_SCMS_COPY_NEVER
+ **                  BTA_AV_CP_SCMS_COPY_ONCE
+ **                  BTA_AV_CP_SCMS_COPY_FREE
+ **
+ ** Returns          The current flag value
+ **
+ *******************************************************************************/
+UINT8 bta_av_co_cp_get_flag(void)
+{
+    FUNC_TRACE();
+    return bta_av_co_cb.cp.flag;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_cp_set_flag
+ **
+ ** Description      Set content protection flag
+ **                  BTA_AV_CP_SCMS_COPY_NEVER
+ **                  BTA_AV_CP_SCMS_COPY_ONCE
+ **                  BTA_AV_CP_SCMS_COPY_FREE
+ **
+ ** Returns          TRUE if setting the SCMS flag is supported else FALSE
+ **
+ *******************************************************************************/
+BOOLEAN bta_av_co_cp_set_flag(UINT8 cp_flag)
+{
+    FUNC_TRACE();
+
+#if defined(BTA_AV_CO_CP_SCMS_T) && (BTA_AV_CO_CP_SCMS_T == TRUE)
+#else
+    if (cp_flag != BTA_AV_CP_SCMS_COPY_FREE)
+    {
+        return FALSE;
+    }
+#endif
+    bta_av_co_cb.cp.flag = cp_flag;
+    return TRUE;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_get_peer
+ **
+ ** Description      find the peer entry for a given handle
+ **
+ ** Returns          the control block
+ **
+ *******************************************************************************/
+static tBTA_AV_CO_PEER *bta_av_co_get_peer(tBTA_AV_HNDL hndl)
+{
+    UINT8 index;
+    FUNC_TRACE();
+
+    index = BTA_AV_CO_AUDIO_HNDL_TO_INDX(hndl);
+
+    /* Sanity check */
+    if (index >= BTA_AV_CO_NUM_ELEMENTS(bta_av_co_cb.peers))
+    {
+        APPL_TRACE_ERROR("bta_av_co_get_peer peer index out of bounds:%d", index);
+        return NULL;
+    }
+
+    return &bta_av_co_cb.peers[index];
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_init
+ **
+ ** Description      This callout function is executed by AV when it is
+ **                  started by calling BTA_AvRegister().  This function can be
+ **                  used by the phone to initialize audio paths or for other
+ **                  initialization purposes.
+ **
+ **
+ ** Returns          Stream codec and content protection capabilities info.
+ **
+ *******************************************************************************/
+BOOLEAN bta_av_co_audio_init(UINT8 *p_codec_type, UINT8 *p_codec_info, UINT8 *p_num_protect,
+        UINT8 *p_protect_info, UINT8 index)
+{
+    FUNC_TRACE();
+
+    APPL_TRACE_DEBUG("bta_av_co_audio_init: %d", index);
+
+    /* By default - no content protection info */
+    *p_num_protect = 0;
+    *p_protect_info = 0;
+
+    /* reset remote preference through setconfig */
+    bta_av_co_cb.codec_cfg_setconfig.id = BTIF_AV_CODEC_NONE;
+
+    switch (index)
+    {
+    case BTIF_SV_AV_AA_SBC_INDEX:
+#if defined(BTA_AV_CO_CP_SCMS_T) && (BTA_AV_CO_CP_SCMS_T == TRUE)
+    {
+        UINT8 *p = p_protect_info;
+
+        /* Content protection info - support SCMS-T */
+        *p_num_protect = 1;
+        *p++ = BTA_AV_CP_LOSC;
+        UINT16_TO_STREAM(p, BTA_AV_CP_SCMS_T_ID);
+
+    }
+#endif
+        /* Set up for SBC codec  for SRC*/
+        *p_codec_type = BTA_AV_CODEC_SBC;
+
+        /* This should not fail because we are using constants for parameters */
+        A2D_BldSbcInfo(AVDT_MEDIA_AUDIO, (tA2D_SBC_CIE *) &bta_av_co_sbc_caps, p_codec_info);
+
+        /* Codec is valid */
+        return TRUE;
+#if (BTA_AV_SINK_INCLUDED == TRUE)
+    case BTIF_SV_AV_AA_SBC_SINK_INDEX:
+        *p_codec_type = BTA_AV_CODEC_SBC;
+
+        /* This should not fail because we are using constants for parameters */
+        A2D_BldSbcInfo(AVDT_MEDIA_AUDIO, (tA2D_SBC_CIE *) &bta_av_co_sbc_sink_caps, p_codec_info);
+
+        /* Codec is valid */
+        return TRUE;
+#endif
+    default:
+        /* Not valid */
+        return FALSE;
+    }
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_disc_res
+ **
+ ** Description      This callout function is executed by AV to report the
+ **                  number of stream end points (SEP) were found during the
+ **                  AVDT stream discovery process.
+ **
+ **
+ ** Returns          void.
+ **
+ *******************************************************************************/
+void bta_av_co_audio_disc_res(tBTA_AV_HNDL hndl, UINT8 num_seps, UINT8 num_snk,
+        UINT8 num_src, BD_ADDR addr, UINT16 uuid_local)
+{
+    tBTA_AV_CO_PEER *p_peer;
+
+    FUNC_TRACE();
+
+    APPL_TRACE_DEBUG("bta_av_co_audio_disc_res h:x%x num_seps:%d num_snk:%d num_src:%d",
+            hndl, num_seps, num_snk, num_src);
+
+    /* Find the peer info */
+    p_peer = bta_av_co_get_peer(hndl);
+    if (p_peer == NULL)
+    {
+        APPL_TRACE_ERROR("bta_av_co_audio_disc_res could not find peer entry");
+        return;
+    }
+
+    /* Sanity check : this should never happen */
+    if (p_peer->opened)
+    {
+        APPL_TRACE_ERROR("bta_av_co_audio_disc_res peer already opened");
+    }
+
+    /* Copy the discovery results */
+    bdcpy(p_peer->addr, addr);
+    p_peer->num_snks = num_snk;
+    p_peer->num_srcs = num_src;
+    p_peer->num_seps = num_seps;
+    p_peer->num_rx_snks = 0;
+    p_peer->num_rx_srcs = 0;
+    p_peer->num_sup_snks = 0;
+    if (uuid_local == UUID_SERVCLASS_AUDIO_SINK)
+        p_peer->uuid_to_connect = UUID_SERVCLASS_AUDIO_SOURCE;
+    else if (uuid_local == UUID_SERVCLASS_AUDIO_SOURCE)
+        p_peer->uuid_to_connect = UUID_SERVCLASS_AUDIO_SINK;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_build_src_cfg
+ **
+ ** Description      This function will build preferred config from src capabilities
+ **
+ **
+ ** Returns          Pass or Fail for current getconfig.
+ **
+ *******************************************************************************/
+void bta_av_build_src_cfg (UINT8 *p_pref_cfg, UINT8 *p_src_cap)
+{
+    tA2D_SBC_CIE    src_cap;
+    tA2D_SBC_CIE    pref_cap;
+    UINT8           status = 0;
+
+    /* initialize it to default SBC configuration */
+    A2D_BldSbcInfo(AVDT_MEDIA_AUDIO, (tA2D_SBC_CIE *) &btif_av_sbc_default_config, p_pref_cfg);
+    /* now try to build a preferred one */
+    /* parse configuration */
+    if ((status = A2D_ParsSbcInfo(&src_cap, p_src_cap, TRUE)) != 0)
+    {
+         APPL_TRACE_DEBUG(" Cant parse src cap ret = %d", status);
+         return ;
+    }
+
+    if (src_cap.samp_freq & A2D_SBC_IE_SAMP_FREQ_48)
+        pref_cap.samp_freq = A2D_SBC_IE_SAMP_FREQ_48;
+    else if (src_cap.samp_freq & A2D_SBC_IE_SAMP_FREQ_44)
+        pref_cap.samp_freq = A2D_SBC_IE_SAMP_FREQ_44;
+
+    if (src_cap.ch_mode & A2D_SBC_IE_CH_MD_JOINT)
+        pref_cap.ch_mode = A2D_SBC_IE_CH_MD_JOINT;
+    else if (src_cap.ch_mode & A2D_SBC_IE_CH_MD_STEREO)
+        pref_cap.ch_mode = A2D_SBC_IE_CH_MD_STEREO;
+    else if (src_cap.ch_mode & A2D_SBC_IE_CH_MD_DUAL)
+        pref_cap.ch_mode = A2D_SBC_IE_CH_MD_DUAL;
+    else if (src_cap.ch_mode & A2D_SBC_IE_CH_MD_MONO)
+        pref_cap.ch_mode = A2D_SBC_IE_CH_MD_MONO;
+
+    if (src_cap.block_len & A2D_SBC_IE_BLOCKS_16)
+        pref_cap.block_len = A2D_SBC_IE_BLOCKS_16;
+    else if (src_cap.block_len & A2D_SBC_IE_BLOCKS_12)
+        pref_cap.block_len = A2D_SBC_IE_BLOCKS_12;
+    else if (src_cap.block_len & A2D_SBC_IE_BLOCKS_8)
+        pref_cap.block_len = A2D_SBC_IE_BLOCKS_8;
+    else if (src_cap.block_len & A2D_SBC_IE_BLOCKS_4)
+        pref_cap.block_len = A2D_SBC_IE_BLOCKS_4;
+
+    if (src_cap.num_subbands & A2D_SBC_IE_SUBBAND_8)
+        pref_cap.num_subbands = A2D_SBC_IE_SUBBAND_8;
+    else if(src_cap.num_subbands & A2D_SBC_IE_SUBBAND_4)
+        pref_cap.num_subbands = A2D_SBC_IE_SUBBAND_4;
+
+    if (src_cap.alloc_mthd & A2D_SBC_IE_ALLOC_MD_L)
+        pref_cap.alloc_mthd = A2D_SBC_IE_ALLOC_MD_L;
+    else if(src_cap.alloc_mthd & A2D_SBC_IE_ALLOC_MD_S)
+        pref_cap.alloc_mthd = A2D_SBC_IE_ALLOC_MD_S;
+
+    pref_cap.max_bitpool = src_cap.max_bitpool;
+    pref_cap.min_bitpool = src_cap.min_bitpool;
+
+    A2D_BldSbcInfo(AVDT_MEDIA_AUDIO, (tA2D_SBC_CIE *) &pref_cap, p_pref_cfg);
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_audio_sink_getconfig
+ **
+ ** Description      This callout function is executed by AV to retrieve the
+ **                  desired codec and content protection configuration for the
+ **                  A2DP Sink audio stream in Initiator.
+ **
+ **
+ ** Returns          Pass or Fail for current getconfig.
+ **
+ *******************************************************************************/
+UINT8 bta_av_audio_sink_getconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+        UINT8 *p_codec_info, UINT8 *p_sep_info_idx, UINT8 seid, UINT8 *p_num_protect,
+        UINT8 *p_protect_info)
+{
+
+    UINT8 result = A2D_FAIL;
+    BOOLEAN supported;
+    tBTA_AV_CO_PEER *p_peer;
+    tBTA_AV_CO_SINK *p_src;
+    UINT8 pref_cfg[AVDT_CODEC_SIZE];
+    UINT8 index;
+
+    FUNC_TRACE();
+
+    APPL_TRACE_DEBUG("bta_av_audio_sink_getconfig handle:0x%x codec_type:%d seid:%d",
+                                                               hndl, codec_type, seid);
+    APPL_TRACE_DEBUG("num_protect:0x%02x protect_info:0x%02x%02x%02x",
+        *p_num_protect, p_protect_info[0], p_protect_info[1], p_protect_info[2]);
+
+    /* Retrieve the peer info */
+    p_peer = bta_av_co_get_peer(hndl);
+    if (p_peer == NULL)
+    {
+        APPL_TRACE_ERROR("bta_av_audio_sink_getconfig could not find peer entry");
+        return A2D_FAIL;
+    }
+
+    APPL_TRACE_DEBUG("bta_av_audio_sink_getconfig peer(o=%d,n_snks=%d,n_rx_snks=%d,n_sup_snks=%d)",
+            p_peer->opened, p_peer->num_srcs, p_peer->num_rx_srcs, p_peer->num_sup_srcs);
+
+    p_peer->num_rx_srcs++;
+
+    /* Check if this is a supported configuration */
+    supported = FALSE;
+    switch (codec_type)
+    {
+        case BTA_AV_CODEC_SBC:
+            supported = TRUE;
+            break;
+
+        default:
+            break;
+    }
+
+    if (supported)
+    {
+        /* If there is room for a new one */
+        if (p_peer->num_sup_srcs < BTA_AV_CO_NUM_ELEMENTS(p_peer->srcs))
+        {
+            p_src = &p_peer->srcs[p_peer->num_sup_srcs++];
+
+            APPL_TRACE_DEBUG("bta_av_audio_sink_getconfig saved caps[%x:%x:%x:%x:%x:%x]",
+                    p_codec_info[1], p_codec_info[2], p_codec_info[3],
+                    p_codec_info[4], p_codec_info[5], p_codec_info[6]);
+
+            memcpy(p_src->codec_caps, p_codec_info, AVDT_CODEC_SIZE);
+            p_src->codec_type = codec_type;
+            p_src->sep_info_idx = *p_sep_info_idx;
+            p_src->seid = seid;
+            p_src->num_protect = *p_num_protect;
+            memcpy(p_src->protect_info, p_protect_info, BTA_AV_CP_INFO_LEN);
+        }
+        else
+        {
+            APPL_TRACE_ERROR("bta_av_audio_sink_getconfig no more room for SRC info");
+        }
+    }
+
+    /* If last SNK get capabilities or all supported codec caps retrieved */
+    if ((p_peer->num_rx_srcs == p_peer->num_srcs) ||
+        (p_peer->num_sup_srcs == BTA_AV_CO_NUM_ELEMENTS(p_peer->srcs)))
+    {
+        APPL_TRACE_DEBUG("bta_av_audio_sink_getconfig last SRC reached");
+
+        /* Protect access to bta_av_co_cb.codec_cfg */
+        GKI_disable();
+
+        /* Find a src that matches the codec config */
+        if (bta_av_co_audio_peer_src_supports_codec(p_peer, &index))
+        {
+            APPL_TRACE_DEBUG(" Codec Supported ");
+            p_src = &p_peer->srcs[index];
+
+            /* Build the codec configuration for this sink */
+            {
+                /* Save the new configuration */
+                p_peer->p_src = p_src;
+                /* get preferred config from src_caps */
+                bta_av_build_src_cfg(pref_cfg, p_src->codec_caps);
+                memcpy(p_peer->codec_cfg, pref_cfg, AVDT_CODEC_SIZE);
+
+                APPL_TRACE_DEBUG("bta_av_audio_sink_getconfig  p_codec_info[%x:%x:%x:%x:%x:%x]",
+                        p_peer->codec_cfg[1], p_peer->codec_cfg[2], p_peer->codec_cfg[3],
+                        p_peer->codec_cfg[4], p_peer->codec_cfg[5], p_peer->codec_cfg[6]);
+                /* By default, no content protection */
+                *p_num_protect = 0;
+
+#if defined(BTA_AV_CO_CP_SCMS_T) && (BTA_AV_CO_CP_SCMS_T == TRUE)
+                    p_peer->cp_active = FALSE;
+                    bta_av_co_cb.cp.active = FALSE;
+#endif
+
+                    *p_sep_info_idx = p_src->sep_info_idx;
+                    memcpy(p_codec_info, p_peer->codec_cfg, AVDT_CODEC_SIZE);
+                result =  A2D_SUCCESS;
+            }
+        }
+        /* Protect access to bta_av_co_cb.codec_cfg */
+        GKI_enable();
+    }
+    return result;
+}
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_getconfig
+ **
+ ** Description      This callout function is executed by AV to retrieve the
+ **                  desired codec and content protection configuration for the
+ **                  audio stream.
+ **
+ **
+ ** Returns          Stream codec and content protection configuration info.
+ **
+ *******************************************************************************/
+UINT8 bta_av_co_audio_getconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                UINT8 *p_codec_info, UINT8 *p_sep_info_idx, UINT8 seid, UINT8 *p_num_protect,
+                                UINT8 *p_protect_info)
+
+{
+    UINT8 result = A2D_FAIL;
+    BOOLEAN supported;
+    tBTA_AV_CO_PEER *p_peer;
+    tBTA_AV_CO_SINK *p_sink;
+    UINT8 codec_cfg[AVDT_CODEC_SIZE];
+    UINT8 index;
+
+    FUNC_TRACE();
+
+    /* Retrieve the peer info */
+    p_peer = bta_av_co_get_peer(hndl);
+    if (p_peer == NULL)
+    {
+        APPL_TRACE_ERROR("bta_av_co_audio_getconfig could not find peer entry");
+        return A2D_FAIL;
+    }
+
+    if (p_peer->uuid_to_connect == UUID_SERVCLASS_AUDIO_SOURCE)
+    {
+        result = bta_av_audio_sink_getconfig(hndl, codec_type, p_codec_info, p_sep_info_idx,
+                                             seid, p_num_protect, p_protect_info);
+        return result;
+    }
+    APPL_TRACE_DEBUG("bta_av_co_audio_getconfig handle:0x%x codec_type:%d seid:%d",
+                                                              hndl, codec_type, seid);
+    APPL_TRACE_DEBUG("num_protect:0x%02x protect_info:0x%02x%02x%02x",
+        *p_num_protect, p_protect_info[0], p_protect_info[1], p_protect_info[2]);
+
+    APPL_TRACE_DEBUG("bta_av_co_audio_getconfig peer(o=%d,n_snks=%d,n_rx_snks=%d,n_sup_snks=%d)",
+            p_peer->opened, p_peer->num_snks, p_peer->num_rx_snks, p_peer->num_sup_snks);
+
+    p_peer->num_rx_snks++;
+
+    /* Check if this is a supported configuration */
+    supported = FALSE;
+    switch (codec_type)
+    {
+    case BTA_AV_CODEC_SBC:
+        supported = TRUE;
+        break;
+
+    default:
+        break;
+    }
+
+    if (supported)
+    {
+        /* If there is room for a new one */
+        if (p_peer->num_sup_snks < BTA_AV_CO_NUM_ELEMENTS(p_peer->snks))
+        {
+            p_sink = &p_peer->snks[p_peer->num_sup_snks++];
+
+            APPL_TRACE_DEBUG("bta_av_co_audio_getconfig saved caps[%x:%x:%x:%x:%x:%x]",
+                    p_codec_info[1], p_codec_info[2], p_codec_info[3],
+                    p_codec_info[4], p_codec_info[5], p_codec_info[6]);
+
+            memcpy(p_sink->codec_caps, p_codec_info, AVDT_CODEC_SIZE);
+            p_sink->codec_type = codec_type;
+            p_sink->sep_info_idx = *p_sep_info_idx;
+            p_sink->seid = seid;
+            p_sink->num_protect = *p_num_protect;
+            memcpy(p_sink->protect_info, p_protect_info, BTA_AV_CP_INFO_LEN);
+        }
+        else
+        {
+            APPL_TRACE_ERROR("bta_av_co_audio_getconfig no more room for SNK info");
+        }
+    }
+
+    /* If last SNK get capabilities or all supported codec capa retrieved */
+    if ((p_peer->num_rx_snks == p_peer->num_snks) ||
+        (p_peer->num_sup_snks == BTA_AV_CO_NUM_ELEMENTS(p_peer->snks)))
+    {
+        APPL_TRACE_DEBUG("bta_av_co_audio_getconfig last sink reached");
+
+        /* Protect access to bta_av_co_cb.codec_cfg */
+        GKI_disable();
+
+        /* Find a sink that matches the codec config */
+        if (bta_av_co_audio_peer_supports_codec(p_peer, &index))
+        {
+            /* stop fetching caps once we retrieved a supported codec */
+            if (p_peer->acp)
+            {
+                *p_sep_info_idx = p_peer->num_seps;
+                APPL_TRACE_EVENT("no need to fetch more SEPs");
+            }
+
+            p_sink = &p_peer->snks[index];
+
+            /* Build the codec configuration for this sink */
+            if (bta_av_co_audio_codec_build_config(p_sink->codec_caps, codec_cfg))
+            {
+                APPL_TRACE_DEBUG("bta_av_co_audio_getconfig reconfig p_codec_info[%x:%x:%x:%x:%x:%x]",
+                        codec_cfg[1], codec_cfg[2], codec_cfg[3],
+                        codec_cfg[4], codec_cfg[5], codec_cfg[6]);
+
+                /* Save the new configuration */
+                p_peer->p_snk = p_sink;
+                memcpy(p_peer->codec_cfg, codec_cfg, AVDT_CODEC_SIZE);
+
+                /* By default, no content protection */
+                *p_num_protect = 0;
+
+#if defined(BTA_AV_CO_CP_SCMS_T) && (BTA_AV_CO_CP_SCMS_T == TRUE)
+                /* Check if this sink supports SCMS */
+                if (bta_av_co_audio_sink_has_scmst(p_sink))
+                {
+                    p_peer->cp_active = TRUE;
+                    bta_av_co_cb.cp.active = TRUE;
+                    *p_num_protect = BTA_AV_CP_INFO_LEN;
+                    memcpy(p_protect_info, bta_av_co_cp_scmst, BTA_AV_CP_INFO_LEN);
+                }
+                else
+                {
+                    p_peer->cp_active = FALSE;
+                    bta_av_co_cb.cp.active = FALSE;
+                }
+#endif
+
+                /* If acceptor -> reconfig otherwise reply for configuration */
+                if (p_peer->acp)
+                {
+                    if (p_peer->recfg_needed)
+                    {
+                        APPL_TRACE_DEBUG("bta_av_co_audio_getconfig call BTA_AvReconfig(x%x)", hndl);
+                        BTA_AvReconfig(hndl, TRUE, p_sink->sep_info_idx, p_peer->codec_cfg, *p_num_protect, (UINT8 *)bta_av_co_cp_scmst);
+                    }
+                }
+                else
+                {
+                    *p_sep_info_idx = p_sink->sep_info_idx;
+                    memcpy(p_codec_info, p_peer->codec_cfg, AVDT_CODEC_SIZE);
+                }
+                result =  A2D_SUCCESS;
+            }
+        }
+        /* Protect access to bta_av_co_cb.codec_cfg */
+        GKI_enable();
+    }
+    return result;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_setconfig
+ **
+ ** Description      This callout function is executed by AV to set the codec and
+ **                  content protection configuration of the audio stream.
+ **
+ **
+ ** Returns          void
+ **
+ *******************************************************************************/
+void bta_av_co_audio_setconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+        UINT8 *p_codec_info, UINT8 seid, BD_ADDR addr, UINT8 num_protect, UINT8 *p_protect_info,
+        UINT8 t_local_sep, UINT8 avdt_handle)
+{
+    tBTA_AV_CO_PEER *p_peer;
+    UINT8 status = A2D_SUCCESS;
+    UINT8 category = A2D_SUCCESS;
+    BOOLEAN recfg_needed = FALSE;
+    BOOLEAN codec_cfg_supported = FALSE;
+    UNUSED(seid);
+    UNUSED(addr);
+
+    FUNC_TRACE();
+
+    APPL_TRACE_DEBUG("bta_av_co_audio_setconfig p_codec_info[%x:%x:%x:%x:%x:%x]",
+            p_codec_info[1], p_codec_info[2], p_codec_info[3],
+            p_codec_info[4], p_codec_info[5], p_codec_info[6]);
+    APPL_TRACE_DEBUG("num_protect:0x%02x protect_info:0x%02x%02x%02x",
+        num_protect, p_protect_info[0], p_protect_info[1], p_protect_info[2]);
+
+    /* Retrieve the peer info */
+    p_peer = bta_av_co_get_peer(hndl);
+    if (p_peer == NULL)
+    {
+        APPL_TRACE_ERROR("bta_av_co_audio_setconfig could not find peer entry");
+
+        /* Call call-in rejecting the configuration */
+        bta_av_ci_setconfig(hndl, A2D_BUSY, AVDT_ASC_CODEC, 0, NULL, FALSE, avdt_handle);
+        return;
+    }
+    APPL_TRACE_DEBUG("bta_av_co_audio_setconfig peer(o=%d,n_snks=%d,n_rx_snks=%d,n_sup_snks=%d)",
+            p_peer->opened, p_peer->num_snks, p_peer->num_rx_snks, p_peer->num_sup_snks);
+
+    /* Sanity check: should not be opened at this point */
+    if (p_peer->opened)
+    {
+        APPL_TRACE_ERROR("bta_av_co_audio_setconfig peer already in use");
+    }
+
+#if defined(BTA_AV_CO_CP_SCMS_T) && (BTA_AV_CO_CP_SCMS_T == TRUE)
+    if (num_protect != 0)
+    {
+        /* If CP is supported */
+        if ((num_protect != 1) ||
+            (bta_av_co_cp_is_scmst(p_protect_info) == FALSE))
+        {
+            APPL_TRACE_ERROR("bta_av_co_audio_setconfig wrong CP configuration");
+            status = A2D_BAD_CP_TYPE;
+            category = AVDT_ASC_PROTECT;
+        }
+    }
+#else
+    /* Do not support content protection for the time being */
+    if (num_protect != 0)
+    {
+        APPL_TRACE_ERROR("bta_av_co_audio_setconfig wrong CP configuration");
+        status = A2D_BAD_CP_TYPE;
+        category = AVDT_ASC_PROTECT;
+    }
+#endif
+    if (status == A2D_SUCCESS)
+    {
+        if(AVDT_TSEP_SNK == t_local_sep)
+        {
+            codec_cfg_supported = bta_av_co_audio_sink_supports_config(codec_type, p_codec_info);
+            APPL_TRACE_DEBUG(" Peer is  A2DP SRC ");
+        }
+        if(AVDT_TSEP_SRC == t_local_sep)
+        {
+            codec_cfg_supported = bta_av_co_audio_media_supports_config(codec_type, p_codec_info);
+            APPL_TRACE_DEBUG(" Peer is A2DP SINK ");
+        }
+        /* Check if codec configuration is supported */
+        if (codec_cfg_supported)
+        {
+
+            /* Protect access to bta_av_co_cb.codec_cfg */
+            GKI_disable();
+
+            /* Check if the configuration matches the current codec config */
+            switch (bta_av_co_cb.codec_cfg.id)
+            {
+            case BTIF_AV_CODEC_SBC:
+                if ((codec_type != BTA_AV_CODEC_SBC) || memcmp(p_codec_info, bta_av_co_cb.codec_cfg.info, 5))
+                {
+                    recfg_needed = TRUE;
+                }
+                else if ((num_protect == 1) && (!bta_av_co_cb.cp.active))
+                {
+                    recfg_needed = TRUE;
+                }
+
+                /* if remote side requests a restricted notify sinks preferred bitpool range as all other params are
+                   already checked for validify */
+                APPL_TRACE_EVENT("remote peer setconfig bitpool range [%d:%d]",
+                   p_codec_info[BTA_AV_CO_SBC_MIN_BITPOOL_OFF],
+                   p_codec_info[BTA_AV_CO_SBC_MAX_BITPOOL_OFF] );
+
+                bta_av_co_cb.codec_cfg_setconfig.id = BTIF_AV_CODEC_SBC;
+                memcpy(bta_av_co_cb.codec_cfg_setconfig.info, p_codec_info, AVDT_CODEC_SIZE);
+                if(AVDT_TSEP_SNK == t_local_sep)
+                {
+                    /* If Peer is SRC, and our cfg subset matches with what is requested by peer, then
+                                         just accept what peer wants */
+                    memcpy(bta_av_co_cb.codec_cfg.info, p_codec_info, AVDT_CODEC_SIZE);
+                    recfg_needed = FALSE;
+                }
+                break;
+
+
+            default:
+                APPL_TRACE_ERROR("bta_av_co_audio_setconfig unsupported cid %d", bta_av_co_cb.codec_cfg.id);
+                recfg_needed = TRUE;
+                break;
+            }
+            /* Protect access to bta_av_co_cb.codec_cfg */
+            GKI_enable();
+        }
+        else
+        {
+            category = AVDT_ASC_CODEC;
+            status = A2D_WRONG_CODEC;
+        }
+    }
+
+    if (status != A2D_SUCCESS)
+    {
+        APPL_TRACE_DEBUG("bta_av_co_audio_setconfig reject s=%d c=%d", status, category);
+
+        /* Call call-in rejecting the configuration */
+        bta_av_ci_setconfig(hndl, status, category, 0, NULL, FALSE, avdt_handle);
+    }
+    else
+    {
+        /* Mark that this is an acceptor peer */
+        p_peer->acp = TRUE;
+        p_peer->recfg_needed = recfg_needed;
+
+        APPL_TRACE_DEBUG("bta_av_co_audio_setconfig accept reconf=%d", recfg_needed);
+
+        /* Call call-in accepting the configuration */
+        bta_av_ci_setconfig(hndl, A2D_SUCCESS, A2D_SUCCESS, 0, NULL, recfg_needed, avdt_handle);
+    }
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_open
+ **
+ ** Description      This function is called by AV when the audio stream connection
+ **                  is opened.
+ **
+ **
+ ** Returns          void
+ **
+ *******************************************************************************/
+void bta_av_co_audio_open(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type, UINT8 *p_codec_info,
+                          UINT16 mtu)
+{
+    tBTA_AV_CO_PEER *p_peer;
+    UNUSED(p_codec_info);
+
+    FUNC_TRACE();
+
+    APPL_TRACE_DEBUG("bta_av_co_audio_open mtu:%d codec_type:%d", mtu, codec_type);
+
+    /* Retrieve the peer info */
+    p_peer = bta_av_co_get_peer(hndl);
+    if (p_peer == NULL)
+    {
+        APPL_TRACE_ERROR("bta_av_co_audio_setconfig could not find peer entry");
+    }
+    else
+    {
+        p_peer->opened = TRUE;
+        p_peer->mtu = mtu;
+    }
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_close
+ **
+ ** Description      This function is called by AV when the audio stream connection
+ **                  is closed.
+ **
+ **
+ ** Returns          void
+ **
+ *******************************************************************************/
+void bta_av_co_audio_close(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type, UINT16 mtu)
+
+{
+    tBTA_AV_CO_PEER *p_peer;
+    UNUSED(codec_type);
+    UNUSED(mtu);
+
+    FUNC_TRACE();
+
+    APPL_TRACE_DEBUG("bta_av_co_audio_close");
+
+    /* Retrieve the peer info */
+    p_peer = bta_av_co_get_peer(hndl);
+    if (p_peer)
+    {
+        /* Mark the peer closed and clean the peer info */
+        memset(p_peer, 0, sizeof(*p_peer));
+    }
+    else
+    {
+        APPL_TRACE_ERROR("bta_av_co_audio_close could not find peer entry");
+    }
+
+    /* reset remote preference through setconfig */
+    bta_av_co_cb.codec_cfg_setconfig.id = BTIF_AV_CODEC_NONE;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_start
+ **
+ ** Description      This function is called by AV when the audio streaming data
+ **                  transfer is started.
+ **
+ **
+ ** Returns          void
+ **
+ *******************************************************************************/
+void bta_av_co_audio_start(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                           UINT8 *p_codec_info, BOOLEAN *p_no_rtp_hdr)
+{
+    UNUSED(hndl);
+    UNUSED(codec_type);
+    UNUSED(p_codec_info);
+    UNUSED(p_no_rtp_hdr);
+
+    FUNC_TRACE();
+
+    APPL_TRACE_DEBUG("bta_av_co_audio_start");
+
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_stop
+ **
+ ** Description      This function is called by AV when the audio streaming data
+ **                  transfer is stopped.
+ **
+ **
+ ** Returns          void
+ **
+ *******************************************************************************/
+extern void bta_av_co_audio_stop(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type)
+{
+    UNUSED(hndl);
+    UNUSED(codec_type);
+
+    FUNC_TRACE();
+
+    APPL_TRACE_DEBUG("bta_av_co_audio_stop");
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_src_data_path
+ **
+ ** Description      This function is called to manage data transfer from
+ **                  the audio codec to AVDTP.
+ **
+ ** Returns          Pointer to the GKI buffer to send, NULL if no buffer to send
+ **
+ *******************************************************************************/
+void * bta_av_co_audio_src_data_path(tBTA_AV_CODEC codec_type, UINT32 *p_len,
+                                     UINT32 *p_timestamp)
+{
+    BT_HDR *p_buf;
+    UNUSED(p_len);
+
+    FUNC_TRACE();
+
+    p_buf = btif_media_aa_readbuf();
+    if (p_buf != NULL)
+    {
+        switch (codec_type)
+        {
+        case BTA_AV_CODEC_SBC:
+            /* In media packet SBC, the following information is available:
+             * p_buf->layer_specific : number of SBC frames in the packet
+             * p_buf->word[0] : timestamp
+             */
+            /* Retrieve the timestamp information from the media packet */
+            *p_timestamp = *((UINT32 *) (p_buf + 1));
+
+            /* Set up packet header */
+            bta_av_sbc_bld_hdr(p_buf, p_buf->layer_specific);
+            break;
+
+
+        default:
+            APPL_TRACE_ERROR("bta_av_co_audio_src_data_path Unsupported codec type (%d)", codec_type);
+            break;
+        }
+#if defined(BTA_AV_CO_CP_SCMS_T) && (BTA_AV_CO_CP_SCMS_T == TRUE)
+        {
+            UINT8 *p;
+            if (bta_av_co_cp_is_active())
+            {
+                p_buf->len++;
+                p_buf->offset--;
+                p = (UINT8 *)(p_buf + 1) + p_buf->offset;
+                *p = bta_av_co_cp_get_flag();
+            }
+        }
+#endif
+    }
+    return p_buf;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_drop
+ **
+ ** Description      An Audio packet is dropped. .
+ **                  It's very likely that the connected headset with this handle
+ **                  is moved far away. The implementation may want to reduce
+ **                  the encoder bit rate setting to reduce the packet size.
+ **
+ ** Returns          void
+ **
+ *******************************************************************************/
+void bta_av_co_audio_drop(tBTA_AV_HNDL hndl)
+{
+    FUNC_TRACE();
+
+    APPL_TRACE_ERROR("bta_av_co_audio_drop dropped: x%x", hndl);
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_delay
+ **
+ ** Description      This function is called by AV when the audio stream connection
+ **                  needs to send the initial delay report to the connected SRC.
+ **
+ **
+ ** Returns          void
+ **
+ *******************************************************************************/
+void bta_av_co_audio_delay(tBTA_AV_HNDL hndl, UINT16 delay)
+{
+    FUNC_TRACE();
+
+    APPL_TRACE_ERROR("bta_av_co_audio_delay handle: x%x, delay:0x%x", hndl, delay);
+}
+
+
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_codec_build_config
+ **
+ ** Description      Build the codec configuration
+ **
+ ** Returns          TRUE if the codec was built successfully, FALSE otherwise
+ **
+ *******************************************************************************/
+static BOOLEAN bta_av_co_audio_codec_build_config(const UINT8 *p_codec_caps, UINT8 *p_codec_cfg)
+{
+    FUNC_TRACE();
+
+    memset(p_codec_cfg, 0, AVDT_CODEC_SIZE);
+
+    switch (bta_av_co_cb.codec_cfg.id)
+    {
+    case BTIF_AV_CODEC_SBC:
+        /*  only copy the relevant portions for this codec to avoid issues when
+            comparing codec configs covering larger codec sets than SBC (7 bytes) */
+        memcpy(p_codec_cfg, bta_av_co_cb.codec_cfg.info, BTA_AV_CO_SBC_MAX_BITPOOL_OFF+1);
+
+        /* Update the bit pool boundaries with the codec capabilities */
+        p_codec_cfg[BTA_AV_CO_SBC_MIN_BITPOOL_OFF] = p_codec_caps[BTA_AV_CO_SBC_MIN_BITPOOL_OFF];
+        p_codec_cfg[BTA_AV_CO_SBC_MAX_BITPOOL_OFF] = p_codec_caps[BTA_AV_CO_SBC_MAX_BITPOOL_OFF];
+
+        APPL_TRACE_EVENT("bta_av_co_audio_codec_build_config : bitpool min %d, max %d",
+                    p_codec_cfg[BTA_AV_CO_SBC_MIN_BITPOOL_OFF],
+                    p_codec_caps[BTA_AV_CO_SBC_MAX_BITPOOL_OFF]);
+        break;
+    default:
+        APPL_TRACE_ERROR("bta_av_co_audio_codec_build_config: unsupported codec id %d", bta_av_co_cb.codec_cfg.id);
+        return FALSE;
+        break;
+    }
+    return TRUE;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_codec_cfg_matches_caps
+ **
+ ** Description      Check if a codec config matches a codec capabilities
+ **
+ ** Returns          TRUE if it codec config is supported, FALSE otherwise
+ **
+ *******************************************************************************/
+static BOOLEAN bta_av_co_audio_codec_cfg_matches_caps(UINT8 codec_id, const UINT8 *p_codec_caps, const UINT8 *p_codec_cfg)
+{
+    FUNC_TRACE();
+
+    switch(codec_id)
+    {
+    case BTIF_AV_CODEC_SBC:
+
+        APPL_TRACE_EVENT("bta_av_co_audio_codec_cfg_matches_caps : min %d/%d max %d/%d",
+           p_codec_caps[BTA_AV_CO_SBC_MIN_BITPOOL_OFF],
+           p_codec_cfg[BTA_AV_CO_SBC_MIN_BITPOOL_OFF],
+           p_codec_caps[BTA_AV_CO_SBC_MAX_BITPOOL_OFF],
+           p_codec_cfg[BTA_AV_CO_SBC_MAX_BITPOOL_OFF]);
+
+        /* Must match all items exactly except bitpool boundaries which can be adjusted */
+        if (!((p_codec_caps[BTA_AV_CO_SBC_FREQ_CHAN_OFF] & p_codec_cfg[BTA_AV_CO_SBC_FREQ_CHAN_OFF]) &&
+              (p_codec_caps[BTA_AV_CO_SBC_BLOCK_BAND_OFF] & p_codec_cfg[BTA_AV_CO_SBC_BLOCK_BAND_OFF])))
+        {
+            APPL_TRACE_EVENT("FALSE %x %x %x %x",
+                    p_codec_caps[BTA_AV_CO_SBC_FREQ_CHAN_OFF],
+                    p_codec_cfg[BTA_AV_CO_SBC_FREQ_CHAN_OFF],
+                    p_codec_caps[BTA_AV_CO_SBC_BLOCK_BAND_OFF],
+                    p_codec_cfg[BTA_AV_CO_SBC_BLOCK_BAND_OFF]);
+            return FALSE;
+        }
+        break;
+
+
+    default:
+        APPL_TRACE_ERROR("bta_av_co_audio_codec_cfg_matches_caps: unsupported codec id %d", codec_id);
+        return FALSE;
+        break;
+    }
+    APPL_TRACE_EVENT("TRUE");
+
+    return TRUE;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_codec_match
+ **
+ ** Description      Check if a codec capabilities supports the codec config
+ **
+ ** Returns          TRUE if the connection supports this codec, FALSE otherwise
+ **
+ *******************************************************************************/
+static BOOLEAN bta_av_co_audio_codec_match(const UINT8 *p_codec_caps)
+{
+    FUNC_TRACE();
+
+    return bta_av_co_audio_codec_cfg_matches_caps(bta_av_co_cb.codec_cfg.id, p_codec_caps, bta_av_co_cb.codec_cfg.info);
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_peer_reset_config
+ **
+ ** Description      Reset the peer codec configuration
+ **
+ ** Returns          Nothing
+ **
+ *******************************************************************************/
+static void bta_av_co_audio_peer_reset_config(tBTA_AV_CO_PEER *p_peer)
+{
+    FUNC_TRACE();
+
+    /* Indicate that there is no currently selected sink */
+    p_peer->p_snk = NULL;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_cp_is_scmst
+ **
+ ** Description      Check if a content protection service is SCMS-T
+ **
+ ** Returns          TRUE if this CP is SCMS-T, FALSE otherwise
+ **
+ *******************************************************************************/
+static BOOLEAN bta_av_co_cp_is_scmst(const UINT8 *p_protectinfo)
+{
+    UINT16 cp_id;
+    FUNC_TRACE();
+
+    if (*p_protectinfo >= BTA_AV_CP_LOSC)
+    {
+        p_protectinfo++;
+        STREAM_TO_UINT16(cp_id, p_protectinfo);
+        if (cp_id == BTA_AV_CP_SCMS_T_ID)
+        {
+            APPL_TRACE_DEBUG("bta_av_co_cp_is_scmst: SCMS-T found");
+            return TRUE;
+        }
+    }
+
+    return FALSE;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_sink_has_scmst
+ **
+ ** Description      Check if a sink supports SCMS-T
+ **
+ ** Returns          TRUE if the sink supports this CP, FALSE otherwise
+ **
+ *******************************************************************************/
+static BOOLEAN bta_av_co_audio_sink_has_scmst(const tBTA_AV_CO_SINK *p_sink)
+{
+    UINT8 index;
+    const UINT8 *p;
+    FUNC_TRACE();
+
+    /* Check if sink supports SCMS-T */
+    index = p_sink->num_protect;
+    p = &p_sink->protect_info[0];
+
+    while (index)
+    {
+        if (bta_av_co_cp_is_scmst(p))
+        {
+            return TRUE;
+        }
+        /* Move to the next SC */
+        p += *p + 1;
+        /* Decrement the SC counter */
+        index--;
+    }
+    APPL_TRACE_DEBUG("bta_av_co_audio_sink_has_scmst: SCMS-T not found");
+    return FALSE;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_sink_supports_cp
+ **
+ ** Description      Check if a sink supports the current content protection
+ **
+ ** Returns          TRUE if the sink supports this CP, FALSE otherwise
+ **
+ *******************************************************************************/
+static BOOLEAN bta_av_co_audio_sink_supports_cp(const tBTA_AV_CO_SINK *p_sink)
+{
+    FUNC_TRACE();
+
+    /* Check if content protection is enabled for this stream */
+    if (bta_av_co_cp_get_flag() != BTA_AV_CP_SCMS_COPY_FREE)
+    {
+        return bta_av_co_audio_sink_has_scmst(p_sink);
+    }
+    else
+    {
+        APPL_TRACE_DEBUG("bta_av_co_audio_sink_supports_cp: not required");
+        return TRUE;
+    }
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_peer_supports_codec
+ **
+ ** Description      Check if a connection supports the codec config
+ **
+ ** Returns          TRUE if the connection supports this codec, FALSE otherwise
+ **
+ *******************************************************************************/
+static BOOLEAN bta_av_co_audio_peer_supports_codec(tBTA_AV_CO_PEER *p_peer, UINT8 *p_snk_index)
+{
+    int index;
+    UINT8 codec_type;
+    FUNC_TRACE();
+
+    /* Configure the codec type to look for */
+    codec_type = bta_av_co_cb.codec_cfg.id;
+
+
+    for (index = 0; index < p_peer->num_sup_snks; index++)
+    {
+        if (p_peer->snks[index].codec_type == codec_type)
+        {
+            switch (bta_av_co_cb.codec_cfg.id)
+            {
+            case BTIF_AV_CODEC_SBC:
+                if (p_snk_index) *p_snk_index = index;
+                return bta_av_co_audio_codec_match(p_peer->snks[index].codec_caps);
+                break;
+
+
+            default:
+                APPL_TRACE_ERROR("bta_av_co_audio_peer_supports_codec: unsupported codec id %d", bta_av_co_cb.codec_cfg.id);
+                return FALSE;
+                break;
+            }
+        }
+    }
+    return FALSE;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_peer_src_supports_codec
+ **
+ ** Description      Check if a peer acting as src supports codec config
+ **
+ ** Returns          TRUE if the connection supports this codec, FALSE otherwise
+ **
+ *******************************************************************************/
+static BOOLEAN bta_av_co_audio_peer_src_supports_codec(tBTA_AV_CO_PEER *p_peer, UINT8 *p_src_index)
+{
+    int index;
+    UINT8 codec_type;
+    FUNC_TRACE();
+
+    /* Configure the codec type to look for */
+    codec_type = bta_av_co_cb.codec_cfg.id;
+
+
+    for (index = 0; index < p_peer->num_sup_srcs; index++)
+    {
+        if (p_peer->srcs[index].codec_type == codec_type)
+        {
+            switch (bta_av_co_cb.codec_cfg.id)
+            {
+            case BTIF_AV_CODEC_SBC:
+                if (p_src_index) *p_src_index = index;
+                if (0 ==  bta_av_sbc_cfg_matches_cap((UINT8 *)p_peer->srcs[index].codec_caps,
+                                                     (tA2D_SBC_CIE *)&bta_av_co_sbc_sink_caps))
+                {
+                    return TRUE;
+                }
+                break;
+
+            default:
+                APPL_TRACE_ERROR("peer_src_supports_codec: unsupported codec id %d",
+                                                            bta_av_co_cb.codec_cfg.id);
+                return FALSE;
+                break;
+            }
+        }
+    }
+    return FALSE;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_sink_supports_config
+ **
+ ** Description      Check if the media source supports a given configuration
+ **
+ ** Returns          TRUE if the media source supports this config, FALSE otherwise
+ **
+ *******************************************************************************/
+static BOOLEAN bta_av_co_audio_sink_supports_config(UINT8 codec_type, const UINT8 *p_codec_cfg)
+{
+    FUNC_TRACE();
+
+    switch (codec_type)
+    {
+    case BTA_AV_CODEC_SBC:
+        if (bta_av_sbc_cfg_in_cap((UINT8 *)p_codec_cfg, (tA2D_SBC_CIE *)&bta_av_co_sbc_sink_caps))
+        {
+            return FALSE;
+        }
+        break;
+
+
+    default:
+        APPL_TRACE_ERROR("bta_av_co_audio_media_supports_config unsupported codec type %d", codec_type);
+        return FALSE;
+        break;
+    }
+    return TRUE;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_media_supports_config
+ **
+ ** Description      Check if the media sink supports a given configuration
+ **
+ ** Returns          TRUE if the media source supports this config, FALSE otherwise
+ **
+ *******************************************************************************/
+static BOOLEAN bta_av_co_audio_media_supports_config(UINT8 codec_type, const UINT8 *p_codec_cfg)
+{
+    FUNC_TRACE();
+
+    switch (codec_type)
+    {
+    case BTA_AV_CODEC_SBC:
+        if (bta_av_sbc_cfg_in_cap((UINT8 *)p_codec_cfg, (tA2D_SBC_CIE *)&bta_av_co_sbc_caps))
+        {
+            return FALSE;
+        }
+        break;
+
+
+    default:
+        APPL_TRACE_ERROR("bta_av_co_audio_media_supports_config unsupported codec type %d", codec_type);
+        return FALSE;
+        break;
+    }
+    return TRUE;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_codec_supported
+ **
+ ** Description      Check if all opened connections are compatible with a codec
+ **                  configuration and content protection
+ **
+ ** Returns          TRUE if all opened devices support this codec, FALSE otherwise
+ **
+ *******************************************************************************/
+BOOLEAN bta_av_co_audio_codec_supported(tBTIF_STATUS *p_status)
+{
+    UINT8 index;
+    UINT8 snk_index;
+    tBTA_AV_CO_PEER *p_peer;
+    tBTA_AV_CO_SINK *p_sink;
+    UINT8 codec_cfg[AVDT_CODEC_SIZE];
+    UINT8 num_protect = 0;
+#if defined(BTA_AV_CO_CP_SCMS_T) && (BTA_AV_CO_CP_SCMS_T == TRUE)
+    BOOLEAN cp_active;
+#endif
+
+    FUNC_TRACE();
+
+    APPL_TRACE_DEBUG("bta_av_co_audio_codec_supported");
+
+    /* Check AV feeding is supported */
+    *p_status = BTIF_ERROR_SRV_AV_FEEDING_NOT_SUPPORTED;
+
+    for (index = 0; index < BTA_AV_CO_NUM_ELEMENTS(bta_av_co_cb.peers); index++)
+    {
+        p_peer = &bta_av_co_cb.peers[index];
+        if (p_peer->opened)
+        {
+            if (bta_av_co_audio_peer_supports_codec(p_peer, &snk_index))
+            {
+                p_sink = &p_peer->snks[snk_index];
+
+                /* Check that this sink is compatible with the CP */
+                if (!bta_av_co_audio_sink_supports_cp(p_sink))
+                {
+                    APPL_TRACE_DEBUG("bta_av_co_audio_codec_supported sink %d of peer %d doesn't support cp",
+                            snk_index, index);
+                    *p_status = BTIF_ERROR_SRV_AV_CP_NOT_SUPPORTED;
+                    return FALSE;
+                }
+
+                /* Build the codec configuration for this sink */
+                if (bta_av_co_audio_codec_build_config(p_sink->codec_caps, codec_cfg))
+                {
+#if defined(BTA_AV_CO_CP_SCMS_T) && (BTA_AV_CO_CP_SCMS_T == TRUE)
+                    /* Check if this sink supports SCMS */
+                    cp_active = bta_av_co_audio_sink_has_scmst(p_sink);
+#endif
+                    /* Check if this is a new configuration (new sink or new config) */
+                    if ((p_sink != p_peer->p_snk) ||
+                        (memcmp(codec_cfg, p_peer->codec_cfg, AVDT_CODEC_SIZE))
+#if defined(BTA_AV_CO_CP_SCMS_T) && (BTA_AV_CO_CP_SCMS_T == TRUE)
+                        || (p_peer->cp_active != cp_active)
+#endif
+                        )
+                    {
+                        /* Save the new configuration */
+                        p_peer->p_snk = p_sink;
+                        memcpy(p_peer->codec_cfg, codec_cfg, AVDT_CODEC_SIZE);
+#if defined(BTA_AV_CO_CP_SCMS_T) && (BTA_AV_CO_CP_SCMS_T == TRUE)
+                        p_peer->cp_active = cp_active;
+                        if (p_peer->cp_active)
+                        {
+                            bta_av_co_cb.cp.active = TRUE;
+                            num_protect = BTA_AV_CP_INFO_LEN;
+                        }
+                        else
+                        {
+                            bta_av_co_cb.cp.active = FALSE;
+                        }
+#endif
+                        APPL_TRACE_DEBUG("bta_av_co_audio_codec_supported call BTA_AvReconfig(x%x)", BTA_AV_CO_AUDIO_INDX_TO_HNDL(index));
+                        BTA_AvReconfig(BTA_AV_CO_AUDIO_INDX_TO_HNDL(index), TRUE, p_sink->sep_info_idx,
+                                p_peer->codec_cfg, num_protect, (UINT8 *)bta_av_co_cp_scmst);
+                    }
+                }
+            }
+            else
+            {
+                APPL_TRACE_DEBUG("bta_av_co_audio_codec_supported index %d doesn't support codec", index);
+                return FALSE;
+            }
+        }
+    }
+
+    *p_status = BTIF_SUCCESS;
+    return TRUE;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_codec_reset
+ **
+ ** Description      Reset the current codec configuration
+ **
+ ** Returns          void
+ **
+ *******************************************************************************/
+void bta_av_co_audio_codec_reset(void)
+{
+    GKI_disable();
+    FUNC_TRACE();
+
+    /* Reset the current configuration to SBC */
+    bta_av_co_cb.codec_cfg.id = BTIF_AV_CODEC_SBC;
+
+    if (A2D_BldSbcInfo(A2D_MEDIA_TYPE_AUDIO, (tA2D_SBC_CIE *)&btif_av_sbc_default_config, bta_av_co_cb.codec_cfg.info) != A2D_SUCCESS)
+    {
+        APPL_TRACE_ERROR("bta_av_co_audio_codec_reset A2D_BldSbcInfo failed");
+    }
+
+    GKI_enable();
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_set_codec
+ **
+ ** Description      Set the current codec configuration from the feeding type.
+ **                  This function is starting to modify the configuration, it
+ **                  should be protected.
+ **
+ ** Returns          TRUE if successful, FALSE otherwise
+ **
+ *******************************************************************************/
+BOOLEAN bta_av_co_audio_set_codec(const tBTIF_AV_MEDIA_FEEDINGS *p_feeding, tBTIF_STATUS *p_status)
+{
+    tA2D_SBC_CIE sbc_config;
+    tBTIF_AV_CODEC_INFO new_cfg;
+
+    FUNC_TRACE();
+
+    /* Check AV feeding is supported */
+    *p_status = BTIF_ERROR_SRV_AV_FEEDING_NOT_SUPPORTED;
+
+    APPL_TRACE_DEBUG("bta_av_co_audio_set_codec cid=%d", p_feeding->format);
+
+    /* Supported codecs */
+    switch (p_feeding->format)
+    {
+    case BTIF_AV_CODEC_PCM:
+        new_cfg.id = BTIF_AV_CODEC_SBC;
+
+        sbc_config = btif_av_sbc_default_config;
+        if ((p_feeding->cfg.pcm.num_channel != 1) &&
+            (p_feeding->cfg.pcm.num_channel != 2))
+        {
+            APPL_TRACE_ERROR("bta_av_co_audio_set_codec PCM channel number unsupported");
+            return FALSE;
+        }
+        if ((p_feeding->cfg.pcm.bit_per_sample != 8) &&
+            (p_feeding->cfg.pcm.bit_per_sample != 16))
+        {
+            APPL_TRACE_ERROR("bta_av_co_audio_set_codec PCM sample size unsupported");
+            return FALSE;
+        }
+        switch (p_feeding->cfg.pcm.sampling_freq)
+        {
+        case 8000:
+        case 12000:
+        case 16000:
+        case 24000:
+        case 32000:
+        case 48000:
+            sbc_config.samp_freq = A2D_SBC_IE_SAMP_FREQ_48;
+            break;
+
+        case 11025:
+        case 22050:
+        case 44100:
+            sbc_config.samp_freq = A2D_SBC_IE_SAMP_FREQ_44;
+            break;
+        default:
+            APPL_TRACE_ERROR("bta_av_co_audio_set_codec PCM sampling frequency unsupported");
+            return FALSE;
+            break;
+        }
+        /* Build the codec config */
+        if (A2D_BldSbcInfo(A2D_MEDIA_TYPE_AUDIO, &sbc_config, new_cfg.info) != A2D_SUCCESS)
+        {
+            APPL_TRACE_ERROR("bta_av_co_audio_set_codec A2D_BldSbcInfo failed");
+            return FALSE;
+        }
+        break;
+
+
+    default:
+        APPL_TRACE_ERROR("bta_av_co_audio_set_codec Feeding format unsupported");
+        return FALSE;
+        break;
+    }
+
+    /* The new config was correctly built */
+    bta_av_co_cb.codec_cfg = new_cfg;
+
+
+    /* Check all devices support it */
+    *p_status = BTIF_SUCCESS;
+    return bta_av_co_audio_codec_supported(p_status);
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_get_sbc_config
+ **
+ ** Description      Retrieves the SBC codec configuration.  If the codec in use
+ **                  is not SBC, return the default SBC codec configuration.
+ **
+ ** Returns          TRUE if codec is SBC, FALSE otherwise
+ **
+ *******************************************************************************/
+BOOLEAN bta_av_co_audio_get_sbc_config(tA2D_SBC_CIE *p_sbc_config, UINT16 *p_minmtu)
+{
+    BOOLEAN result = FALSE;
+    UINT8 index, jndex;
+    tBTA_AV_CO_PEER *p_peer;
+    tBTA_AV_CO_SINK *p_sink;
+
+    APPL_TRACE_EVENT("bta_av_co_cb.codec_cfg.id : codec 0x%x", bta_av_co_cb.codec_cfg.id);
+
+    /* Minimum MTU is by default very large */
+    *p_minmtu = 0xFFFF;
+
+    GKI_disable();
+    if (bta_av_co_cb.codec_cfg.id == BTIF_AV_CODEC_SBC)
+    {
+        if (A2D_ParsSbcInfo(p_sbc_config, bta_av_co_cb.codec_cfg.info, FALSE) == A2D_SUCCESS)
+        {
+            for (index = 0; index < BTA_AV_CO_NUM_ELEMENTS(bta_av_co_cb.peers); index++)
+            {
+                p_peer = &bta_av_co_cb.peers[index];
+                if (p_peer->opened)
+                {
+                    if (p_peer->mtu < *p_minmtu)
+                    {
+                        *p_minmtu = p_peer->mtu;
+                    }
+                    for (jndex = 0; jndex < p_peer->num_sup_snks; jndex++)
+                    {
+                        p_sink = &p_peer->snks[jndex];
+                        if (p_sink->codec_type == A2D_MEDIA_CT_SBC)
+                        {
+                            /* Update the bitpool boundaries of the current config */
+                            p_sbc_config->min_bitpool =
+                               BTA_AV_CO_MAX(p_sink->codec_caps[BTA_AV_CO_SBC_MIN_BITPOOL_OFF],
+                                             p_sbc_config->min_bitpool);
+                            p_sbc_config->max_bitpool =
+                               BTA_AV_CO_MIN(p_sink->codec_caps[BTA_AV_CO_SBC_MAX_BITPOOL_OFF],
+                                             p_sbc_config->max_bitpool);
+                            APPL_TRACE_EVENT("bta_av_co_audio_get_sbc_config : sink bitpool min %d, max %d",
+                                 p_sbc_config->min_bitpool, p_sbc_config->max_bitpool);
+                            break;
+                        }
+                    }
+                }
+            }
+            result = TRUE;
+        }
+    }
+
+    if (!result)
+    {
+        /* Not SBC, still return the default values */
+        *p_sbc_config = btif_av_sbc_default_config;
+    }
+    GKI_enable();
+
+    return result;
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_audio_discard_config
+ **
+ ** Description      Discard the codec configuration of a connection
+ **
+ ** Returns          Nothing
+ **
+ *******************************************************************************/
+void bta_av_co_audio_discard_config(tBTA_AV_HNDL hndl)
+{
+    tBTA_AV_CO_PEER *p_peer;
+
+    FUNC_TRACE();
+
+    /* Find the peer info */
+    p_peer = bta_av_co_get_peer(hndl);
+    if (p_peer == NULL)
+    {
+        APPL_TRACE_ERROR("bta_av_co_audio_discard_config could not find peer entry");
+        return;
+    }
+
+    /* Reset the peer codec configuration */
+    bta_av_co_audio_peer_reset_config(p_peer);
+}
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_init
+ **
+ ** Description      Initialization
+ **
+ ** Returns          Nothing
+ **
+ *******************************************************************************/
+void bta_av_co_init(void)
+{
+    FUNC_TRACE();
+
+    /* Reset the control block */
+    memset(&bta_av_co_cb, 0, sizeof(bta_av_co_cb));
+
+    bta_av_co_cb.codec_cfg_setconfig.id = BTIF_AV_CODEC_NONE;
+
+#if defined(BTA_AV_CO_CP_SCMS_T) && (BTA_AV_CO_CP_SCMS_T == TRUE)
+    bta_av_co_cp_set_flag(BTA_AV_CP_SCMS_COPY_NEVER);
+#else
+    bta_av_co_cp_set_flag(BTA_AV_CP_SCMS_COPY_FREE);
+#endif
+
+    /* Reset the current config */
+    bta_av_co_audio_codec_reset();
+}
+
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_peer_cp_supported
+ **
+ ** Description      Checks if the peer supports CP
+ **
+ ** Returns          TRUE if the peer supports CP
+ **
+ *******************************************************************************/
+BOOLEAN bta_av_co_peer_cp_supported(tBTA_AV_HNDL hndl)
+{
+    tBTA_AV_CO_PEER *p_peer;
+    tBTA_AV_CO_SINK *p_sink;
+    UINT8 index;
+
+    FUNC_TRACE();
+
+    /* Find the peer info */
+    p_peer = bta_av_co_get_peer(hndl);
+    if (p_peer == NULL)
+    {
+        APPL_TRACE_ERROR("bta_av_co_peer_cp_supported could not find peer entry");
+        return FALSE;
+    }
+
+    for (index = 0; index < p_peer->num_sup_snks; index++)
+    {
+        p_sink = &p_peer->snks[index];
+        if (p_sink->codec_type == A2D_MEDIA_CT_SBC)
+        {
+            return bta_av_co_audio_sink_has_scmst(p_sink);
+        }
+    }
+    APPL_TRACE_ERROR("bta_av_co_peer_cp_supported did not find SBC sink");
+    return FALSE;
+}
+
+
+/*******************************************************************************
+ **
+ ** Function         bta_av_co_get_remote_bitpool_pref
+ **
+ ** Description      Check if remote side did a setconfig within the limits
+ **                  of our exported bitpool range. If set we will set the
+ **                  remote preference.
+ **
+ ** Returns          TRUE if config set, FALSE otherwize
+ **
+ *******************************************************************************/
+
+BOOLEAN bta_av_co_get_remote_bitpool_pref(UINT8 *min, UINT8 *max)
+{
+    /* check if remote peer did a set config */
+    if (bta_av_co_cb.codec_cfg_setconfig.id == BTIF_AV_CODEC_NONE)
+        return FALSE;
+
+    *min = bta_av_co_cb.codec_cfg_setconfig.info[BTA_AV_CO_SBC_MIN_BITPOOL_OFF];
+    *max = bta_av_co_cb.codec_cfg_setconfig.info[BTA_AV_CO_SBC_MAX_BITPOOL_OFF];
+
+    return TRUE;
+}
+
+
+#else /* #if 0 */
+
+extern BOOLEAN bta_av_co_audio_init(UINT8 *p_codec_type, UINT8 *p_codec_info,
+                                    UINT8 *p_num_protect, UINT8 *p_protect_info, UINT8 index)
+{
+    return FALSE;
+}
+
+extern void bta_av_co_audio_disc_res(tBTA_AV_HNDL hndl, UINT8 num_seps,
+                    UINT8 num_snk, UINT8 num_src, BD_ADDR addr, UINT16 uuid_local)
+{
+    return;
+}
+
+extern void bta_av_co_video_disc_res(tBTA_AV_HNDL hndl, UINT8 num_seps,
+                                     UINT8 num_snk, BD_ADDR addr)
+{
+    return;
+}
+
+extern UINT8 bta_av_co_audio_getconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                       UINT8 *p_codec_info, UINT8 *p_sep_info_idx, UINT8 seid,
+                                       UINT8 *p_num_protect, UINT8 *p_protect_info)
+{
+    return 0;
+}
+
+extern UINT8 bta_av_co_video_getconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                       UINT8 *p_codec_info, UINT8 *p_sep_info_idx, UINT8 seid,
+                                       UINT8 *p_num_protect, UINT8 *p_protect_info)
+{
+    return 0;
+}
+
+extern void bta_av_co_audio_setconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                        UINT8 *p_codec_info, UINT8 seid, BD_ADDR addr,
+                                        UINT8 num_protect, UINT8 *p_protect_info,UINT8 t_local_sep, UINT8 avdt_handle)
+{
+    return;
+}
+
+extern void bta_av_co_video_setconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                      UINT8 *p_codec_info, UINT8 seid, BD_ADDR addr,
+                                      UINT8 num_protect, UINT8 *p_protect_info)
+{
+    return;
+}
+
+extern void bta_av_co_audio_open(tBTA_AV_HNDL hndl,
+                                 tBTA_AV_CODEC codec_type, UINT8 *p_codec_info,
+                                 UINT16 mtu)
+{
+    return;
+}
+
+extern void bta_av_co_video_open(tBTA_AV_HNDL hndl,
+                                 tBTA_AV_CODEC codec_type, UINT8 *p_codec_info,
+                                 UINT16 mtu)
+{
+    return;
+}
+
+extern void bta_av_co_audio_close(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                  UINT16 mtu)
+{
+    return;
+}
+
+extern void bta_av_co_video_close(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                  UINT16 mtu)
+{
+    return;
+}
+
+extern void bta_av_co_audio_start(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                  UINT8 *p_codec_info, BOOLEAN *p_no_rtp_hdr)
+{
+    return;
+}
+
+extern void bta_av_co_video_start(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
+                                  UINT8 *p_codec_info, BOOLEAN *p_no_rtp_hdr)
+{
+    return;
+}
+
+extern void bta_av_co_audio_stop(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type)
+{
+    return;
+}
+
+extern void bta_av_co_video_stop(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type)
+{
+    return;
+}
+
+extern void * bta_av_co_audio_src_data_path(tBTA_AV_CODEC codec_type,
+                                            UINT32 *p_len, UINT32 *p_timestamp)
+{
+    return NULL;
+}
+
+extern void * bta_av_co_video_src_data_path(tBTA_AV_CODEC codec_type,
+                                            UINT32 *p_len, UINT32 *p_timestamp)
+{
+    return NULL;
+}
+
+extern void bta_av_co_audio_drop(tBTA_AV_HNDL hndl)
+{
+    return;
+}
+
+extern void bta_av_co_video_report_conn (BOOLEAN open, UINT8 avdt_handle)
+{
+    return;
+}
+
+extern void bta_av_co_video_report_rr (UINT32 packet_lost)
+{
+    return;
+}
+
+extern void bta_av_co_audio_delay(tBTA_AV_HNDL hndl, UINT16 delay)
+{
+    return;
+}
+
+extern void bta_av_co_video_delay(tBTA_AV_HNDL hndl, UINT16 delay)
+{
+    return;
+}
+
+#endif
index fd129359f4530e1c660e3dd3f26627031a34a99e..96ebfb090906de5edf1dce3b995069773111ae27 100644 (file)
@@ -47,6 +47,8 @@ COMPONENT_SRCDIRS :=  bluedroid/bta/dm                        \
                        bluedroid/bta/gatt                      \
                        bluedroid/bta/hh                        \
                        bluedroid/bta/sdp                       \
+                       bluedroid/bta/av                        \
+                       bluedroid/bta/ar                        \
                        bluedroid/bta/sys                       \
                        bluedroid/bta                           \
                        bluedroid/btcore                        \