1 /******************************************************************************
2 *
3 * Copyright 2016 The Android Open Source Project
4 * Copyright 2009-2012 Broadcom Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ******************************************************************************/
19
20 #define LOG_TAG "bt_btif_a2dp"
21
22 #include <stdbool.h>
23
24 #include "audio_a2dp_hw/include/audio_a2dp_hw.h"
25 #include "audio_hal_interface/a2dp_encoding.h"
26 #include "bt_common.h"
27 #include "bta_av_api.h"
28 #include "btif_a2dp.h"
29 #include "btif_a2dp_audio_interface.h"
30 #include "btif_a2dp_control.h"
31 #include "btif_a2dp_sink.h"
32 #include "btif_a2dp_source.h"
33 #include "btif_av.h"
34 #include "btif_av_co.h"
35 #include "btif_hf.h"
36 #include "btif_util.h"
37 #include "osi/include/log.h"
38
btif_a2dp_on_idle(void)39 void btif_a2dp_on_idle(void) {
40 LOG_INFO(LOG_TAG, "%s: ## ON A2DP IDLE ## peer_sep = %d", __func__,
41 btif_av_get_peer_sep());
42 if (btif_av_get_peer_sep() == AVDT_TSEP_SNK) {
43 btif_a2dp_source_on_idle();
44 } else if (btif_av_get_peer_sep() == AVDT_TSEP_SRC) {
45 btif_a2dp_sink_on_idle();
46 }
47 }
48
btif_a2dp_on_started(const RawAddress & peer_addr,tBTA_AV_START * p_av_start)49 bool btif_a2dp_on_started(const RawAddress& peer_addr, tBTA_AV_START* p_av_start) {
50 LOG(INFO) << __func__ << ": ## ON A2DP STARTED ## peer " << peer_addr << " p_av_start:" << p_av_start;
51
52 if (p_av_start == NULL) {
53 tA2DP_CTRL_ACK status = A2DP_CTRL_ACK_SUCCESS;
54 if (!bluetooth::headset::IsCallIdle()) {
55 LOG(ERROR) << __func__ << ": peer " << peer_addr << " call in progress, do not start A2DP stream";
56 status = A2DP_CTRL_ACK_INCALL_FAILURE;
57 }
58 /* just ack back a local start request, do not start the media encoder since
59 * this is not for BTA_AV_START_EVT. */
60 if (bluetooth::audio::a2dp::is_hal_2_0_enabled()) {
61 bluetooth::audio::a2dp::ack_stream_started(status);
62 } else if (btif_av_is_a2dp_offload_enabled()) {
63 btif_a2dp_audio_on_started(status);
64 } else {
65 btif_a2dp_command_ack(status);
66 }
67 return true;
68 }
69
70 LOG(INFO) << __func__ << ": peer " << peer_addr << " status:" << +p_av_start->status
71 << " suspending:" << logbool(p_av_start->suspending) << " initiator:" << logbool(p_av_start->initiator);
72
73 if (p_av_start->status == BTA_AV_SUCCESS) {
74 if (p_av_start->suspending) {
75 LOG(WARNING) << __func__ << ": peer " << peer_addr << " A2DP is suspending and ignores the started event";
76 return false;
77 }
78 if (btif_av_is_a2dp_offload_enabled()) {
79 btif_av_stream_start_offload();
80 } else if (bluetooth::audio::a2dp::is_hal_2_0_enabled()) {
81 if (btif_av_get_peer_sep() == AVDT_TSEP_SNK) {
82 /* Start the media encoder to do the SW audio stream */
83 btif_a2dp_source_start_audio_req();
84 }
85 if (p_av_start->initiator) {
86 bluetooth::audio::a2dp::ack_stream_started(A2DP_CTRL_ACK_SUCCESS);
87 return true;
88 }
89 } else {
90 if (p_av_start->initiator) {
91 btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
92 return true;
93 }
94 /* media task is auto-started upon UIPC connection of a2dp audiopath */
95 }
96 } else if (p_av_start->initiator) {
97 LOG(ERROR) << __func__ << ": peer " << peer_addr << " A2DP start request failed: status = " << +p_av_start->status;
98 if (bluetooth::audio::a2dp::is_hal_2_0_enabled()) {
99 bluetooth::audio::a2dp::ack_stream_started(A2DP_CTRL_ACK_FAILURE);
100 } else if (btif_av_is_a2dp_offload_enabled()) {
101 btif_a2dp_audio_on_started(p_av_start->status);
102 } else {
103 btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
104 }
105 return true;
106 }
107 return false;
108 }
109
btif_a2dp_on_stopped(tBTA_AV_SUSPEND * p_av_suspend)110 void btif_a2dp_on_stopped(tBTA_AV_SUSPEND* p_av_suspend) {
111 LOG_INFO(LOG_TAG, "%s: ## ON A2DP STOPPED ## p_av_suspend=%p", __func__,
112 p_av_suspend);
113
114 if (btif_av_get_peer_sep() == AVDT_TSEP_SRC) {
115 btif_a2dp_sink_on_stopped(p_av_suspend);
116 return;
117 }
118 if (bluetooth::audio::a2dp::is_hal_2_0_enabled() ||
119 !btif_av_is_a2dp_offload_enabled()) {
120 btif_a2dp_source_on_stopped(p_av_suspend);
121 } else if (p_av_suspend != NULL) {
122 btif_a2dp_audio_on_stopped(p_av_suspend->status);
123 }
124 }
125
btif_a2dp_on_suspended(tBTA_AV_SUSPEND * p_av_suspend)126 void btif_a2dp_on_suspended(tBTA_AV_SUSPEND* p_av_suspend) {
127 LOG_INFO(LOG_TAG, "%s: ## ON A2DP SUSPENDED ## p_av_suspend=%p", __func__,
128 p_av_suspend);
129 if (btif_av_get_peer_sep() == AVDT_TSEP_SRC) {
130 btif_a2dp_sink_on_suspended(p_av_suspend);
131 return;
132 }
133 if (bluetooth::audio::a2dp::is_hal_2_0_enabled() ||
134 !btif_av_is_a2dp_offload_enabled()) {
135 btif_a2dp_source_on_suspended(p_av_suspend);
136 } else if (p_av_suspend != NULL) {
137 btif_a2dp_audio_on_suspended(p_av_suspend->status);
138 }
139 }
140
btif_a2dp_on_offload_started(const RawAddress & peer_addr,tBTA_AV_STATUS status)141 void btif_a2dp_on_offload_started(const RawAddress& peer_addr,
142 tBTA_AV_STATUS status) {
143 tA2DP_CTRL_ACK ack;
144 LOG_INFO(LOG_TAG, "%s: peer %s status %d", __func__,
145 peer_addr.ToString().c_str(), status);
146
147 switch (status) {
148 case BTA_AV_SUCCESS:
149 ack = A2DP_CTRL_ACK_SUCCESS;
150 break;
151 case BTA_AV_FAIL_RESOURCES:
152 LOG_ERROR(LOG_TAG, "%s: peer %s FAILED UNSUPPORTED", __func__,
153 peer_addr.ToString().c_str());
154 ack = A2DP_CTRL_ACK_UNSUPPORTED;
155 break;
156 default:
157 LOG_ERROR(LOG_TAG, "%s: peer %s FAILED: status = %d", __func__,
158 peer_addr.ToString().c_str(), status);
159 ack = A2DP_CTRL_ACK_FAILURE;
160 break;
161 }
162 if (btif_av_is_a2dp_offload_enabled()) {
163 if (ack != BTA_AV_SUCCESS && btif_av_stream_started_ready()) {
164 // Offload request will return with failure from btif_av sm if
165 // suspend is triggered for remote start. Disconnect only if SoC
166 // returned failure for offload VSC
167 LOG_ERROR(LOG_TAG, "%s: peer %s offload start failed", __func__,
168 peer_addr.ToString().c_str());
169 btif_av_src_disconnect_sink(peer_addr);
170 }
171 }
172 if (bluetooth::audio::a2dp::is_hal_2_0_enabled()) {
173 bluetooth::audio::a2dp::ack_stream_started(ack);
174 } else {
175 btif_a2dp_command_ack(ack);
176 btif_a2dp_audio_on_started(status);
177 }
178 }
179
btif_debug_a2dp_dump(int fd)180 void btif_debug_a2dp_dump(int fd) {
181 btif_a2dp_source_debug_dump(fd);
182 btif_a2dp_sink_debug_dump(fd);
183 btif_a2dp_codec_debug_dump(fd);
184 }
185