OSDN Git Service

Merge commit '07772caf5ca22365dd62bc042de179a4da55f665' into remote
[android-x86/system-bt.git] / btif / src / btif_av.c
1 /******************************************************************************
2  *  Copyright (c) 2014, The Linux Foundation. All rights reserved.
3  *  Not a Contribution.
4  *
5  *  Copyright (C) 2009-2012 Broadcom Corporation
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at:
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  ******************************************************************************/
20
21 #define LOG_TAG "btif_av"
22
23 #include "btif_av.h"
24
25 #include <assert.h>
26 #include <string.h>
27
28 #include <system/audio.h>
29 #include <hardware/bluetooth.h>
30 #include <hardware/bt_av.h>
31
32 #include "bt_utils.h"
33 #include "bta_api.h"
34 #include "btif_media.h"
35 #include "btif_profile_queue.h"
36 #include "btif_util.h"
37 #include "btu.h"
38 #include "bt_common.h"
39 #include "osi/include/allocator.h"
40 #include <cutils/properties.h>
41
42 /*****************************************************************************
43 **  Constants & Macros
44 ******************************************************************************/
45 #define BTIF_AV_SERVICE_NAME "Advanced Audio"
46 #define BTIF_AVK_SERVICE_NAME "Advanced Audio Sink"
47
48 #define BTIF_TIMEOUT_AV_OPEN_ON_RC_MS  (2 * 1000)
49
50 /* Number of BTIF-AV control blocks */
51 /* Now supports Two AV connections. */
52 #define BTIF_AV_NUM_CB       2
53 #define HANDLE_TO_INDEX(x) ((x & BTA_AV_HNDL_MSK) - 1)
54 #define INVALID_INDEX        -1
55
56 typedef enum {
57     BTIF_AV_STATE_IDLE = 0x0,
58     BTIF_AV_STATE_OPENING,
59     BTIF_AV_STATE_OPENED,
60     BTIF_AV_STATE_STARTED,
61     BTIF_AV_STATE_CLOSING
62 } btif_av_state_t;
63
64 /* Should not need dedicated suspend state as actual actions are no
65    different than open state. Suspend flags are needed however to prevent
66    media task from trying to restart stream during remote suspend or while
67    we are in the process of a local suspend */
68
69 #define BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING 0x1
70 #define BTIF_AV_FLAG_REMOTE_SUSPEND        0x2
71 #define BTIF_AV_FLAG_PENDING_START         0x4
72 #define BTIF_AV_FLAG_PENDING_STOP          0x8
73 /* Host role defenitions */
74 #define HOST_ROLE_MASTER                   0x00
75 #define HOST_ROLE_SLAVE                    0x01
76 #define HOST_ROLE_UNKNOWN                  0xff
77
78 /*****************************************************************************
79 **  Local type definitions
80 ******************************************************************************/
81
82 typedef struct
83 {
84     tBTA_AV_HNDL bta_handle;
85     bt_bdaddr_t peer_bda;
86     btif_sm_handle_t sm_handle;
87     UINT8 flags;
88     tBTA_AV_EDR edr;
89     UINT8 peer_sep;  /* sep type of peer device */
90     UINT8 edr_3mbps;
91     BOOLEAN dual_handoff;
92     BOOLEAN current_playing;
93     btif_sm_state_t state;
94     int service;
95     BOOLEAN is_slave;
96     BOOLEAN is_device_playing;
97 #ifdef BTA_AV_SPLIT_A2DP_ENABLED
98     UINT16 channel_id;
99 #endif
100 } btif_av_cb_t;
101
102 typedef struct
103 {
104     bt_bdaddr_t *target_bda;
105     uint16_t uuid;
106 } btif_av_connect_req_t;
107
108 typedef struct
109 {
110     int sample_rate;
111     int channel_count;
112     bt_bdaddr_t peer_bd;
113 } btif_av_sink_config_req_t;
114
115 typedef struct
116 {
117     BOOLEAN sbc_offload;
118     BOOLEAN aptx_offload;
119     BOOLEAN aac_offload;
120     BOOLEAN aptxhd_offload;
121 } btif_av_a2dp_offloaded_codec_cap_t;
122
123 typedef enum {
124     SBC,
125     APTX,
126     AAC,
127     APTXHD,
128 }btif_av_codec_list;
129
130 /*****************************************************************************
131 **  Static variables
132 ******************************************************************************/
133 static btav_callbacks_t *bt_av_src_callbacks = NULL;
134 static btav_callbacks_t *bt_av_sink_callbacks = NULL;
135 static alarm_t *av_open_on_rc_timer = NULL;
136 static btif_av_cb_t btif_av_cb[BTIF_AV_NUM_CB];
137 static btif_sm_event_t idle_rc_event;
138 static tBTA_AV idle_rc_data;
139 int btif_max_av_clients = 1;
140 static BOOLEAN enable_multicast = FALSE;
141 static BOOLEAN is_multicast_supported = FALSE;
142 static BOOLEAN multicast_disabled = FALSE;
143 BOOLEAN bt_split_a2dp_enabled = FALSE;
144 btif_av_a2dp_offloaded_codec_cap_t btif_av_codec_offload;
145 /* both interface and media task needs to be ready to alloc incoming request */
146 #define CHECK_BTAV_INIT() if (((bt_av_src_callbacks == NULL) &&(bt_av_sink_callbacks == NULL)) \
147         || (btif_av_cb[0].sm_handle == NULL))\
148 {\
149      BTIF_TRACE_WARNING("%s: BTAV not initialized", __FUNCTION__);\
150      return BT_STATUS_NOT_READY;\
151 }\
152 else\
153 {\
154      BTIF_TRACE_EVENT("%s", __FUNCTION__);\
155 }
156
157 /* Helper macro to avoid code duplication in the state machine handlers */
158 #define CHECK_RC_EVENT(e, d) \
159     case BTA_AV_RC_CLOSE_EVT: \
160     case BTA_AV_REMOTE_CMD_EVT: \
161     case BTA_AV_VENDOR_CMD_EVT: \
162     case BTA_AV_META_MSG_EVT: \
163     case BTA_AV_BROWSE_MSG_EVT: \
164     case BTA_AV_RC_FEAT_EVT: \
165     case BTA_AV_REMOTE_RSP_EVT: \
166     { \
167          btif_rc_handler(e, d);\
168     }break; \
169
170
171 static BOOLEAN btif_av_state_idle_handler(btif_sm_event_t event, void *data, int index);
172 static BOOLEAN btif_av_state_opening_handler(btif_sm_event_t event, void *data, int index);
173 static BOOLEAN btif_av_state_opened_handler(btif_sm_event_t event, void *data, int index);
174 static BOOLEAN btif_av_state_started_handler(btif_sm_event_t event, void *data,int index);
175
176 static BOOLEAN btif_av_state_closing_handler(btif_sm_event_t event, void *data,int index);
177
178 static BOOLEAN btif_av_get_valid_idx(int idx);
179 static UINT8 btif_av_idx_by_bdaddr( BD_ADDR bd_addr);
180 int btif_get_latest_playing_device_idx();
181 static int btif_get_latest_device_idx_to_start();
182 static int btif_av_get_valid_idx_for_rc_events(BD_ADDR bd_addr, int rc_handle);
183 static int btif_get_conn_state_of_device(BD_ADDR address);
184 static bt_status_t connect_int(bt_bdaddr_t *bd_addr, uint16_t uuid);
185 static void btif_av_update_current_playing_device(int index);
186 static void btif_av_check_rc_connection_priority(void *p_data);
187 #ifdef AVK_BACKPORT
188 void btif_av_request_audio_focus( BOOLEAN enable);
189 #endif
190 static const btif_sm_handler_t btif_av_state_handlers[] =
191 {
192     btif_av_state_idle_handler,
193     btif_av_state_opening_handler,
194     btif_av_state_opened_handler,
195     btif_av_state_started_handler,
196     btif_av_state_closing_handler
197 };
198
199 static void btif_av_event_free_data(btif_sm_event_t event, void *p_data);
200
201 /*************************************************************************
202 ** Extern functions
203 *************************************************************************/
204 extern void btif_rc_handler(tBTA_AV_EVT event, tBTA_AV *p_data);
205 extern BOOLEAN btif_rc_get_connected_peer(BD_ADDR peer_addr);
206 extern UINT8 btif_rc_get_connected_peer_handle(BD_ADDR peer_addr);
207 extern void btif_rc_check_handle_pending_play (BD_ADDR peer_addr, BOOLEAN bSendToApp);
208 extern void btif_rc_get_playing_device(BD_ADDR address);
209 extern void btif_rc_clear_playing_state(BOOLEAN play);
210 extern void btif_rc_clear_priority(BD_ADDR address);
211 extern void btif_rc_send_pause_command();
212 extern UINT16 btif_dm_get_br_edr_links();
213 extern UINT16 btif_dm_get_le_links();
214 extern UINT16 btif_hf_is_call_idle();
215
216 extern fixed_queue_t *btu_general_alarm_queue;
217
218 /*****************************************************************************
219 ** Local helper functions
220 ******************************************************************************/
221 void btif_av_trigger_dual_handoff(BOOLEAN handoff, BD_ADDR address);
222 BOOLEAN btif_av_is_device_connected(BD_ADDR address);
223
224 BOOLEAN btif_av_is_connected_on_other_idx(int current_index);
225 BOOLEAN btif_av_is_playing_on_other_idx(int current_index);
226 BOOLEAN btif_av_is_playing();
227 void btif_av_update_multicast_state(int index);
228 BOOLEAN btif_av_get_ongoing_multicast();
229 tBTA_AV_HNDL btif_av_get_playing_device_hdl();
230 tBTA_AV_HNDL btif_av_get_av_hdl_from_idx(UINT8 idx);
231 int btif_av_get_other_connected_idx(int current_index);
232 #ifdef BTA_AV_SPLIT_A2DP_ENABLED
233 BOOLEAN btif_av_is_codec_offload_supported(int codec);
234 int btif_av_get_current_playing_dev_idx();
235 BOOLEAN btif_av_is_under_handoff();
236 #else
237 #define btif_av_is_codec_offload_supported(codec) (0)
238 #define btif_av_get_current_playing_dev_idx() (0)
239 #define btif_av_is_under_handoff() (0)
240 #endif
241
242 const char *dump_av_sm_state_name(btif_av_state_t state)
243 {
244     switch (state)
245     {
246         CASE_RETURN_STR(BTIF_AV_STATE_IDLE)
247         CASE_RETURN_STR(BTIF_AV_STATE_OPENING)
248         CASE_RETURN_STR(BTIF_AV_STATE_OPENED)
249         CASE_RETURN_STR(BTIF_AV_STATE_STARTED)
250         CASE_RETURN_STR(BTIF_AV_STATE_CLOSING)
251         default: return "UNKNOWN_STATE";
252     }
253 }
254
255 const char *dump_av_sm_event_name(btif_av_sm_event_t event)
256 {
257     switch((int)event)
258     {
259         CASE_RETURN_STR(BTA_AV_ENABLE_EVT)
260         CASE_RETURN_STR(BTA_AV_REGISTER_EVT)
261         CASE_RETURN_STR(BTA_AV_OPEN_EVT)
262         CASE_RETURN_STR(BTA_AV_CLOSE_EVT)
263         CASE_RETURN_STR(BTA_AV_START_EVT)
264         CASE_RETURN_STR(BTA_AV_STOP_EVT)
265         CASE_RETURN_STR(BTA_AV_PROTECT_REQ_EVT)
266         CASE_RETURN_STR(BTA_AV_PROTECT_RSP_EVT)
267         CASE_RETURN_STR(BTA_AV_RC_OPEN_EVT)
268         CASE_RETURN_STR(BTA_AV_RC_CLOSE_EVT)
269         CASE_RETURN_STR(BTA_AV_REMOTE_CMD_EVT)
270         CASE_RETURN_STR(BTA_AV_REMOTE_RSP_EVT)
271         CASE_RETURN_STR(BTA_AV_VENDOR_CMD_EVT)
272         CASE_RETURN_STR(BTA_AV_VENDOR_RSP_EVT)
273         CASE_RETURN_STR(BTA_AV_RECONFIG_EVT)
274         CASE_RETURN_STR(BTA_AV_SUSPEND_EVT)
275         CASE_RETURN_STR(BTA_AV_PENDING_EVT)
276         CASE_RETURN_STR(BTA_AV_META_MSG_EVT)
277         CASE_RETURN_STR(BTA_AV_REJECT_EVT)
278         CASE_RETURN_STR(BTA_AV_RC_FEAT_EVT)
279         CASE_RETURN_STR(BTA_AV_OFFLOAD_START_RSP_EVT)
280         CASE_RETURN_STR(BTIF_SM_ENTER_EVT)
281         CASE_RETURN_STR(BTIF_SM_EXIT_EVT)
282         CASE_RETURN_STR(BTIF_AV_CONNECT_REQ_EVT)
283         CASE_RETURN_STR(BTIF_AV_DISCONNECT_REQ_EVT)
284         CASE_RETURN_STR(BTIF_AV_START_STREAM_REQ_EVT)
285         CASE_RETURN_STR(BTIF_AV_STOP_STREAM_REQ_EVT)
286         CASE_RETURN_STR(BTIF_AV_SUSPEND_STREAM_REQ_EVT)
287         CASE_RETURN_STR(BTIF_AV_SINK_CONFIG_REQ_EVT)
288         CASE_RETURN_STR(BTIF_AV_OFFLOAD_START_REQ_EVT)
289 #ifdef USE_AUDIO_TRACK
290         CASE_RETURN_STR(BTIF_AV_SINK_FOCUS_REQ_EVT)
291 #endif
292         CASE_RETURN_STR(BTIF_AV_UPDATE_ENCODER_REQ_EVT)
293         default: return "UNKNOWN_EVENT";
294    }
295 }
296
297 const char *dump_av_codec_name(btif_av_codec_list codec)
298 {
299     switch((int)codec)
300     {
301         CASE_RETURN_STR(SBC)
302         CASE_RETURN_STR(APTX)
303         CASE_RETURN_STR(AAC)
304         CASE_RETURN_STR(APTXHD)
305         default: return "UNKNOWN_CODEC";
306     }
307 }
308 //TODO.. We will remove this data structure
309 static BD_ADDR bd_null= {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
310
311 /****************************************************************************
312 **  Local helper functions
313 *****************************************************************************/
314 /*******************************************************************************
315 **
316 ** Function         btif_initiate_av_open_timer_timeout
317 **
318 ** Description      Timer to trigger AV open if the remote headset establishes
319 **                  RC connection w/o AV connection. The timer is needed to IOP
320 **                  with headsets that do establish AV after RC connection.
321 **
322 ** Returns          void
323 **
324 *******************************************************************************/
325 static void btif_initiate_av_open_timer_timeout(UNUSED_ATTR void *data)
326 {
327     BD_ADDR peer_addr;
328
329     /* is there at least one RC connection - There should be */
330     /*We have Two Connections.*/
331     if (btif_rc_get_connected_peer(peer_addr))
332     {
333         /*Check if this peer_addr is same as currently connected AV*/
334         if (btif_get_conn_state_of_device(peer_addr) == BTIF_AV_STATE_OPENED)
335         {
336             BTIF_TRACE_DEBUG("AV is already connected");
337         }
338         else
339         {
340             UINT8 rc_handle;
341             int index;
342             /* Multicast: Check if AV slot is available for connection
343              * If not available, AV got connected to different devices.
344              * Disconnect this RC connection without AV connection.
345              */
346             rc_handle = btif_rc_get_connected_peer_handle(peer_addr);
347             index = btif_av_get_valid_idx_for_rc_events(peer_addr, rc_handle);
348             if(index >= btif_max_av_clients)
349             {
350                 BTIF_TRACE_ERROR("%s No slot free for AV connection, back off",
351                             __FUNCTION__);
352                 return;
353             }
354             BTIF_TRACE_DEBUG("%s Issuing connect to the remote RC peer", __FUNCTION__);
355             if(bt_av_sink_callbacks != NULL)
356                 btif_queue_connect(UUID_SERVCLASS_AUDIO_SINK, (bt_bdaddr_t*)&peer_addr,
357                         connect_int);
358             if(bt_av_src_callbacks != NULL)
359                 btif_queue_connect(UUID_SERVCLASS_AUDIO_SOURCE, (bt_bdaddr_t*)&peer_addr,
360                         connect_int);
361         }
362     }
363     else
364     {
365         BTIF_TRACE_ERROR("%s No connected RC peers", __FUNCTION__);
366     }
367
368 }
369
370
371 /*****************************************************************************
372 **  Static functions
373 ******************************************************************************/
374
375 /*******************************************************************************
376 **
377 ** Function         btif_report_connection_state
378 **
379 ** Description      Updates the components via the callbacks about the connection
380 **                  state of a2dp connection.
381 **
382 ** Returns          None
383 **
384 *******************************************************************************/
385 static void btif_report_connection_state(btav_connection_state_t state, bt_bdaddr_t *bd_addr)
386 {
387     if (bt_av_sink_callbacks != NULL) {
388         HAL_CBACK(bt_av_sink_callbacks, connection_state_cb, state, bd_addr);
389     } else if ( bt_av_src_callbacks != NULL) {
390         HAL_CBACK(bt_av_src_callbacks, connection_state_cb, state, bd_addr);
391     }
392 }
393
394 /*******************************************************************************
395 **
396 ** Function         btif_report_audio_state
397 **
398 ** Description      Updates the components via the callbacks about the audio
399 **                  state of a2dp connection. The state is updated when either
400 **                  the remote ends starts streaming (started state) or whenever
401 **                  it transitions out of started state (to opened or streaming)
402 **                  state.
403 **
404 ** Returns          None
405 **
406 *******************************************************************************/
407 static void btif_report_audio_state(btav_audio_state_t state, bt_bdaddr_t *bd_addr)
408 {
409     if (bt_av_sink_callbacks != NULL) {
410         HAL_CBACK(bt_av_sink_callbacks, audio_state_cb, state, bd_addr);
411     } else if (bt_av_src_callbacks != NULL) {
412         HAL_CBACK(bt_av_src_callbacks, audio_state_cb, state, bd_addr);
413     }
414 }
415
416 /*****************************************************************************
417 **
418 ** Function     btif_av_state_idle_handler
419 **
420 ** Description  State managing disconnected AV link
421 **
422 ** Returns      TRUE if event was processed, FALSE otherwise
423 **
424 *******************************************************************************/
425
426 static BOOLEAN btif_av_state_idle_handler(btif_sm_event_t event, void *p_data, int index)
427 {
428     char a2dp_role[255] = "false";
429
430     BTIF_TRACE_IMP("%s event:%s flags %x on Index = %d", __FUNCTION__,
431                      dump_av_sm_event_name(event), btif_av_cb[index].flags, index);
432
433     switch (event)
434     {
435         case BTIF_SM_ENTER_EVT:
436             /* clear the peer_bda */
437             BTIF_TRACE_EVENT("IDLE state for index: %d", index);
438             memset(&btif_av_cb[index].peer_bda, 0, sizeof(bt_bdaddr_t));
439             btif_av_cb[index].flags = 0;
440             btif_av_cb[index].edr_3mbps = 0;
441             btif_av_cb[index].edr = 0;
442             btif_av_cb[index].current_playing = FALSE;
443             btif_av_cb[index].is_slave = FALSE;
444             btif_av_cb[index].is_device_playing = FALSE;
445             for (int i = 0; i < btif_max_av_clients; i++)
446             {
447                 btif_av_cb[i].dual_handoff = FALSE;
448             }
449             property_get("persist.service.bt.a2dp.sink", a2dp_role, "false");
450             if (!strncmp("false", a2dp_role, 5)) {
451                 btif_av_cb[index].peer_sep = AVDT_TSEP_SNK;
452                 btif_a2dp_set_peer_sep(AVDT_TSEP_SNK);
453             } else {
454                 btif_av_cb[index].peer_sep = AVDT_TSEP_SRC;
455                 btif_a2dp_set_peer_sep(AVDT_TSEP_SRC);
456             }
457 #ifdef BTA_AV_SPLIT_A2DP_ENABLED
458             btif_av_cb[index].channel_id = 0;
459 #endif
460             /* This API will be called twice at initialization
461             ** Idle can be moved when device is disconnected too.
462             ** Take care of other connected device here.*/
463             if (!btif_av_is_connected())
464             {
465                 BTIF_TRACE_EVENT("reset A2dp states in IDLE ");
466                 btif_a2dp_on_idle();
467             }
468             else
469             {
470                 //There is another AV connection, update current playin
471                 BTIF_TRACE_EVENT("reset A2dp states in IDLE ");
472                 //btif_media_send_reset_vendor_state();
473                 btif_av_update_current_playing_device(index);
474             }
475             if (!btif_av_is_playing_on_other_idx(index) &&
476                  bt_split_a2dp_enabled)
477             {
478                 BTIF_TRACE_EVENT("reset Vendor flag A2DP state is IDLE");
479                 btif_media_send_reset_vendor_state();
480             }
481             break;
482
483         case BTIF_SM_EXIT_EVT:
484             break;
485
486         case BTA_AV_ENABLE_EVT:
487             BTIF_TRACE_EVENT("AV is enabled now for index: %d", index);
488             break;
489
490         case BTA_AV_REGISTER_EVT:
491             BTIF_TRACE_EVENT("The AV Handle:%d", ((tBTA_AV*)p_data)->registr.hndl);
492             btif_av_cb[index].bta_handle = ((tBTA_AV*)p_data)->registr.hndl;
493             break;
494
495            /*
496             * In case Signalling channel is not down
497             * and remote started Streaming Procedure
498             * we have to handle config and open event in
499             * idle_state. We hit these scenarios while running
500             * PTS test case for AVRCP Controller
501             */
502         case BTIF_AV_SINK_CONFIG_REQ_EVT:
503         {
504             btif_av_sink_config_req_t req;
505             // copy to avoid alignment problems
506             memcpy(&req, p_data, sizeof(req));
507
508             BTIF_TRACE_WARNING("BTIF_AV_SINK_CONFIG_REQ_EVT %d %d", req.sample_rate,
509                     req.channel_count);
510             if (bt_av_sink_callbacks != NULL) {
511                 HAL_CBACK(bt_av_sink_callbacks, audio_config_cb, &(req.peer_bd),
512                         req.sample_rate, req.channel_count);
513             }
514         } break;
515
516         case BTIF_AV_CONNECT_REQ_EVT:
517             /* For outgoing connect stack and app are in sync.
518             */
519             memcpy(&btif_av_cb[index].peer_bda, ((btif_av_connect_req_t*)p_data)->target_bda,
520                                                                         sizeof(bt_bdaddr_t));
521             BTA_AvOpen(btif_av_cb[index].peer_bda.address, btif_av_cb[index].bta_handle,
522                         TRUE, BTA_SEC_NONE, ((btif_av_connect_req_t*)p_data)->uuid);
523             btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_OPENING);
524             break;
525
526         case BTA_AV_PENDING_EVT:
527         case BTA_AV_RC_OPEN_EVT:
528             /* IOP_FIX: Jabra 620 only does RC open without AV open whenever it connects. So
529              * as per the AV WP, an AVRC connection cannot exist without an AV connection. Therefore,
530              * we initiate an AV connection if an RC_OPEN_EVT is received when we are in AV_CLOSED state.
531              * We initiate the AV connection after a small 3s timeout to avoid any collisions from the
532              * headsets, as some headsets initiate the AVRC connection first and then
533              * immediately initiate the AV connection
534              *
535              * TODO: We may need to do this only on an AVRCP Play. FixMe
536              */
537             /* Check if connection allowed with this device */
538             /* In Dual A2dp case, this event can come for both the headsets.
539              * Reject second connection request as we are already checking
540              * for device priority for first device and we cannot queue
541              * incoming connections requests.
542              */
543
544             if (idle_rc_event != 0)
545             {
546                 BTIF_TRACE_DEBUG("Processing another RC Event ");
547                 return FALSE;
548             }
549             memcpy(&idle_rc_data, ((tBTA_AV*)p_data), sizeof(tBTA_AV));
550             if (event == BTA_AV_RC_OPEN_EVT)
551             {
552                 if (((tBTA_AV*)p_data)->rc_open.status == BTA_AV_SUCCESS)
553                 {
554                     bdcpy(btif_av_cb[index].peer_bda.address,
555                         ((tBTA_AV*)p_data)->rc_open.peer_addr);
556                 }
557                 else
558                 {
559                     idle_rc_event = 0;
560                     return TRUE;
561                 }
562             }
563             else
564             {
565                 bdcpy(btif_av_cb[index].peer_bda.address, ((tBTA_AV*)p_data)->pend.bd_addr);
566             }
567
568             // Only for AVDTP connection request move to opening state
569             if (event == BTA_AV_PENDING_EVT)
570                 btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_OPENING);
571
572             if (bt_av_src_callbacks != NULL)
573             {
574                 BTIF_TRACE_DEBUG("Calling connection priority callback ");
575                 idle_rc_event = event;
576                 HAL_CBACK(bt_av_src_callbacks, connection_priority_cb,
577                          &(btif_av_cb[index].peer_bda));
578             }
579             if (bt_av_sink_callbacks != NULL)
580             {
581                 if(event == BTA_AV_PENDING_EVT)
582                 {
583                     BTA_AvOpen(btif_av_cb[index].peer_bda.address, btif_av_cb[index].bta_handle,
584                        TRUE, BTA_SEC_NONE, UUID_SERVCLASS_AUDIO_SINK);
585                 }
586                 else if(event == BTA_AV_RC_OPEN_EVT)
587                 {
588                             alarm_set_on_queue(av_open_on_rc_timer,
589                                                           BTIF_TIMEOUT_AV_OPEN_ON_RC_MS,
590                                                           btif_initiate_av_open_timer_timeout, NULL,
591                                                           btu_general_alarm_queue);
592                     btif_rc_handler(event, p_data);
593                 }
594             }
595             break;
596
597         case BTA_AV_OPEN_EVT:
598         {
599             /* We get this event in Idle State if Signaling
600              * channel is not closed, only Streaming channel was
601              * closed earlier, and now only stream setup process is
602              * initiated.
603              */
604             tBTA_AV *p_bta_data = (tBTA_AV*)p_data;
605             btav_connection_state_t state;
606             BTIF_TRACE_DEBUG("status:%d, edr 0x%x",p_bta_data->open.status,
607                                p_bta_data->open.edr);
608
609             if (p_bta_data->open.status == BTA_AV_SUCCESS)
610             {
611                  state = BTAV_CONNECTION_STATE_CONNECTED;
612                  btif_av_cb[index].edr = p_bta_data->open.edr;
613                  if (p_bta_data->open.role == HOST_ROLE_SLAVE)
614                  {
615                     btif_av_cb[index].is_slave = TRUE;
616                  }
617                  btif_av_cb[index].peer_sep = p_bta_data->open.sep;
618                  btif_a2dp_set_peer_sep(p_bta_data->open.sep);
619
620                  if (p_bta_data->open.edr & BTA_AV_EDR_3MBPS)
621                  {
622                      BTIF_TRACE_DEBUG("remote supports 3 mbps");
623                      btif_av_cb[index].edr_3mbps = TRUE;
624                  }
625
626                  bdcpy(btif_av_cb[index].peer_bda.address, ((tBTA_AV*)p_data)->open.bd_addr);
627 #ifdef BTA_AV_SPLIT_A2DP_ENABLED
628                  btif_av_cb[index].channel_id = p_bta_data->open.stream_chnl_id;
629                  BTIF_TRACE_DEBUG("streaming channel id updated as : 0x%x",
630                     btif_av_cb[index].channel_id);
631 #endif
632             }
633             else
634             {
635                 BTIF_TRACE_WARNING("BTA_AV_OPEN_EVT::FAILED status: %d",
636                                      p_bta_data->open.status );
637                 state = BTAV_CONNECTION_STATE_DISCONNECTED;
638             }
639
640             /* change state to open based on the status */
641             if (p_bta_data->open.status == BTA_AV_SUCCESS)
642             {
643                 /* inform the application of the event */
644                 btif_report_connection_state(state, &(btif_av_cb[index].peer_bda));
645                 btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_OPENED);
646                 /* BTIF AV State updated, now check
647                  * and update multicast state
648                  */
649                 btif_av_update_multicast_state(index);
650             }
651
652             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
653             {
654                 /* if queued PLAY command,  send it now */
655                 btif_rc_check_handle_pending_play(p_bta_data->open.bd_addr,
656                                              (p_bta_data->open.status == BTA_AV_SUCCESS));
657             }
658             else if ((btif_av_cb[index].peer_sep == AVDT_TSEP_SRC) &&
659                     (p_bta_data->open.status == BTA_AV_SUCCESS))
660             {
661                 /* if queued PLAY command,  send it now */
662                 btif_rc_check_handle_pending_play(p_bta_data->open.bd_addr, FALSE);
663                 /* Bring up AVRCP connection too */
664                 BTA_AvOpenRc(btif_av_cb[index].bta_handle);
665             }
666             btif_queue_advance();
667         } break;
668
669         case BTA_AV_REMOTE_CMD_EVT:
670         case BTA_AV_VENDOR_CMD_EVT:
671         case BTA_AV_META_MSG_EVT:
672         case BTA_AV_RC_FEAT_EVT:
673         case BTA_AV_REMOTE_RSP_EVT:
674         case BTA_AV_BROWSE_MSG_EVT:
675             btif_rc_handler(event, (tBTA_AV*)p_data);
676             break;
677
678         case BTA_AV_RC_CLOSE_EVT:
679             BTIF_TRACE_DEBUG("BTA_AV_RC_CLOSE_EVT: Stopping AV timer.");
680             alarm_cancel(av_open_on_rc_timer);
681             btif_rc_handler(event, p_data);
682             break;
683
684         case BTIF_AV_OFFLOAD_START_REQ_EVT:
685             BTIF_TRACE_ERROR("BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started IDLE");
686             btif_a2dp_on_offload_started(BTA_AV_FAIL);
687             break;
688
689         default:
690             BTIF_TRACE_WARNING("%s : unhandled event:%s", __FUNCTION__,
691                                 dump_av_sm_event_name(event));
692             return FALSE;
693
694     }
695
696     return TRUE;
697 }
698 /*****************************************************************************
699 **
700 ** Function        btif_av_state_opening_handler
701 **
702 ** Description     Intermediate state managing events during establishment
703 **                 of avdtp channel
704 **
705 ** Returns         TRUE if event was processed, FALSE otherwise
706 **
707 *******************************************************************************/
708
709 static BOOLEAN btif_av_state_opening_handler(btif_sm_event_t event, void *p_data, int index)
710 {
711     int i;
712     BTIF_TRACE_IMP("%s event:%s flags %x on index = %d", __FUNCTION__,
713                      dump_av_sm_event_name(event), btif_av_cb[index].flags, index);
714     switch (event)
715     {
716         case BTIF_SM_ENTER_EVT:
717             /* inform the application that we are entering connecting state */
718             if (bt_av_sink_callbacks != NULL)
719             {
720                 HAL_CBACK(bt_av_sink_callbacks, connection_state_cb,
721                          BTAV_CONNECTION_STATE_CONNECTING, &(btif_av_cb[index].peer_bda));
722             }
723             else if (bt_av_src_callbacks != NULL)
724             {
725                 HAL_CBACK(bt_av_src_callbacks, connection_state_cb,
726                          BTAV_CONNECTION_STATE_CONNECTING, &(btif_av_cb[index].peer_bda));
727             }
728             break;
729
730         case BTIF_SM_EXIT_EVT:
731             break;
732
733         case BTA_AV_REJECT_EVT:
734             BTIF_TRACE_DEBUG(" Received  BTA_AV_REJECT_EVT ");
735             btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
736                                         &(btif_av_cb[index].peer_bda));
737             btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_IDLE);
738             break;
739
740         case BTA_AV_OPEN_EVT:
741         {
742             tBTA_AV *p_bta_data = (tBTA_AV*)p_data;
743             btav_connection_state_t state;
744             btif_sm_state_t av_state;
745             BTIF_TRACE_DEBUG("status:%d, edr 0x%x, role: 0x%x",p_bta_data->open.status,
746                              p_bta_data->open.edr, p_bta_data->open.role);
747
748             if (p_bta_data->open.status == BTA_AV_SUCCESS)
749             {
750                  state = BTAV_CONNECTION_STATE_CONNECTED;
751                  av_state = BTIF_AV_STATE_OPENED;
752                  btif_av_cb[index].edr = p_bta_data->open.edr;
753                  if (p_bta_data->open.role == HOST_ROLE_SLAVE)
754                  {
755                     btif_av_cb[index].is_slave = TRUE;
756                  }
757                  btif_av_cb[index].peer_sep = p_bta_data->open.sep;
758                  btif_a2dp_set_peer_sep(p_bta_data->open.sep);
759                  if (p_bta_data->open.edr & BTA_AV_EDR_3MBPS)
760                  {
761                      BTIF_TRACE_DEBUG("remote supports 3 mbps");
762                      btif_av_cb[index].edr_3mbps = TRUE;
763                  }
764 #ifdef BTA_AV_SPLIT_A2DP_ENABLED
765                  btif_av_cb[index].channel_id = p_bta_data->open.stream_chnl_id;
766                  BTIF_TRACE_DEBUG("streaming channel id updated as : 0x%x",
767                         btif_av_cb[index].channel_id);
768 #endif
769             }
770             else
771             {
772                 BTIF_TRACE_WARNING("BTA_AV_OPEN_EVT::FAILED status: %d",
773                                      p_bta_data->open.status );
774                 /* Multicast: Check if connected to AVRC only device
775                  * disconnect when Dual A2DP/Multicast is supported.
776                  */
777                 BD_ADDR peer_addr;
778                 if ((btif_rc_get_connected_peer(peer_addr))
779                     &&(!bdcmp(btif_av_cb[index].peer_bda.address, peer_addr)))
780                 {
781                     /* Do not disconnect AVRCP connection if A2DP
782                      * connection failed due to SDP failure since remote
783                      * may not support A2DP. In such case we will keep
784                      * AVRCP only connection.
785                      */
786                     if (p_bta_data->open.status != BTA_AV_FAIL_SDP)
787                     {
788                         BTIF_TRACE_WARNING("Disconnecting AVRCP ");
789                         BTA_AvCloseRc(btif_rc_get_connected_peer_handle(peer_addr));
790                     }
791                     else
792                     {
793                         BTIF_TRACE_WARNING("Keep AVRCP only connection");
794                     }
795                 }
796                 state = BTAV_CONNECTION_STATE_DISCONNECTED;
797                 av_state  = BTIF_AV_STATE_IDLE;
798             }
799
800             /* inform the application of the event */
801             btif_report_connection_state(state, &(btif_av_cb[index].peer_bda));
802             /* change state to open/idle based on the status */
803             btif_sm_change_state(btif_av_cb[index].sm_handle, av_state);
804             /* Check if the other connected AV is playing,
805             * If YES, trigger DUAL Handoff. */
806             if (p_bta_data->open.status == BTA_AV_SUCCESS)
807             {
808                 /* BTIF AV State updated, now check
809                  * and update multicast state
810                  */
811                 btif_av_update_multicast_state(index);
812
813                 /*This device should be now ready for all next playbacks*/
814                 btif_av_cb[index].current_playing = TRUE;
815                 if (enable_multicast == FALSE)
816                 {
817                     for (i = 0; i < btif_max_av_clients; i++)
818                     {
819                         //Other device is not current playing
820                         if (i != index)
821                             btif_av_cb[i].current_playing = FALSE;
822                     }
823                     /* In A2dp Multicast, stack will take care of starting
824                      * the stream on newly connected A2dp device. If Handoff
825                      * is supported, trigger Handoff here. */
826                     if (btif_av_is_playing())
827                     {
828                         BTIF_TRACE_DEBUG("Trigger Dual A2dp Handoff on %d", index);
829                         btif_av_trigger_dual_handoff(TRUE, btif_av_cb[index].peer_bda.address);
830                     }
831                 }
832                 if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
833                 {
834                     /* if queued PLAY command,  send it now */
835                     btif_rc_check_handle_pending_play(p_bta_data->open.bd_addr,
836                                 (p_bta_data->open.status == BTA_AV_SUCCESS));
837                 }
838                 else if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC)
839                 {
840                     /* if queued PLAY command,  send it now */
841                     btif_rc_check_handle_pending_play(p_bta_data->open.bd_addr, FALSE);
842                     /* Bring up AVRCP connection too */
843                     BTA_AvOpenRc(btif_av_cb[index].bta_handle);
844                 }
845             }
846             btif_queue_advance();
847         } break;
848
849         case BTIF_AV_SINK_CONFIG_REQ_EVT:
850         {
851             btif_av_sink_config_req_t req;
852             // copy to avoid alignment problems
853             memcpy(&req, p_data, sizeof(req));
854
855             BTIF_TRACE_WARNING("BTIF_AV_SINK_CONFIG_REQ_EVT %d %d", req.sample_rate,
856                     req.channel_count);
857             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC && bt_av_sink_callbacks != NULL) {
858                 HAL_CBACK(bt_av_sink_callbacks, audio_config_cb, &(btif_av_cb[index].peer_bda),
859                         req.sample_rate, req.channel_count);
860             }
861         } break;
862
863         case BTIF_AV_CONNECT_REQ_EVT:
864             // Check for device, if same device which moved to opening then ignore callback
865             if (memcmp ((bt_bdaddr_t*)p_data, &(btif_av_cb[index].peer_bda),
866                 sizeof(btif_av_cb[index].peer_bda)) == 0)
867             {
868                 BTIF_TRACE_DEBUG("%s: Same device moved to Opening state,ignore Connect Req", __func__);
869                 btif_queue_advance();
870                 break;
871             }
872             else
873             {
874                 BTIF_TRACE_DEBUG("%s: Moved from idle by Incoming Connection request", __func__);
875                 btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, (bt_bdaddr_t*)p_data);
876                 btif_queue_advance();
877                 break;
878             }
879
880         case BTA_AV_PENDING_EVT:
881             // Check for device, if same device which moved to opening then ignore callback
882             if (memcmp (((tBTA_AV*)p_data)->pend.bd_addr, &(btif_av_cb[index].peer_bda),
883                 sizeof(btif_av_cb[index].peer_bda)) == 0)
884             {
885                 BTIF_TRACE_DEBUG("%s: Same device moved to Opening state,ignore Pending Req", __func__);
886                 break;
887             }
888             else
889             {
890                 BTIF_TRACE_DEBUG("%s: Moved from idle by outgoing Connection request", __func__);
891                 BTA_AvDisconnect(((tBTA_AV*)p_data)->pend.bd_addr);
892                 break;
893             }
894
895         case BTIF_AV_OFFLOAD_START_REQ_EVT:
896             btif_a2dp_on_offload_started(BTA_AV_FAIL);
897             BTIF_TRACE_ERROR("BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started OPENING");
898             break;
899
900         case BTA_AV_CLOSE_EVT:
901             /* avdtp link is closed */
902             /* Check if any other device is playing
903             * and this is not the one.*/
904             if (!btif_av_is_playing())
905             {
906                 btif_a2dp_on_stopped(NULL);
907             }
908             /* inform the application that we are disconnected */
909             btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
910                     &(btif_av_cb[index].peer_bda));
911             btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_IDLE);
912             break;
913
914         case BTIF_AV_DISCONNECT_REQ_EVT:
915             btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
916                 &(btif_av_cb[index].peer_bda));
917             BTA_AvClose(btif_av_cb[index].bta_handle);
918             btif_queue_advance();
919             btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_IDLE);
920             break;
921
922         case BTA_AV_RC_OPEN_EVT:
923              btif_rc_handler(event, p_data);;
924             break;
925
926         CHECK_RC_EVENT(event, p_data);
927
928         default:
929             BTIF_TRACE_WARNING("%s : unhandled event:%s", __FUNCTION__,
930                                 dump_av_sm_event_name(event));
931             return FALSE;
932
933    }
934    return TRUE;
935 }
936
937 /*****************************************************************************
938 **
939 ** Function        btif_av_state_closing_handler
940 **
941 ** Description     Intermediate state managing events during closing
942 **                 of avdtp channel
943 **
944 ** Returns         TRUE if event was processed, FALSE otherwise
945 **
946 *******************************************************************************/
947
948 static BOOLEAN btif_av_state_closing_handler(btif_sm_event_t event, void *p_data, int index)
949 {
950     BTIF_TRACE_IMP("%s event:%s flags %x and index = %d", __FUNCTION__,
951                      dump_av_sm_event_name(event), btif_av_cb[index].flags, index);
952
953     switch (event)
954     {
955         case BTIF_SM_ENTER_EVT:
956             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
957             {
958                 /* Multicast/Soft Hand-off:
959                  * If MC/SHO is enabled we need to keep/start playing on
960                  * other device.
961                  */
962                 if (btif_av_is_connected_on_other_idx(index))
963                 {
964                     if (btif_av_is_playing())
965                     {
966                         APPL_TRACE_DEBUG("Keep playing on other device");
967                     }
968                     else
969                     {
970                         APPL_TRACE_DEBUG("Not playing on other devie: Set Flush");
971                         btif_a2dp_set_tx_flush(TRUE);
972                     }
973                 }
974                 else
975                 {
976                     /* Single connections scenario:
977                      * Immediately stop transmission of frames
978                      * wait for audioflinger to stop a2dp
979                      */
980                     btif_a2dp_set_tx_flush(TRUE);
981                 }
982             }
983             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC)
984             {
985                 btif_a2dp_set_rx_flush(TRUE);
986             }
987             break;
988
989         case BTA_AV_STOP_EVT:
990         case BTIF_AV_STOP_STREAM_REQ_EVT:
991             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
992             {
993                 /* Dont stop in DUAL A2dp connections, as
994                 * UIPC will keep waiting for Audio CTRL channel
995                 * to get closed which is not required in Dual A2dp.
996                 * We will stop only when only single A2dp conn is present.*/
997                 if (btif_av_is_connected_on_other_idx(index))
998                 {
999                     if (!btif_av_is_playing())
1000                     {
1001                         APPL_TRACE_WARNING("Suspend the AV Data channel");
1002                         //Flush and close media channel
1003                         btif_a2dp_set_tx_flush(TRUE);
1004                         btif_media_task_stop_aa_req();
1005                     }
1006                 }
1007                 else
1008                 {
1009                     /* immediately flush any pending tx frames while suspend is pending */
1010                     APPL_TRACE_WARNING("Stop the AV Data channel");
1011                     btif_a2dp_set_tx_flush(TRUE);
1012                     btif_a2dp_on_stopped(NULL);
1013                 }
1014             }
1015             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC)
1016             {
1017                 btif_a2dp_set_rx_flush(TRUE);
1018                 btif_a2dp_on_stopped(NULL);
1019             }
1020             break;
1021
1022         case BTIF_SM_EXIT_EVT:
1023             break;
1024
1025         case BTA_AV_CLOSE_EVT:
1026
1027             /* inform the application that we are disconnecting */
1028             btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb[index].peer_bda));
1029
1030             btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_IDLE);
1031             break;
1032
1033         /* Handle the RC_CLOSE event for the cleanup */
1034         case BTA_AV_RC_CLOSE_EVT:
1035             btif_rc_handler(event, (tBTA_AV*)p_data);
1036             break;
1037
1038         case BTIF_AV_OFFLOAD_START_REQ_EVT:
1039             btif_a2dp_on_offload_started(BTA_AV_FAIL);
1040             BTIF_TRACE_ERROR("BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started Closing");
1041             break;
1042
1043         default:
1044             BTIF_TRACE_WARNING("%s : unhandled event:%s", __FUNCTION__,
1045                                 dump_av_sm_event_name(event));
1046             return FALSE;
1047    }
1048    return TRUE;
1049 }
1050
1051 /*****************************************************************************
1052 **
1053 ** Function     btif_av_state_opened_handler
1054 **
1055 ** Description  Handles AV events while AVDTP is in OPEN state
1056 **
1057 ** Returns      TRUE if event was processed, FALSE otherwise
1058 **
1059 *******************************************************************************/
1060
1061 static BOOLEAN btif_av_state_opened_handler(btif_sm_event_t event, void *p_data, int index)
1062 {
1063     tBTA_AV *p_av = (tBTA_AV*)p_data;
1064
1065     BTIF_TRACE_IMP("%s event:%s flags %x and index = %d", __FUNCTION__,
1066                      dump_av_sm_event_name(event), btif_av_cb[index].flags, index);
1067
1068     if ((event == BTA_AV_REMOTE_CMD_EVT) &&
1069          (p_av->remote_cmd.rc_id == BTA_AV_RC_PLAY) )
1070     {
1071         for (int i = 0; i < btif_max_av_clients; i++)
1072         {
1073             if (btif_av_cb[i].flags & BTIF_AV_FLAG_REMOTE_SUSPEND)
1074             {
1075                 BTIF_TRACE_EVENT("%s: Resetting remote suspend flag on RC PLAY",
1076                         __FUNCTION__);
1077                 btif_av_cb[i].flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
1078             }
1079         }
1080     }
1081
1082     switch (event)
1083     {
1084         case BTIF_SM_ENTER_EVT:
1085             btif_av_cb[index].flags &= ~BTIF_AV_FLAG_PENDING_STOP;
1086             btif_av_cb[index].flags &= ~BTIF_AV_FLAG_PENDING_START;
1087             break;
1088
1089         case BTIF_SM_EXIT_EVT:
1090             break;
1091
1092         case BTIF_AV_START_STREAM_REQ_EVT:
1093             /* update multicast state here if new device is connected
1094              * after A2dp connection. New A2dp device is connected
1095              * whlie playing */
1096             btif_av_update_multicast_state(index);
1097             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC)
1098             {
1099                 BTA_AvStart(btif_av_cb[index].bta_handle);
1100                 btif_av_cb[index].flags |= BTIF_AV_FLAG_PENDING_START;
1101                 break;
1102             }
1103             tBTIF_STATUS status = btif_a2dp_setup_codec(btif_av_cb[index].bta_handle);
1104             if (status == BTIF_SUCCESS)
1105             {
1106                 int idx = 0;
1107                 BTA_AvStart(btif_av_cb[index].bta_handle);
1108                 if (enable_multicast == TRUE)
1109                 {
1110                     /* In A2dp Multicast, DUT initiated stream request
1111                     * should be true for all connected A2dp devices. */
1112                     for (; idx < btif_max_av_clients; idx++)
1113                     {
1114                         btif_av_cb[idx].flags |= BTIF_AV_FLAG_PENDING_START;
1115                     }
1116                 }
1117                 else
1118                 {
1119                     btif_av_cb[index].flags |= BTIF_AV_FLAG_PENDING_START;
1120                 }
1121             }
1122             else if (status == BTIF_ERROR_SRV_AV_CP_NOT_SUPPORTED)
1123             {
1124 #if defined(BTA_AV_DISCONNECT_IF_NO_SCMS_T) && (BTA_AV_DISCONNECT_IF_NO_SCMS_T == TRUE)
1125                 BTIF_TRACE_ERROR0("SCMST enabled, disconnect as remote does not support SCMST");
1126                 BTA_AvDisconnect(btif_av_cb[index].peer_bda.address);
1127 #else
1128                 BTIF_TRACE_WARNING("SCMST enabled, connecting to non SCMST SEP");
1129                 BTA_AvStart(btif_av_cb[index].bta_handle);
1130                 btif_av_cb[index].flags |= BTIF_AV_FLAG_PENDING_START;
1131 #endif
1132             }
1133             else
1134             {
1135                 BTIF_TRACE_ERROR("## AV Disconnect## status : %x",status);
1136                 BTA_AvDisconnect(btif_av_cb[index].peer_bda.address);
1137             }
1138             break;
1139
1140         case BTIF_AV_UPDATE_ENCODER_REQ_EVT:
1141             btif_a2dp_update_codec();
1142             break;
1143
1144         case BTA_AV_START_EVT:
1145         {
1146             BTIF_TRACE_DEBUG("BTA_AV_START_EVT status %d, suspending %d, init %d",
1147                 p_av->start.status, p_av->start.suspending, p_av->start.initiator);
1148             BTIF_TRACE_DEBUG("BTA_AV_START_EVT role: %d", p_av->start.role);
1149             if (p_av->start.role == HOST_ROLE_SLAVE)
1150             {
1151                 btif_av_cb[index].is_slave = TRUE;
1152             }
1153             else
1154             {
1155                 // update if we are master after role switch before start
1156                 btif_av_cb[index].is_slave = FALSE;
1157             }
1158             /* There can be role switch after device is connected,
1159              * hence check for role before starting multicast, and
1160              * disable if we are in slave role for any connection
1161              */
1162             btif_av_update_multicast_state(index);
1163
1164             if ((p_av->start.status == BTA_SUCCESS) && (p_av->start.suspending == TRUE))
1165                 return TRUE;
1166
1167             /* if remote tries to start a2dp when call is in progress, suspend it right away */
1168             if ((!(btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START)) && (!btif_hf_is_call_idle())) {
1169                 BTIF_TRACE_EVENT("%s: trigger suspend as call is in progress!!", __FUNCTION__);
1170                 btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0);
1171             }
1172
1173             /* if remote tries to start a2dp when DUT is a2dp source
1174              * then suspend. In case a2dp is sink and call is active
1175              * then disconnect the AVDTP channel
1176              */
1177             if (!(btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START))
1178             {
1179                 if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
1180                 {
1181                     if (enable_multicast)
1182                     {
1183                         /* Stack will start the playback on newly connected
1184                          * A2dp device, if the playback is already happening on
1185                          * other connected device.*/
1186                         if (btif_av_is_playing())
1187                         {
1188                             /* when HS2 is connected during HS1 playing, stack directly
1189                              * sends start event hence update encoder so that least L2CAP
1190                              *  MTU is selected.*/
1191                             //btif_a2dp_update_codec();
1192                             BTIF_TRACE_DEBUG("%s: A2dp Multicast playback",
1193                                     __FUNCTION__);
1194                         }
1195                         /* initiate suspend if start is initiate by remote and multicast
1196                          * is enabled.
1197                          * Avoid suspend if stream is started as quick suspend-start
1198                          * creates IOT issue, seen with SBH50.
1199                          */
1200
1201                         if (!p_av->start.initiator && !btif_av_is_playing())
1202                         {
1203                             BTIF_TRACE_DEBUG("initiate suspend for remote start");
1204                             btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0);
1205                         }
1206                     }
1207                     else
1208                     {
1209                         if ((btif_av_cb[index].flags & BTIF_AV_FLAG_REMOTE_SUSPEND))
1210                         {
1211                             BTIF_TRACE_DEBUG("%s: clear remote suspend flag on remote start",
1212                                 __FUNCTION__);
1213                             btif_av_cb[index].flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
1214                         }
1215                         else
1216                         {
1217                             BTIF_TRACE_DEBUG("%s: trigger suspend as remote initiated!!",
1218                                 __FUNCTION__);
1219                             btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0);
1220                         }
1221                     }
1222                 }
1223             }
1224
1225             /* remain in open state if status failed */
1226             /* Multicast-soft Handoff:
1227              * START failed, cleanup Handoff flag.
1228              */
1229             if (p_av->start.status != BTA_AV_SUCCESS)
1230             {
1231                 int i;
1232
1233                 /* In case peer is A2DP SRC we do not want to ack commands on UIPC */
1234                 if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
1235                 {
1236                     if (btif_a2dp_on_started(&p_av->start,
1237                         ((btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START) != 0),
1238                         btif_av_cb[index].bta_handle))
1239                     {
1240                         /* only clear pending flag after acknowledgement */
1241                         btif_av_cb[index].flags &= ~BTIF_AV_FLAG_PENDING_START;
1242                     }
1243                 }
1244                 /* Clear dual handoff flag */
1245                 for (i = 0; i < btif_max_av_clients; i++)
1246                 {
1247                     btif_av_cb[i].dual_handoff = FALSE;
1248                 }
1249                 return FALSE;
1250             }
1251
1252 #ifndef AVK_BACKPORT
1253             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC)
1254             {
1255                 btif_a2dp_set_rx_flush(FALSE); /*  remove flush state, ready for streaming*/
1256             }
1257 #endif
1258
1259             btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_STARTED);
1260
1261         } break;
1262
1263         case BTIF_AV_DISCONNECT_REQ_EVT:
1264             BTA_AvClose(btif_av_cb[index].bta_handle);
1265             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC) {
1266                 BTA_AvCloseRc(btif_av_cb[index].bta_handle);
1267             }
1268
1269             /* inform the application that we are disconnecting */
1270             btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTING, &(btif_av_cb[index].peer_bda));
1271             break;
1272
1273         case BTA_AV_CLOSE_EVT:
1274              /* avdtp link is closed */
1275              /*Dont close the A2dp when Dual playback is happening*/
1276              if (btif_av_is_connected_on_other_idx(index))
1277              {
1278                  APPL_TRACE_WARNING("Conn is closing,close AV data channel");
1279                  if (!btif_av_is_playing())
1280                  {
1281                      APPL_TRACE_WARNING("Suspend the AV Data channel");
1282                      /* ensure tx frames are immediately suspended */
1283                      btif_a2dp_set_tx_flush(TRUE);
1284                      btif_media_task_stop_aa_req();
1285                  }
1286              }
1287              else
1288              {
1289                  APPL_TRACE_WARNING("Stop the AV Data channel");
1290                  btif_a2dp_on_stopped(NULL);
1291              }
1292
1293             /* inform the application that we are disconnected */
1294             btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
1295                                         &(btif_av_cb[index].peer_bda));
1296
1297             /* change state to idle, send acknowledgement if start is pending */
1298             if (btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START) {
1299                 btif_a2dp_ack_fail();
1300                 btif_av_cb[index].flags &= ~BTIF_AV_FLAG_PENDING_START;
1301             }
1302
1303             btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_IDLE);
1304             break;
1305
1306         case BTA_AV_RECONFIG_EVT:
1307             if((btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START) &&
1308                 (p_av->reconfig.status == BTA_AV_SUCCESS))
1309             {
1310                APPL_TRACE_WARNING("reconfig done BTA_AVstart()");
1311                BTA_AvStart(btif_av_cb[index].bta_handle);
1312             }
1313             else if(btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START)
1314             {
1315                btif_av_cb[index].flags &= ~BTIF_AV_FLAG_PENDING_START;
1316                btif_a2dp_ack_fail();
1317             }
1318             break;
1319
1320         case BTIF_AV_CONNECT_REQ_EVT:
1321             if (memcmp ((bt_bdaddr_t*)p_data, &(btif_av_cb[index].peer_bda),
1322                 sizeof(btif_av_cb[index].peer_bda)) == 0)
1323             {
1324                 BTIF_TRACE_DEBUG("%s: Ignore BTIF_AV_CONNECT_REQ_EVT for same device", __func__);
1325             }
1326             else
1327             {
1328                 BTIF_TRACE_DEBUG("%s: Moved to opened by Other Incoming Conn req", __func__);
1329                 btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
1330                         (bt_bdaddr_t*)p_data);
1331             }
1332             btif_queue_advance();
1333             break;
1334
1335         case BTIF_AV_OFFLOAD_START_REQ_EVT:
1336             btif_a2dp_on_offload_started(BTA_AV_FAIL);
1337             BTIF_TRACE_ERROR("BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started Opened");
1338             break;
1339
1340         case BTA_AV_RC_OPEN_EVT:
1341             btif_av_check_rc_connection_priority(p_data);
1342             break;
1343         CHECK_RC_EVENT(event, p_data);
1344
1345         default:
1346             BTIF_TRACE_WARNING("%s : unhandled event:%s", __FUNCTION__,
1347                                dump_av_sm_event_name(event));
1348             return FALSE;
1349
1350     }
1351     return TRUE;
1352 }
1353
1354 /*****************************************************************************
1355 **
1356 ** Function     btif_av_state_started_handler
1357 **
1358 ** Description  Handles AV events while A2DP stream is started
1359 **
1360 ** Returns      TRUE if event was processed, FALSE otherwise
1361 **
1362 *******************************************************************************/
1363
1364 static BOOLEAN btif_av_state_started_handler(btif_sm_event_t event, void *p_data, int index)
1365 {
1366     tBTA_AV *p_av = (tBTA_AV*)p_data;
1367     btif_sm_state_t state = BTIF_AV_STATE_IDLE;
1368     int i;
1369
1370     BTIF_TRACE_IMP("%s event:%s flags %x  index =%d", __FUNCTION__,
1371                      dump_av_sm_event_name(event), btif_av_cb[index].flags, index);
1372
1373     switch (event)
1374     {
1375         case BTIF_SM_ENTER_EVT:
1376             /*Ack from entry point of started handler instead of open state to avoid race condition*/
1377             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
1378             {
1379                 if (btif_a2dp_on_started(&p_av->start,
1380                     ((btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START) != 0),
1381                       btif_av_cb[index].bta_handle))
1382                 {
1383                     /* only clear pending flag after acknowledgement */
1384                     btif_av_cb[index].flags &= ~BTIF_AV_FLAG_PENDING_START;
1385                 }
1386             }
1387
1388             /* Already changed state to started, send acknowledgement if start is pending */
1389             if (btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START) {
1390                 if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
1391                     btif_a2dp_on_started(NULL, TRUE, btif_av_cb[index].bta_handle);
1392                 btif_av_cb[index].flags &= ~BTIF_AV_FLAG_PENDING_START;
1393             }
1394
1395             /* we are again in started state, clear any remote suspend flags */
1396             btif_av_cb[index].flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
1397
1398             btif_report_audio_state(BTAV_AUDIO_STATE_STARTED, &(btif_av_cb[index].peer_bda));
1399             btif_av_cb[index].is_device_playing = TRUE;
1400
1401             /* increase the a2dp consumer task priority temporarily when start
1402             ** audio playing, to avoid overflow the audio packet queue. */
1403             adjust_priority_a2dp(TRUE);
1404 #ifdef AVK_BACKPORT
1405             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC)
1406             {
1407                 btif_av_request_audio_focus(TRUE);
1408             }
1409 #endif
1410             //Clear Dual Handoff for all SCBs
1411             for (i = 0; i < btif_max_av_clients; i++)
1412             {
1413                 btif_av_cb[i].dual_handoff = FALSE;
1414                 //Other device is not current playing
1415                 if (i != index)
1416                     btif_av_cb[i].current_playing = FALSE;
1417             }
1418             //This is latest device to play now
1419             btif_av_cb[index].current_playing = TRUE;
1420             break;
1421
1422         case BTIF_SM_EXIT_EVT:
1423             /* restore the a2dp consumer task priority when stop audio playing. */
1424             adjust_priority_a2dp(FALSE);
1425
1426             break;
1427
1428         case BTIF_AV_START_STREAM_REQ_EVT:
1429             /* we were remotely started, just ack back the local request */
1430             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
1431                 btif_a2dp_on_started(NULL, TRUE, btif_av_cb[index].bta_handle);
1432             break;
1433
1434         case BTIF_AV_UPDATE_ENCODER_REQ_EVT:
1435             btif_a2dp_update_codec();
1436             break;
1437
1438         /* fixme -- use suspend = true always to work around issue with BTA AV */
1439         case BTIF_AV_STOP_STREAM_REQ_EVT:
1440         case BTIF_AV_SUSPEND_STREAM_REQ_EVT:
1441
1442             /* set pending flag to ensure btif task is not trying to restart
1443              * stream while suspend is in progress.
1444              * Multicast: If streaming is happening on both devices, we need
1445              * to update flag for both connections as SUSPEND request will
1446              * be sent to only one stream as internally BTA takes care of
1447              * suspending both streams.
1448              */
1449             for(i = 0; i < btif_max_av_clients; i++)
1450             {
1451                 state = btif_sm_get_state(btif_av_cb[i].sm_handle);
1452                 if (state == BTIF_AV_STATE_STARTED)
1453                 {
1454                     btif_av_cb[i].flags |= BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING;
1455                 }
1456             }
1457
1458             /* if we were remotely suspended but suspend locally, local suspend
1459                always overrides */
1460             btif_av_cb[index].flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
1461
1462             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
1463             {
1464             /* immediately stop transmission of frames while suspend is pending */
1465                 btif_a2dp_set_tx_flush(TRUE);
1466             }
1467
1468             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC) {
1469                 btif_a2dp_set_rx_flush(TRUE);
1470                 btif_a2dp_on_stopped(NULL);
1471             }
1472
1473             BTA_AvStop(TRUE, btif_av_cb[index].bta_handle);
1474             break;
1475
1476         case BTIF_AV_DISCONNECT_REQ_EVT:
1477
1478             //Now it is not the current playing
1479             btif_av_cb[index].current_playing = FALSE;
1480             btif_av_update_current_playing_device(index);
1481             btif_rc_clear_priority(btif_av_cb[index].peer_bda.address);
1482             if (bt_split_a2dp_enabled && btif_av_is_connected_on_other_idx(index))
1483             {
1484                /*Fake handoff state to switch streaming to other coddeced
1485                   device */
1486                 btif_av_cb[index].dual_handoff = TRUE;
1487             }
1488             /* request avdtp to close */
1489             BTA_AvClose(btif_av_cb[index].bta_handle);
1490             if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC) {
1491                 BTA_AvCloseRc(btif_av_cb[index].bta_handle);
1492             }
1493
1494             /* inform the application that we are disconnecting */
1495             btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTING, &(btif_av_cb[index].peer_bda));
1496
1497             /* wait in closing state until fully closed */
1498             btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_CLOSING);
1499             if (bt_split_a2dp_enabled &&
1500                 btif_av_is_connected_on_other_idx(index))
1501             {
1502                 BTIF_TRACE_DEBUG("%s: Notify framework to reconfig",__func__);
1503                 int idx = btif_av_get_other_connected_idx(index);
1504                 /* Fix for below Klockwork Issue
1505                  * Array 'btif_av_cb' of size 2 may use index value(s) -1 */
1506                 if (idx != INVALID_INDEX)
1507                 {
1508                     HAL_CBACK(bt_av_src_callbacks, reconfig_a2dp_trigger_cb, 1,
1509                                                     &(btif_av_cb[idx].peer_bda));
1510                 }
1511             }
1512             break;
1513
1514         case BTA_AV_SUSPEND_EVT:
1515
1516             BTIF_TRACE_EVENT("BTA_AV_SUSPEND_EVT status %d, init %d",
1517                  p_av->suspend.status, p_av->suspend.initiator);
1518             //Check if this suspend is due to DUAL_Handoff
1519             if ((btif_av_cb[index].dual_handoff) &&
1520                 (p_av->suspend.status == BTA_AV_SUCCESS))
1521             {
1522                 BTIF_TRACE_EVENT("BTA_AV_SUSPEND_EVT: Dual handoff");
1523                 btif_dispatch_sm_event(BTIF_AV_START_STREAM_REQ_EVT, NULL, 0);
1524             }
1525             if (p_av->suspend.initiator != TRUE)
1526             {
1527                 /* remote suspend, notify HAL and await audioflinger to
1528                  * suspend/stop stream
1529                  * set remote suspend flag to block media task from restarting
1530                  * stream only if we did not already initiate a local suspend
1531                  * set remote suspend flag before suspending stream as in race conditions
1532                  * when stream is suspended, but flag is things ge tossed up
1533                  */
1534                 BTIF_TRACE_EVENT("Clear before suspending");
1535                 if ((btif_av_cb[index].flags & BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING) == 0)
1536                     btif_av_cb[index].flags |= BTIF_AV_FLAG_REMOTE_SUSPEND;
1537                 for (int i = 0; i < btif_max_av_clients; i++)
1538                 {
1539                     if ((i != index) && btif_av_get_ongoing_multicast())
1540                     {
1541                         multicast_disabled = TRUE;
1542                         btif_av_update_multicast_state(index);
1543                         BTIF_TRACE_EVENT("Initiate suspend for other HS also");
1544                         btif_sm_dispatch(btif_av_cb[i].sm_handle,
1545                                 BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL);
1546                     }
1547                 }
1548             }
1549
1550             /* a2dp suspended, stop media task until resumed */
1551             /* Multicast: If streaming on other device, don't call onsuspended
1552              * as it unblocks the audio process and audio process may send
1553              * subsequent commands and create problem during the time where we
1554              * still did not receive response for SUSPEND sent to other device.
1555              * Keep the suspend failure handling untouched and handle
1556              * only success case to check and avoid calling onsuspended.
1557              */
1558             if ((p_av->suspend.status != BTA_AV_SUCCESS) ||
1559                 !btif_av_is_playing_on_other_idx(index))
1560             {
1561                 btif_a2dp_on_suspended(&p_av->suspend);
1562             }
1563             else if(btif_av_is_playing_on_other_idx(index))
1564             {
1565                 BTIF_TRACE_DEBUG("Other device not suspended, don't ack the suspend");
1566             }
1567
1568             /* if not successful, remain in current state */
1569             if (p_av->suspend.status != BTA_AV_SUCCESS)
1570             {
1571                 btif_av_cb[index].flags &= ~BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING;
1572
1573                if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
1574                {
1575                 /* suspend failed, reset back tx flush state */
1576                     btif_a2dp_set_tx_flush(FALSE);
1577                }
1578                 return FALSE;
1579             }
1580
1581             if (p_av->suspend.initiator != TRUE)
1582             {
1583                 btif_report_audio_state(BTAV_AUDIO_STATE_REMOTE_SUSPEND, &(btif_av_cb[index].peer_bda));
1584             }
1585             else
1586             {
1587                 btif_report_audio_state(BTAV_AUDIO_STATE_REMOTE_SUSPEND, &(btif_av_cb[index].peer_bda));
1588             }
1589             btif_av_cb[index].is_device_playing = FALSE;
1590             btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_OPENED);
1591
1592             /* suspend completed and state changed, clear pending status */
1593             btif_av_cb[index].flags &= ~BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING;
1594             break;
1595
1596         case BTA_AV_STOP_EVT:
1597
1598             btif_av_cb[index].flags |= BTIF_AV_FLAG_PENDING_STOP;
1599             btif_av_cb[index].current_playing = FALSE;
1600             if (btif_av_is_connected_on_other_idx(index))
1601             {
1602                 if (enable_multicast == FALSE)
1603                 {
1604                     APPL_TRACE_WARNING("other Idx is connected, move to SUSPENDED");
1605                     if (!bt_split_a2dp_enabled) {
1606                         btif_rc_send_pause_command();
1607                     }
1608                     btif_a2dp_on_stopped(&p_av->suspend);
1609                 }
1610             }
1611             else
1612             {
1613                 APPL_TRACE_WARNING("Stop the AV Data channel as no connection is present");
1614                 btif_a2dp_on_stopped(&p_av->suspend);
1615             }
1616             btif_av_cb[index].is_device_playing = FALSE;
1617
1618
1619             btif_report_audio_state(BTAV_AUDIO_STATE_STOPPED, &(btif_av_cb[index].peer_bda));
1620             /* if stop was successful, change state to open */
1621             if (p_av->suspend.status == BTA_AV_SUCCESS)
1622                 btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_OPENED);
1623
1624             if (bt_split_a2dp_enabled &&
1625                 btif_av_is_connected_on_other_idx(index))
1626             {
1627                /*Fake handoff state to switch streaming to other coddeced
1628                   device */
1629                 btif_av_cb[index].dual_handoff = TRUE;
1630                 BTIF_TRACE_DEBUG("%s: Notify framework to reconfig",__func__);
1631                 int idx = btif_av_get_other_connected_idx(index);
1632                 /* Fix for below Klockwork Issue
1633                  * Array 'btif_av_cb' of size 2 may use index value(s) -1 */
1634                 if (idx != INVALID_INDEX)
1635                 {
1636                     HAL_CBACK(bt_av_src_callbacks, reconfig_a2dp_trigger_cb, 1,
1637                                                     &(btif_av_cb[idx].peer_bda));
1638                 }
1639             }
1640
1641             break;
1642
1643         case BTA_AV_CLOSE_EVT:
1644
1645              btif_av_cb[index].flags |= BTIF_AV_FLAG_PENDING_STOP;
1646
1647             /* avdtp link is closed */
1648             APPL_TRACE_WARNING("Stop the AV Data channel");
1649             btif_a2dp_on_stopped(NULL);
1650
1651             /* inform the application that we are disconnected */
1652             btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
1653                                         &(btif_av_cb[index].peer_bda));
1654
1655             btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_IDLE);
1656             break;
1657
1658         case BTA_AV_RC_OPEN_EVT:
1659             btif_av_check_rc_connection_priority(p_data);
1660             break;
1661
1662         case BTIF_AV_OFFLOAD_START_REQ_EVT:
1663             BTA_AvOffloadStart(btif_av_cb[index].bta_handle);
1664             break;
1665
1666         case BTA_AV_OFFLOAD_START_RSP_EVT:
1667             btif_a2dp_on_offload_started(p_av->status);
1668             break;
1669
1670         CHECK_RC_EVENT(event, p_data);
1671
1672         default:
1673             BTIF_TRACE_WARNING("%s : unhandled event:%s", __FUNCTION__,
1674                                  dump_av_sm_event_name(event));
1675             return FALSE;
1676
1677     }
1678     return TRUE;
1679 }
1680
1681
1682 void btif_av_event_deep_copy(UINT16 event, char *p_dest, char *p_src)
1683 {
1684     tBTA_AV *av_src = (tBTA_AV *)p_src;
1685     tBTA_AV *av_dest = (tBTA_AV *)p_dest;
1686
1687     // First copy the structure
1688     maybe_non_aligned_memcpy(av_dest, av_src, sizeof(*av_src));
1689
1690     switch (event)
1691     {
1692         case BTA_AV_META_MSG_EVT:
1693             if (av_src->meta_msg.p_data && av_src->meta_msg.len)
1694             {
1695                 av_dest->meta_msg.p_data = osi_calloc(av_src->meta_msg.len);
1696                 memcpy(av_dest->meta_msg.p_data, av_src->meta_msg.p_data,
1697                        av_src->meta_msg.len);
1698             }
1699
1700             if (av_src->meta_msg.p_msg)
1701             {
1702                 av_dest->meta_msg.p_msg = osi_calloc(sizeof(tAVRC_MSG));
1703                 memcpy(av_dest->meta_msg.p_msg, av_src->meta_msg.p_msg,
1704                        sizeof(tAVRC_MSG));
1705
1706                 if (av_src->meta_msg.p_msg->vendor.p_vendor_data &&
1707                     av_src->meta_msg.p_msg->vendor.vendor_len)
1708                 {
1709                     av_dest->meta_msg.p_msg->vendor.p_vendor_data = osi_calloc(
1710                         av_src->meta_msg.p_msg->vendor.vendor_len);
1711                     memcpy(av_dest->meta_msg.p_msg->vendor.p_vendor_data,
1712                         av_src->meta_msg.p_msg->vendor.p_vendor_data,
1713                         av_src->meta_msg.p_msg->vendor.vendor_len);
1714                 }
1715             }
1716             break;
1717         case BTA_AV_BROWSE_MSG_EVT:
1718             if (av_src->browse_msg.p_msg)
1719             {
1720                 av_dest->browse_msg.p_msg = osi_calloc(sizeof(tAVRC_MSG));
1721                 assert(av_dest->browse_msg.p_msg);
1722                 memcpy(av_dest->browse_msg.p_msg, av_src->browse_msg.p_msg, sizeof(tAVRC_MSG));
1723
1724                 if (av_src->browse_msg.p_msg->browse.p_browse_data &&
1725                     av_src->browse_msg.p_msg->browse.browse_len)
1726                 {
1727                     av_dest->browse_msg.p_msg->browse.p_browse_data = osi_calloc(
1728                         av_src->browse_msg.p_msg->browse.browse_len);
1729                     assert(av_dest->browse_msg.p_msg->browse.p_browse_data);
1730                     memcpy(av_dest->browse_msg.p_msg->browse.p_browse_data,
1731                         av_src->browse_msg.p_msg->browse.p_browse_data,
1732                         av_src->browse_msg.p_msg->browse.browse_len);
1733                 }
1734             }
1735             break;
1736
1737         default:
1738             break;
1739     }
1740 }
1741
1742 static void btif_av_event_free_data(btif_sm_event_t event, void *p_data)
1743 {
1744     switch (event)
1745     {
1746         case BTA_AV_META_MSG_EVT:
1747             {
1748                 tBTA_AV *av = (tBTA_AV *)p_data;
1749                 osi_free_and_reset((void **)&av->meta_msg.p_data);
1750
1751                 if (av->meta_msg.p_msg) {
1752                     osi_free(av->meta_msg.p_msg->vendor.p_vendor_data);
1753                     osi_free_and_reset((void **)&av->meta_msg.p_msg);
1754                 }
1755             }
1756             break;
1757         case BTA_AV_BROWSE_MSG_EVT:
1758             {
1759                 tBTA_AV *av = (tBTA_AV*)p_data;
1760
1761                 if (av->browse_msg.p_msg)
1762                 {
1763                     if (av->browse_msg.p_msg->browse.p_browse_data)
1764                         osi_free(av->browse_msg.p_msg->browse.p_browse_data);
1765                     osi_free(av->browse_msg.p_msg);
1766                 }
1767             }
1768             break;
1769
1770         default:
1771             break;
1772     }
1773 }
1774
1775 /*****************************************************************************
1776 **  Local event handlers
1777 ******************************************************************************/
1778
1779 static void btif_av_handle_event(UINT16 event, char* p_param)
1780 {
1781     int index = 0;
1782     tBTA_AV *p_bta_data = (tBTA_AV*)p_param;
1783     bt_bdaddr_t * bt_addr;
1784     UINT8 role;
1785     int uuid;
1786
1787     switch (event)
1788     {
1789         case BTIF_AV_INIT_REQ_EVT:
1790             BTIF_TRACE_IMP("%s: BTIF_AV_INIT_REQ_EVT", __FUNCTION__);
1791             if(btif_a2dp_start_media_task())
1792                 btif_a2dp_on_init();
1793             break;
1794         /*events from Upper layer and Media Task*/
1795         case BTIF_AV_CLEANUP_REQ_EVT: /*Clean up to be called on default index*/
1796             BTIF_TRACE_IMP("%s: BTIF_AV_CLEANUP_REQ_EVT", __FUNCTION__);
1797             uuid = (int)*p_param;
1798             if (uuid == BTA_A2DP_SOURCE_SERVICE_ID)
1799             {
1800                 if (bt_av_src_callbacks)
1801                 {
1802                     bt_av_src_callbacks = NULL;
1803                     if (bt_av_sink_callbacks != NULL)
1804                         break;
1805                 }
1806             }
1807             else
1808             {
1809                 if (bt_av_sink_callbacks)
1810                 {
1811                     bt_av_sink_callbacks = NULL;
1812                     if (bt_av_src_callbacks != NULL)
1813                         break;
1814                 }
1815             }
1816
1817             btif_a2dp_stop_media_task();
1818             return;
1819         case BTIF_AV_CONNECT_REQ_EVT:
1820             break;
1821         case BTIF_AV_DISCONNECT_REQ_EVT:
1822             /*Bd address passed should help us in getting the handle*/
1823             bt_addr = (bt_bdaddr_t *)p_param;
1824             index = btif_av_idx_by_bdaddr(bt_addr->address);
1825 #ifdef BTA_AV_SPLIT_A2DP_ENABLED
1826             if (bt_split_a2dp_enabled && (btif_av_get_current_playing_dev_idx() == index))
1827             {
1828                 BTIF_TRACE_DEBUG("%s:Disconnecting playing device,send VS STOP",__func__);
1829                 btif_media_on_stop_vendor_command();
1830             }
1831 #endif
1832             break;
1833         case BTIF_AV_UPDATE_ENCODER_REQ_EVT:
1834         case BTIF_AV_START_STREAM_REQ_EVT:
1835             /* Get the last connected device on which START can be issued
1836             * Get the Dual A2dp Handoff Device first, if none is present,
1837             * go for lastest connected.
1838             * In A2dp Multicast, the index selected can be any of the
1839             * connected device. Stack will ensure to START the steaming
1840             * on both the devices. */
1841             index = btif_get_latest_device_idx_to_start();
1842             break;
1843         case BTIF_AV_STOP_STREAM_REQ_EVT:
1844         case BTIF_AV_SUSPEND_STREAM_REQ_EVT:
1845             /*Should be handled by current STARTED*/
1846 #ifdef BTA_AV_SPLIT_A2DP_ENABLED
1847             if (bt_split_a2dp_enabled)
1848                 btif_media_on_stop_vendor_command();
1849 #endif
1850             index = btif_get_latest_playing_device_idx();
1851             break;
1852         /*Events from the stack, BTA*/
1853         case BTA_AV_ENABLE_EVT:
1854             index = 0;
1855             break;
1856         case BTA_AV_REGISTER_EVT:
1857             index = HANDLE_TO_INDEX(p_bta_data->registr.hndl);
1858             break;
1859         case BTA_AV_OPEN_EVT:
1860             index = HANDLE_TO_INDEX(p_bta_data->open.hndl);
1861             break;
1862         case BTA_AV_ROLE_CHANGED_EVT:
1863             index = HANDLE_TO_INDEX(p_bta_data->role_changed.hndl);
1864             role = p_bta_data->role_changed.new_role;
1865             BTIF_TRACE_EVENT("Role change: 0x%x: new role: %s",
1866                 p_bta_data->role_changed.hndl, (role == HOST_ROLE_SLAVE) ? "Slave" : "Master");
1867             if (index >= 0 && index < btif_max_av_clients)
1868             {
1869                 btif_av_cb[index].is_slave = (role == HOST_ROLE_SLAVE) ? TRUE : FALSE;
1870                 btif_av_update_multicast_state(index);
1871             }
1872             else
1873             {
1874                 BTIF_TRACE_ERROR("%s: Invalid index for connection", __FUNCTION__);
1875             }
1876             return;
1877         case BTA_AV_PENDING_EVT:
1878             index = HANDLE_TO_INDEX(p_bta_data->pend.hndl);
1879             break;
1880         case BTA_AV_REJECT_EVT:
1881             index = HANDLE_TO_INDEX(p_bta_data->reject.hndl);
1882             break;
1883         case BTA_AV_STOP_EVT:
1884             index = HANDLE_TO_INDEX(p_bta_data->suspend.hndl);
1885             break;
1886         case BTA_AV_CLOSE_EVT:
1887             index = HANDLE_TO_INDEX(p_bta_data->close.hndl);
1888             break;
1889         case BTA_AV_START_EVT:
1890             index = HANDLE_TO_INDEX(p_bta_data->start.hndl);
1891             break;
1892         case BTA_AV_RECONFIG_EVT:
1893             index = HANDLE_TO_INDEX(p_bta_data->reconfig.hndl);
1894             break;
1895         case BTA_AV_SUSPEND_EVT:
1896             index = HANDLE_TO_INDEX(p_bta_data->suspend.hndl);
1897             break;
1898
1899         /* Handle all RC events on default index. RC handling should take
1900          * care of the events. All events come with BD Address
1901          * Handled well in AV Opening, opened and started state
1902          * AV Idle handler needs to take care of this event properly.
1903          */
1904         case BTA_AV_RC_OPEN_EVT:
1905             index = btif_av_get_valid_idx_for_rc_events(p_bta_data->rc_open.peer_addr,
1906                     p_bta_data->rc_open.rc_handle);
1907             break;
1908         case BTA_AV_RC_CLOSE_EVT:
1909         /* If there is no entry in the connection table
1910          * RC handler has to be called for cleanup.
1911          * Directly call the RC handler as we cannot
1912          * associate any AV handle to it.
1913          */
1914             index = btif_av_idx_by_bdaddr(p_bta_data->rc_open.peer_addr);
1915             if (index == btif_max_av_clients)
1916             {
1917                 btif_rc_handler(event, p_bta_data);
1918             }
1919             break;
1920         /* Let the RC handler decide on these passthrough cmds
1921          * Use rc_handle to get the active AV device and use that mapping.
1922          */
1923         case BTA_AV_REMOTE_CMD_EVT:
1924         case BTA_AV_VENDOR_CMD_EVT:
1925         case BTA_AV_META_MSG_EVT:
1926         case BTA_AV_RC_FEAT_EVT:
1927         case BTA_AV_BROWSE_MSG_EVT:
1928             index = 0;
1929             BTIF_TRACE_EVENT("RC events: on index = %d", index);
1930             break;
1931         default:
1932             BTIF_TRACE_ERROR("Unhandled event = %d", event);
1933             break;
1934     }
1935     BTIF_TRACE_DEBUG("Handle the AV event = %x on index = %d", event, index);
1936     if (index >= 0 && index < btif_max_av_clients)
1937         btif_sm_dispatch(btif_av_cb[index].sm_handle, event, (void*)p_param);
1938     else
1939         BTIF_TRACE_ERROR("Unhandled Index = %d", index);
1940     btif_av_event_free_data(event, p_param);
1941
1942 }
1943
1944 /*******************************************************************************
1945 **
1946 ** Function         btif_av_get_valid_idx
1947 **
1948 ** Description      Check the validity of the current index for the connection
1949 **
1950 ** Returns          Boolean
1951 **
1952 *******************************************************************************/
1953
1954 static BOOLEAN btif_av_get_valid_idx(int idx)
1955 {
1956     btif_sm_state_t state = btif_sm_get_state(btif_av_cb[idx].sm_handle);
1957     return ((state == BTIF_AV_STATE_OPENED) ||
1958             (state ==  BTIF_AV_STATE_STARTED) ||
1959             (state == BTIF_AV_STATE_OPENING));
1960 }
1961
1962 /*******************************************************************************
1963 **
1964 ** Function         btif_av_idx_by_bdaddr
1965 **
1966 ** Description      Get the index corresponding to BD addr
1967 **
1968 ** Returns          UNIT8
1969 **
1970 *******************************************************************************/
1971
1972 static UINT8 btif_av_idx_by_bdaddr(BD_ADDR bd_addr)
1973 {
1974     int i;
1975     for (i = 0; i < btif_max_av_clients; i++)
1976     {
1977         if ((bdcmp(bd_addr,
1978                   btif_av_cb[i].peer_bda.address) == 0))
1979             return i;
1980     }
1981     return i;
1982 }
1983
1984 BOOLEAN btif_av_is_current_device(BD_ADDR address)
1985 {
1986     UINT8 index;
1987
1988     index = btif_av_idx_by_bdaddr(address);
1989     if((index < btif_max_av_clients) && btif_av_cb[index].current_playing)
1990     {
1991         return TRUE;
1992     }
1993     return FALSE;
1994 }
1995
1996 /*******************************************************************************
1997 **
1998 ** Function         btif_get_latest_device_idx_to_start
1999 **
2000 ** Description      Get the index of the AV where streaming is to be started
2001 **
2002 ** Returns          int
2003 **
2004 *******************************************************************************/
2005
2006 static int btif_get_latest_device_idx_to_start()
2007 {
2008     int i, j;
2009     BD_ADDR playing_address;
2010
2011     /* Get the device which sent PLAY command
2012      * If found, START on that index.
2013      */
2014     memset(playing_address, 0, sizeof(BD_ADDR));
2015     btif_rc_get_playing_device(playing_address);
2016     if (bdcmp(playing_address, bd_addr_null) != 0)
2017     {
2018         /* Got some valid Playing device.
2019          * Get the AV index for this device.
2020          */
2021         i = btif_av_idx_by_bdaddr(playing_address);
2022         if (i == btif_max_av_clients)
2023             return btif_max_av_clients;
2024         BTIF_TRACE_EVENT("Got some valid Playing device; %d", i);
2025         /*Clear the Current playing device*/
2026         for (j = 0; j < btif_max_av_clients; j++)
2027         {
2028             if (j != i)
2029               btif_av_cb[j].current_playing = FALSE;
2030         }
2031         /*Clear the Play command in RC*/
2032         btif_rc_clear_playing_state(FALSE);
2033         return i;
2034     }
2035
2036     /*No playing device, get the latest*/
2037     for (i = 0; i < btif_max_av_clients; i++)
2038     {
2039         if (btif_av_cb[i].current_playing)
2040             break;
2041     }
2042     if (i == btif_max_av_clients)
2043     {
2044         BTIF_TRACE_ERROR("Play on default");
2045         i = 0; /*play on default*/
2046     }
2047     return i;
2048 }
2049
2050 /*******************************************************************************
2051 **
2052 ** Function         btif_get_latest_playing_device_idx
2053 **
2054 ** Description      Get the index of AV where streaming is happening
2055 **
2056 ** Returns          int
2057 **
2058 *******************************************************************************/
2059
2060 int btif_get_latest_playing_device_idx()
2061 {
2062     int i;
2063     btif_sm_state_t state;
2064     for (i = 0; i < btif_max_av_clients; i++)
2065     {
2066         state = btif_sm_get_state(btif_av_cb[i].sm_handle);
2067         if (state == BTIF_AV_STATE_STARTED)
2068         {
2069             break;
2070         }
2071     }
2072     return i;
2073 }
2074
2075 /*******************************************************************************
2076 **
2077 ** Function         btif_av_is_playing
2078 **
2079 ** Description      Is AV in streaming state
2080 **
2081 ** Returns          BOOLEAN
2082 **
2083 *******************************************************************************/
2084
2085 BOOLEAN btif_av_is_playing()
2086 {
2087     int i;
2088     for (i = 0; i < btif_max_av_clients; i++)
2089     {
2090         btif_av_cb[i].state = btif_sm_get_state(btif_av_cb[i].sm_handle);
2091         if (btif_av_cb[i].state == BTIF_AV_STATE_STARTED)
2092         {
2093             BTIF_TRACE_EVENT("btif_av_is_playing on index= %d", i);
2094             return TRUE;
2095         }
2096     }
2097     return FALSE;
2098 }
2099
2100 /*******************************************************************************
2101 **
2102 ** Function         btif_get_conn_state_of_device
2103 **
2104 ** Description      Returns the state of AV scb
2105 **
2106 ** Returns          int
2107 **
2108 *******************************************************************************/
2109
2110 static int btif_get_conn_state_of_device(BD_ADDR address)
2111 {
2112     btif_sm_state_t state = BTIF_AV_STATE_IDLE;
2113     int i;
2114     for (i = 0; i < btif_max_av_clients; i++)
2115     {
2116         if ((bdcmp(address,
2117             btif_av_cb[i].peer_bda.address) == 0))
2118         {
2119             state = btif_sm_get_state(btif_av_cb[i].sm_handle);
2120             BTIF_TRACE_EVENT("BD Found: %02X %02X %02X %02X %02X %02X :state: %s",
2121                 address[5], address[4], address[3],
2122                 address[2], address[1], address[0],
2123                 dump_av_sm_state_name(state));
2124         }
2125     }
2126     return state;
2127 }
2128
2129 /*******************************************************************************
2130 **
2131 ** Function         btif_av_get_valid_idx_for_rc_events
2132 **
2133 ** Description      gets th valid index for the RC event address
2134 **
2135 ** Returns          int
2136 **
2137 *******************************************************************************/
2138
2139 static int btif_av_get_valid_idx_for_rc_events(BD_ADDR bd_addr, int rc_handle)
2140 {
2141     int index = 0;
2142     /* First try to find if it is first event in AV IF
2143     * both the handles would be in IDLE state, pick the first
2144     * If we get second RC event while processing the priority
2145     * for the first, reject the second connection. */
2146
2147     /*Get the index from connected SCBs*/
2148     index = btif_av_idx_by_bdaddr(bd_addr);
2149     if (index == btif_max_av_clients)
2150     {
2151         /* None of the SCBS matched
2152         * Allocate free SCB, null address SCB*/
2153         index = btif_av_idx_by_bdaddr(bd_null);
2154         BTIF_TRACE_EVENT("btif_av_get_valid_idx_for_rc_events is %d", index);
2155         if (index >= btif_max_av_clients)
2156         {
2157             BTIF_TRACE_EVENT("disconnect only AVRCP device rc_handle %d", rc_handle);
2158             BTA_AvCloseRc(rc_handle);
2159         }
2160     }
2161     return index;
2162 }
2163
2164 /*******************************************************************************
2165 **
2166 ** Function         btif_av_check_rc_connection_priority
2167 **
2168 ** Description      Handles Priority callback for RC connections
2169 **
2170 ** Returns          void
2171 **
2172 *******************************************************************************/
2173
2174 static void btif_av_check_rc_connection_priority(void *p_data)
2175 {
2176     bt_bdaddr_t peer_bda;
2177
2178     /*Check if it is for same AV device*/
2179     if (btif_av_is_device_connected(((tBTA_AV*)p_data)->rc_open.peer_addr))
2180     {
2181         /*AV is connected */
2182         BTIF_TRACE_DEBUG("AV is connected, process RC connect event");
2183         btif_rc_handler(BTA_AV_RC_OPEN_EVT, (tBTA_AV*)p_data);
2184         return;
2185     }
2186     BTIF_TRACE_DEBUG("btif_av_check_rc_connection_priority");
2187     bdcpy(peer_bda.address, ((tBTA_AV*)p_data)->rc_open.peer_addr);
2188
2189     if (idle_rc_event != 0)
2190     {
2191         BTIF_TRACE_DEBUG("Processing another RC Event ");
2192         return;
2193     }
2194     idle_rc_event = BTA_AV_RC_OPEN_EVT;
2195     memcpy(&idle_rc_data, ((tBTA_AV*)p_data), sizeof(tBTA_AV));
2196     if (((tBTA_AV*)p_data)->rc_open.status == BTA_AV_SUCCESS)
2197     {
2198         BTIF_TRACE_DEBUG("RC conn is success ");
2199         if (bt_av_src_callbacks != NULL)
2200         {
2201             BTIF_TRACE_DEBUG(" Check Device priority");
2202             HAL_CBACK(bt_av_src_callbacks, connection_priority_cb,
2203                     &peer_bda);
2204         }
2205     }
2206     else
2207     {
2208         idle_rc_event = 0;
2209         memset(&idle_rc_data, 0, sizeof(tBTA_AV));
2210     }
2211     return;
2212 }
2213
2214
2215 static void bte_av_callback(tBTA_AV_EVT event, tBTA_AV *p_data)
2216 {
2217     btif_transfer_context(btif_av_handle_event, event,
2218                           (char*)p_data, sizeof(tBTA_AV), btif_av_event_deep_copy);
2219 }
2220
2221 /*Called only in case of A2dp SInk, which runs on index 0*/
2222 static void bte_av_media_callback(tBTA_AV_EVT event, tBTA_AV_MEDIA *p_data)
2223 {
2224     btif_sm_state_t state;
2225     UINT8 que_len;
2226     tA2D_STATUS a2d_status;
2227     tA2D_SBC_CIE sbc_cie;
2228     btif_av_sink_config_req_t config_req;
2229     int index =0;
2230
2231     if (event == BTA_AV_MEDIA_DATA_EVT)/* Switch to BTIF_MEDIA context */
2232     {
2233         state= btif_sm_get_state(btif_av_cb[index].sm_handle);
2234         if ( (state == BTIF_AV_STATE_STARTED) || /* send SBC packets only in Started State */
2235              (state == BTIF_AV_STATE_OPENED) )
2236         {
2237             que_len = btif_media_sink_enque_buf((BT_HDR *)p_data);
2238             BTIF_TRACE_DEBUG(" Packets in Que %d",que_len);
2239         }
2240         else
2241             return;
2242     }
2243
2244     if (event == BTA_AV_MEDIA_SINK_CFG_EVT) {
2245         /* send a command to BT Media Task */
2246         btif_reset_decoder((UINT8*)(p_data->avk_config.codec_info));
2247         a2d_status = A2D_ParsSbcInfo(&sbc_cie, (UINT8 *)(p_data->avk_config.codec_info), FALSE);
2248         if (a2d_status == A2D_SUCCESS) {
2249             /* Switch to BTIF context */
2250             config_req.sample_rate = btif_a2dp_get_track_frequency(sbc_cie.samp_freq);
2251             config_req.channel_count = btif_a2dp_get_track_channel_count(sbc_cie.ch_mode);
2252             memcpy(&config_req.peer_bd,(UINT8*)(p_data->avk_config.bd_addr),
2253                                                               sizeof(config_req.peer_bd));
2254             btif_transfer_context(btif_av_handle_event, BTIF_AV_SINK_CONFIG_REQ_EVT,
2255                                      (char*)&config_req, sizeof(config_req), NULL);
2256         } else {
2257             APPL_TRACE_ERROR("ERROR dump_codec_info A2D_ParsSbcInfo fail:%d", a2d_status);
2258         }
2259     }
2260 }
2261
2262 /******************************************************************************
2263 ** Function       a2dp_offload_codec_cap_parser
2264 **
2265 ** Description    Parse the offload supported codec capability during init
2266 **
2267 ** Returns
2268 *****************************************************************************/
2269 static void a2dp_offload_codec_cap_parser(const char *value)
2270 {
2271     char *tok = NULL;
2272     char *tmp_token = NULL;
2273     /* Fix for below Klockwork Issue
2274      * 'strtok' has been deprecated; replace it with a safe function. */
2275     tok = strtok_r((char*)value, "-", &tmp_token);
2276     while (tok != NULL)
2277     {
2278         if (strcmp(tok,"sbc") == 0)
2279         {
2280             BTIF_TRACE_ERROR("%s: SBC offload supported",__func__);
2281             btif_av_codec_offload.sbc_offload = TRUE;
2282         }
2283         else if (strcmp(tok,"aptx") == 0)
2284         {
2285             BTIF_TRACE_ERROR("%s: aptX offload supported",__func__);
2286             btif_av_codec_offload.aptx_offload = TRUE;
2287         }
2288         else if (strcmp(tok,"aac") == 0)
2289         {
2290             BTIF_TRACE_ERROR("%s: AAC offload supported",__func__);
2291             btif_av_codec_offload.aac_offload = TRUE;
2292         }
2293         else if (strcmp(tok,"aptxhd") == 0)
2294         {
2295             BTIF_TRACE_ERROR("%s: APTXHD offload supported",__func__);
2296             btif_av_codec_offload.aptxhd_offload = TRUE;
2297         }
2298         tok = strtok_r(NULL, "-", &tmp_token);
2299     };
2300 }
2301
2302 /******************************************************************************
2303 ** Function       get_offload_codec_capabilities
2304 **
2305 ** Description    Read offload supported codecs
2306 **                To set offload capabilities:
2307 **                adb shell setprop persist.bt.a2dp_offload_cap "sbc-aptx"
2308 **
2309 ** Returns
2310 *****************************************************************************/
2311 static void get_offload_codec_capabilities(const char* codec_cap)
2312 {
2313     BTIF_TRACE_DEBUG("%s",__func__);
2314     a2dp_offload_codec_cap_parser(codec_cap);
2315     return;
2316 }
2317 /*******************************************************************************
2318 **
2319 ** Function         btif_av_init
2320 **
2321 ** Description      Initializes btif AV if not already done
2322 **
2323 ** Returns          bt_status_t
2324 **
2325 *******************************************************************************/
2326
2327 bt_status_t btif_av_init(int service_id)
2328 {
2329     int i;
2330     if (btif_av_cb[0].sm_handle == NULL)
2331     {
2332         alarm_free(av_open_on_rc_timer);
2333         av_open_on_rc_timer = alarm_new("btif_av.av_open_on_rc_timer");
2334         BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2335         if(!btif_a2dp_is_media_task_stopped())
2336             return BT_STATUS_FAIL;
2337         btif_av_cb[0].service = service_id;
2338
2339         /* Also initialize the AV state machine */
2340         for (i = 0; i < btif_max_av_clients; i++)
2341         {
2342             btif_av_cb[i].sm_handle = btif_sm_init((const btif_sm_handler_t*)btif_av_state_handlers,
2343                                                     BTIF_AV_STATE_IDLE, i);
2344         }
2345
2346         btif_transfer_context(btif_av_handle_event, BTIF_AV_INIT_REQ_EVT,
2347                 (char*)&service_id, sizeof(int), NULL);
2348
2349         btif_enable_service(service_id);
2350     }
2351
2352     return BT_STATUS_SUCCESS;
2353 }
2354
2355 /*******************************************************************************
2356 **
2357 ** Function         init_src
2358 **
2359 ** Description      Initializes the AV interface for source mode
2360 **
2361 ** Returns          bt_status_t
2362 **
2363 *******************************************************************************/
2364
2365 static bt_status_t init_src(btav_callbacks_t* callbacks, int max_a2dp_connections,
2366                             int a2dp_multicast_state, const char* offload_cap)
2367 {
2368     bt_status_t status;
2369
2370     BTIF_TRACE_EVENT("%s with max conn = %d", __FUNCTION__, max_a2dp_connections);
2371
2372     if (bt_av_sink_callbacks != NULL) {
2373         // already did btif_av_init()
2374         status = BT_STATUS_SUCCESS;
2375     }
2376     else
2377     {
2378         if (a2dp_multicast_state)
2379         {
2380             is_multicast_supported = TRUE;
2381         }
2382         if (offload_cap)
2383         {
2384             bt_split_a2dp_enabled = TRUE;
2385             get_offload_codec_capabilities(offload_cap);
2386             is_multicast_supported = FALSE; //Disable multicast in Split A2dp mode
2387         }
2388         btif_max_av_clients = max_a2dp_connections;
2389         status = btif_av_init(BTA_A2DP_SOURCE_SERVICE_ID);
2390     }
2391
2392     if (status == BT_STATUS_SUCCESS) {
2393         bt_av_src_callbacks = callbacks;
2394     }
2395
2396     return status;
2397 }
2398
2399 /*******************************************************************************
2400 **
2401 ** Function         init_sink
2402 **
2403 ** Description      Initializes the AV interface for sink mode
2404 **
2405 ** Returns          bt_status_t
2406 **
2407 *******************************************************************************/
2408
2409 static bt_status_t init_sink(btav_callbacks_t* callbacks, int max,
2410                              int a2dp_multicast_state, const char *offload_cap)
2411 {
2412     bt_status_t status;
2413
2414     BTIF_TRACE_EVENT("%s", __FUNCTION__);
2415
2416     if (bt_av_src_callbacks != NULL) {
2417         // already did btif_av_init()
2418         status = BT_STATUS_SUCCESS;
2419     }
2420     else
2421     {
2422         enable_multicast = FALSE; // Clear multicast flag for sink
2423         bt_split_a2dp_enabled = FALSE; //Clear split a2dp for sink
2424         if (max > 1)
2425         {
2426             BTIF_TRACE_ERROR("Only one Sink can be initialized");
2427             max = 1;
2428         }
2429         btif_max_av_clients = max; //Should be 1
2430         status = btif_av_init(BTA_A2DP_SINK_SERVICE_ID);
2431     }
2432
2433     if (status == BT_STATUS_SUCCESS) {
2434         bt_av_sink_callbacks = callbacks;
2435         //BTA_AvEnable_Sink(TRUE);
2436     }
2437
2438     return status;
2439 }
2440
2441 #ifdef USE_AUDIO_TRACK
2442 /*******************************************************************************
2443 **
2444 ** Function         update_audio_focus_state
2445 **
2446 ** Description      Updates the final focus state reported by components calling
2447 **                  this module.
2448 **
2449 ** Returns          None
2450 **
2451 *******************************************************************************/
2452 void update_audio_focus_state(int state)
2453 {
2454     BTIF_TRACE_DEBUG("%s state %d ",__func__, state);
2455     btif_a2dp_set_audio_focus_state(state);
2456 }
2457
2458 /*******************************************************************************
2459 **
2460 ** Function         update_audio_track_gain
2461 **
2462 ** Description      Updates the track gain (used for ducking).
2463 **
2464 ** Returns          None
2465 **
2466 *******************************************************************************/
2467 void update_audio_track_gain(float gain)
2468 {
2469     BTIF_TRACE_DEBUG("%s gain %f ",__func__, gain);
2470     btif_a2dp_set_audio_track_gain(gain);
2471 }
2472 #endif
2473
2474
2475 void btif_get_latest_playing_device(BD_ADDR address)
2476 {
2477     int index;
2478     index = btif_get_latest_playing_device_idx();
2479     if (index < btif_max_av_clients)
2480     {
2481         //copy bdaddrsss
2482         bdcpy(address, btif_av_cb[index].peer_bda.address);
2483     }
2484     else
2485     {
2486         bdcpy(address, bd_null);
2487     }
2488 }
2489
2490 BOOLEAN btif_av_is_device_connected(BD_ADDR address)
2491 {
2492     btif_sm_state_t state = btif_get_conn_state_of_device(address);
2493
2494     if ((state == BTIF_AV_STATE_OPENED) ||
2495         (state == BTIF_AV_STATE_STARTED))
2496         return TRUE;
2497     else
2498         return FALSE;
2499 }
2500
2501 /*This function will trigger remote suspend for currently
2502 * playing device and then initiate START on Handoff device
2503 * whose address is passed as an argument. */
2504 /*******************************************************************************
2505 **
2506 ** Function         btif_av_trigger_dual_handoff
2507 **
2508 ** Description      Trigger the DUAL HANDOFF
2509 **
2510 ** Returns          void
2511 **
2512 *******************************************************************************/
2513
2514 void btif_av_trigger_dual_handoff(BOOLEAN handoff, BD_ADDR address)
2515 {
2516     int index,next_idx;
2517     /*Get the current playing device*/
2518     BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2519     index = btif_get_latest_playing_device_idx();
2520     if (index != btif_max_av_clients)
2521     {
2522         btif_av_cb[index].dual_handoff = handoff; /*Initiate Handoff*/
2523         if (bt_split_a2dp_enabled)
2524             btif_media_on_stop_vendor_command();
2525         /*Initiate SUSPEND for this device*/
2526         BTIF_TRACE_DEBUG("Initiate SUSPEND for this device on index = %d", index);
2527         btif_sm_dispatch(btif_av_cb[index].sm_handle, BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL);
2528     }
2529     else
2530     {
2531         BTIF_TRACE_ERROR("Handoff on invalid index");
2532     }
2533     if (bt_split_a2dp_enabled)
2534     {
2535         btif_media_send_reset_vendor_state();
2536         next_idx = btif_av_get_other_connected_idx(index);
2537         /* Fix for below Klockwork Issue
2538         Array 'btif_av_cb' of size 2 may use index value(s) -1 */
2539         if (next_idx != INVALID_INDEX && next_idx != btif_max_av_clients)
2540         {
2541             HAL_CBACK(bt_av_src_callbacks, reconfig_a2dp_trigger_cb, 1,
2542                                     &(btif_av_cb[next_idx].peer_bda));
2543         }
2544     }
2545 }
2546
2547 /*******************************************************************************
2548 **
2549 ** Function         btif_av_trigger_suspend
2550 **
2551 ** Description      Trigger suspend when multicast is ongoing for tuch tones
2552 **                  and new ACL is created.
2553 **
2554 ** Returns          void
2555 **
2556 *******************************************************************************/
2557
2558 void btif_av_trigger_suspend()
2559 {
2560     int index;
2561     /*Get the current playing device*/
2562     BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2563     index = btif_get_latest_playing_device_idx();
2564     if (index <= btif_max_av_clients)
2565     {
2566         /*Initiate SUSPEND for this device*/
2567         BTIF_TRACE_DEBUG("Initiate SUSPEND for this device on index = %d", index);
2568         btif_sm_dispatch(btif_av_cb[index].sm_handle, BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL);
2569     }
2570     else
2571     {
2572         BTIF_TRACE_ERROR("suspend on invalid index");
2573     }
2574 }
2575
2576 /*******************************************************************************
2577 **
2578 ** Function         connect
2579 **
2580 ** Description      Establishes the AV signalling channel with the remote headset
2581 **
2582 ** Returns          bt_status_t
2583 **
2584 *******************************************************************************/
2585
2586 static bt_status_t connect_int(bt_bdaddr_t *bd_addr, uint16_t uuid)
2587 {
2588     btif_av_connect_req_t connect_req;
2589     int i;
2590     connect_req.target_bda = bd_addr;
2591     connect_req.uuid = uuid;
2592     BTIF_TRACE_EVENT("%s", __FUNCTION__);
2593
2594     for (i = 0; i < btif_max_av_clients;)
2595     {
2596         if(btif_av_get_valid_idx(i))
2597         {
2598             if (bdcmp(bd_addr->address, btif_av_cb[i].peer_bda.address) == 0)
2599             {
2600                 BTIF_TRACE_ERROR("Attempting connection for non idle device.. back off ");
2601                 btif_queue_advance();
2602                 return BT_STATUS_SUCCESS;
2603             }
2604             i++;
2605         }
2606         else
2607             break;
2608     }
2609     if (i == btif_max_av_clients)
2610     {
2611         UINT8 rc_handle;
2612
2613         BTIF_TRACE_ERROR("%s: All indexes are full", __FUNCTION__);
2614
2615         /* Multicast: Check if AV slot is available for connection
2616          * If not available, AV got connected to different devices.
2617          * Disconnect this RC connection without AV connection.
2618          */
2619         rc_handle = btif_rc_get_connected_peer_handle(bd_addr->address);
2620         if (rc_handle != BTIF_RC_HANDLE_NONE)
2621         {
2622             BTA_AvCloseRc(rc_handle);
2623         }
2624         btif_queue_advance();
2625         return BT_STATUS_FAIL;
2626     }
2627
2628     btif_sm_dispatch(btif_av_cb[i].sm_handle, BTIF_AV_CONNECT_REQ_EVT, (char*)&connect_req);
2629
2630
2631     return BT_STATUS_SUCCESS;
2632 }
2633
2634 static bt_status_t src_connect_sink(bt_bdaddr_t *bd_addr)
2635 {
2636     BTIF_TRACE_EVENT("%s", __FUNCTION__);
2637     CHECK_BTAV_INIT();
2638
2639     return btif_queue_connect(UUID_SERVCLASS_AUDIO_SOURCE, bd_addr, connect_int);
2640 }
2641
2642 static bt_status_t sink_connect_src(bt_bdaddr_t *bd_addr)
2643 {
2644     BTIF_TRACE_EVENT("%s", __FUNCTION__);
2645     CHECK_BTAV_INIT();
2646
2647     return btif_queue_connect(UUID_SERVCLASS_AUDIO_SINK, bd_addr, connect_int);
2648 }
2649
2650 /*******************************************************************************
2651 **
2652 ** Function         disconnect
2653 **
2654 ** Description      Tears down the AV signalling channel with the remote headset
2655 **
2656 ** Returns          bt_status_t
2657 **
2658 *******************************************************************************/
2659 static bt_status_t disconnect(bt_bdaddr_t *bd_addr)
2660 {
2661     BTIF_TRACE_EVENT("%s", __FUNCTION__);
2662
2663     CHECK_BTAV_INIT();
2664
2665     /* Switch to BTIF context */
2666     return btif_transfer_context(btif_av_handle_event, BTIF_AV_DISCONNECT_REQ_EVT,
2667                                  (char*)bd_addr, sizeof(bt_bdaddr_t), NULL);
2668 }
2669
2670 /*******************************************************************************
2671 **
2672 ** Function         cleanup
2673 **
2674 ** Description      Shuts down the AV interface and does the cleanup
2675 **
2676 ** Returns          None
2677 **
2678 *******************************************************************************/
2679 static void cleanup(int service_uuid)
2680 {
2681     BTIF_TRACE_IMP("AV %s", __FUNCTION__);
2682
2683     btif_transfer_context(btif_av_handle_event, BTIF_AV_CLEANUP_REQ_EVT,
2684             (char*)&service_uuid, sizeof(int), NULL);
2685
2686     btif_disable_service(service_uuid);
2687 }
2688
2689 static void cleanup_src(void) {
2690     BTIF_TRACE_EVENT("%s", __FUNCTION__);
2691     cleanup(BTA_A2DP_SOURCE_SERVICE_ID);
2692     BTIF_TRACE_EVENT("%s completed", __FUNCTION__);
2693 }
2694
2695 static void cleanup_sink(void) {
2696     BTIF_TRACE_EVENT("%s", __FUNCTION__);
2697     cleanup(BTA_A2DP_SINK_SERVICE_ID);
2698 }
2699
2700 static void allow_connection(int is_valid, bt_bdaddr_t *bd_addr)
2701 {
2702     int index = 0;
2703     BTIF_TRACE_DEBUG(" %s isValid is %d event %d", __FUNCTION__,is_valid,idle_rc_event);
2704     switch (idle_rc_event)
2705     {
2706         case BTA_AV_RC_OPEN_EVT:
2707             if (is_valid)
2708             {
2709                 BTIF_TRACE_DEBUG("allowconn for RC connection");
2710                                 alarm_set_on_queue(av_open_on_rc_timer,
2711                                                   BTIF_TIMEOUT_AV_OPEN_ON_RC_MS,
2712                                                   btif_initiate_av_open_timer_timeout, NULL,
2713                                                   btu_general_alarm_queue);
2714                 btif_rc_handler(idle_rc_event, &idle_rc_data);
2715             }
2716             else
2717             {
2718                 UINT8 rc_handle =  idle_rc_data.rc_open.rc_handle;
2719                 BTA_AvCloseRc(rc_handle);
2720             }
2721             break;
2722
2723         case BTA_AV_PENDING_EVT:
2724             if (is_valid)
2725             {
2726                 index = btif_av_idx_by_bdaddr(bd_addr->address);
2727                 if (index >= btif_max_av_clients)
2728                 {
2729                     BTIF_TRACE_DEBUG("Invalid index for device");
2730                     break;
2731                 }
2732                 BTIF_TRACE_DEBUG("The connection is allowed for the device at index = %d", index);
2733                 BTA_AvOpen(btif_av_cb[index].peer_bda.address, btif_av_cb[index].bta_handle,
2734                        TRUE, BTA_SEC_NONE, UUID_SERVCLASS_AUDIO_SOURCE);
2735             }
2736             else
2737             {
2738                 BTA_AvDisconnect(idle_rc_data.pend.bd_addr);
2739             }
2740             break;
2741
2742         default:
2743             BTIF_TRACE_DEBUG("%s : unhandled event:%s", __FUNCTION__,
2744                                 dump_av_sm_event_name(idle_rc_event));
2745     }
2746     idle_rc_event = 0;
2747     memset(&idle_rc_data, 0, sizeof(tBTA_AV));
2748 }
2749
2750 static const btav_interface_t bt_av_src_interface = {
2751     sizeof(btav_interface_t),
2752     init_src,
2753     src_connect_sink,
2754     disconnect,
2755     cleanup_src,
2756     NULL,
2757     NULL,
2758     allow_connection,
2759 };
2760
2761 static const btav_interface_t bt_av_sink_interface = {
2762     sizeof(btav_interface_t),
2763     init_sink,
2764     sink_connect_src,
2765     disconnect,
2766     cleanup_sink,
2767 #ifdef USE_AUDIO_TRACK
2768     update_audio_focus_state,
2769     update_audio_track_gain,
2770 #else
2771     NULL,
2772     NULL,
2773 #endif
2774         NULL,
2775 };
2776
2777 /*******************************************************************************
2778 **
2779 ** Function         btif_av_get_sm_handle
2780 **
2781 ** Description      Fetches current av SM handle
2782 **
2783 ** Returns          None
2784 **
2785 *******************************************************************************/
2786 /* Media task uses this info
2787 * But dont use it. */
2788 btif_sm_handle_t btif_av_get_sm_handle(void)
2789 {
2790     return btif_av_cb[0].sm_handle;
2791 }
2792
2793 /*******************************************************************************
2794 **
2795 ** Function         btif_av_get_addr
2796 **
2797 ** Description      Fetches current AV BD address
2798 **
2799 ** Returns          BD address
2800 **
2801 *******************************************************************************/
2802
2803 bt_bdaddr_t btif_av_get_addr(BD_ADDR address)
2804 {
2805     int i;
2806     bt_bdaddr_t not_found ;
2807     memset (&not_found, 0, sizeof(bt_bdaddr_t));
2808     for (i = 0; i < btif_max_av_clients; i++)
2809     {
2810         if (bdcmp(btif_av_cb[i].peer_bda.address, address) == 0)
2811             return btif_av_cb[i].peer_bda;
2812     }
2813     return not_found;
2814 }
2815
2816 /*******************************************************************************
2817 ** Function         btif_av_is_sink_enabled
2818 **
2819 ** Description      Checks if A2DP Sink is enabled or not
2820 **
2821 ** Returns          TRUE if A2DP Sink is enabled, false otherwise
2822 **
2823 *******************************************************************************/
2824
2825 BOOLEAN btif_av_is_sink_enabled(void)
2826 {
2827     return (bt_av_sink_callbacks != NULL) ? TRUE : FALSE;
2828 }
2829
2830 /*******************************************************************************
2831 **
2832 ** Function         btif_av_stream_ready
2833 **
2834 ** Description      Checks whether AV is ready for starting a stream
2835 **
2836 ** Returns          None
2837 **
2838 *******************************************************************************/
2839
2840 BOOLEAN btif_av_stream_ready(void)
2841 {
2842     int i;
2843     BOOLEAN status = FALSE;
2844     /* also make sure main adapter is enabled */
2845     if (btif_is_enabled() == 0)
2846     {
2847         BTIF_TRACE_EVENT("main adapter not enabled");
2848         return FALSE;
2849     }
2850
2851     for (i = 0; i < btif_max_av_clients; i++)
2852     {
2853         btif_av_cb[i].state = btif_sm_get_state(btif_av_cb[i].sm_handle);
2854         /* Multicast:
2855          * If any of the stream is in pending suspend state when
2856          * we initiate start, it will result in inconsistent behavior
2857          * Check the pending SUSPEND flag and return failure
2858          * if suspend is in progress.
2859          */
2860         if (btif_av_cb[i].dual_handoff ||
2861             (btif_av_cb[i].flags & BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING))
2862         {
2863             status = FALSE;
2864             break;
2865         }
2866         else if (btif_av_cb[i].flags &
2867             (BTIF_AV_FLAG_REMOTE_SUSPEND|BTIF_AV_FLAG_PENDING_STOP))
2868         {
2869             status = FALSE;
2870             break;
2871         }
2872         else if (btif_av_cb[i].state == BTIF_AV_STATE_OPENED)
2873         {
2874             status = TRUE;
2875         }
2876     }
2877     BTIF_TRACE_DEBUG("btif_av_stream_ready: %d", status);
2878     return status;
2879 }
2880
2881 /*******************************************************************************
2882 **
2883 ** Function         btif_av_stream_started_ready
2884 **
2885 ** Description      Checks whether AV ready for media start in streaming state
2886 **
2887 ** Returns          None
2888 **
2889 *******************************************************************************/
2890
2891 BOOLEAN btif_av_stream_started_ready(void)
2892 {
2893     int i;
2894     BOOLEAN status = FALSE;
2895
2896     for (i = 0; i < btif_max_av_clients; i++)
2897     {
2898         btif_av_cb[i].state = btif_sm_get_state(btif_av_cb[i].sm_handle);
2899         if (btif_av_cb[i].dual_handoff)
2900         {
2901             BTIF_TRACE_ERROR("%s: Under Dual handoff ",__FUNCTION__ );
2902             status = FALSE;
2903             break;
2904         } else if (btif_av_cb[i].flags &
2905             (BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING |
2906             BTIF_AV_FLAG_REMOTE_SUSPEND |
2907             BTIF_AV_FLAG_PENDING_STOP))
2908         {
2909             status = FALSE;
2910             break;
2911         } else if (btif_av_cb[i].state == BTIF_AV_STATE_STARTED)
2912         {
2913             status = TRUE;
2914         }
2915     }
2916     BTIF_TRACE_DEBUG("btif_av_stream_started_ready: %d", status);
2917     return status;
2918 }
2919
2920 /*******************************************************************************
2921 **
2922 ** Function         btif_dispatch_sm_event
2923 **
2924 ** Description      Send event to AV statemachine
2925 **
2926 ** Returns          None
2927 **
2928 *******************************************************************************/
2929
2930 /* used to pass events to AV statemachine from other tasks */
2931 void btif_dispatch_sm_event(btif_av_sm_event_t event, void *p_data, int len)
2932 {
2933     /* Switch to BTIF context */
2934     BTIF_TRACE_IMP("%s: event: %d, len: %d", __FUNCTION__, event, len);
2935     btif_transfer_context(btif_av_handle_event, event,
2936                           (char*)p_data, len, NULL);
2937     BTIF_TRACE_IMP("%s: event %d sent", __FUNCTION__, event);
2938 }
2939
2940 /*******************************************************************************
2941 **
2942 ** Function         btif_av_execute_service
2943 **
2944 ** Description      Initializes/Shuts down the service
2945 **
2946 ** Returns          BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
2947 **
2948 *******************************************************************************/
2949 bt_status_t btif_av_execute_service(BOOLEAN b_enable)
2950 {
2951     int i;
2952     btif_sm_state_t state;
2953     BTIF_TRACE_IMP("%s: enable: %d", __FUNCTION__, b_enable);
2954     if (b_enable)
2955     {
2956         /* TODO: Removed BTA_SEC_AUTHORIZE since the Java/App does not
2957         * handle this request in order to allow incoming connections to succeed.
2958         * We need to put this back once support for this is added */
2959
2960         /* Added BTA_AV_FEAT_NO_SCO_SSPD - this ensures that the BTA does not
2961         * auto-suspend av streaming on AG events(SCO or Call). The suspend shall
2962         * be initiated by the app/audioflinger layers */
2963 #if (AVRC_METADATA_INCLUDED == TRUE)
2964         BTA_AvEnable(BTA_SEC_AUTHENTICATE,
2965             BTA_AV_FEAT_RCTG|BTA_AV_FEAT_METADATA|BTA_AV_FEAT_VENDOR|BTA_AV_FEAT_NO_SCO_SSPD
2966             |BTA_AV_FEAT_ACP_START
2967 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
2968             |BTA_AV_FEAT_RCCT
2969             |BTA_AV_FEAT_ADV_CTRL
2970                         |BTA_AV_FEAT_BROWSE
2971 #endif
2972             ,bte_av_callback);
2973 #else
2974         BTA_AvEnable(BTA_SEC_AUTHENTICATE, (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_NO_SCO_SSPD
2975             |BTA_AV_FEAT_ACP_START), bte_av_callback);
2976 #endif
2977         for (i = 0; i < btif_max_av_clients; i++)
2978         {
2979             BTIF_TRACE_DEBUG("%s: BTA_AvRegister : %d", __FUNCTION__, i);
2980             BTA_AvRegister(BTA_AV_CHNL_AUDIO, BTIF_AV_SERVICE_NAME, 0, bte_av_media_callback,
2981             UUID_SERVCLASS_AUDIO_SOURCE);
2982         }
2983         BTA_AvUpdateMaxAVClient(btif_max_av_clients);
2984     }
2985     else
2986     {
2987         /* Also shut down the AV state machine */
2988         for (i = 0; i < btif_max_av_clients; i++ )
2989         {
2990             if (btif_av_cb[i].sm_handle != NULL)
2991             {
2992                 state = btif_sm_get_state(btif_av_cb[i].sm_handle);
2993                 if(state==BTIF_AV_STATE_OPENING)
2994                 {
2995                     BTIF_TRACE_DEBUG("Moving State from Opening to Idle due to BT ShutDown");
2996                     btif_sm_change_state(btif_av_cb[i].sm_handle, BTIF_AV_STATE_IDLE);
2997                     btif_queue_advance();
2998                 }
2999                 btif_sm_shutdown(btif_av_cb[i].sm_handle);
3000                 btif_av_cb[i].sm_handle = NULL;
3001             }
3002         }
3003         for (i = 0; i < btif_max_av_clients; i++)
3004         {
3005             BTA_AvDeregister(btif_av_cb[i].bta_handle);
3006         }
3007         BTA_AvDisable();
3008     }
3009     BTIF_TRACE_IMP("%s: enable: %d completed", __FUNCTION__, b_enable);
3010     return BT_STATUS_SUCCESS;
3011 }
3012
3013 /*******************************************************************************
3014 **
3015 ** Function         btif_av_sink_execute_service
3016 **
3017 ** Description      Initializes/Shuts down the service
3018 **
3019 ** Returns          BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
3020 **
3021 *******************************************************************************/
3022 bt_status_t btif_av_sink_execute_service(BOOLEAN b_enable)
3023 {
3024      int i;
3025      BTIF_TRACE_IMP("%s: enable: %d", __FUNCTION__, b_enable);
3026
3027      if (b_enable)
3028      {
3029          /* Added BTA_AV_FEAT_NO_SCO_SSPD - this ensures that the BTA does not
3030           * auto-suspend av streaming on AG events(SCO or Call). The suspend shall
3031           * be initiated by the app/audioflinger layers */
3032          BTA_AvEnable(BTA_SEC_AUTHENTICATE, BTA_AV_FEAT_NO_SCO_SSPD|BTA_AV_FEAT_RCCT|
3033                                             BTA_AV_FEAT_METADATA|BTA_AV_FEAT_VENDOR|
3034                                             BTA_AV_FEAT_ADV_CTRL|BTA_AV_FEAT_RCTG,
3035                                                                         bte_av_callback);
3036          BTA_AvRegister(BTA_AV_CHNL_AUDIO, BTIF_AVK_SERVICE_NAME, 0, bte_av_media_callback,
3037                                                                 UUID_SERVCLASS_AUDIO_SINK);
3038      }
3039      else {
3040          /* Also shut down the AV state machine */
3041         for (i = 0; i < btif_max_av_clients; i++ )
3042         {
3043             if (btif_av_cb[i].sm_handle != NULL)
3044             {
3045                 BTIF_TRACE_IMP("%s: shutting down AV SM", __FUNCTION__);
3046                 btif_sm_shutdown(btif_av_cb[i].sm_handle);
3047                 btif_av_cb[i].sm_handle = NULL;
3048             }
3049         }
3050         BTA_AvDeregister(btif_av_cb[0].bta_handle);
3051         BTA_AvDisable();
3052      }
3053      BTIF_TRACE_IMP("%s: enable: %d completed", __FUNCTION__, b_enable);
3054      return BT_STATUS_SUCCESS;
3055 }
3056
3057 /*******************************************************************************
3058 **
3059 ** Function         btif_av_get_src_interface
3060 **
3061 ** Description      Get the AV callback interface for A2DP source profile
3062 **
3063 ** Returns          btav_interface_t
3064 **
3065 *******************************************************************************/
3066 const btav_interface_t *btif_av_get_src_interface(void)
3067 {
3068     BTIF_TRACE_EVENT("%s", __FUNCTION__);
3069     return &bt_av_src_interface;
3070 }
3071
3072 /*******************************************************************************
3073 **
3074 ** Function         btif_av_get_sink_interface
3075 **
3076 ** Description      Get the AV callback interface for A2DP sink profile
3077 **
3078 ** Returns          btav_interface_t
3079 **
3080 *******************************************************************************/
3081 const btav_interface_t *btif_av_get_sink_interface(void)
3082 {
3083     BTIF_TRACE_EVENT("%s", __FUNCTION__);
3084     return &bt_av_sink_interface;
3085 }
3086
3087 /*******************************************************************************
3088 **
3089 ** Function         btif_av_is_connected
3090 **
3091 ** Description      Checks if av has a connected sink
3092 **
3093 ** Returns          BOOLEAN
3094 **
3095 *******************************************************************************/
3096 BOOLEAN btif_av_is_connected(void)
3097 {
3098     int i;
3099     BOOLEAN status = FALSE;
3100     for (i = 0; i < btif_max_av_clients; i++)
3101     {
3102         btif_av_cb[i].state = btif_sm_get_state(btif_av_cb[i].sm_handle);
3103         if ((btif_av_cb[i].state == BTIF_AV_STATE_OPENED) ||
3104             (btif_av_cb[i].state ==  BTIF_AV_STATE_STARTED))
3105             status = TRUE;
3106     }
3107     return status;
3108 }
3109
3110 /*******************************************************************************
3111 **
3112 ** Function         btif_av_is_connected_on_other_idx
3113 **
3114 ** Description      Checks if any other AV SCB is connected
3115 **
3116 ** Returns          BOOLEAN
3117 **
3118 *******************************************************************************/
3119
3120 BOOLEAN btif_av_is_connected_on_other_idx(int current_index)
3121 {
3122     //return true if other IDx is connected
3123     btif_sm_state_t state = BTIF_AV_STATE_IDLE;
3124     int i;
3125     for (i = 0; i < btif_max_av_clients; i++)
3126     {
3127         if (i != current_index)
3128         {
3129             state = btif_sm_get_state(btif_av_cb[i].sm_handle);
3130             if ((state == BTIF_AV_STATE_OPENED) ||
3131                 (state == BTIF_AV_STATE_STARTED))
3132                 return TRUE;
3133         }
3134     }
3135     return FALSE;
3136 }
3137
3138 /*******************************************************************************
3139 **
3140 ** Function         btif_av_get_other_connected_idx
3141 **
3142 ** Description      Checks if any AV SCB is connected other than the current
3143 **                  index
3144 **
3145 ** Returns          BOOLEAN
3146 **
3147 *******************************************************************************/
3148 int btif_av_get_other_connected_idx(int current_index)
3149 {
3150     //return true if other IDx is connected
3151     btif_sm_state_t state = BTIF_AV_STATE_IDLE;
3152     int i;
3153     for (i = 0; i < btif_max_av_clients; i++)
3154     {
3155         if (i != current_index)
3156         {
3157             state = btif_sm_get_state(btif_av_cb[i].sm_handle);
3158             if ((state == BTIF_AV_STATE_OPENED) ||
3159                 (state == BTIF_AV_STATE_STARTED))
3160                 return i;
3161         }
3162     }
3163     return INVALID_INDEX;
3164 }
3165
3166 /*******************************************************************************
3167 **
3168 ** Function         btif_av_is_playing_on_other_idx
3169 **
3170 ** Description      Checks if any other AV SCB is connected
3171 **
3172 ** Returns          BOOLEAN
3173 **
3174 *******************************************************************************/
3175
3176 BOOLEAN btif_av_is_playing_on_other_idx(int current_index)
3177 {
3178     //return true if other IDx is playing
3179     btif_sm_state_t state = BTIF_AV_STATE_IDLE;
3180     int i;
3181     for (i = 0; i < btif_max_av_clients; i++)
3182     {
3183         if (i != current_index)
3184         {
3185             state = btif_sm_get_state(btif_av_cb[i].sm_handle);
3186             if (state == BTIF_AV_STATE_STARTED)
3187                 return TRUE;
3188         }
3189     }
3190     return FALSE;
3191 }
3192
3193 /*******************************************************************************
3194 **
3195 ** Function         btif_av_update_current_playing_device
3196 **
3197 ** Description      Update the next connected device as playing
3198 **
3199 ** Returns          void
3200 **
3201 *******************************************************************************/
3202
3203 static void btif_av_update_current_playing_device(int index)
3204 {
3205     int i;
3206     for (i = 0; i < btif_max_av_clients; i++)
3207     {
3208         if (i != index)
3209             btif_av_cb[i].current_playing = TRUE;
3210     }
3211 }
3212
3213 /*******************************************************************************
3214 **
3215 ** Function         btif_av_is_peer_edr
3216 **
3217 ** Description      Check if the connected a2dp device supports
3218 **                  EDR or not. Only when connected this function
3219 **                  will accurately provide a true capability of
3220 **                  remote peer. If not connected it will always be false.
3221 **
3222 ** Returns          TRUE if remote device is capable of EDR
3223 **
3224 *******************************************************************************/
3225 BOOLEAN btif_av_is_peer_edr(void)
3226 {
3227     btif_sm_state_t state;
3228     BOOLEAN peer_edr = FALSE;
3229
3230     ASSERTC(btif_av_is_connected(), "No active a2dp connection", 0);
3231
3232     /* If any of the remote in streaming state is BR
3233      * return FALSE to ensure proper configuration
3234      * is used. Ideally, since multicast is not supported
3235      * if any of the connected device is BR device,
3236      * we should not see both devices in START state.
3237      */
3238     for (int index = 0; index < btif_max_av_clients; index ++)
3239     {
3240         state = btif_sm_get_state(btif_av_cb[index].sm_handle);
3241         if ((btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START)
3242             || (state == BTIF_AV_STATE_STARTED))
3243         {
3244             if (btif_av_cb[index].edr)
3245             {
3246                 peer_edr = TRUE;
3247             }
3248             else
3249             {
3250                 return FALSE;
3251             }
3252         }
3253     }
3254     return peer_edr;
3255 }
3256
3257 /*******************************************************************************
3258 **
3259 ** Function         btif_av_any_br_peer
3260 **
3261 ** Description      Check if the any of connected devices is BR device.
3262 **
3263 ** Returns          TRUE if connected to any BR device, FALSE otherwise.
3264 **
3265 *******************************************************************************/
3266 BOOLEAN btif_av_any_br_peer(void)
3267 {
3268     btif_sm_state_t state;
3269     for (int index = 0; index < btif_max_av_clients; index ++)
3270     {
3271         state = btif_sm_get_state(btif_av_cb[index].sm_handle);
3272         if (state >= BTIF_AV_STATE_OPENED)
3273         {
3274             if (!btif_av_cb[index].edr)
3275             {
3276                 BTIF_TRACE_WARNING("%s : Connected to BR device :", __FUNCTION__);
3277                 return TRUE;
3278             }
3279         }
3280     }
3281     return FALSE;
3282 }
3283
3284 /*******************************************************************************
3285 **
3286 ** Function         btif_av_peer_supports_3mbps
3287 **
3288 ** Description      check if the connected a2dp device supports
3289 **                  3mbps edr. Only when connected this function
3290 **                  will accurately provide a true capability of
3291 **                  remote peer. If not connected it will always be false.
3292 **
3293 ** Returns          TRUE if remote device is EDR and supports 3mbps
3294 **
3295 *******************************************************************************/
3296 BOOLEAN btif_av_peer_supports_3mbps(void)
3297 {
3298     btif_sm_state_t state;
3299     ASSERTC(btif_av_is_connected(), "No active a2dp connection", 0);
3300
3301     for (int index = 0; index < btif_max_av_clients; index ++)
3302     {
3303         state = btif_sm_get_state(btif_av_cb[index].sm_handle);
3304         if ((btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START)
3305             || (state == BTIF_AV_STATE_STARTED))
3306         {
3307             if(btif_av_cb[index].edr_3mbps)
3308                 return TRUE;
3309         }
3310     }
3311     return FALSE;
3312 }
3313
3314 /******************************************************************************
3315 **
3316 ** Function        btif_av_clear_remote_suspend_flag
3317 **
3318 ** Description     Clears btif_av_cd.flags if BTIF_AV_FLAG_REMOTE_SUSPEND is set
3319 **
3320 ** Returns         void
3321 ******************************************************************************/
3322 void btif_av_clear_remote_suspend_flag(void)
3323 {
3324     int i;
3325     for (i = 0; i < btif_max_av_clients; i++)
3326     {
3327         BTIF_TRACE_DEBUG(" flag :%x",btif_av_cb[i].flags);
3328         btif_av_cb[i].flags  &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
3329     }
3330 }
3331
3332 /*******************************************************************************
3333 **
3334 ** Function         btif_av_move_idle
3335 **
3336 ** Description      Opening state is intermediate state. It cannot handle
3337 **                  incoming/outgoing connect/disconnect requests.When ACL
3338 **                  is disconnected and we are in opening state then move back
3339 **                  to idle state which is proper to handle connections.
3340 **
3341 ** Returns          Void
3342 **
3343 *******************************************************************************/
3344 void btif_av_move_idle(bt_bdaddr_t bd_addr)
3345 {
3346     int index =0;
3347     /* inform the application that ACL is disconnected and move to idle state */
3348     index = btif_av_idx_by_bdaddr(bd_addr.address);
3349     if (index == btif_max_av_clients)
3350     {
3351         BTIF_TRACE_DEBUG("btif_av_move_idle: Already in IDLE");
3352         return;
3353     }
3354     btif_sm_state_t state = btif_sm_get_state(btif_av_cb[index].sm_handle);
3355     BTIF_TRACE_DEBUG("ACL Disconnected state %d  is same device %d",state,
3356             memcmp (&bd_addr, &(btif_av_cb[index].peer_bda), sizeof(bd_addr)));
3357     if (state == BTIF_AV_STATE_OPENING &&
3358             (memcmp (&bd_addr, &(btif_av_cb[index].peer_bda), sizeof(bd_addr)) == 0))
3359     {
3360         BTIF_TRACE_DEBUG("Moving BTIF State from Opening to Idle due to ACL disconnect");
3361         btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb[index].peer_bda));
3362         BTA_AvClose(btif_av_cb[index].bta_handle);
3363         btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_IDLE);
3364         btif_queue_advance();
3365     }
3366 }
3367 /******************************************************************************
3368 **
3369 ** Function        btif_av_get_num_playing_devices
3370 **
3371 ** Description     Return number of A2dp playing devices
3372 **
3373 ** Returns         int
3374 ******************************************************************************/
3375 UINT16 btif_av_get_num_playing_devices(void)
3376 {
3377     UINT16 i;
3378     UINT16 playing_devices = 0;
3379     for (i = 0; i < btif_max_av_clients; i++)
3380     {
3381         btif_av_cb[i].state = btif_sm_get_state(btif_av_cb[i].sm_handle);
3382         if (btif_av_cb[i].state ==  BTIF_AV_STATE_STARTED)
3383         {
3384             playing_devices++;
3385         }
3386     }
3387     BTIF_TRACE_DEBUG("AV devices playing: %d", playing_devices);
3388
3389     return playing_devices;
3390 }
3391 /*******************************************************************************
3392 **
3393 ** Function        btif_av_get_num_connected_devices
3394 **
3395 ** Description     Return number of A2dp connected devices
3396 **
3397 ** Returns         int
3398 ******************************************************************************/
3399 UINT16 btif_av_get_num_connected_devices(void)
3400 {
3401     UINT16 i;
3402     UINT16 connected_devies = 0;
3403     for (i = 0; i < btif_max_av_clients; i++)
3404     {
3405         btif_av_cb[i].state = btif_sm_get_state(btif_av_cb[i].sm_handle);
3406         if ((btif_av_cb[i].state == BTIF_AV_STATE_OPENED) ||
3407             (btif_av_cb[i].state ==  BTIF_AV_STATE_STARTED))
3408         {
3409             connected_devies++;
3410         }
3411     }
3412     BTIF_TRACE_DEBUG("AV Connection count: %d", connected_devies);
3413
3414     return connected_devies;
3415 }
3416
3417 /******************************************************************************
3418 **
3419 ** Function        btif_av_update_multicast_state
3420 **
3421 ** Description     Enable Multicast only if below conditions are satisfied
3422 **                 1. Connected to only 2 EDR HS.
3423 **                 2. Connected to both HS as master.
3424 **                 3. Connected to 2 EDR HS and one BLE device
3425 **                 Multicast will fall back to soft handsoff in below conditions
3426 **                 1. Number of ACL links is more than 2,like connected to HID
3427 **                    initiating connection for HS1 and HS2.
3428 **                 2. Connected to BR and EDR HS.
3429 **                 3. Connected to more then 1 BLE device
3430 **
3431 ** Returns         void
3432 ******************************************************************************/
3433 void btif_av_update_multicast_state(int index)
3434 {
3435     UINT16 num_connected_br_edr_devices = 0;
3436     UINT16 num_connected_le_devices = 0;
3437     UINT16 num_av_connected = 0;
3438     UINT16 i = 0;
3439     BOOLEAN is_slave = FALSE;
3440     BOOLEAN is_br_hs_connected = FALSE;
3441     BOOLEAN prev_multicast_state = enable_multicast;
3442
3443     if (!is_multicast_supported)
3444     {
3445         BTIF_TRACE_DEBUG("%s Multicast is Disabled", __FUNCTION__);
3446         return;
3447     }
3448
3449     if (multicast_disabled == TRUE)
3450     {
3451         multicast_disabled = FALSE;
3452         enable_multicast = FALSE;
3453         BTA_AvEnableMultiCast(FALSE, btif_av_cb[index].bta_handle);
3454         return;
3455     }
3456
3457     BTIF_TRACE_DEBUG("%s Multicast previous state : %s", __FUNCTION__,
3458         enable_multicast ? "Enabled" : "Disabled" );
3459
3460     num_connected_br_edr_devices = btif_dm_get_br_edr_links();
3461     num_connected_le_devices = btif_dm_get_le_links();
3462     num_av_connected = btif_av_get_num_connected_devices();
3463     is_br_hs_connected = btif_av_any_br_peer();
3464
3465     for (i = 0; i < btif_max_av_clients; i++)
3466     {
3467         if (btif_av_cb[i].is_slave == TRUE)
3468         {
3469             BTIF_TRACE_WARNING("Conected as slave");
3470             is_slave = TRUE;
3471             break;
3472         }
3473     }
3474
3475     if ((num_av_connected <= 2) && (is_br_hs_connected != TRUE) &&
3476         (is_slave == FALSE) && ((num_connected_br_edr_devices <= 2) &&
3477         (num_connected_le_devices <= 1)))
3478     {
3479         enable_multicast = TRUE;
3480     }
3481     else
3482     {
3483         enable_multicast = FALSE;
3484     }
3485
3486     BTIF_TRACE_DEBUG("%s Multicast current state : %s", __FUNCTION__,
3487         enable_multicast ? "Enabled" : "Disabled" );
3488
3489     if (prev_multicast_state != enable_multicast)
3490     {
3491         BTA_AvEnableMultiCast(enable_multicast,
3492                 btif_av_cb[index].bta_handle);
3493         HAL_CBACK(bt_av_src_callbacks, multicast_state_cb,
3494               enable_multicast);
3495     }
3496 }
3497 /******************************************************************************
3498 **
3499 ** Function        btif_av_get_multicast_state
3500 **
3501 ** Description     Returns TRUE if multicast is enabled else false
3502 **
3503 ** Returns         BOOLEAN
3504 ******************************************************************************/
3505 BOOLEAN btif_av_get_multicast_state()
3506 {
3507     return enable_multicast;
3508 }
3509 /******************************************************************************
3510 **
3511 ** Function        btif_av_get_ongoing_multicast
3512 **
3513 ** Description     Returns TRUE if multicast is ongoing
3514 **
3515 ** Returns         BOOLEAN
3516 ******************************************************************************/
3517 BOOLEAN btif_av_get_ongoing_multicast()
3518 {
3519     int i = 0, j = 0;
3520     if (!is_multicast_supported)
3521     {
3522         BTIF_TRACE_DEBUG("Multicast is Disabled");
3523         return FALSE;
3524     }
3525     for (i = 0; i < btif_max_av_clients; i++)
3526     {
3527         if (btif_av_cb[i].is_device_playing)
3528         {
3529             j++;
3530         }
3531     }
3532     if (j == btif_max_av_clients)
3533     {
3534         return TRUE;
3535     }
3536     else
3537     {
3538         return FALSE;
3539     }
3540 }
3541
3542 /******************************************************************************
3543 **
3544 ** Function        btif_av_is_multicast_supported
3545 **
3546 ** Description     Returns TRUE if multicast is supported
3547 **
3548 ** Returns         BOOLEAN
3549 ******************************************************************************/
3550 BOOLEAN btif_av_is_multicast_supported()
3551 {
3552     return is_multicast_supported;
3553 }
3554
3555 /******************************************************************************
3556 **
3557 ** Function        btif_av_is_offload_supported
3558 **
3559 ** Description     Returns split mode status
3560 **
3561 ** Returns         TRUE if split mode is enabled, FALSE otherwise
3562 ********************************************************************************/
3563 BOOLEAN btif_av_is_offload_supported()
3564 {
3565     return bt_split_a2dp_enabled;
3566 }
3567
3568 #ifdef BTA_AV_SPLIT_A2DP_ENABLED
3569 int btif_av_get_current_playing_dev_idx(void)
3570 {
3571     int i;
3572
3573     for (i = 0; i < btif_max_av_clients; i++)
3574     {
3575         if (btif_av_cb[i].current_playing == TRUE)
3576         {
3577             BTIF_TRACE_DEBUG("current playing on index = %d",i);
3578             return i;
3579         }
3580     }
3581     return -1;
3582 }
3583 /******************************************************************************
3584 **
3585 ** Function         btif_av_get_streaming_channel_id
3586 **
3587 ** Description     Returns streaming channel id
3588 **
3589 ** Returns          channel id
3590 ********************************************************************************/
3591 UINT16 btif_av_get_streaming_channel_id(void)
3592 {
3593     int index;
3594
3595     index = btif_av_get_current_playing_dev_idx();
3596     if (index != -1)
3597     {
3598         BTIF_TRACE_DEBUG("btif_av_get_streaming_channel_id: %u",
3599                         btif_av_cb[index].channel_id);
3600         return btif_av_cb[index].channel_id;
3601     }
3602     return 0;
3603 }
3604
3605 /******************************************************************************
3606 **
3607 ** Function         btif_av_get_peer_addr
3608 **
3609 ** Description     Returns peer device address
3610 **
3611 ** Returns          peer address
3612 ********************************************************************************/
3613 void btif_av_get_peer_addr(bt_bdaddr_t *peer_bda)
3614 {
3615     btif_sm_state_t state = BTIF_AV_STATE_IDLE;
3616     int i;
3617     memset(peer_bda, 0, sizeof(bt_bdaddr_t));
3618     for (i = 0; i < btif_max_av_clients; i++)
3619     {
3620         state = btif_sm_get_state(btif_av_cb[i].sm_handle);
3621         if (state == BTIF_AV_STATE_STARTED)
3622         {
3623             BTIF_TRACE_DEBUG("btif_av_get_peer_addr: %u",
3624                     btif_av_cb[i].peer_bda);
3625             memcpy(peer_bda, &btif_av_cb[i].peer_bda,
3626                                     sizeof(bt_bdaddr_t));
3627         }
3628     }
3629 }
3630
3631 /******************************************************************************
3632 **
3633 ** Function         btif_av_get_playing_device_hdl
3634 **
3635 ** Description      Returns current playing device's bta handle
3636 **
3637 ** Returns         BTA HANDLE
3638 ********************************************************************************/
3639 tBTA_AV_HNDL btif_av_get_playing_device_hdl()
3640 {
3641     int i;
3642     btif_sm_state_t state = BTIF_AV_STATE_IDLE;
3643
3644     for (i = 0; i < btif_max_av_clients; i++)
3645     {
3646         state = btif_sm_get_state(btif_av_cb[i].sm_handle);
3647         if (state == BTIF_AV_STATE_STARTED)
3648         {
3649             return btif_av_cb[i].bta_handle;
3650         }
3651     }
3652     return 0;
3653 }
3654
3655 /******************************************************************************
3656 **
3657 ** Function         btif_av_get_av_hdl_from_idx
3658 **
3659 ** Description      Returns bta handle from the device index
3660 **
3661 ** Returns         BTA HANDLE
3662 ********************************************************************************/
3663 tBTA_AV_HNDL btif_av_get_av_hdl_from_idx(UINT8 idx)
3664 {
3665     if (idx == btif_max_av_clients)
3666     {
3667         BTIF_TRACE_ERROR("%s: Invalid handle",__func__);
3668         return -1;
3669     }
3670     return btif_av_cb[idx].bta_handle;
3671 }
3672
3673 /******************************************************************************
3674 **
3675 ** Function         btif_av_is_codec_offload_supported
3676 **
3677 ** Description     check if the correpsonding codec is supported in offload
3678 **
3679 ** Returns         TRUE if supported, FALSE otherwise
3680 ********************************************************************************/
3681 BOOLEAN btif_av_is_codec_offload_supported(int codec)
3682 {
3683     BOOLEAN ret = FALSE;
3684     BTIF_TRACE_DEBUG("btif_av_is_codec_offload_supported = %s",dump_av_codec_name(codec));
3685     switch(codec)
3686     {
3687         case SBC:
3688             ret = btif_av_codec_offload.sbc_offload;
3689             break;
3690         case APTX:
3691             ret = btif_av_codec_offload.aptx_offload;
3692             break;
3693         case AAC:
3694             ret = btif_av_codec_offload.aac_offload;
3695             break;
3696         case APTXHD:
3697             ret = btif_av_codec_offload.aptxhd_offload;
3698             break;
3699         default:
3700             ret = FALSE;
3701     }
3702     BTIF_TRACE_DEBUG("btif_av_is_codec_offload_supported %s codec supported = %d",dump_av_codec_name(codec),ret);
3703     return ret;
3704 }
3705
3706 /******************************************************************************
3707 **
3708 ** Function         btif_av_is_under_handoff
3709 **
3710 ** Description     check if AV state is under handoff
3711 **
3712 ** Returns         TRUE if handoff is triggered, FALSE otherwise
3713 ********************************************************************************/
3714 BOOLEAN btif_av_is_under_handoff()
3715 {
3716     int i;
3717     btif_sm_state_t state = BTIF_AV_STATE_IDLE;
3718
3719     BTIF_TRACE_DEBUG("btif_av_is_under_handoff");
3720
3721     for (i = 0; i < btif_max_av_clients; i++)
3722     {
3723         state = btif_sm_get_state(btif_av_cb[i].sm_handle);
3724         if (btif_av_cb[i].dual_handoff &&
3725             (state == BTIF_AV_STATE_STARTED || state == BTIF_AV_STATE_OPENED))
3726         {
3727             /* If a2dp reconfigure is triggered when playing device disconnect is
3728              * initiated locally then return false, otherwise wait till the suspend cfm
3729              * is received from the remote.
3730              */
3731             return TRUE;
3732         }
3733     }
3734     return FALSE;
3735 }
3736 #endif
3737