1 /******************************************************************************
3 * Copyright 2004-2012 Broadcom Corporation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 /******************************************************************************
21 * This file contains action functions for advanced audio/video stream
22 * state machine. these functions are shared by both audio and video
25 ******************************************************************************/
27 #define LOG_TAG "bt_bta_av"
29 #include <base/strings/stringprintf.h>
34 #include "bt_target.h" // Must be first to define build configuration
36 #include "bta/av/bta_av_int.h"
37 #include "bta/include/bta_ar_api.h"
38 #include "bta/include/bta_av_co.h"
39 #include "btif/avrcp/avrcp_service.h"
40 #include "btif/include/btif_av_co.h"
41 #include "btif/include/btif_config.h"
42 #include "btif/include/btif_storage.h"
43 #include "device/include/interop.h"
44 #include "main/shim/dumpsys.h"
45 #include "osi/include/log.h"
46 #include "osi/include/osi.h"
47 #include "osi/include/properties.h"
48 #include "stack/include/a2dp_sbc.h"
49 #include "stack/include/acl_api.h"
50 #include "stack/include/btm_api.h"
51 #include "stack/include/btm_client_interface.h"
52 #include "stack/include/l2c_api.h"
53 #include "types/hci_role.h"
57 constexpr char kBtmLogTag[] = "A2DP";
61 /*****************************************************************************
63 ****************************************************************************/
65 /* the delay time in milliseconds to start service discovery on AVRCP */
66 #ifndef BTA_AV_RC_DISC_TIME_VAL
67 #define BTA_AV_RC_DISC_TIME_VAL 3500
70 /* the timer in milliseconds to guard against link busy and AVDT_CloseReq failed
72 #ifndef BTA_AV_CLOSE_REQ_TIME_VAL
73 #define BTA_AV_CLOSE_REQ_TIME_VAL 4000
76 /* number to retry on reconfigure failure - some headsets requirs this number to
78 #ifndef BTA_AV_RECONFIG_RETRY
79 #define BTA_AV_RECONFIG_RETRY 6
82 /* ACL quota we are letting FW use for A2DP Offload Tx. */
83 #define BTA_AV_A2DP_OFFLOAD_XMIT_QUOTA 4
85 static void bta_av_offload_codec_builder(tBTA_AV_SCB* p_scb,
86 tBT_A2DP_OFFLOAD* p_a2dp_offload);
88 /* state machine states */
98 /* the call out functions for audio stream */
99 const tBTA_AV_CO_FUNCTS bta_av_a2dp_cos = {bta_av_co_audio_init,
100 bta_av_co_audio_disc_res,
101 bta_av_co_audio_getconfig,
102 bta_av_co_audio_setconfig,
103 bta_av_co_audio_open,
104 bta_av_co_audio_close,
105 bta_av_co_audio_start,
106 bta_av_co_audio_stop,
107 bta_av_co_audio_source_data_path,
108 bta_av_co_audio_delay,
109 bta_av_co_audio_update_mtu,
110 bta_av_co_get_scmst_info};
112 /* these tables translate AVDT events to SSM events */
113 static const uint16_t bta_av_stream_evt_ok[] = {
114 BTA_AV_STR_DISC_OK_EVT, /* AVDT_DISCOVER_CFM_EVT */
115 BTA_AV_STR_GETCAP_OK_EVT, /* AVDT_GETCAP_CFM_EVT */
116 BTA_AV_STR_OPEN_OK_EVT, /* AVDT_OPEN_CFM_EVT */
117 BTA_AV_STR_OPEN_OK_EVT, /* AVDT_OPEN_IND_EVT */
118 BTA_AV_STR_CONFIG_IND_EVT, /* AVDT_CONFIG_IND_EVT */
119 BTA_AV_STR_START_OK_EVT, /* AVDT_START_CFM_EVT */
120 BTA_AV_STR_START_OK_EVT, /* AVDT_START_IND_EVT */
121 BTA_AV_STR_SUSPEND_CFM_EVT, /* AVDT_SUSPEND_CFM_EVT */
122 BTA_AV_STR_SUSPEND_CFM_EVT, /* AVDT_SUSPEND_IND_EVT */
123 BTA_AV_STR_CLOSE_EVT, /* AVDT_CLOSE_CFM_EVT */
124 BTA_AV_STR_CLOSE_EVT, /* AVDT_CLOSE_IND_EVT */
125 BTA_AV_STR_RECONFIG_CFM_EVT, /* AVDT_RECONFIG_CFM_EVT */
126 0, /* AVDT_RECONFIG_IND_EVT */
127 BTA_AV_STR_SECURITY_CFM_EVT, /* AVDT_SECURITY_CFM_EVT */
128 BTA_AV_STR_SECURITY_IND_EVT, /* AVDT_SECURITY_IND_EVT */
129 BTA_AV_STR_WRITE_CFM_EVT, /* AVDT_WRITE_CFM_EVT */
130 BTA_AV_AVDT_CONNECT_EVT, /* AVDT_CONNECT_IND_EVT */
131 BTA_AV_AVDT_DISCONNECT_EVT, /* AVDT_DISCONNECT_IND_EVT */
132 BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_CONN_EVT */
133 BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_DISCONN_EVT */
134 BTA_AV_AVDT_DELAY_RPT_EVT, /* AVDT_DELAY_REPORT_EVT */
135 BTA_AV_AVDT_DELAY_RPT_CFM_EVT, /* AVDT_DELAY_REPORT_CFM_EVT */
138 static const uint16_t bta_av_stream_evt_fail[] = {
139 BTA_AV_STR_DISC_FAIL_EVT, /* AVDT_DISCOVER_CFM_EVT */
140 BTA_AV_STR_GETCAP_FAIL_EVT, /* AVDT_GETCAP_CFM_EVT */
141 BTA_AV_STR_OPEN_FAIL_EVT, /* AVDT_OPEN_CFM_EVT */
142 BTA_AV_STR_OPEN_OK_EVT, /* AVDT_OPEN_IND_EVT */
143 BTA_AV_STR_CONFIG_IND_EVT, /* AVDT_CONFIG_IND_EVT */
144 BTA_AV_STR_START_FAIL_EVT, /* AVDT_START_CFM_EVT */
145 BTA_AV_STR_START_OK_EVT, /* AVDT_START_IND_EVT */
146 BTA_AV_STR_SUSPEND_CFM_EVT, /* AVDT_SUSPEND_CFM_EVT */
147 BTA_AV_STR_SUSPEND_CFM_EVT, /* AVDT_SUSPEND_IND_EVT */
148 BTA_AV_STR_CLOSE_EVT, /* AVDT_CLOSE_CFM_EVT */
149 BTA_AV_STR_CLOSE_EVT, /* AVDT_CLOSE_IND_EVT */
150 BTA_AV_STR_RECONFIG_CFM_EVT, /* AVDT_RECONFIG_CFM_EVT */
151 0, /* AVDT_RECONFIG_IND_EVT */
152 BTA_AV_STR_SECURITY_CFM_EVT, /* AVDT_SECURITY_CFM_EVT */
153 BTA_AV_STR_SECURITY_IND_EVT, /* AVDT_SECURITY_IND_EVT */
154 BTA_AV_STR_WRITE_CFM_EVT, /* AVDT_WRITE_CFM_EVT */
155 BTA_AV_AVDT_CONNECT_EVT, /* AVDT_CONNECT_IND_EVT */
156 BTA_AV_AVDT_DISCONNECT_EVT, /* AVDT_DISCONNECT_IND_EVT */
157 BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_CONN_EVT */
158 BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_DISCONN_EVT */
159 BTA_AV_AVDT_DELAY_RPT_EVT, /* AVDT_DELAY_REPORT_EVT */
160 BTA_AV_AVDT_DELAY_RPT_CFM_EVT, /* AVDT_DELAY_REPORT_CFM_EVT */
163 /***********************************************
165 * Function bta_get_scb_handle
167 * Description gives the registered AVDT handle.by checking with sep_type.
171 **********************************************/
172 static uint8_t bta_av_get_scb_handle(tBTA_AV_SCB* p_scb, uint8_t local_sep) {
173 for (int i = 0; i < BTAV_A2DP_CODEC_INDEX_MAX; i++) {
174 if ((p_scb->seps[i].tsep == local_sep) &&
175 A2DP_CodecTypeEquals(p_scb->seps[i].codec_info,
176 p_scb->cfg.codec_info)) {
177 return (p_scb->seps[i].av_handle);
180 APPL_TRACE_DEBUG("%s: local sep_type %d not found", __func__, local_sep)
181 return 0; /* return invalid handle */
184 /***********************************************
186 * Function bta_av_get_scb_sep_type
188 * Description gives the sep type by cross-checking with AVDT handle
192 **********************************************/
193 static uint8_t bta_av_get_scb_sep_type(tBTA_AV_SCB* p_scb,
194 uint8_t tavdt_handle) {
195 for (int i = 0; i < BTAV_A2DP_CODEC_INDEX_MAX; i++) {
196 if (p_scb->seps[i].av_handle == tavdt_handle) return (p_scb->seps[i].tsep);
198 APPL_TRACE_DEBUG("%s: avdt_handle %d not found", __func__, tavdt_handle)
199 return AVDT_TSEP_INVALID;
202 /*******************************************************************************
204 * Function bta_av_save_addr
206 * Description copy the bd_addr and maybe reset the supported flags
211 ******************************************************************************/
212 static void bta_av_save_addr(tBTA_AV_SCB* p_scb, const RawAddress& bd_addr) {
213 APPL_TRACE_DEBUG("%s: peer=%s recfg_sup:%d, suspend_sup:%d", __func__,
214 bd_addr.ToString().c_str(), p_scb->recfg_sup,
216 if (p_scb->PeerAddress() != bd_addr) {
217 LOG_INFO("%s: reset flags old_addr=%s new_addr=%s", __func__,
218 p_scb->PeerAddress().ToString().c_str(),
219 bd_addr.ToString().c_str());
220 /* a new addr, reset the supported flags */
221 p_scb->recfg_sup = true;
222 p_scb->suspend_sup = true;
225 /* do this copy anyway, just in case the first addr matches
226 * the control block one by accident */
227 p_scb->OnConnected(bd_addr);
230 /*******************************************************************************
232 * Function notify_start_failed
234 * Description notify up-layer AV start failed
239 ******************************************************************************/
240 static void notify_start_failed(tBTA_AV_SCB* p_scb) {
241 LOG_ERROR("%s: peer %s role:0x%x bta_channel:%d bta_handle:0x%x", __func__,
242 p_scb->PeerAddress().ToString().c_str(), p_scb->role, p_scb->chnl,
245 /* if start failed, clear role */
246 p_scb->role &= ~BTA_AV_ROLE_START_INT;
247 start.chnl = p_scb->chnl;
248 start.status = BTA_AV_FAIL;
249 start.initiator = true;
250 start.hndl = p_scb->hndl;
253 bta_av_data.start = start;
254 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, &bta_av_data);
257 /*******************************************************************************
259 * Function bta_av_st_rc_timer
261 * Description start the AVRC timer if no RC connection & CT is supported &
263 * as ACP (we do not really know if we want AVRC)
267 ******************************************************************************/
268 void bta_av_st_rc_timer(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
269 APPL_TRACE_DEBUG("%s: rc_handle:%d, use_rc: %d", __func__, p_scb->rc_handle,
271 /* for outgoing RC connection as INT/CT */
272 if ((p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE) &&
273 /* (bta_av_cb.features & BTA_AV_FEAT_RCCT) && */
274 (p_scb->use_rc || (p_scb->role & BTA_AV_ROLE_AD_ACP))) {
275 if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0) {
276 bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_RC_DISC_TIME_VAL,
277 BTA_AV_AVRC_TIMER_EVT, p_scb->hndl);
279 p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
284 /*******************************************************************************
286 * Function bta_av_next_getcap
288 * Description The function gets the capabilities of the next available
289 * stream found in the discovery results.
291 * Returns true if we sent request to AVDT, false otherwise.
293 ******************************************************************************/
294 static bool bta_av_next_getcap(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
296 bool sent_cmd = false;
297 uint16_t uuid_int = p_scb->uuid_int;
298 uint8_t sep_requested = 0;
300 if (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE)
301 sep_requested = AVDT_TSEP_SNK;
302 else if (uuid_int == UUID_SERVCLASS_AUDIO_SINK)
303 sep_requested = AVDT_TSEP_SRC;
305 for (i = p_scb->sep_info_idx; i < p_scb->num_seps; i++) {
306 /* steam not in use, is a sink, and is the right media type (audio/video) */
307 if ((!p_scb->sep_info[i].in_use) &&
308 (p_scb->sep_info[i].tsep == sep_requested) &&
309 (p_scb->sep_info[i].media_type == p_scb->media_type)) {
310 p_scb->sep_info_idx = i;
312 /* we got a stream; get its capabilities */
313 bool get_all_cap = (p_scb->AvdtpVersion() >= AVDT_VERSION_1_3) &&
314 (A2DP_GetAvdtpVersion() >= AVDT_VERSION_1_3);
315 AVDT_GetCapReq(p_scb->PeerAddress(), p_scb->hdi, p_scb->sep_info[i].seid,
316 &p_scb->peer_cap, &bta_av_proc_stream_evt, get_all_cap);
322 /* if no streams available then stream open fails */
324 APPL_TRACE_ERROR("%s: BTA_AV_STR_GETCAP_FAIL_EVT: peer_addr=%s", __func__,
325 p_scb->PeerAddress().ToString().c_str());
326 bta_av_ssm_execute(p_scb, BTA_AV_STR_GETCAP_FAIL_EVT, p_data);
332 /*******************************************************************************
334 * Function bta_av_proc_stream_evt
336 * Description Utility function to compose stream events.
340 ******************************************************************************/
341 void bta_av_proc_stream_evt(uint8_t handle, const RawAddress& bd_addr,
342 uint8_t event, tAVDT_CTRL* p_data,
344 CHECK_LT(scb_index, BTA_AV_NUM_STRS);
345 tBTA_AV_SCB* p_scb = bta_av_cb.p_scb[scb_index];
346 uint16_t sec_len = 0;
349 "%s: peer_address: %s avdt_handle: %d event=0x%x scb_index=%d p_scb=%p",
350 __func__, bd_addr.ToString().c_str(), handle, event, scb_index, p_scb);
353 if (event == AVDT_SECURITY_IND_EVT) {
354 sec_len = (p_data->security_ind.len < BTA_AV_SECURITY_MAX_LEN)
355 ? p_data->security_ind.len
356 : BTA_AV_SECURITY_MAX_LEN;
357 } else if (event == AVDT_SECURITY_CFM_EVT && p_data->hdr.err_code == 0) {
358 sec_len = (p_data->security_cfm.len < BTA_AV_SECURITY_MAX_LEN)
359 ? p_data->security_cfm.len
360 : BTA_AV_SECURITY_MAX_LEN;
365 tBTA_AV_STR_MSG* p_msg =
366 (tBTA_AV_STR_MSG*)osi_malloc(sizeof(tBTA_AV_STR_MSG) + sec_len);
368 /* copy event data, bd addr, and handle to event message buffer */
369 p_msg->hdr.offset = 0;
371 p_msg->bd_addr = bd_addr;
372 p_msg->scb_index = scb_index;
373 APPL_TRACE_EVENT("%s: stream event bd_addr: %s scb_index: %u", __func__,
374 p_msg->bd_addr.ToString().c_str(), scb_index);
376 if (p_data != NULL) {
377 memcpy(&p_msg->msg, p_data, sizeof(tAVDT_CTRL));
378 /* copy config params to event message buffer */
380 case AVDT_CONFIG_IND_EVT:
381 p_msg->cfg = *p_data->config_ind.p_cfg;
384 case AVDT_SECURITY_IND_EVT:
385 p_msg->msg.security_ind.p_data = (uint8_t*)(p_msg + 1);
386 memcpy(p_msg->msg.security_ind.p_data, p_data->security_ind.p_data,
390 case AVDT_SECURITY_CFM_EVT:
391 p_msg->msg.security_cfm.p_data = (uint8_t*)(p_msg + 1);
392 if (p_data->hdr.err_code == 0) {
393 memcpy(p_msg->msg.security_cfm.p_data, p_data->security_cfm.p_data,
398 case AVDT_SUSPEND_IND_EVT:
399 p_msg->msg.hdr.err_code = 0;
402 case AVDT_CONNECT_IND_EVT:
403 p_scb->recfg_sup = true;
404 p_scb->suspend_sup = true;
411 p_msg->msg.hdr.err_code = 0;
414 /* look up application event */
415 if ((p_data == NULL) || (p_data->hdr.err_code == 0)) {
416 p_msg->hdr.event = bta_av_stream_evt_ok[event];
418 p_msg->hdr.event = bta_av_stream_evt_fail[event];
421 p_msg->initiator = false;
422 if (event == AVDT_SUSPEND_CFM_EVT) p_msg->initiator = true;
424 APPL_TRACE_VERBOSE("%s: bta_handle:0x%x avdt_handle:%d", __func__,
425 p_scb->hndl, handle);
426 p_msg->hdr.layer_specific = p_scb->hndl;
427 p_msg->handle = handle;
428 p_msg->avdt_event = event;
429 bta_sys_sendmsg(p_msg);
433 bta_av_conn_cback(handle, bd_addr, event, p_data, scb_index);
435 APPL_TRACE_ERROR("%s: p_data is null", __func__);
439 /*******************************************************************************
441 * Function bta_av_sink_data_cback
443 * Description This is the AVDTP callback function for sink stream events.
447 ******************************************************************************/
448 void bta_av_sink_data_cback(uint8_t handle, BT_HDR* p_pkt, uint32_t time_stamp,
453 "%s: avdt_handle: %d pkt_len=0x%x offset = 0x%x "
454 "number of frames 0x%x sequence number 0x%x",
455 __func__, handle, p_pkt->len, p_pkt->offset,
456 *((uint8_t*)(p_pkt + 1) + p_pkt->offset), p_pkt->layer_specific);
457 /* Get SCB and correct sep type */
458 for (index = 0; index < BTA_AV_NUM_STRS; index++) {
459 p_scb = bta_av_cb.p_scb[index];
460 if ((p_scb->avdt_handle == handle) &&
461 (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK)) {
465 if (index == BTA_AV_NUM_STRS) {
466 /* cannot find correct handler */
470 p_pkt->event = BTA_AV_SINK_MEDIA_DATA_EVT;
471 p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback(
472 p_scb->PeerAddress(), BTA_AV_SINK_MEDIA_DATA_EVT, (tBTA_AV_MEDIA*)p_pkt);
473 /* Free the buffer: a copy of the packet has been delivered */
477 /*******************************************************************************
479 * Function bta_av_a2dp_sdp_cback
481 * Description A2DP service discovery callback.
485 ******************************************************************************/
486 static void bta_av_a2dp_sdp_cback(bool found, tA2DP_Service* p_service,
487 const RawAddress& peer_address) {
488 APPL_TRACE_DEBUG("%s: peer %s : found=%s", __func__,
489 peer_address.ToString().c_str(), (found) ? "true" : "false");
491 tBTA_AV_SCB* p_scb = NULL;
492 if (peer_address != RawAddress::kEmpty) {
493 p_scb = bta_av_addr_to_scb(peer_address);
496 p_scb = bta_av_hndl_to_scb(bta_av_cb.handle);
499 APPL_TRACE_ERROR("%s: no scb found for SDP handle(0x%x)", __func__,
503 if (bta_av_cb.handle != p_scb->hndl) {
504 APPL_TRACE_WARNING("%s: SDP bta_handle expected=0x%x processing=0x%x",
505 __func__, bta_av_cb.handle, p_scb->hndl);
509 APPL_TRACE_ERROR("%s: peer %s A2DP service discovery failed", __func__,
510 p_scb->PeerAddress().ToString().c_str());
512 APPL_TRACE_DEBUG("%s: peer %s found=%s", __func__,
513 p_scb->PeerAddress().ToString().c_str(),
514 (found) ? "true" : "false");
516 tBTA_AV_SDP_RES* p_msg =
517 (tBTA_AV_SDP_RES*)osi_malloc(sizeof(tBTA_AV_SDP_RES));
519 p_msg->hdr.event = BTA_AV_SDP_DISC_OK_EVT;
521 p_msg->hdr.event = BTA_AV_SDP_DISC_FAIL_EVT;
522 APPL_TRACE_ERROR("%s: BTA_AV_SDP_DISC_FAIL_EVT: peer_addr=%s", __func__,
523 p_scb->PeerAddress().ToString().c_str());
525 if (found && (p_service != NULL)) {
526 p_scb->SetAvdtpVersion(p_service->avdt_version);
527 if (p_service->avdt_version != 0) {
528 if (btif_config_set_bin(p_scb->PeerAddress().ToString(),
529 AVDTP_VERSION_CONFIG_KEY,
530 (const uint8_t*)&p_service->avdt_version,
531 sizeof(p_service->avdt_version))) {
534 APPL_TRACE_WARNING("%s: Failed to store peer AVDTP version for %s",
535 __func__, p_scb->PeerAddress().ToString().c_str());
539 p_scb->SetAvdtpVersion(0);
541 p_msg->hdr.layer_specific = p_scb->hndl;
543 bta_sys_sendmsg(p_msg);
546 /*******************************************************************************
548 * Function bta_av_adjust_seps_idx
550 * Description adjust the sep_idx
554 ******************************************************************************/
555 static void bta_av_adjust_seps_idx(tBTA_AV_SCB* p_scb, uint8_t avdt_handle) {
556 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
557 A2DP_CodecName(p_scb->cfg.codec_info));
558 for (int i = 0; i < BTAV_A2DP_CODEC_INDEX_MAX; i++) {
559 APPL_TRACE_DEBUG("%s: avdt_handle: %d codec: %s", __func__,
560 p_scb->seps[i].av_handle,
561 A2DP_CodecName(p_scb->seps[i].codec_info));
562 if (p_scb->seps[i].av_handle && (p_scb->seps[i].av_handle == avdt_handle) &&
563 A2DP_CodecTypeEquals(p_scb->seps[i].codec_info,
564 p_scb->cfg.codec_info)) {
566 p_scb->avdt_handle = p_scb->seps[i].av_handle;
572 /*******************************************************************************
574 * Function bta_av_switch_role
576 * Description Switch role was not started and a timer was started.
577 * another attempt to switch role now - still opening.
581 ******************************************************************************/
582 void bta_av_switch_role(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
583 tBTA_AV_RS_RES switch_res = BTA_AV_RS_NONE;
584 tBTA_AV_API_OPEN* p_buf = &p_scb->q_info.open;
586 APPL_TRACE_DEBUG("%s: peer %s wait:0x%x", __func__,
587 p_scb->PeerAddress().ToString().c_str(), p_scb->wait);
588 if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_START)
589 p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RETRY;
591 /* clear the masks set when the timer is started */
593 ~(BTA_AV_WAIT_ROLE_SW_RES_OPEN | BTA_AV_WAIT_ROLE_SW_RES_START);
595 if (p_scb->q_tag == BTA_AV_Q_TAG_OPEN) {
596 if (bta_av_switch_if_needed(p_scb) ||
597 !bta_av_link_role_ok(p_scb, A2DP_SET_MULTL_BIT)) {
598 p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
600 /* this should not happen in theory. Just in case...
601 * continue to do_disc_a2dp */
602 switch_res = BTA_AV_RS_DONE;
605 /* report failure on OPEN */
606 APPL_TRACE_ERROR("%s: peer %s role switch failed (wait=0x%x)", __func__,
607 p_scb->PeerAddress().ToString().c_str(), p_scb->wait);
608 switch_res = BTA_AV_RS_FAIL;
611 if (switch_res != BTA_AV_RS_NONE) {
612 if (bta_av_cb.rs_idx == (p_scb->hdi + 1)) {
613 bta_av_cb.rs_idx = 0;
615 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_RETRY;
617 p_buf->switch_res = switch_res;
618 bta_av_do_disc_a2dp(p_scb, (tBTA_AV_DATA*)p_buf);
622 /*******************************************************************************
624 * Function bta_av_role_res
626 * Description Handle the role changed event
631 ******************************************************************************/
632 void bta_av_role_res(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
633 bool initiator = false;
635 APPL_TRACE_DEBUG("%s: peer %s q_tag:%d, wait:0x%x, role:0x%x", __func__,
636 p_scb->PeerAddress().ToString().c_str(), p_scb->q_tag,
637 p_scb->wait, p_scb->role);
638 if (p_scb->role & BTA_AV_ROLE_START_INT) initiator = true;
640 if (p_scb->q_tag == BTA_AV_Q_TAG_START) {
641 if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_STARTED) {
642 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
643 if (p_data->role_res.hci_status != HCI_SUCCESS) {
644 p_scb->role &= ~BTA_AV_ROLE_START_INT;
645 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->PeerAddress());
646 /* start failed because of role switch. */
648 start.chnl = p_scb->chnl;
649 start.status = BTA_AV_FAIL_ROLE;
650 start.hndl = p_scb->hndl;
651 start.initiator = initiator;
653 bta_av_data.start = start;
654 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, &bta_av_data);
656 bta_av_start_ok(p_scb, p_data);
658 } else if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_START)
659 p_scb->wait |= BTA_AV_WAIT_ROLE_SW_FAILED;
660 } else if (p_scb->q_tag == BTA_AV_Q_TAG_OPEN) {
661 if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_OPEN) {
662 p_scb->role &= ~BTA_AV_ROLE_START_INT;
663 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
665 if (p_data->role_res.hci_status != HCI_SUCCESS) {
666 /* Open failed because of role switch. */
667 tBTA_AV_OPEN av_open;
668 av_open.bd_addr = p_scb->PeerAddress();
669 av_open.chnl = p_scb->chnl;
670 av_open.hndl = p_scb->hndl;
671 av_open.status = BTA_AV_FAIL_ROLE;
672 if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC) {
673 av_open.sep = AVDT_TSEP_SNK;
674 } else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK) {
675 av_open.sep = AVDT_TSEP_SRC;
678 bta_av_data.open = av_open;
679 (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, &bta_av_data);
681 /* Continue av open process */
682 p_scb->q_info.open.switch_res = BTA_AV_RS_DONE;
683 bta_av_do_disc_a2dp(p_scb, (tBTA_AV_DATA*)&(p_scb->q_info.open));
687 "%s: peer %s unexpected role switch event: q_tag = %d wait = 0x%x",
688 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->q_tag,
693 APPL_TRACE_DEBUG("%s: peer %s wait:0x%x, role:0x%x", __func__,
694 p_scb->PeerAddress().ToString().c_str(), p_scb->wait,
698 /*******************************************************************************
700 * Function bta_av_delay_co
702 * Description Call the delay call-out function to report the delay report
707 ******************************************************************************/
708 void bta_av_delay_co(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
709 APPL_TRACE_DEBUG("%s: peer %s bta_handle:0x%x delay:%d", __func__,
710 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
711 p_data->str_msg.msg.delay_rpt_cmd.delay);
712 p_scb->p_cos->delay(p_scb->hndl, p_scb->PeerAddress(),
713 p_data->str_msg.msg.delay_rpt_cmd.delay);
716 /*******************************************************************************
718 * Function bta_av_do_disc_a2dp
720 * Description Do service discovery for A2DP.
724 ******************************************************************************/
725 void bta_av_do_disc_a2dp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
726 bool ok_continue = false;
727 tA2DP_SDP_DB_PARAMS db_params;
728 uint16_t attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
729 ATTR_ID_PROTOCOL_DESC_LIST,
730 ATTR_ID_BT_PROFILE_DESC_LIST};
731 uint16_t sdp_uuid = 0; /* UUID for which SDP has to be done */
733 APPL_TRACE_DEBUG("%s: peer_addr: %s use_rc: %d switch_res:%d, oc:%d",
734 __func__, p_data->api_open.bd_addr.ToString().c_str(),
735 p_data->api_open.use_rc, p_data->api_open.switch_res,
736 bta_av_cb.audio_open_cnt);
738 memcpy(&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
740 switch (p_data->api_open.switch_res) {
742 if (bta_av_switch_if_needed(p_scb) ||
743 !bta_av_link_role_ok(p_scb, A2DP_SET_MULTL_BIT)) {
744 /* waiting for role switch result. save the api to control block */
745 memcpy(&p_scb->q_info.open, &p_data->api_open,
746 sizeof(tBTA_AV_API_OPEN));
747 p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
748 p_scb->q_tag = BTA_AV_Q_TAG_OPEN;
755 /* report a new failure event */
756 p_scb->open_status = BTA_AV_FAIL_ROLE;
757 APPL_TRACE_ERROR("%s: BTA_AV_SDP_DISC_FAIL_EVT: peer_addr=%s", __func__,
758 p_scb->PeerAddress().ToString().c_str());
759 bta_av_ssm_execute(p_scb, BTA_AV_SDP_DISC_FAIL_EVT, NULL);
763 p_data = (tBTA_AV_DATA*)&p_scb->q_info.open;
764 /* continue to open if link role is ok */
765 if (bta_av_link_role_ok(p_scb, A2DP_SET_MULTL_BIT)) {
768 p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
777 APPL_TRACE_DEBUG("%s: ok_continue: %d wait:0x%x, q_tag: %d", __func__,
778 ok_continue, p_scb->wait, p_scb->q_tag);
779 if (!ok_continue) return;
781 /* clear the role switch bits */
782 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
784 if (p_scb->wait & BTA_AV_WAIT_CHECK_RC) {
785 p_scb->wait &= ~BTA_AV_WAIT_CHECK_RC;
786 bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_RC_DISC_TIME_VAL,
787 BTA_AV_AVRC_TIMER_EVT, p_scb->hndl);
790 /* store peer addr other parameters */
791 bta_av_save_addr(p_scb, p_data->api_open.bd_addr);
792 p_scb->use_rc = p_data->api_open.use_rc;
794 bta_sys_app_open(BTA_ID_AV, p_scb->app_id, p_scb->PeerAddress());
796 /* set up parameters */
797 db_params.db_len = BTA_AV_DISC_BUF_SIZE;
798 db_params.num_attr = 3;
799 db_params.p_attrs = attr_list;
800 p_scb->uuid_int = p_data->api_open.uuid;
801 p_scb->sdp_discovery_started = true;
802 if (p_scb->uuid_int == UUID_SERVCLASS_AUDIO_SINK)
803 sdp_uuid = UUID_SERVCLASS_AUDIO_SOURCE;
804 else if (p_scb->uuid_int == UUID_SERVCLASS_AUDIO_SOURCE)
805 sdp_uuid = UUID_SERVCLASS_AUDIO_SINK;
808 "%s: Initiate SDP discovery for peer %s : uuid_int=0x%x "
810 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->uuid_int,
812 tA2DP_STATUS find_service_status = A2DP_FindService(
813 sdp_uuid, p_scb->PeerAddress(), &db_params, bta_av_a2dp_sdp_cback);
814 if (find_service_status != A2DP_SUCCESS) {
816 "%s: A2DP_FindService() failed for peer %s uuid_int=0x%x "
817 "sdp_uuid=0x%x : status=%d",
818 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->uuid_int,
819 sdp_uuid, find_service_status);
820 bta_av_a2dp_sdp_cback(false, nullptr, p_scb->PeerAddress());
822 /* only one A2DP find service is active at a time */
823 bta_av_cb.handle = p_scb->hndl;
827 /*******************************************************************************
829 * Function bta_av_cleanup
831 * Description cleanup AV stream control block.
835 ******************************************************************************/
836 void bta_av_cleanup(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
837 tBTA_AV_CONN_CHG msg;
838 uint8_t role = BTA_AV_ROLE_AD_INT;
840 LOG_INFO("%s peer %s", __func__, p_scb->PeerAddress().ToString().c_str());
842 /* free any buffers */
843 p_scb->sdp_discovery_started = false;
844 p_scb->SetAvdtpVersion(0);
846 /* initialize some control block variables */
847 p_scb->open_status = BTA_AV_SUCCESS;
849 /* if de-registering shut everything down */
850 msg.hdr.layer_specific = p_scb->hndl;
851 p_scb->started = false;
852 p_scb->use_rtp_header_marker_bit = false;
855 p_scb->cur_psc_mask = 0;
857 p_scb->num_disc_snks = 0;
858 p_scb->coll_mask = 0;
859 alarm_cancel(p_scb->avrc_ct_timer);
860 alarm_cancel(p_scb->link_signalling_timer);
861 alarm_cancel(p_scb->accept_signalling_timer);
863 /* TODO(eisenbach): RE-IMPLEMENT USING VSC OR HAL EXTENSION
864 vendor_get_interface()->send_command(
865 (vendor_opcode_t)BT_VND_OP_A2DP_OFFLOAD_STOP, (void*)&p_scb->l2c_cid);
866 if (p_scb->offload_start_pending) {
867 tBTA_AV_STATUS status = BTA_AV_FAIL_STREAM;
869 bta_av_data.status = status;
870 (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, &bta_av_data);
874 if (p_scb->deregistering) {
876 for (int i = 0; i < BTAV_A2DP_CODEC_INDEX_MAX; i++) {
877 if (p_scb->seps[i].av_handle) AVDT_RemoveStream(p_scb->seps[i].av_handle);
878 p_scb->seps[i].av_handle = 0;
881 bta_av_dereg_comp((tBTA_AV_DATA*)&msg);
883 /* report stream closed to main SM */
885 msg.peer_addr = p_scb->PeerAddress();
886 bta_av_conn_chg((tBTA_AV_DATA*)&msg);
890 /*******************************************************************************
892 * Function bta_av_free_sdb
894 * Description Free service discovery db buffer.
898 ******************************************************************************/
899 void bta_av_free_sdb(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
900 p_scb->sdp_discovery_started = false;
903 /*******************************************************************************
905 * Function bta_av_config_ind
907 * Description Handle a stream configuration indication from the peer.
911 ******************************************************************************/
912 void bta_av_config_ind(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
913 tBTA_AV_CI_SETCONFIG setconfig{};
914 tAVDT_SEP_INFO* p_info;
915 const AvdtpSepConfig* p_evt_cfg = &p_data->str_msg.cfg;
916 uint8_t psc_mask = (p_evt_cfg->psc_mask | p_scb->cfg.psc_mask);
918 local_sep; /* sep type of local handle on which connection was received */
919 tBTA_AV_STR_MSG* p_msg = (tBTA_AV_STR_MSG*)p_data;
921 local_sep = bta_av_get_scb_sep_type(p_scb, p_msg->handle);
922 p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
924 APPL_TRACE_DEBUG("%s: peer %s bta_handle:0x%x local_sep:%d", __func__,
925 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
927 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
928 A2DP_CodecInfoString(p_evt_cfg->codec_info).c_str());
930 memcpy(p_scb->cfg.codec_info, p_evt_cfg->codec_info, AVDT_CODEC_SIZE);
931 bta_av_save_addr(p_scb, p_data->str_msg.bd_addr);
933 /* Clear collision mask */
934 p_scb->coll_mask = 0;
935 alarm_cancel(p_scb->accept_signalling_timer);
937 /* if no codec parameters in configuration, fail */
938 if ((p_evt_cfg->num_codec == 0) ||
939 /* or the peer requests for a service we do not support */
940 ((psc_mask != p_scb->cfg.psc_mask) &&
941 (psc_mask != (p_scb->cfg.psc_mask & ~AVDT_PSC_DELAY_RPT)))) {
942 setconfig.hndl = p_scb->hndl; /* we may not need this */
943 setconfig.err_code = AVDT_ERR_UNSUP_CFG;
944 bta_av_ssm_execute(p_scb, BTA_AV_CI_SETCONFIG_FAIL_EVT,
945 (tBTA_AV_DATA*)&setconfig);
947 p_info = &p_scb->sep_info[0];
949 p_info->media_type = p_scb->media_type;
950 p_info->seid = p_data->str_msg.msg.config_ind.int_seid;
952 /* Sep type of Peer will be oppsite role to our local sep */
953 if (local_sep == AVDT_TSEP_SRC)
954 p_info->tsep = AVDT_TSEP_SNK;
955 else if (local_sep == AVDT_TSEP_SNK)
956 p_info->tsep = AVDT_TSEP_SRC;
958 p_scb->role |= BTA_AV_ROLE_AD_ACP;
959 p_scb->cur_psc_mask = p_evt_cfg->psc_mask;
960 if (bta_av_cb.features & BTA_AV_FEAT_RCTG)
961 p_scb->use_rc = true;
963 p_scb->use_rc = false;
966 p_scb->sep_info_idx = 0;
967 APPL_TRACE_DEBUG("%s: SEID: %d use_rc: %d cur_psc_mask:0x%x", __func__,
968 p_info->seid, p_scb->use_rc, p_scb->cur_psc_mask);
969 /* in case of A2DP SINK this is the first time peer data is being sent to
971 if (local_sep == AVDT_TSEP_SNK) {
972 p_scb->p_cos->setcfg(p_scb->hndl, p_scb->PeerAddress(),
973 p_evt_cfg->codec_info, p_info->seid,
974 p_evt_cfg->num_protect, p_evt_cfg->protect_info,
975 AVDT_TSEP_SNK, p_msg->handle);
977 p_scb->p_cos->setcfg(p_scb->hndl, p_scb->PeerAddress(),
978 p_evt_cfg->codec_info, p_info->seid,
979 p_evt_cfg->num_protect, p_evt_cfg->protect_info,
980 AVDT_TSEP_SRC, p_msg->handle);
985 /*******************************************************************************
987 * Function bta_av_disconnect_req
989 * Description Disconnect AVDTP connection.
993 ******************************************************************************/
994 void bta_av_disconnect_req(tBTA_AV_SCB* p_scb,
995 UNUSED_ATTR tBTA_AV_DATA* p_data) {
998 APPL_TRACE_API("%s: conn_lcb: 0x%x peer_addr: %s", __func__,
999 bta_av_cb.conn_lcb, p_scb->PeerAddress().ToString().c_str());
1001 alarm_cancel(p_scb->link_signalling_timer);
1002 alarm_cancel(p_scb->accept_signalling_timer);
1003 alarm_cancel(p_scb->avrc_ct_timer);
1005 // conn_lcb is the index bitmask of all used LCBs, and since LCB and SCB use
1006 // the same index, it should be safe to use SCB index here.
1007 if ((bta_av_cb.conn_lcb & (1 << p_scb->hdi)) != 0) {
1008 p_rcb = bta_av_get_rcb_by_shdl((uint8_t)(p_scb->hdi + 1));
1009 if (p_rcb) bta_av_del_rc(p_rcb);
1010 AVDT_DisconnectReq(p_scb->PeerAddress(), &bta_av_proc_stream_evt);
1012 APPL_TRACE_WARNING("%s: conn_lcb=0x%x bta_handle=0x%x (hdi=%u) no link",
1013 __func__, bta_av_cb.conn_lcb, p_scb->hndl, p_scb->hdi);
1014 bta_av_ssm_execute(p_scb, BTA_AV_AVDT_DISCONNECT_EVT, NULL);
1018 /*******************************************************************************
1020 * Function bta_av_security_req
1022 * Description Send an AVDTP security request.
1026 ******************************************************************************/
1027 void bta_av_security_req(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1028 if (bta_av_cb.features & BTA_AV_FEAT_PROTECT) {
1029 AVDT_SecurityReq(p_scb->avdt_handle, p_data->api_protect_req.p_data,
1030 p_data->api_protect_req.len);
1034 /*******************************************************************************
1036 * Function bta_av_security_rsp
1038 * Description Send an AVDTP security response.
1042 ******************************************************************************/
1043 void bta_av_security_rsp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1044 if (bta_av_cb.features & BTA_AV_FEAT_PROTECT) {
1045 AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label,
1046 p_data->api_protect_rsp.error_code,
1047 p_data->api_protect_rsp.p_data,
1048 p_data->api_protect_rsp.len);
1050 AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC, NULL,
1055 /*******************************************************************************
1057 * Function bta_av_setconfig_rsp
1059 * Description setconfig is OK
1063 ******************************************************************************/
1064 void bta_av_setconfig_rsp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1065 uint8_t num = p_data->ci_setconfig.num_seid + 1;
1066 uint8_t avdt_handle = p_data->ci_setconfig.avdt_handle;
1067 uint8_t* p_seid = p_data->ci_setconfig.p_seid;
1071 /* we like this codec_type. find the sep_idx */
1072 local_sep = bta_av_get_scb_sep_type(p_scb, avdt_handle);
1073 bta_av_adjust_seps_idx(p_scb, avdt_handle);
1075 "%s: peer %s bta_handle=0x%x avdt_handle=%d sep_idx=%d cur_psc_mask:0x%x",
1076 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
1077 p_scb->avdt_handle, p_scb->sep_idx, p_scb->cur_psc_mask);
1079 if ((AVDT_TSEP_SNK == local_sep) &&
1080 (p_data->ci_setconfig.err_code == AVDT_SUCCESS) &&
1081 (p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback != NULL)) {
1082 tBTA_AV_MEDIA av_sink_codec_info;
1083 av_sink_codec_info.avk_config.bd_addr = p_scb->PeerAddress();
1084 av_sink_codec_info.avk_config.codec_info = p_scb->cfg.codec_info;
1085 p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback(
1086 p_scb->PeerAddress(), BTA_AV_SINK_MEDIA_CFG_EVT, &av_sink_codec_info);
1089 AVDT_ConfigRsp(p_scb->avdt_handle, p_scb->avdt_label,
1090 p_data->ci_setconfig.err_code, p_data->ci_setconfig.category);
1092 alarm_cancel(p_scb->link_signalling_timer);
1094 if (p_data->ci_setconfig.err_code == AVDT_SUCCESS) {
1095 p_scb->wait = BTA_AV_WAIT_ACP_CAPS_ON;
1096 if (p_data->ci_setconfig.recfg_needed)
1097 p_scb->role |= BTA_AV_ROLE_SUSPEND_OPT;
1098 APPL_TRACE_DEBUG("%s: recfg_needed:%d role:0x%x num:%d", __func__,
1099 p_data->ci_setconfig.recfg_needed, p_scb->role, num);
1100 /* callout module tells BTA the number of "good" SEPs and their SEIDs.
1101 * getcap on these SEID */
1102 p_scb->num_seps = num;
1104 if (p_scb->cur_psc_mask & AVDT_PSC_DELAY_RPT)
1105 p_scb->SetAvdtpVersion(AVDT_VERSION_1_3);
1107 if (A2DP_GetCodecType(p_scb->cfg.codec_info) == A2DP_MEDIA_CT_SBC ||
1109 /* if SBC is used by the SNK as INT, discover req is not sent in
1110 * bta_av_config_ind.
1111 * call disc_res now */
1112 /* this is called in A2DP SRC path only, In case of SINK we don't need it
1114 if (local_sep == AVDT_TSEP_SRC)
1115 p_scb->p_cos->disc_res(p_scb->hndl, p_scb->PeerAddress(), num, num, 0,
1116 UUID_SERVCLASS_AUDIO_SOURCE);
1118 /* we do not know the peer device and it is using non-SBC codec
1119 * we need to know all the SEPs on SNK */
1120 if (p_scb->uuid_int == 0) p_scb->uuid_int = p_scb->open_api.uuid;
1121 bta_av_discover_req(p_scb, NULL);
1125 for (i = 1; i < num; i++) {
1126 APPL_TRACE_DEBUG("%s: sep_info[%d] SEID: %d", __func__, i, p_seid[i - 1]);
1127 /* initialize the sep_info[] to get capabilities */
1128 p_scb->sep_info[i].in_use = false;
1129 p_scb->sep_info[i].tsep = AVDT_TSEP_SNK;
1130 p_scb->sep_info[i].media_type = p_scb->media_type;
1131 p_scb->sep_info[i].seid = p_seid[i - 1];
1134 /* only in case of local sep as SRC we need to look for other SEPs, In case
1135 * of SINK we don't */
1136 if (local_sep == AVDT_TSEP_SRC) {
1137 /* Make sure UUID has been initialized... */
1138 if (p_scb->uuid_int == 0) p_scb->uuid_int = p_scb->open_api.uuid;
1139 bta_av_next_getcap(p_scb, p_data);
1144 /*******************************************************************************
1146 * Function bta_av_str_opened
1148 * Description Stream opened OK (incoming/outgoing).
1152 ******************************************************************************/
1153 void bta_av_str_opened(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1154 tBTA_AV_CONN_CHG msg;
1155 char remote_name[BTM_MAX_REM_BD_NAME_LEN] = "";
1158 APPL_TRACE_DEBUG("%s: peer %s bta_handle: 0x%x", __func__,
1159 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl);
1161 msg.hdr.layer_specific = p_scb->hndl;
1163 msg.peer_addr = p_scb->PeerAddress();
1164 p_scb->l2c_cid = AVDT_GetL2CapChannel(p_scb->avdt_handle);
1165 bta_av_conn_chg((tBTA_AV_DATA*)&msg);
1166 /* set the congestion flag, so AV would not send media packets by accident */
1168 // Don't use AVDTP SUSPEND for restrict listed devices
1169 btif_storage_get_stored_remote_name(p_scb->PeerAddress(), remote_name);
1170 if (interop_match_name(INTEROP_DISABLE_AVDTP_SUSPEND, remote_name) ||
1171 interop_match_addr(INTEROP_DISABLE_AVDTP_SUSPEND,
1172 &p_scb->PeerAddress())) {
1173 LOG_INFO("%s: disable AVDTP SUSPEND: interop matched name %s address %s",
1174 __func__, remote_name, p_scb->PeerAddress().ToString().c_str());
1175 p_scb->suspend_sup = false;
1179 p_data->str_msg.msg.open_ind.peer_mtu - AVDT_MEDIA_HDR_SIZE;
1180 APPL_TRACE_DEBUG("%s: l2c_cid: 0x%x stream_mtu: %d", __func__, p_scb->l2c_cid,
1183 /* Set the media channel as high priority */
1184 L2CA_SetTxPriority(p_scb->l2c_cid, L2CAP_CHNL_PRIORITY_HIGH);
1185 L2CA_SetChnlFlushability(p_scb->l2c_cid, true);
1187 bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->PeerAddress());
1188 memset(&p_scb->q_info, 0, sizeof(tBTA_AV_Q_INFO));
1190 p_scb->l2c_bufs = 0;
1191 p_scb->p_cos->open(p_scb->hndl, p_scb->PeerAddress(), p_scb->stream_mtu);
1194 /* TODO check if other audio channel is open.
1195 * If yes, check if reconfig is needed
1196 * Rigt now we do not do this kind of checking.
1197 * BTA-AV is INT for 2nd audio connection.
1198 * The application needs to make sure the current codec_info is proper.
1199 * If one audio connection is open and another SNK attempts to connect to
1201 * the connection will be rejected.
1203 /* check if other audio channel is started. If yes, start */
1205 open.bd_addr = p_scb->PeerAddress();
1206 open.chnl = p_scb->chnl;
1207 open.hndl = p_scb->hndl;
1208 open.status = BTA_AV_SUCCESS;
1210 p = BTM_ReadRemoteFeatures(p_scb->PeerAddress());
1212 if (HCI_EDR_ACL_2MPS_SUPPORTED(p)) open.edr |= BTA_AV_EDR_2MBPS;
1213 if (HCI_EDR_ACL_3MPS_SUPPORTED(p)) {
1214 if (!interop_match_addr(INTEROP_2MBPS_LINK_ONLY,
1215 &p_scb->PeerAddress())) {
1216 open.edr |= BTA_AV_EDR_3MBPS;
1220 bta_ar_avdt_conn(BTA_ID_AV, open.bd_addr, p_scb->hdi);
1221 if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC) {
1222 open.starting = false;
1223 open.sep = AVDT_TSEP_SNK;
1224 } else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK) {
1225 open.starting = bta_av_chk_start(p_scb);
1226 open.sep = AVDT_TSEP_SRC;
1229 tBTA_AV bta_av_data;
1230 bta_av_data.open = open;
1231 (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, &bta_av_data);
1232 if (open.starting) {
1233 bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
1237 // This code is used to pass PTS TC for AVDTP ABORT
1238 char value[PROPERTY_VALUE_MAX] = {0};
1239 if ((osi_property_get("bluetooth.pts.force_a2dp_abort", value, "false")) &&
1240 (!strcmp(value, "true"))) {
1241 APPL_TRACE_ERROR("%s: Calling AVDT_AbortReq", __func__);
1242 AVDT_AbortReq(p_scb->avdt_handle);
1246 /*******************************************************************************
1248 * Function bta_av_security_ind
1250 * Description Handle an AVDTP security indication.
1254 ******************************************************************************/
1255 void bta_av_security_ind(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1256 p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
1258 if (bta_av_cb.features & BTA_AV_FEAT_PROTECT) {
1259 tBTA_AV_PROTECT_REQ protect_req;
1260 protect_req.chnl = p_scb->chnl;
1261 protect_req.hndl = p_scb->hndl;
1262 protect_req.p_data = p_data->str_msg.msg.security_ind.p_data;
1263 protect_req.len = p_data->str_msg.msg.security_ind.len;
1265 tBTA_AV bta_av_data;
1266 bta_av_data.protect_req = protect_req;
1267 (*bta_av_cb.p_cback)(BTA_AV_PROTECT_REQ_EVT, &bta_av_data);
1269 /* app doesn't support security indication; respond with failure */
1271 AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC, NULL,
1276 /*******************************************************************************
1278 * Function bta_av_security_cfm
1280 * Description Handle an AVDTP security confirm.
1284 ******************************************************************************/
1285 void bta_av_security_cfm(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1286 if (bta_av_cb.features & BTA_AV_FEAT_PROTECT) {
1287 tBTA_AV_PROTECT_RSP protect_rsp;
1288 protect_rsp.chnl = p_scb->chnl;
1289 protect_rsp.hndl = p_scb->hndl;
1290 protect_rsp.p_data = p_data->str_msg.msg.security_cfm.p_data;
1291 protect_rsp.len = p_data->str_msg.msg.security_cfm.len;
1292 protect_rsp.err_code = p_data->str_msg.msg.hdr.err_code;
1294 tBTA_AV bta_av_data;
1295 bta_av_data.protect_rsp = protect_rsp;
1296 (*bta_av_cb.p_cback)(BTA_AV_PROTECT_RSP_EVT, &bta_av_data);
1300 /*******************************************************************************
1302 * Function bta_av_do_close
1304 * Description Close stream.
1308 ******************************************************************************/
1309 void bta_av_do_close(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1310 APPL_TRACE_DEBUG("%s: p_scb->co_started=%d", __func__, p_scb->co_started);
1312 /* stop stream if started */
1313 if (p_scb->co_started) {
1314 bta_av_str_stopped(p_scb, NULL);
1316 alarm_cancel(p_scb->link_signalling_timer);
1319 p_scb->started = false;
1320 p_scb->use_rtp_header_marker_bit = false;
1322 /* drop the buffers queued in L2CAP */
1323 L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
1325 AVDT_CloseReq(p_scb->avdt_handle);
1326 /* just in case that the link is congested, link is flow controled by peer or
1327 * for whatever reason the the close request can not be sent in time.
1328 * when this timer expires, AVDT_DisconnectReq will be called to disconnect
1331 bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_CLOSE_REQ_TIME_VAL,
1332 BTA_AV_API_CLOSE_EVT, p_scb->hndl);
1335 /*******************************************************************************
1337 * Function bta_av_connect_req
1339 * Description Connect AVDTP connection.
1343 ******************************************************************************/
1344 void bta_av_connect_req(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1345 APPL_TRACE_DEBUG("%s: peer %s coll_mask=0x%02x", __func__,
1346 p_scb->PeerAddress().ToString().c_str(), p_scb->coll_mask);
1347 p_scb->sdp_discovery_started = false;
1348 if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR) {
1349 /* SNK initiated L2C connection while SRC was doing SDP. */
1350 /* Wait until timeout to check if SNK starts signalling. */
1351 APPL_TRACE_WARNING("%s: coll_mask=0x%02x incoming timer is up", __func__,
1353 p_scb->coll_mask |= BTA_AV_COLL_API_CALLED;
1354 APPL_TRACE_EVENT("%s: updated coll_mask=0x%02x", __func__,
1359 AVDT_ConnectReq(p_scb->PeerAddress(), p_scb->hdi, &bta_av_proc_stream_evt);
1362 /*******************************************************************************
1364 * Function bta_av_sdp_failed
1366 * Description Service discovery failed.
1370 ******************************************************************************/
1371 void bta_av_sdp_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1372 APPL_TRACE_ERROR("%s: peer_addr=%s open_status=%d", __func__,
1373 p_scb->PeerAddress().ToString().c_str(), p_scb->open_status);
1375 if (p_scb->open_status == BTA_AV_SUCCESS) {
1376 p_scb->open_status = BTA_AV_FAIL_SDP;
1379 p_scb->sdp_discovery_started = false;
1380 bta_av_str_closed(p_scb, p_data);
1383 /*******************************************************************************
1385 * Function bta_av_disc_results
1387 * Description Handle the AVDTP discover results. Search through the
1388 * results and find the first available stream, and get
1393 ******************************************************************************/
1394 void bta_av_disc_results(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1395 uint8_t num_snks = 0, num_srcs = 0, i;
1396 /* our uuid in case we initiate connection */
1397 uint16_t uuid_int = p_scb->uuid_int;
1399 APPL_TRACE_DEBUG("%s: peer %s bta_handle: 0x%x initiator UUID 0x%x", __func__,
1400 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
1403 /* store number of stream endpoints returned */
1404 p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
1406 for (i = 0; i < p_scb->num_seps; i++) {
1407 /* steam not in use, is a sink, and is audio */
1408 if ((!p_scb->sep_info[i].in_use) &&
1409 (p_scb->sep_info[i].media_type == p_scb->media_type)) {
1410 if ((p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
1411 (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE))
1414 if ((p_scb->sep_info[i].tsep == AVDT_TSEP_SRC) &&
1415 (uuid_int == UUID_SERVCLASS_AUDIO_SINK))
1420 p_scb->p_cos->disc_res(p_scb->hndl, p_scb->PeerAddress(), p_scb->num_seps,
1421 num_snks, num_srcs, uuid_int);
1422 p_scb->num_disc_snks = num_snks;
1423 p_scb->num_disc_srcs = num_srcs;
1426 if (p_scb->num_seps > 0) {
1427 /* initialize index into discovery results */
1428 p_scb->sep_info_idx = 0;
1430 /* get the capabilities of the first available stream */
1431 bta_av_next_getcap(p_scb, p_data);
1433 /* else we got discover response but with no streams; we're done */
1435 APPL_TRACE_ERROR("%s: BTA_AV_STR_DISC_FAIL_EVT: peer_addr=%s", __func__,
1436 p_scb->PeerAddress().ToString().c_str());
1437 bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
1441 /*******************************************************************************
1443 * Function bta_av_disc_res_as_acp
1445 * Description Handle the AVDTP discover results. Search through the
1446 * results and find the first available stream, and get
1451 ******************************************************************************/
1452 void bta_av_disc_res_as_acp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1453 uint8_t num_snks = 0, i;
1455 APPL_TRACE_DEBUG("%s: peer %s bta_handle: 0x%x", __func__,
1456 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl);
1458 /* store number of stream endpoints returned */
1459 p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
1461 for (i = 0; i < p_scb->num_seps; i++) {
1462 /* steam is a sink, and is audio */
1463 if ((p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
1464 (p_scb->sep_info[i].media_type == p_scb->media_type)) {
1465 p_scb->sep_info[i].in_use = false;
1469 p_scb->p_cos->disc_res(p_scb->hndl, p_scb->PeerAddress(), p_scb->num_seps,
1470 num_snks, 0, UUID_SERVCLASS_AUDIO_SOURCE);
1471 p_scb->num_disc_snks = num_snks;
1472 p_scb->num_disc_srcs = 0;
1475 if (p_scb->num_seps > 0) {
1476 /* initialize index into discovery results */
1477 p_scb->sep_info_idx = 0;
1479 /* get the capabilities of the first available stream */
1480 bta_av_next_getcap(p_scb, p_data);
1482 /* else we got discover response but with no streams; we're done */
1484 APPL_TRACE_ERROR("%s: BTA_AV_STR_DISC_FAIL_EVT: peer_addr=%s", __func__,
1485 p_scb->PeerAddress().ToString().c_str());
1486 bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
1490 /*******************************************************************************
1492 * Function bta_av_save_caps
1494 * Description report the SNK SEP capabilities to application
1498 ******************************************************************************/
1499 void bta_av_save_caps(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1501 tAVDT_SEP_INFO* p_info = &p_scb->sep_info[p_scb->sep_info_idx];
1502 uint8_t old_wait = p_scb->wait;
1503 bool getcap_done = false;
1506 "%s: peer %s bta_handle:0x%x num_seps:%d sep_info_idx:%d wait:0x%x",
1507 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
1508 p_scb->num_seps, p_scb->sep_info_idx, p_scb->wait);
1509 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
1510 A2DP_CodecInfoString(p_scb->peer_cap.codec_info).c_str());
1512 cfg = p_scb->peer_cap;
1513 /* let application know the capability of the SNK */
1514 if (p_scb->p_cos->getcfg(p_scb->hndl, p_scb->PeerAddress(), cfg.codec_info,
1515 &p_scb->sep_info_idx, p_info->seid, &cfg.num_protect,
1516 cfg.protect_info) != A2DP_SUCCESS) {
1517 p_scb->sep_info_idx++;
1518 APPL_TRACE_DEBUG("%s: result: next sep_info_idx:%d", __func__,
1519 p_scb->sep_info_idx);
1521 // All capabilities found
1523 APPL_TRACE_DEBUG("%s: result: done sep_info_idx:%d", __func__,
1524 p_scb->sep_info_idx);
1526 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
1527 A2DP_CodecInfoString(cfg.codec_info).c_str());
1529 if (p_scb->num_seps > p_scb->sep_info_idx && !getcap_done) {
1530 /* Some devices have seps at the end of the discover list, which is not */
1531 /* matching media type(video not audio). */
1532 /* In this case, we are done with getcap without sending another */
1533 /* request to AVDT. */
1534 if (!bta_av_next_getcap(p_scb, p_data)) getcap_done = true;
1540 APPL_TRACE_DEBUG("%s: getcap_done: num_seps:%d sep_info_idx:%d wait:0x%x",
1541 __func__, p_scb->num_seps, p_scb->sep_info_idx,
1543 p_scb->wait &= ~(BTA_AV_WAIT_ACP_CAPS_ON | BTA_AV_WAIT_ACP_CAPS_STARTED);
1544 if (old_wait & BTA_AV_WAIT_ACP_CAPS_STARTED) {
1545 bta_av_start_ok(p_scb, NULL);
1550 /*******************************************************************************
1552 * Function bta_av_set_use_rc
1554 * Description set to use AVRC for this stream control block.
1558 ******************************************************************************/
1559 void bta_av_set_use_rc(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1560 p_scb->use_rc = true;
1563 /*******************************************************************************
1565 * Function bta_av_cco_close
1567 * Description call close call-out function.
1571 ******************************************************************************/
1572 void bta_av_cco_close(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1573 APPL_TRACE_DEBUG("%s: peer %s bta_handle:0x%x", __func__,
1574 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl);
1575 p_scb->p_cos->close(p_scb->hndl, p_scb->PeerAddress());
1578 /*******************************************************************************
1580 * Function bta_av_open_failed
1582 * Description Failed to open an AVDT stream
1586 ******************************************************************************/
1587 void bta_av_open_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1588 bool is_av_opened = false;
1589 tBTA_AV_SCB* p_opened_scb = NULL;
1592 APPL_TRACE_ERROR("%s: peer_addr=%s", __func__,
1593 p_scb->PeerAddress().ToString().c_str());
1594 p_scb->open_status = BTA_AV_FAIL_STREAM;
1595 bta_av_cco_close(p_scb, p_data);
1597 /* check whether there is already an opened audio or video connection with the
1599 for (idx = 0; (idx < BTA_AV_NUM_STRS) && (!is_av_opened); idx++) {
1600 p_opened_scb = bta_av_cb.p_scb[idx];
1601 if (p_opened_scb && (p_opened_scb->state == BTA_AV_OPEN_SST) &&
1602 (p_opened_scb->PeerAddress() == p_scb->PeerAddress()))
1603 is_av_opened = true;
1606 /* if there is already an active AV connnection with the same bd_addr,
1607 don't send disconnect req, just report the open event with
1608 BTA_AV_FAIL_GET_CAP status */
1611 open.bd_addr = p_scb->PeerAddress();
1612 open.chnl = p_scb->chnl;
1613 open.hndl = p_scb->hndl;
1614 open.status = BTA_AV_FAIL_GET_CAP;
1615 open.starting = bta_av_chk_start(p_scb);
1617 /* set the state back to initial state */
1618 bta_av_set_scb_sst_init(p_scb);
1620 if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC) {
1621 open.sep = AVDT_TSEP_SNK;
1622 } else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK) {
1623 open.sep = AVDT_TSEP_SRC;
1627 "%s: there is already an active connection: peer_addr=%s chnl=%d "
1628 "hndl=0x%x status=%d starting=%d edr=%d",
1629 __func__, open.bd_addr.ToString().c_str(), open.chnl, open.hndl,
1630 open.status, open.starting, open.edr);
1632 tBTA_AV bta_av_data;
1633 bta_av_data.open = open;
1634 (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, &bta_av_data);
1636 AVDT_DisconnectReq(p_scb->PeerAddress(), &bta_av_proc_stream_evt);
1640 /*******************************************************************************
1642 * Function bta_av_getcap_results
1644 * Description Handle the AVDTP get capabilities results. Check the codec
1645 * type and see if it matches ours. If it does not, get the
1646 * capabilities of the next stream, if any.
1650 ******************************************************************************/
1651 void bta_av_getcap_results(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1652 AvdtpSepConfig cfg = p_scb->cfg;
1653 uint8_t media_type = A2DP_GetMediaType(p_scb->peer_cap.codec_info);
1654 tAVDT_SEP_INFO* p_info = &p_scb->sep_info[p_scb->sep_info_idx];
1657 cfg.num_protect = p_scb->peer_cap.num_protect;
1658 memcpy(cfg.codec_info, p_scb->peer_cap.codec_info, AVDT_CODEC_SIZE);
1659 memcpy(cfg.protect_info, p_scb->peer_cap.protect_info, AVDT_PROTECT_SIZE);
1661 APPL_TRACE_DEBUG("%s: peer %s bta_handle:0x%x num_codec:%d psc_mask=0x%x",
1662 __func__, p_scb->PeerAddress().ToString().c_str(),
1663 p_scb->hndl, p_scb->peer_cap.num_codec, p_scb->cfg.psc_mask);
1664 APPL_TRACE_DEBUG("%s: media type 0x%x, 0x%x", __func__, media_type,
1666 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
1667 A2DP_CodecInfoString(p_scb->cfg.codec_info).c_str());
1669 /* if codec present and we get a codec configuration */
1670 if ((p_scb->peer_cap.num_codec != 0) && (media_type == p_scb->media_type) &&
1671 (p_scb->p_cos->getcfg(p_scb->hndl, p_scb->PeerAddress(), cfg.codec_info,
1672 &p_scb->sep_info_idx, p_info->seid,
1674 cfg.protect_info) == A2DP_SUCCESS)) {
1675 /* UUID for which connection was initiatied */
1676 uint16_t uuid_int = p_scb->uuid_int;
1678 /* save copy of codec configuration */
1681 APPL_TRACE_DEBUG("%s: result: sep_info_idx=%d", __func__,
1682 p_scb->sep_info_idx);
1683 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
1684 A2DP_CodecInfoString(p_scb->cfg.codec_info).c_str());
1686 APPL_TRACE_DEBUG("%s: initiator UUID = 0x%x", __func__, uuid_int);
1687 if (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE)
1688 bta_av_adjust_seps_idx(p_scb,
1689 bta_av_get_scb_handle(p_scb, AVDT_TSEP_SRC));
1690 else if (uuid_int == UUID_SERVCLASS_AUDIO_SINK)
1691 bta_av_adjust_seps_idx(p_scb,
1692 bta_av_get_scb_handle(p_scb, AVDT_TSEP_SNK));
1693 LOG_INFO("%s: sep_idx=%d avdt_handle=%d bta_handle=0x%x", __func__,
1694 p_scb->sep_idx, p_scb->avdt_handle, p_scb->hndl);
1696 /* use only the services peer supports */
1697 cfg.psc_mask &= p_scb->peer_cap.psc_mask;
1698 p_scb->cur_psc_mask = cfg.psc_mask;
1700 "%s: peer %s bta_handle:0x%x sep_idx:%d sep_info_idx:%d "
1701 "cur_psc_mask:0x%x",
1702 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
1703 p_scb->sep_idx, p_scb->sep_info_idx, p_scb->cur_psc_mask);
1705 if ((uuid_int == UUID_SERVCLASS_AUDIO_SINK) &&
1706 (p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback != NULL)) {
1707 APPL_TRACE_DEBUG("%s: configure decoder for Sink connection", __func__);
1708 tBTA_AV_MEDIA av_sink_codec_info;
1709 av_sink_codec_info.avk_config.bd_addr = p_scb->PeerAddress();
1710 av_sink_codec_info.avk_config.codec_info = p_scb->cfg.codec_info;
1711 p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback(
1712 p_scb->PeerAddress(), BTA_AV_SINK_MEDIA_CFG_EVT, &av_sink_codec_info);
1715 if (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE) {
1716 A2DP_AdjustCodec(cfg.codec_info);
1719 /* open the stream */
1720 AVDT_OpenReq(p_scb->seps[p_scb->sep_idx].av_handle, p_scb->PeerAddress(),
1721 p_scb->hdi, p_scb->sep_info[p_scb->sep_info_idx].seid, &cfg);
1723 /* try the next stream, if any */
1724 p_scb->sep_info_idx++;
1725 bta_av_next_getcap(p_scb, p_data);
1729 /*******************************************************************************
1731 * Function bta_av_setconfig_rej
1733 * Description Send AVDTP set config reject.
1737 ******************************************************************************/
1738 void bta_av_setconfig_rej(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1739 tBTA_AV_REJECT reject;
1740 uint8_t avdt_handle = p_data->ci_setconfig.avdt_handle;
1742 bta_av_adjust_seps_idx(p_scb, avdt_handle);
1743 LOG_INFO("%s: sep_idx=%d avdt_handle=%d bta_handle=0x%x", __func__,
1744 p_scb->sep_idx, p_scb->avdt_handle, p_scb->hndl);
1745 AVDT_ConfigRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_UNSUP_CFG, 0);
1747 reject.bd_addr = p_data->str_msg.bd_addr;
1748 reject.hndl = p_scb->hndl;
1750 tBTA_AV bta_av_data;
1751 bta_av_data.reject = reject;
1752 (*bta_av_cb.p_cback)(BTA_AV_REJECT_EVT, &bta_av_data);
1755 /*******************************************************************************
1757 * Function bta_av_discover_req
1759 * Description Send an AVDTP discover request to the peer.
1763 ******************************************************************************/
1764 void bta_av_discover_req(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1765 /* send avdtp discover request */
1767 AVDT_DiscoverReq(p_scb->PeerAddress(), p_scb->hdi, p_scb->sep_info,
1768 BTA_AV_NUM_SEPS, &bta_av_proc_stream_evt);
1771 /*******************************************************************************
1773 * Function bta_av_conn_failed
1775 * Description AVDTP connection failed.
1779 ******************************************************************************/
1780 void bta_av_conn_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1781 APPL_TRACE_ERROR("%s: peer_addr=%s open_status=%d", __func__,
1782 p_scb->PeerAddress().ToString().c_str(), p_scb->open_status);
1784 p_scb->open_status = BTA_AV_FAIL_STREAM;
1785 bta_av_str_closed(p_scb, p_data);
1788 /*******************************************************************************
1790 * Function bta_av_do_start
1792 * Description Start stream.
1796 ******************************************************************************/
1797 void bta_av_do_start(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1799 "A2dp stream start peer:%s sco_occupied:%s av_role:0x%x started:%s "
1801 PRIVATE_ADDRESS(p_scb->PeerAddress()),
1802 logbool(bta_av_cb.sco_occupied).c_str(), p_scb->role,
1803 logbool(p_scb->started).c_str(), p_scb->wait);
1804 if (bta_av_cb.sco_occupied) {
1805 LOG_WARN("A2dp stream start failed");
1806 bta_av_start_failed(p_scb, p_data);
1810 if (p_scb->started) {
1811 p_scb->role |= BTA_AV_ROLE_START_INT;
1812 if (p_scb->wait != 0) {
1814 "%s: peer %s start stream request ignored: "
1815 "already waiting: sco_occupied:%s role:0x%x started:%s wait:0x%x",
1816 __func__, p_scb->PeerAddress().ToString().c_str(),
1817 logbool(bta_av_cb.sco_occupied).c_str(), p_scb->role,
1818 logbool(p_scb->started).c_str(), p_scb->wait);
1821 if (p_scb->role & BTA_AV_ROLE_SUSPEND) {
1822 notify_start_failed(p_scb);
1824 bta_av_start_ok(p_scb, NULL);
1829 if ((p_scb->role & BTA_AV_ROLE_START_INT) != 0) {
1831 "%s: peer %s start stream request ignored: "
1832 "already initiated: sco_occupied:%s role:0x%x started:%s wait:0x%x",
1833 __func__, p_scb->PeerAddress().ToString().c_str(),
1834 logbool(bta_av_cb.sco_occupied).c_str(), p_scb->role,
1835 logbool(p_scb->started).c_str(), p_scb->wait);
1839 p_scb->role |= BTA_AV_ROLE_START_INT;
1840 bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->PeerAddress());
1841 /* disallow role switch during streaming, only if we are the central role
1842 * i.e. allow role switch, if we are peripheral.
1843 * It would not hurt us, if the peer device wants us to be central
1844 * disable sniff mode unconditionally during streaming */
1846 if ((BTM_GetRole(p_scb->PeerAddress(), &cur_role) == BTM_SUCCESS) &&
1847 (cur_role == HCI_ROLE_CENTRAL)) {
1848 BTM_block_role_switch_and_sniff_mode_for(p_scb->PeerAddress());
1850 BTM_block_sniff_mode_for(p_scb->PeerAddress());
1853 uint16_t result = AVDT_StartReq(&p_scb->avdt_handle, 1);
1854 if (result != AVDT_SUCCESS) {
1855 LOG_ERROR("%s: AVDT_StartReq failed for peer %s result:%d", __func__,
1856 p_scb->PeerAddress().ToString().c_str(), result);
1857 bta_av_start_failed(p_scb, p_data);
1860 "%s: peer %s start requested: sco_occupied:%s role:0x%x "
1861 "started:%s wait:0x%x",
1862 __func__, p_scb->PeerAddress().ToString().c_str(),
1863 logbool(bta_av_cb.sco_occupied).c_str(), p_scb->role,
1864 logbool(p_scb->started).c_str(), p_scb->wait);
1867 /*******************************************************************************
1869 * Function bta_av_str_stopped
1871 * Description Stream stopped.
1875 ******************************************************************************/
1876 void bta_av_str_stopped(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1877 tBTA_AV_SUSPEND suspend_rsp;
1878 uint8_t start = p_scb->started;
1879 bool sus_evt = true;
1883 "%s: peer %s bta_handle:0x%x audio_open_cnt:%d, p_data %p start:%d",
1884 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
1885 bta_av_cb.audio_open_cnt, p_data, start);
1887 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->PeerAddress());
1888 BTM_unblock_role_switch_and_sniff_mode_for(p_scb->PeerAddress());
1890 if (p_scb->co_started) {
1891 if (bta_av_cb.offload_started_hndl == p_scb->hndl) {
1892 bta_av_vendor_offload_stop();
1893 bta_av_cb.offload_started_hndl = 0;
1894 } else if (bta_av_cb.offload_start_pending_hndl == p_scb->hndl) {
1895 APPL_TRACE_WARNING("%s: Stop pending offload start command", __func__);
1896 bta_av_vendor_offload_stop();
1897 bta_av_cb.offload_start_pending_hndl = 0;
1900 bta_av_stream_chg(p_scb, false);
1901 p_scb->co_started = false;
1903 p_scb->p_cos->stop(p_scb->hndl, p_scb->PeerAddress());
1906 /* if q_info.a2dp_list is not empty, drop it now */
1907 if (BTA_AV_CHNL_AUDIO == p_scb->chnl) {
1908 while (!list_is_empty(p_scb->a2dp_list)) {
1909 p_buf = (BT_HDR*)list_front(p_scb->a2dp_list);
1910 list_remove(p_scb->a2dp_list, p_buf);
1914 /* drop the audio buffers queued in L2CAP */
1915 if (p_data && p_data->api_stop.flush)
1916 L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
1919 suspend_rsp.chnl = p_scb->chnl;
1920 suspend_rsp.hndl = p_scb->hndl;
1922 if (p_data && p_data->api_stop.suspend) {
1923 APPL_TRACE_DEBUG("%s: peer %s suspending: %d, sup:%d", __func__,
1924 p_scb->PeerAddress().ToString().c_str(), start,
1925 p_scb->suspend_sup);
1926 if ((start) && (p_scb->suspend_sup)) {
1928 p_scb->l2c_bufs = 0;
1929 AVDT_SuspendReq(&p_scb->avdt_handle, 1);
1932 /* send SUSPEND_EVT event only if not in reconfiguring state and sus_evt is
1934 if ((sus_evt) && (p_scb->state != BTA_AV_RCFG_SST)) {
1935 suspend_rsp.status = BTA_AV_SUCCESS;
1936 suspend_rsp.initiator = true;
1937 tBTA_AV bta_av_data;
1938 bta_av_data.suspend = suspend_rsp;
1939 (*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, &bta_av_data);
1942 suspend_rsp.status = BTA_AV_SUCCESS;
1943 suspend_rsp.initiator = true;
1944 APPL_TRACE_EVENT("%s: status %d", __func__, suspend_rsp.status);
1946 // Send STOP_EVT event only if not in reconfiguring state.
1947 // However, we should send STOP_EVT if we are reconfiguring when taking
1948 // the Close->Configure->Open->Start path.
1949 if (p_scb->state != BTA_AV_RCFG_SST ||
1950 (p_data && p_data->api_stop.reconfig_stop)) {
1951 tBTA_AV bta_av_data;
1952 bta_av_data.suspend = suspend_rsp;
1953 (*bta_av_cb.p_cback)(BTA_AV_STOP_EVT, &bta_av_data);
1958 /*******************************************************************************
1960 * Function bta_av_reconfig
1962 * Description process the reconfigure request.
1963 * save the parameter in control block and
1964 * suspend, reconfigure or close the stream
1968 ******************************************************************************/
1969 void bta_av_reconfig(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1970 AvdtpSepConfig* p_cfg;
1971 tBTA_AV_API_STOP stop;
1972 tBTA_AV_API_RCFG* p_rcfg = &p_data->api_reconfig;
1974 APPL_TRACE_DEBUG("%s: r:%d, s:%d idx: %d (o:%d)", __func__, p_scb->recfg_sup,
1975 p_scb->suspend_sup, p_scb->rcfg_idx, p_scb->sep_info_idx);
1977 p_scb->num_recfg = 0;
1978 /* store the new configuration in control block */
1979 p_cfg = &p_scb->cfg;
1981 alarm_cancel(p_scb->avrc_ct_timer);
1983 LOG_DEBUG("p_scb->sep_info_idx=%d p_scb->rcfg_idx=%d p_rcfg->sep_info_idx=%d",
1984 p_scb->sep_info_idx, p_scb->rcfg_idx, p_rcfg->sep_info_idx);
1985 LOG_DEBUG("Peer capable codec: %s",
1986 A2DP_CodecInfoString(p_scb->peer_cap.codec_info).c_str());
1987 LOG_DEBUG("Current codec: %s",
1988 A2DP_CodecInfoString(p_scb->cfg.codec_info).c_str());
1989 LOG_DEBUG("Reconfig codec: %s",
1990 A2DP_CodecInfoString(p_rcfg->codec_info).c_str());
1993 kBtmLogTag, p_scb->PeerAddress(), "Codec reconfig",
1994 base::StringPrintf("%s => %s", A2DP_CodecName(p_scb->cfg.codec_info),
1995 A2DP_CodecName(p_rcfg->codec_info)));
1997 p_cfg->num_protect = p_rcfg->num_protect;
1998 memcpy(p_cfg->codec_info, p_rcfg->codec_info, AVDT_CODEC_SIZE);
1999 memcpy(p_cfg->protect_info, p_rcfg->p_protect_info, p_rcfg->num_protect);
2000 p_scb->rcfg_idx = p_rcfg->sep_info_idx;
2001 p_cfg->psc_mask = p_scb->cur_psc_mask;
2003 // If the requested SEP index is same as the current one, then we
2004 // can Suspend->Reconfigure->Start.
2005 // Otherwise, we have to Close->Configure->Open->Start or
2006 // Close->Configure->Open for streams that are / are not started.
2007 if ((p_scb->rcfg_idx == p_scb->sep_info_idx) && p_rcfg->suspend &&
2008 p_scb->recfg_sup && p_scb->suspend_sup) {
2009 if (p_scb->started) {
2010 // Suspend->Reconfigure->Start
2012 stop.suspend = true;
2013 stop.reconfig_stop = false;
2014 bta_av_str_stopped(p_scb, (tBTA_AV_DATA*)&stop);
2017 APPL_TRACE_DEBUG("%s: reconfig", __func__);
2018 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
2019 A2DP_CodecInfoString(p_scb->cfg.codec_info).c_str());
2020 AVDT_ReconfigReq(p_scb->avdt_handle, &p_scb->cfg);
2021 p_scb->cfg.psc_mask = p_scb->cur_psc_mask;
2024 // Close the stream first, and then Configure it
2025 APPL_TRACE_DEBUG("%s: Close/Open started: %d state: %d num_protect: %d",
2026 __func__, p_scb->started, p_scb->state,
2027 p_cfg->num_protect);
2028 if (p_scb->started) {
2029 // Close->Configure->Open->Start
2030 if ((p_scb->rcfg_idx != p_scb->sep_info_idx) && p_scb->recfg_sup) {
2031 // Make sure we trigger STOP_EVT when taking the longer road to
2032 // reconfiguration, otherwise we don't call Start.
2034 stop.suspend = false;
2035 stop.reconfig_stop = true;
2036 bta_av_str_stopped(p_scb, (tBTA_AV_DATA*)&stop);
2038 bta_av_str_stopped(p_scb, NULL);
2040 p_scb->started = false;
2042 // Close->Configure->Open
2043 bta_av_str_stopped(p_scb, NULL);
2045 // Drop the buffers queued in L2CAP
2046 L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
2047 AVDT_CloseReq(p_scb->avdt_handle);
2051 /*******************************************************************************
2053 * Function bta_av_data_path
2055 * Description Handle stream data path.
2059 ******************************************************************************/
2060 void bta_av_data_path(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2061 BT_HDR* p_buf = NULL;
2063 bool new_buf = false;
2064 uint8_t m_pt = 0x60;
2065 tAVDT_DATA_OPT_MASK opt;
2067 if (!p_scb->started) return;
2069 if (p_scb->cong) return;
2071 if (p_scb->use_rtp_header_marker_bit) {
2072 m_pt |= AVDT_MARKER_SET;
2075 // Always get the current number of bufs que'd up
2077 (uint8_t)L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_GET);
2079 if (!list_is_empty(p_scb->a2dp_list)) {
2080 p_buf = (BT_HDR*)list_front(p_scb->a2dp_list);
2081 list_remove(p_scb->a2dp_list, p_buf);
2082 /* use q_info.a2dp data, read the timestamp */
2083 timestamp = *(uint32_t*)(p_buf + 1);
2086 /* A2DP_list empty, call co_data, dup data to other channels */
2087 p_buf = p_scb->p_cos->data(p_scb->cfg.codec_info, ×tamp);
2090 /* use the offset area for the time stamp */
2091 *(uint32_t*)(p_buf + 1) = timestamp;
2093 /* dup the data to other channels */
2094 bta_av_dup_audio_buf(p_scb, p_buf);
2099 if (p_scb->l2c_bufs < (BTA_AV_QUEUE_DATA_CHK_NUM)) {
2100 /* There's a buffer, just queue it to L2CAP.
2101 * There's no need to increment it here, it is always read from
2102 * L2CAP (see above).
2105 /* opt is a bit mask, it could have several options set */
2106 opt = AVDT_DATA_OPT_NONE;
2107 if (p_scb->no_rtp_header) {
2108 opt |= AVDT_DATA_OPT_NO_RTP;
2112 // Fragment the payload if larger than the MTU.
2113 // NOTE: The fragmentation is RTP-compatibie.
2115 size_t extra_fragments_n = 0;
2116 if (p_buf->len > 0) {
2117 extra_fragments_n = (p_buf->len / p_scb->stream_mtu) +
2118 ((p_buf->len % p_scb->stream_mtu) ? 1 : 0) - 1;
2120 std::vector<BT_HDR*> extra_fragments;
2121 extra_fragments.reserve(extra_fragments_n);
2123 uint8_t* data_begin = (uint8_t*)(p_buf + 1) + p_buf->offset;
2124 uint8_t* data_end = (uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len;
2125 while (extra_fragments_n-- > 0) {
2126 data_begin += p_scb->stream_mtu;
2127 size_t fragment_len = data_end - data_begin;
2128 if (fragment_len > p_scb->stream_mtu) fragment_len = p_scb->stream_mtu;
2130 BT_HDR* p_buf2 = (BT_HDR*)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
2131 p_buf2->offset = p_buf->offset;
2133 p_buf2->layer_specific = 0;
2135 (uint8_t*)(p_buf2 + 1) + p_buf2->offset + p_buf2->len;
2136 memcpy(packet2, data_begin, fragment_len);
2137 p_buf2->len += fragment_len;
2138 extra_fragments.push_back(p_buf2);
2139 p_buf->len -= fragment_len;
2142 if (!extra_fragments.empty()) {
2143 // Reset the RTP Marker bit for all fragments except the last one
2144 m_pt &= ~AVDT_MARKER_SET;
2146 AVDT_WriteReqOpt(p_scb->avdt_handle, p_buf, timestamp, m_pt, opt);
2147 for (size_t i = 0; i < extra_fragments.size(); i++) {
2148 if (i + 1 == extra_fragments.size()) {
2149 // Set the RTP Marker bit for the last fragment
2150 m_pt |= AVDT_MARKER_SET;
2152 BT_HDR* p_buf2 = extra_fragments[i];
2153 AVDT_WriteReqOpt(p_scb->avdt_handle, p_buf2, timestamp, m_pt, opt);
2157 /* there's a buffer, but L2CAP does not seem to be moving data */
2159 /* just got this buffer from co_data,
2160 * put it in queue */
2161 list_append(p_scb->a2dp_list, p_buf);
2163 /* just dequeue it from the a2dp_list */
2164 if (list_length(p_scb->a2dp_list) < 3) {
2165 /* put it back to the queue */
2166 list_prepend(p_scb->a2dp_list, p_buf);
2168 /* too many buffers in a2dp_list, drop it. */
2169 bta_av_co_audio_drop(p_scb->hndl, p_scb->PeerAddress());
2177 /*******************************************************************************
2179 * Function bta_av_start_ok
2181 * Description Stream started.
2185 ******************************************************************************/
2186 void bta_av_start_ok(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2187 bool initiator = false;
2188 bool suspend = false;
2189 uint8_t new_role = p_scb->role;
2192 uint8_t local_tsep = p_scb->seps[p_scb->sep_idx].tsep;
2194 LOG_INFO("%s: peer %s bta_handle:0x%x wait:0x%x role:0x%x local_tsep:%d",
2195 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
2196 p_scb->wait, p_scb->role, local_tsep);
2198 p_scb->started = true;
2200 if (local_tsep == AVDT_TSEP_SRC) {
2201 // The RTP Header marker bit for the A2DP Source encoder
2202 A2dpCodecConfig* codec_config =
2203 bta_av_get_a2dp_peer_current_codec(p_scb->PeerAddress());
2204 CHECK(codec_config != nullptr);
2205 p_scb->use_rtp_header_marker_bit = codec_config->useRtpHeaderMarkerBit();
2208 if (p_scb->sco_suspend) {
2209 p_scb->sco_suspend = false;
2212 if (new_role & BTA_AV_ROLE_START_INT) initiator = true;
2214 /* for A2DP SINK we do not send get_caps */
2215 if ((p_scb->avdt_handle == p_scb->seps[p_scb->sep_idx].av_handle) &&
2216 (local_tsep == AVDT_TSEP_SNK)) {
2217 p_scb->wait &= ~(BTA_AV_WAIT_ACP_CAPS_ON);
2218 APPL_TRACE_DEBUG("%s: local SEP type is SNK new wait is 0x%x", __func__,
2221 if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_FAILED) {
2222 /* role switch has failed */
2224 "%s: peer %s role switch failed: bta_handle:0x%x wait:0x%x, role:0x%x",
2225 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
2226 p_scb->wait, p_scb->role);
2227 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_FAILED;
2228 p_data = (tBTA_AV_DATA*)&hdr;
2229 hdr.offset = BTA_AV_RS_FAIL;
2231 APPL_TRACE_DEBUG("%s: peer %s wait:0x%x use_rtp_header_marker_bit:%s",
2232 __func__, p_scb->PeerAddress().ToString().c_str(),
2234 (p_scb->use_rtp_header_marker_bit) ? "true" : "false");
2236 if (p_data && (p_data->hdr.offset != BTA_AV_RS_NONE)) {
2237 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
2238 if (p_data->hdr.offset == BTA_AV_RS_FAIL) {
2239 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->PeerAddress());
2240 tBTA_AV_START start;
2241 start.chnl = p_scb->chnl;
2242 start.status = BTA_AV_FAIL_ROLE;
2243 start.hndl = p_scb->hndl;
2244 start.initiator = initiator;
2245 tBTA_AV bta_av_data;
2246 bta_av_data.start = start;
2247 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, &bta_av_data);
2252 if (!bta_av_link_role_ok(p_scb, A2DP_SET_ONE_BIT))
2253 p_scb->q_tag = BTA_AV_Q_TAG_START;
2255 /* The wait flag may be set here while we are already central on the link */
2256 /* this could happen if a role switch complete event occurred during
2258 /* if we are now central on the link, there is no need to wait for the role
2260 /* complete anymore so we can clear the wait for role switch flag */
2261 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
2265 (BTA_AV_WAIT_ROLE_SW_RES_OPEN | BTA_AV_WAIT_ROLE_SW_RES_START)) {
2266 p_scb->wait |= BTA_AV_WAIT_ROLE_SW_STARTED;
2267 p_scb->q_tag = BTA_AV_Q_TAG_START;
2271 APPL_TRACE_ERROR("%s: peer %s wait:0x%x q_tag:%d not started", __func__,
2272 p_scb->PeerAddress().ToString().c_str(), p_scb->wait,
2274 /* Clear first bit of p_scb->wait and not to return from this point else
2275 * HAL layer gets blocked. And if there is delay in Get Capability response
2277 * first bit of p_scb->wait is cleared hence it ensures bt_av_start_ok is
2279 * again from bta_av_save_caps.
2281 p_scb->wait &= ~BTA_AV_WAIT_ACP_CAPS_ON;
2284 /* tell role manager to check M/S role */
2285 bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->PeerAddress());
2287 bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->PeerAddress());
2289 if (p_scb->media_type == AVDT_MEDIA_TYPE_AUDIO) {
2290 /* in normal logic, conns should be bta_av_cb.audio_count - 1,
2291 * However, bta_av_stream_chg is not called to increase
2292 * bta_av_cb.audio_count yet.
2293 * If the code were to be re-arranged for some reasons, this number may need
2296 p_scb->co_started = bta_av_cb.audio_open_cnt;
2299 /* clear the congestion flag */
2300 p_scb->cong = false;
2302 if (new_role & BTA_AV_ROLE_START_INT) {
2303 new_role &= ~BTA_AV_ROLE_START_INT;
2304 } else if ((new_role & BTA_AV_ROLE_AD_ACP) &&
2305 (new_role & BTA_AV_ROLE_SUSPEND_OPT)) {
2310 p_scb->q_tag = BTA_AV_Q_TAG_STREAM;
2311 bta_av_stream_chg(p_scb, true);
2315 /* If sink starts stream, disable sniff mode here */
2317 /* If souce is the central role, disable role switch during streaming.
2318 * Otherwise allow role switch, if source is peripheral.
2319 * Because it would not hurt source, if the peer device wants source to be
2321 * disable sniff mode unconditionally during streaming */
2322 if ((BTM_GetRole(p_scb->PeerAddress(), &cur_role) == BTM_SUCCESS) &&
2323 (cur_role == HCI_ROLE_CENTRAL)) {
2324 BTM_block_role_switch_and_sniff_mode_for(p_scb->PeerAddress());
2326 BTM_block_sniff_mode_for(p_scb->PeerAddress());
2330 p_scb->role = new_role;
2331 p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
2332 p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
2334 p_scb->no_rtp_header = false;
2335 p_scb->p_cos->start(p_scb->hndl, p_scb->PeerAddress(),
2336 p_scb->cfg.codec_info, &p_scb->no_rtp_header);
2337 p_scb->co_started = true;
2339 APPL_TRACE_DEBUG("%s: peer %s suspending: %d, role:0x%x, init %d", __func__,
2340 p_scb->PeerAddress().ToString().c_str(), suspend,
2341 p_scb->role, initiator);
2343 tBTA_AV_START start;
2344 start.suspending = suspend;
2345 start.initiator = initiator;
2346 start.chnl = p_scb->chnl;
2347 start.status = BTA_AV_SUCCESS;
2348 start.hndl = p_scb->hndl;
2349 tBTA_AV bta_av_data;
2350 bta_av_data.start = start;
2351 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, &bta_av_data);
2354 tBTA_AV_API_STOP stop;
2355 p_scb->role |= BTA_AV_ROLE_SUSPEND;
2356 p_scb->cong = true; /* do not allow the media data to go through */
2357 /* do not duplicate the media packets to this channel */
2358 p_scb->p_cos->stop(p_scb->hndl, p_scb->PeerAddress());
2359 p_scb->co_started = false;
2361 stop.suspend = true;
2362 stop.reconfig_stop = false;
2363 bta_av_ssm_execute(p_scb, BTA_AV_AP_STOP_EVT, (tBTA_AV_DATA*)&stop);
2368 /*******************************************************************************
2370 * Function bta_av_start_failed
2372 * Description Stream start failed.
2376 ******************************************************************************/
2377 void bta_av_start_failed(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2379 "%s: peer %s bta_handle:0x%x audio_open_cnt:%d started:%s co_started:%d",
2380 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
2381 bta_av_cb.audio_open_cnt, logbool(p_scb->started).c_str(),
2384 if (!p_scb->started && !p_scb->co_started) {
2385 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->PeerAddress());
2386 notify_start_failed(p_scb);
2389 BTM_unblock_role_switch_and_sniff_mode_for(p_scb->PeerAddress());
2390 p_scb->sco_suspend = false;
2393 /*******************************************************************************
2395 * Function bta_av_str_closed
2397 * Description Stream closed.
2401 ******************************************************************************/
2402 void bta_av_str_closed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2407 "%s: peer %s bta_handle:0x%x open_status:%d chnl:%d co_started:%d",
2408 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
2409 p_scb->open_status, p_scb->chnl, p_scb->co_started);
2411 BTM_unblock_role_switch_and_sniff_mode_for(p_scb->PeerAddress());
2412 if (bta_av_cb.audio_open_cnt <= 1) {
2413 BTM_default_unblock_role_switch();
2416 if (p_scb->open_status != BTA_AV_SUCCESS) {
2417 /* must be failure when opening the stream */
2418 data.open.bd_addr = p_scb->PeerAddress();
2419 data.open.status = p_scb->open_status;
2420 data.open.chnl = p_scb->chnl;
2421 data.open.hndl = p_scb->hndl;
2423 if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC)
2424 data.open.sep = AVDT_TSEP_SNK;
2425 else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK)
2426 data.open.sep = AVDT_TSEP_SRC;
2428 event = BTA_AV_OPEN_EVT;
2429 p_scb->open_status = BTA_AV_SUCCESS;
2431 bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->PeerAddress());
2432 bta_av_cleanup(p_scb, p_data);
2433 (*bta_av_cb.p_cback)(event, &data);
2435 /* do stop if we were started */
2436 if (p_scb->co_started) {
2437 bta_av_str_stopped(p_scb, NULL);
2441 p_scb->p_cos->close(p_scb->hndl, p_scb->PeerAddress());
2442 data.close.chnl = p_scb->chnl;
2443 data.close.hndl = p_scb->hndl;
2444 event = BTA_AV_CLOSE_EVT;
2446 bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->PeerAddress());
2447 bta_av_cleanup(p_scb, p_data);
2448 (*bta_av_cb.p_cback)(event, &data);
2453 /*******************************************************************************
2455 * Function bta_av_clr_cong
2457 * Description Clear stream congestion flag.
2461 ******************************************************************************/
2462 void bta_av_clr_cong(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2463 APPL_TRACE_DEBUG("%s", __func__);
2464 if (p_scb->co_started) {
2465 p_scb->cong = false;
2469 /*******************************************************************************
2471 * Function bta_av_suspend_cfm
2473 * Description process the suspend response
2477 ******************************************************************************/
2478 void bta_av_suspend_cfm(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2479 tBTA_AV_SUSPEND suspend_rsp;
2480 uint8_t err_code = p_data->str_msg.msg.hdr.err_code;
2482 APPL_TRACE_DEBUG("%s: peer %s bta_handle:0x%x audio_open_cnt:%d err_code:%d",
2483 __func__, p_scb->PeerAddress().ToString().c_str(),
2484 p_scb->hndl, bta_av_cb.audio_open_cnt, err_code);
2486 if (!p_scb->started) {
2487 /* handle the condition where there is a collision of SUSPEND req from
2489 ** Second SUSPEND req could be rejected. Do not treat this as a failure
2491 APPL_TRACE_WARNING("%s: already suspended, ignore, err_code %d", __func__,
2496 suspend_rsp.status = BTA_AV_SUCCESS;
2497 if (err_code && (err_code != AVDT_ERR_BAD_STATE)) {
2498 suspend_rsp.status = BTA_AV_FAIL;
2500 APPL_TRACE_ERROR("%s: suspend failed, closing connection", __func__);
2502 /* SUSPEND failed. Close connection. */
2503 bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
2505 /* only set started to false when suspend is successful */
2506 p_scb->started = false;
2509 if (p_scb->role & BTA_AV_ROLE_SUSPEND) {
2510 p_scb->role &= ~BTA_AV_ROLE_SUSPEND;
2511 p_scb->cong = false;
2514 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->PeerAddress());
2515 BTM_unblock_role_switch_and_sniff_mode_for(p_scb->PeerAddress());
2517 /* in case that we received suspend_ind, we may need to call co_stop here */
2518 if (p_scb->co_started) {
2519 if (bta_av_cb.offload_started_hndl == p_scb->hndl) {
2520 bta_av_vendor_offload_stop();
2521 bta_av_cb.offload_started_hndl = 0;
2522 } else if (bta_av_cb.offload_start_pending_hndl == p_scb->hndl) {
2523 APPL_TRACE_WARNING("%s: Stop pending offload start command", __func__);
2524 bta_av_vendor_offload_stop();
2525 bta_av_cb.offload_start_pending_hndl = 0;
2527 bta_av_stream_chg(p_scb, false);
2530 p_scb->co_started = false;
2531 p_scb->p_cos->stop(p_scb->hndl, p_scb->PeerAddress());
2536 suspend_rsp.chnl = p_scb->chnl;
2537 suspend_rsp.hndl = p_scb->hndl;
2538 suspend_rsp.initiator = p_data->str_msg.initiator;
2539 tBTA_AV bta_av_data;
2540 bta_av_data.suspend = suspend_rsp;
2541 (*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, &bta_av_data);
2545 /*******************************************************************************
2547 * Function bta_av_rcfg_str_ok
2549 * Description report reconfigure successful
2553 ******************************************************************************/
2554 void bta_av_rcfg_str_ok(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2555 p_scb->l2c_cid = AVDT_GetL2CapChannel(p_scb->avdt_handle);
2556 APPL_TRACE_DEBUG("%s: peer %s bta_handle:0x%x l2c_cid:%d", __func__,
2557 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
2560 if (p_data != NULL) {
2561 // p_data could be NULL if the reconfig was triggered by the local device
2563 p_data->str_msg.msg.open_ind.peer_mtu - AVDT_MEDIA_HDR_SIZE;
2564 APPL_TRACE_DEBUG("%s: l2c_cid: 0x%x stream_mtu: %d", __func__,
2565 p_scb->l2c_cid, p_scb->stream_mtu);
2566 p_scb->p_cos->update_mtu(p_scb->hndl, p_scb->PeerAddress(),
2571 bta_av_st_rc_timer(p_scb, NULL);
2573 /* No need to keep the role bits once reconfig is done. */
2574 p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
2575 p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
2576 p_scb->role &= ~BTA_AV_ROLE_START_INT;
2579 /* reconfigure success */
2580 tBTA_AV_RECONFIG reconfig;
2581 reconfig.status = BTA_AV_SUCCESS;
2582 reconfig.chnl = p_scb->chnl;
2583 reconfig.hndl = p_scb->hndl;
2584 tBTA_AV bta_av_data;
2585 bta_av_data.reconfig = reconfig;
2586 (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, &bta_av_data);
2590 /*******************************************************************************
2592 * Function bta_av_rcfg_failed
2594 * Description process reconfigure failed
2598 ******************************************************************************/
2599 void bta_av_rcfg_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2600 APPL_TRACE_ERROR("%s: num_recfg=%d conn_lcb=0x%x peer_addr=%s", __func__,
2601 p_scb->num_recfg, bta_av_cb.conn_lcb,
2602 p_scb->PeerAddress().ToString().c_str());
2604 if (p_scb->num_recfg > BTA_AV_RECONFIG_RETRY) {
2605 bta_av_cco_close(p_scb, p_data);
2606 /* report failure */
2607 tBTA_AV_RECONFIG reconfig;
2608 reconfig.status = BTA_AV_FAIL_STREAM;
2609 reconfig.chnl = p_scb->chnl;
2610 reconfig.hndl = p_scb->hndl;
2611 tBTA_AV bta_av_data;
2612 bta_av_data.reconfig = reconfig;
2613 (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, &bta_av_data);
2614 /* go to closing state */
2615 bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
2617 /* open failed. try again */
2619 // conn_lcb is the index bitmask of all used LCBs, and since LCB and SCB use
2620 // the same index, it should be safe to use SCB index here.
2621 if ((bta_av_cb.conn_lcb & (1 << p_scb->hdi)) != 0) {
2622 AVDT_DisconnectReq(p_scb->PeerAddress(), &bta_av_proc_stream_evt);
2624 APPL_TRACE_WARNING("%s: conn_lcb=0x%x bta_handle=0x%x (hdi=%u) no link",
2625 __func__, bta_av_cb.conn_lcb, p_scb->hndl, p_scb->hdi);
2626 bta_av_connect_req(p_scb, NULL);
2631 /*******************************************************************************
2633 * Function bta_av_rcfg_connect
2635 * Description stream closed. reconnect the stream
2639 ******************************************************************************/
2640 void bta_av_rcfg_connect(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2641 APPL_TRACE_DEBUG("%s", __func__);
2643 p_scb->cong = false;
2645 APPL_TRACE_DEBUG("%s: num_recfg: %d", __func__, p_scb->num_recfg);
2646 if (p_scb->num_recfg > BTA_AV_RECONFIG_RETRY) {
2647 /* let bta_av_rcfg_failed report fail */
2648 bta_av_rcfg_failed(p_scb, NULL);
2650 AVDT_ConnectReq(p_scb->PeerAddress(), p_scb->hdi, &bta_av_proc_stream_evt);
2654 /*******************************************************************************
2656 * Function bta_av_rcfg_discntd
2658 * Description AVDT disconnected. reconnect the stream
2662 ******************************************************************************/
2663 void bta_av_rcfg_discntd(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2664 APPL_TRACE_ERROR("%s: num_recfg=%d conn_lcb=0x%x peer_addr=%s", __func__,
2665 p_scb->num_recfg, bta_av_cb.conn_lcb,
2666 p_scb->PeerAddress().ToString().c_str());
2669 if (p_scb->num_recfg > BTA_AV_RECONFIG_RETRY) {
2670 /* report failure */
2671 tBTA_AV_RECONFIG reconfig;
2672 reconfig.status = BTA_AV_FAIL_STREAM;
2673 reconfig.chnl = p_scb->chnl;
2674 reconfig.hndl = p_scb->hndl;
2675 tBTA_AV bta_av_data;
2676 bta_av_data.reconfig = reconfig;
2677 (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, &bta_av_data);
2678 /* report close event & go to init state */
2679 bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
2681 AVDT_ConnectReq(p_scb->PeerAddress(), p_scb->hdi, &bta_av_proc_stream_evt);
2685 /*******************************************************************************
2687 * Function bta_av_suspend_cont
2689 * Description received the suspend response.
2690 * continue to reconfigure the stream
2694 ******************************************************************************/
2695 void bta_av_suspend_cont(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2696 uint8_t err_code = p_data->str_msg.msg.hdr.err_code;
2698 APPL_TRACE_DEBUG("%s: err_code=%d", __func__, err_code);
2700 p_scb->started = false;
2701 p_scb->cong = false;
2703 if (AVDT_ERR_CONNECT == err_code) {
2704 /* report failure */
2705 tBTA_AV_RECONFIG reconfig;
2706 reconfig.status = BTA_AV_FAIL;
2707 tBTA_AV bta_av_data;
2708 bta_av_data.reconfig = reconfig;
2709 (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, &bta_av_data);
2710 APPL_TRACE_ERROR("%s: BTA_AV_STR_DISC_FAIL_EVT: peer_addr=%s", __func__,
2711 p_scb->PeerAddress().ToString().c_str());
2712 bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
2714 APPL_TRACE_ERROR("%s: suspend rejected, try close", __func__);
2715 /* drop the buffers queued in L2CAP */
2716 L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
2718 AVDT_CloseReq(p_scb->avdt_handle);
2721 APPL_TRACE_DEBUG("%s: calling AVDT_ReconfigReq", __func__);
2722 /* reconfig the stream */
2724 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
2725 A2DP_CodecInfoString(p_scb->cfg.codec_info).c_str());
2726 AVDT_ReconfigReq(p_scb->avdt_handle, &p_scb->cfg);
2727 p_scb->cfg.psc_mask = p_scb->cur_psc_mask;
2731 /*******************************************************************************
2733 * Function bta_av_rcfg_cfm
2735 * Description if reconfigure is successful, report the event
2736 * otherwise, close the stream.
2740 ******************************************************************************/
2741 void bta_av_rcfg_cfm(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2742 uint8_t err_code = p_data->str_msg.msg.hdr.err_code;
2744 APPL_TRACE_DEBUG("%s: err_code = %d", __func__, err_code);
2746 // Disable AVDTP RECONFIGURE for rejectlisted devices
2747 bool disable_avdtp_reconfigure = false;
2749 char remote_name[BTM_MAX_REM_BD_NAME_LEN] = "";
2750 if (btif_storage_get_stored_remote_name(p_scb->PeerAddress(),
2752 if (interop_match_name(INTEROP_DISABLE_AVDTP_RECONFIGURE, remote_name) ||
2753 interop_match_addr(INTEROP_DISABLE_AVDTP_RECONFIGURE,
2754 (const RawAddress*)&p_scb->PeerAddress())) {
2756 "%s: disable AVDTP RECONFIGURE: interop matched "
2757 "name %s address %s",
2758 __func__, remote_name, p_scb->PeerAddress().ToString().c_str());
2759 disable_avdtp_reconfigure = true;
2764 if ((err_code != 0) || disable_avdtp_reconfigure) {
2765 APPL_TRACE_ERROR("%s: reconfig rejected, try close", __func__);
2766 /* Disable reconfiguration feature only with explicit rejection(not with
2768 if ((err_code != AVDT_ERR_TIMEOUT) || disable_avdtp_reconfigure) {
2769 p_scb->recfg_sup = false;
2771 /* started flag is false when reconfigure command is sent */
2772 /* drop the buffers queued in L2CAP */
2773 L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
2774 AVDT_CloseReq(p_scb->avdt_handle);
2776 /* update the codec info after rcfg cfm */
2778 "%s: updating from codec %s to codec %s", __func__,
2779 A2DP_CodecName(p_scb->cfg.codec_info),
2780 A2DP_CodecName(p_data->str_msg.msg.reconfig_cfm.p_cfg->codec_info));
2781 memcpy(p_scb->cfg.codec_info,
2782 p_data->str_msg.msg.reconfig_cfm.p_cfg->codec_info, AVDT_CODEC_SIZE);
2783 /* take the SSM back to OPEN state */
2784 bta_av_ssm_execute(p_scb, BTA_AV_STR_OPEN_OK_EVT, NULL);
2788 /*******************************************************************************
2790 * Function bta_av_rcfg_open
2792 * Description AVDT is connected. open the stream with the new
2797 ******************************************************************************/
2798 void bta_av_rcfg_open(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2799 APPL_TRACE_DEBUG("%s: peer %s bta_handle:0x%x num_disc_snks:%d", __func__,
2800 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
2801 p_scb->num_disc_snks);
2803 if (p_scb->num_disc_snks == 0) {
2804 /* Need to update call-out module so that it will be ready for discover */
2805 p_scb->p_cos->stop(p_scb->hndl, p_scb->PeerAddress());
2807 /* send avdtp discover request */
2808 AVDT_DiscoverReq(p_scb->PeerAddress(), p_scb->hdi, p_scb->sep_info,
2809 BTA_AV_NUM_SEPS, &bta_av_proc_stream_evt);
2811 APPL_TRACE_DEBUG("%s: calling AVDT_OpenReq()", __func__);
2812 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
2813 A2DP_CodecInfoString(p_scb->cfg.codec_info).c_str());
2815 /* we may choose to use a different SEP at reconfig.
2816 * adjust the sep_idx now */
2817 bta_av_adjust_seps_idx(p_scb, bta_av_get_scb_handle(p_scb, AVDT_TSEP_SRC));
2818 LOG_INFO("%s: sep_idx=%d avdt_handle=%d bta_handle=0x%x", __func__,
2819 p_scb->sep_idx, p_scb->avdt_handle, p_scb->hndl);
2821 /* open the stream with the new config */
2822 p_scb->sep_info_idx = p_scb->rcfg_idx;
2823 AVDT_OpenReq(p_scb->avdt_handle, p_scb->PeerAddress(), p_scb->hdi,
2824 p_scb->sep_info[p_scb->sep_info_idx].seid, &p_scb->cfg);
2828 /*******************************************************************************
2830 * Function bta_av_security_rej
2832 * Description Send an AVDTP security reject.
2836 ******************************************************************************/
2837 void bta_av_security_rej(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2838 AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_BAD_STATE,
2842 /*******************************************************************************
2844 * Function bta_av_chk_2nd_start
2846 * Description check if this is 2nd stream and if it needs to be started.
2847 * This function needs to be kept very similar to
2852 ******************************************************************************/
2853 void bta_av_chk_2nd_start(tBTA_AV_SCB* p_scb,
2854 UNUSED_ATTR tBTA_AV_DATA* p_data) {
2856 "%s: peer %s channel:%d bta_av_cb.audio_open_cnt:%d role:0x%x "
2858 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->chnl,
2859 bta_av_cb.audio_open_cnt, p_scb->role, bta_av_cb.features);
2861 if ((p_scb->chnl == BTA_AV_CHNL_AUDIO) && (bta_av_cb.audio_open_cnt >= 2) &&
2862 (((p_scb->role & BTA_AV_ROLE_AD_ACP) == 0) || // Outgoing connection or
2863 (bta_av_cb.features & BTA_AV_FEAT_ACP_START))) { // Auto-starting option
2864 // More than one audio channel is connected.
2865 if (!(p_scb->role & BTA_AV_ROLE_SUSPEND_OPT)) {
2866 // This channel does not need to be reconfigured.
2867 // If there is other channel streaming, start the stream now.
2868 bool new_started = false;
2869 for (int i = 0; i < BTA_AV_NUM_STRS; i++) {
2870 tBTA_AV_SCB* p_scbi = bta_av_cb.p_scb[i];
2871 if (p_scb == p_scbi) {
2874 if (p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started) {
2876 // Start the new stream
2879 "%s: starting new stream for peer %s because peer %s "
2881 __func__, p_scb->PeerAddress().ToString().c_str(),
2882 p_scbi->PeerAddress().ToString().c_str());
2883 bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
2885 // May need to update the flush timeout of this already started stream
2886 if (p_scbi->co_started != bta_av_cb.audio_open_cnt) {
2887 p_scbi->co_started = bta_av_cb.audio_open_cnt;
2895 /*******************************************************************************
2897 * Function bta_av_open_rc
2899 * Description Send a message to main SM to open RC channel.
2903 ******************************************************************************/
2904 void bta_av_open_rc(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2905 APPL_TRACE_DEBUG("%s: use_rc: %d, wait: 0x%x role: 0x%x", __func__,
2906 p_scb->use_rc, p_scb->wait, p_scb->role);
2907 if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) &&
2908 (p_scb->q_tag == BTA_AV_Q_TAG_START)) {
2909 /* waiting for role switch for some reason & the timer expires */
2910 if (!bta_av_link_role_ok(p_scb, A2DP_SET_ONE_BIT)) {
2912 "%s: failed to start streaming for role management reasons!!",
2914 alarm_cancel(p_scb->avrc_ct_timer);
2916 tBTA_AV_START start;
2917 start.chnl = p_scb->chnl;
2918 start.status = BTA_AV_FAIL_ROLE;
2919 start.initiator = true;
2920 start.hndl = p_scb->hndl;
2921 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
2922 bta_av_cb.rs_idx = 0;
2923 tBTA_AV bta_av_data;
2924 bta_av_data.start = start;
2925 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, &bta_av_data);
2927 /* role switch is done. continue to start streaming */
2928 bta_av_cb.rs_idx = 0;
2929 p_data->hdr.offset = BTA_AV_RS_OK;
2930 bta_av_start_ok(p_scb, p_data);
2935 if (p_scb->use_rc || (p_scb->role & BTA_AV_ROLE_AD_ACP)) {
2936 if (bta_av_cb.disc) {
2937 /* AVRC discover db is in use */
2938 if (p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE) {
2939 /* AVRC channel is not connected. delay a little bit */
2940 if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0) {
2941 bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_RC_DISC_TIME_VAL,
2942 BTA_AV_AVRC_TIMER_EVT, p_scb->hndl);
2944 p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
2948 /* use main SM for AVRC SDP activities */
2949 if (is_new_avrcp_enabled()) {
2950 APPL_TRACE_WARNING("%s: Using the new AVRCP Profile", __func__);
2951 bluetooth::avrcp::AvrcpService::Get()->ConnectDevice(
2952 p_scb->PeerAddress());
2954 bta_av_rc_disc((uint8_t)(p_scb->hdi + 1));
2958 if (BTA_AV_RC_HANDLE_NONE != p_scb->rc_handle) {
2959 /* the open API said that this handle does not want a RC connection.
2960 * disconnect it now */
2961 AVRC_Close(p_scb->rc_handle);
2966 /*******************************************************************************
2968 * Function bta_av_open_at_inc
2970 * Description This function is called if API open is called by application
2971 * while state-machine is at incoming state.
2975 ******************************************************************************/
2976 void bta_av_open_at_inc(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2977 memcpy(&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
2979 APPL_TRACE_DEBUG("%s: peer %s coll_mask=0x%02x", __func__,
2980 p_scb->PeerAddress().ToString().c_str(), p_scb->coll_mask);
2982 if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR) {
2983 p_scb->coll_mask |= BTA_AV_COLL_API_CALLED;
2985 /* API open will be handled at timeout if SNK did not start signalling. */
2986 /* API open will be ignored if SNK starts signalling. */
2988 /* SNK did not start signalling, API was called N seconds timeout. */
2989 /* We need to switch to INIT state and start opening connection. */
2990 p_scb->coll_mask = 0;
2991 bta_av_set_scb_sst_init(p_scb);
2993 tBTA_AV_API_OPEN* p_buf =
2994 (tBTA_AV_API_OPEN*)osi_malloc(sizeof(tBTA_AV_API_OPEN));
2995 memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
2996 bta_sys_sendmsg(p_buf);
3000 void offload_vendor_callback(tBTM_VSC_CMPL* param) {
3002 uint8_t sub_opcode = 0;
3003 if (param->param_len) {
3004 APPL_TRACE_DEBUG("%s: param_len = %d status = %d", __func__,
3005 param->param_len, param->p_param_buf[0]);
3006 value.status = static_cast<tBTA_AV_STATUS>(param->p_param_buf[0]);
3008 if (value.status == 0) {
3009 sub_opcode = param->p_param_buf[1];
3010 APPL_TRACE_DEBUG("%s: subopcode = %d", __func__, sub_opcode);
3011 switch (sub_opcode) {
3012 case VS_HCI_A2DP_OFFLOAD_STOP:
3013 APPL_TRACE_DEBUG("%s: VS_HCI_STOP_A2DP_MEDIA successful", __func__);
3015 case VS_HCI_A2DP_OFFLOAD_START:
3016 if (bta_av_cb.offload_start_pending_hndl) {
3017 APPL_TRACE_DEBUG("%s: VS_HCI_START_A2DP_MEDIA successful", __func__);
3018 bta_av_cb.offload_started_hndl = bta_av_cb.offload_start_pending_hndl;
3019 bta_av_cb.offload_start_pending_hndl = 0;
3021 LOG_INFO("%s: No pending start command due to AVDTP suspend immediately", __func__);
3023 (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, &value);
3029 APPL_TRACE_DEBUG("%s: Offload failed for subopcode= %d", __func__,
3031 if (param->opcode != VS_HCI_A2DP_OFFLOAD_STOP) {
3032 bta_av_cb.offload_start_pending_hndl = 0;
3033 (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, &value);
3038 void bta_av_vendor_offload_start(tBTA_AV_SCB* p_scb,
3039 tBT_A2DP_OFFLOAD* offload_start) {
3040 uint8_t param[sizeof(tBT_A2DP_OFFLOAD)];
3041 APPL_TRACE_DEBUG("%s", __func__);
3043 uint8_t* p_param = param;
3044 *p_param++ = VS_HCI_A2DP_OFFLOAD_START;
3046 UINT32_TO_STREAM(p_param, offload_start->codec_type);
3047 UINT16_TO_STREAM(p_param, offload_start->max_latency);
3048 ARRAY_TO_STREAM(p_param, offload_start->scms_t_enable,
3049 static_cast<int>(offload_start->scms_t_enable.size()));
3050 UINT32_TO_STREAM(p_param, offload_start->sample_rate);
3051 UINT8_TO_STREAM(p_param, offload_start->bits_per_sample);
3052 UINT8_TO_STREAM(p_param, offload_start->ch_mode);
3053 UINT32_TO_STREAM(p_param, offload_start->encoded_audio_bitrate);
3054 UINT16_TO_STREAM(p_param, offload_start->acl_hdl);
3055 UINT16_TO_STREAM(p_param, offload_start->l2c_rcid);
3056 UINT16_TO_STREAM(p_param, offload_start->mtu);
3057 ARRAY_TO_STREAM(p_param, offload_start->codec_info,
3058 (int8_t)sizeof(offload_start->codec_info));
3059 bta_av_cb.offload_start_pending_hndl = p_scb->hndl;
3060 BTM_VendorSpecificCommand(HCI_CONTROLLER_A2DP, p_param - param,
3061 param, offload_vendor_callback);
3064 void bta_av_vendor_offload_stop() {
3065 uint8_t param[sizeof(tBT_A2DP_OFFLOAD)];
3066 APPL_TRACE_DEBUG("%s", __func__);
3067 param[0] = VS_HCI_A2DP_OFFLOAD_STOP;
3068 BTM_VendorSpecificCommand(HCI_CONTROLLER_A2DP, 1, param,
3069 offload_vendor_callback);
3071 /*******************************************************************************
3073 * Function bta_av_offload_req
3075 * Description This function is called if application requests offload of
3080 ******************************************************************************/
3081 void bta_av_offload_req(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
3082 tBTA_AV_STATUS status = BTA_AV_FAIL_RESOURCES;
3084 tBT_A2DP_OFFLOAD offload_start;
3085 APPL_TRACE_DEBUG("%s: stream %s, audio channels open %d", __func__,
3086 p_scb->started ? "STARTED" : "STOPPED",
3087 bta_av_cb.audio_open_cnt);
3088 /* Check if stream has already been started. */
3089 /* Support offload if only one audio source stream is open. */
3090 if (p_scb->started != true) {
3091 status = BTA_AV_FAIL_STREAM;
3092 } else if (bta_av_cb.offload_start_pending_hndl ||
3093 bta_av_cb.offload_started_hndl) {
3094 APPL_TRACE_WARNING("%s: offload already started, ignore request", __func__);
3097 bta_av_offload_codec_builder(p_scb, &offload_start);
3098 bta_av_vendor_offload_start(p_scb, &offload_start);
3101 if (status != BTA_AV_SUCCESS) {
3102 tBTA_AV bta_av_data;
3103 bta_av_data.status = status;
3104 (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, &bta_av_data);
3106 /* TODO(eisenbach): RE-IMPLEMENT USING VSC OR HAL EXTENSION
3107 else if (bta_av_cb.audio_open_cnt == 1 &&
3108 p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC &&
3109 p_scb->chnl == BTA_AV_CHNL_AUDIO) {
3110 bt_vendor_op_a2dp_offload_t a2dp_offload_start;
3112 if (L2CA_GetConnectionConfig(
3113 p_scb->l2c_cid, &a2dp_offload_start.acl_data_size,
3114 &a2dp_offload_start.remote_cid, &a2dp_offload_start.lm_handle)) {
3115 APPL_TRACE_DEBUG("%s: l2cmtu %d lcid 0x%02X rcid 0x%02X lm_handle
3117 __func__, a2dp_offload_start.acl_data_size,
3118 p_scb->l2c_cid, a2dp_offload_start.remote_cid,
3119 a2dp_offload_start.lm_handle);
3121 a2dp_offload_start.bta_av_handle = p_scb->hndl;
3122 a2dp_offload_start.xmit_quota = BTA_AV_A2DP_OFFLOAD_XMIT_QUOTA;
3123 a2dp_offload_start.stream_mtu = p_scb->stream_mtu;
3124 a2dp_offload_start.local_cid = p_scb->l2c_cid;
3125 a2dp_offload_start.is_flushable = true;
3126 a2dp_offload_start.stream_source =
3127 ((uint32_t)(p_scb->cfg.codec_info[1] | p_scb->cfg.codec_info[2]));
3129 memcpy(a2dp_offload_start.codec_info, p_scb->cfg.codec_info,
3130 sizeof(a2dp_offload_start.codec_info));
3132 if (!vendor_get_interface()->send_command(
3133 (vendor_opcode_t)BT_VND_OP_A2DP_OFFLOAD_START,
3134 &a2dp_offload_start)) {
3135 status = BTA_AV_SUCCESS;
3136 p_scb->offload_start_pending = true;
3143 /*******************************************************************************
3145 * Function bta_av_offload_rsp
3147 * Description This function is called when the vendor lib responds to
3148 * BT_VND_OP_A2DP_OFFLOAD_START.
3152 ******************************************************************************/
3153 void bta_av_offload_rsp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
3154 tBTA_AV_STATUS status = p_data->api_status_rsp.status;
3156 APPL_TRACE_DEBUG("%s: stream %s status %s", __func__,
3157 p_scb->started ? "STARTED" : "STOPPED",
3158 status ? "FAIL" : "SUCCESS");
3160 /* Check if stream has already been started. */
3161 if (status == BTA_AV_SUCCESS && p_scb->started != true) {
3162 status = BTA_AV_FAIL_STREAM;
3165 bta_av_cb.offload_start_pending_hndl = 0;
3166 tBTA_AV bta_av_data;
3167 bta_av_data.status = status;
3168 (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, &bta_av_data);
3171 static void bta_av_offload_codec_builder(tBTA_AV_SCB* p_scb,
3172 tBT_A2DP_OFFLOAD* p_a2dp_offload) {
3173 A2dpCodecConfig* CodecConfig = bta_av_get_a2dp_current_codec();
3174 btav_a2dp_codec_index_t codec_index =
3175 A2DP_SourceCodecIndex(p_scb->cfg.codec_info);
3176 uint32_t codec_type = 0;
3177 uint16_t mtu = p_scb->stream_mtu;
3178 APPL_TRACE_DEBUG("%s:codec_index = %d", __func__, codec_index);
3179 switch (codec_index) {
3180 case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
3181 codec_type = BTA_AV_CODEC_TYPE_SBC;
3182 if (A2DP_GetMaxBitpoolSbc(p_scb->cfg.codec_info) <=
3183 A2DP_SBC_BITPOOL_MIDDLE_QUALITY) {
3184 APPL_TRACE_WARNING("%s: Restricting streaming MTU size for MQ Bitpool",
3186 mtu = MAX_2MBPS_AVDTP_MTU;
3189 case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
3190 codec_type = BTA_AV_CODEC_TYPE_AAC;
3192 case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX:
3193 codec_type = BTA_AV_CODEC_TYPE_APTX;
3195 case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD:
3196 codec_type = BTA_AV_CODEC_TYPE_APTXHD;
3198 case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC:
3199 codec_type = BTA_AV_CODEC_TYPE_LDAC;
3202 APPL_TRACE_ERROR("%s: Unknown Codec type ", __func__);
3205 if (mtu > MAX_3MBPS_AVDTP_MTU) mtu = MAX_3MBPS_AVDTP_MTU;
3206 p_a2dp_offload->codec_type = codec_type;
3207 p_a2dp_offload->max_latency = 0;
3208 p_a2dp_offload->mtu = mtu;
3209 p_a2dp_offload->acl_hdl =
3210 get_btm_client_interface().lifecycle.BTM_GetHCIConnHandle(
3211 p_scb->PeerAddress(), BT_TRANSPORT_BR_EDR);
3212 btav_a2dp_scmst_info_t scmst_info =
3213 p_scb->p_cos->get_scmst_info(p_scb->PeerAddress());
3214 p_a2dp_offload->scms_t_enable[0] = scmst_info.enable_status;
3215 p_a2dp_offload->scms_t_enable[1] = scmst_info.cp_header;
3217 "%s: SCMS-T_enable status: %d, "
3218 "SCMS-T header (if it's enabled): 0x%02x",
3219 __func__, scmst_info.enable_status, scmst_info.cp_header);
3221 switch (A2DP_GetTrackSampleRate(p_scb->cfg.codec_info)) {
3223 p_a2dp_offload->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
3226 p_a2dp_offload->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
3229 p_a2dp_offload->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
3232 p_a2dp_offload->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
3235 if (L2CA_GetRemoteCid(p_scb->l2c_cid, &p_a2dp_offload->l2c_rcid) == false) {
3236 APPL_TRACE_ERROR("%s: Failed to fetch l2c rcid", __func__);
3239 switch (CodecConfig->getAudioBitsPerSample()) {
3241 p_a2dp_offload->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
3244 p_a2dp_offload->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
3247 p_a2dp_offload->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
3250 p_a2dp_offload->ch_mode = A2DP_GetTrackChannelCount(p_scb->cfg.codec_info);
3251 p_a2dp_offload->encoded_audio_bitrate = CodecConfig->getTrackBitRate();
3252 if (!CodecConfig->getCodecSpecificConfig(p_a2dp_offload)) {
3253 APPL_TRACE_ERROR("%s: not a valid codec info", __func__);