• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "btif_a2dp.h"
23 
24 #include <stdbool.h>
25 
26 #include "audio_a2dp_hw/include/audio_a2dp_hw.h"
27 #include "audio_hal_interface/a2dp_encoding.h"
28 #include "bta_av_api.h"
29 #include "btif_a2dp_control.h"
30 #include "btif_a2dp_sink.h"
31 #include "btif_a2dp_source.h"
32 #include "btif_av.h"
33 #include "btif_av_co.h"
34 #include "btif_hf.h"
35 #include "btif_util.h"
36 #include "osi/include/log.h"
37 #include "types/raw_address.h"
38 
39 #include <base/logging.h>
40 
btif_a2dp_on_idle(void)41 void btif_a2dp_on_idle(void) {
42   LOG_VERBOSE("Peer stream endpoint type:%s",
43               peer_stream_endpoint_text(btif_av_get_peer_sep()).c_str());
44   if (btif_av_get_peer_sep() == AVDT_TSEP_SNK) {
45     btif_a2dp_source_on_idle();
46   } else if (btif_av_get_peer_sep() == AVDT_TSEP_SRC) {
47     btif_a2dp_sink_on_idle();
48   }
49 }
50 
btif_a2dp_on_started(const RawAddress & peer_addr,tBTA_AV_START * p_av_start)51 bool btif_a2dp_on_started(const RawAddress& peer_addr, tBTA_AV_START* p_av_start) {
52   LOG(INFO) << __func__ << ": ## ON A2DP STARTED ## peer " << peer_addr << " p_av_start:" << p_av_start;
53 
54   if (p_av_start == NULL) {
55     tA2DP_CTRL_ACK status = A2DP_CTRL_ACK_SUCCESS;
56     if (!bluetooth::headset::IsCallIdle()) {
57       LOG(ERROR) << __func__ << ": peer " << peer_addr << " call in progress, do not start A2DP stream";
58       status = A2DP_CTRL_ACK_INCALL_FAILURE;
59     }
60     /* just ack back a local start request, do not start the media encoder since
61      * this is not for BTA_AV_START_EVT. */
62     if (bluetooth::audio::a2dp::is_hal_enabled()) {
63       bluetooth::audio::a2dp::ack_stream_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_running()) {
79       btif_av_stream_start_offload();
80     } else if (bluetooth::audio::a2dp::is_hal_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_enabled()) {
99       bluetooth::audio::a2dp::ack_stream_started(A2DP_CTRL_ACK_FAILURE);
100     } else {
101       btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
102     }
103     return true;
104   }
105   return false;
106 }
107 
btif_a2dp_on_stopped(tBTA_AV_SUSPEND * p_av_suspend)108 void btif_a2dp_on_stopped(tBTA_AV_SUSPEND* p_av_suspend) {
109   LOG_INFO("%s: ## ON A2DP STOPPED ## p_av_suspend=%p", __func__, p_av_suspend);
110 
111   if (btif_av_get_peer_sep() == AVDT_TSEP_SRC) {
112     btif_a2dp_sink_on_stopped(p_av_suspend);
113     return;
114   }
115   if (bluetooth::audio::a2dp::is_hal_enabled() ||
116       !btif_av_is_a2dp_offload_running()) {
117     btif_a2dp_source_on_stopped(p_av_suspend);
118   }
119 }
120 
btif_a2dp_on_suspended(tBTA_AV_SUSPEND * p_av_suspend)121 void btif_a2dp_on_suspended(tBTA_AV_SUSPEND* p_av_suspend) {
122   LOG_INFO("%s: ## ON A2DP SUSPENDED ## p_av_suspend=%p", __func__,
123            p_av_suspend);
124   if (btif_av_get_peer_sep() == AVDT_TSEP_SRC) {
125     btif_a2dp_sink_on_suspended(p_av_suspend);
126     return;
127   }
128   if (bluetooth::audio::a2dp::is_hal_enabled() ||
129       !btif_av_is_a2dp_offload_running()) {
130     btif_a2dp_source_on_suspended(p_av_suspend);
131   }
132 }
133 
btif_a2dp_on_offload_started(const RawAddress & peer_addr,tBTA_AV_STATUS status)134 void btif_a2dp_on_offload_started(const RawAddress& peer_addr,
135                                   tBTA_AV_STATUS status) {
136   tA2DP_CTRL_ACK ack;
137   LOG_INFO("%s: peer %s status %d", __func__, peer_addr.ToString().c_str(),
138            status);
139 
140   switch (status) {
141     case BTA_AV_SUCCESS:
142       ack = A2DP_CTRL_ACK_SUCCESS;
143       break;
144     case BTA_AV_FAIL_RESOURCES:
145       LOG_ERROR("%s: peer %s FAILED UNSUPPORTED", __func__,
146                 peer_addr.ToString().c_str());
147       ack = A2DP_CTRL_ACK_UNSUPPORTED;
148       break;
149     default:
150       LOG_ERROR("%s: peer %s FAILED: status = %d", __func__,
151                 peer_addr.ToString().c_str(), status);
152       ack = A2DP_CTRL_ACK_FAILURE;
153       break;
154   }
155   if (btif_av_is_a2dp_offload_running()) {
156     if (ack != BTA_AV_SUCCESS && btif_av_stream_started_ready()) {
157       // Offload request will return with failure from btif_av sm if
158       // suspend is triggered for remote start. Disconnect only if SoC
159       // returned failure for offload VSC
160       LOG_ERROR("%s: peer %s offload start failed", __func__,
161                 peer_addr.ToString().c_str());
162       btif_av_src_disconnect_sink(peer_addr);
163     }
164   }
165   if (bluetooth::audio::a2dp::is_hal_enabled()) {
166     bluetooth::audio::a2dp::ack_stream_started(ack);
167   } else {
168     btif_a2dp_command_ack(ack);
169   }
170 }
171 
btif_debug_a2dp_dump(int fd)172 void btif_debug_a2dp_dump(int fd) {
173   btif_a2dp_source_debug_dump(fd);
174   btif_a2dp_sink_debug_dump(fd);
175   btif_a2dp_codec_debug_dump(fd);
176 }
177