1 /******************************************************************************
2  *
3  *  Copyright 2009-2012 Broadcom Corporation
4  *
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:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  ******************************************************************************/
18 
19 /**
20  * BTIF AV API functions accessed internally.
21  */
22 
23 #ifndef BTIF_AV_H
24 #define BTIF_AV_H
25 
26 #include <cstdint>
27 #include <vector>
28 
29 #include "bta/include/bta_av_api.h"
30 #include "include/hardware/bt_av.h"
31 #include "types/raw_address.h"
32 
33 /** Callback for connection state change.
34  *  state will have one of the values from btav_connection_state_t
35  */
36 typedef void (*btav_connection_state_callback)(const RawAddress& bd_addr,
37                                                btav_connection_state_t state,
38                                                const btav_error_t& error);
39 
40 /** Callback for audiopath state change.
41  *  state will have one of the values from btav_audio_state_t
42  */
43 typedef void (*btav_audio_state_callback)(const RawAddress& bd_addr, btav_audio_state_t state);
44 
45 /** Callback for audio configuration change.
46  *  Used only for the A2DP Source interface.
47  */
48 typedef void (*btav_audio_source_config_callback)(
49         const RawAddress& bd_addr, btav_a2dp_codec_config_t codec_config,
50         std::vector<btav_a2dp_codec_config_t> codecs_local_capabilities,
51         std::vector<btav_a2dp_codec_config_t> codecs_selectable_capabilities);
52 
53 /** Callback for audio configuration change.
54  *  Used only for the A2DP Sink interface.
55  *  sample_rate: sample rate in Hz
56  *  channel_count: number of channels (1 for mono, 2 for stereo)
57  */
58 typedef void (*btav_audio_sink_config_callback)(const RawAddress& bd_addr, uint32_t sample_rate,
59                                                 uint8_t channel_count);
60 
61 /** Callback for querying whether the mandatory codec is more preferred.
62  *  Used only for the A2DP Source interface.
63  *  Return true if optional codecs are not preferred.
64  */
65 typedef bool (*btav_mandatory_codec_preferred_callback)(const RawAddress& bd_addr);
66 
67 /** BT-AV A2DP Source callback structure. */
68 typedef struct {
69   /** set to sizeof(btav_source_callbacks_t) */
70   size_t size;
71   btav_connection_state_callback connection_state_cb;
72   btav_audio_state_callback audio_state_cb;
73   btav_audio_source_config_callback audio_config_cb;
74   btav_mandatory_codec_preferred_callback mandatory_codec_preferred_cb;
75 } btav_source_callbacks_t;
76 
77 /** BT-AV A2DP Sink callback structure. */
78 typedef struct {
79   /** set to sizeof(btav_sink_callbacks_t) */
80   size_t size;
81   btav_connection_state_callback connection_state_cb;
82   btav_audio_state_callback audio_state_cb;
83   btav_audio_sink_config_callback audio_config_cb;
84 } btav_sink_callbacks_t;
85 
86 /* Interface methods for the A2DP source stack. */
87 
88 bt_status_t btif_av_source_init(btav_source_callbacks_t* callbacks, int max_connected_audio_devices,
89                                 const std::vector<btav_a2dp_codec_config_t>& codec_priorities,
90                                 const std::vector<btav_a2dp_codec_config_t>& offloading_preference,
91                                 std::vector<btav_a2dp_codec_info_t>* supported_codecs);
92 bt_status_t btif_av_source_connect(const RawAddress& peer_address);
93 bt_status_t btif_av_source_disconnect(const RawAddress& peer_address);
94 bt_status_t btif_av_source_set_silence_device(const RawAddress& peer_address, bool silence);
95 bt_status_t btif_av_source_set_active_device(const RawAddress& peer_address);
96 bt_status_t btif_av_source_set_codec_config_preference(
97         const RawAddress& peer_address, std::vector<btav_a2dp_codec_config_t> codec_preferences);
98 void btif_av_source_cleanup();
99 
100 /* Interface methods for the A2DP sink stack. */
101 
102 bt_status_t btif_av_sink_init(btav_sink_callbacks_t* callbacks, int max_connected_audio_devices);
103 bt_status_t btif_av_sink_connect(const RawAddress& peer_address);
104 bt_status_t btif_av_sink_disconnect(const RawAddress& peer_address);
105 void btif_av_sink_cleanup();
106 void btif_av_sink_set_audio_focus_state(int focus_state);
107 void btif_av_sink_set_audio_track_gain(float gain);
108 bt_status_t btif_av_sink_set_active_device(const RawAddress& peer_address);
109 
110 /**
111  * Enum to represent the type of local a2dp profile.
112  */
113 enum class A2dpType { kSource, kSink, kUnknown };
114 
115 /**
116  * When the local device is A2DP source, get the address of the active peer.
117  */
118 RawAddress btif_av_source_active_peer(void);
119 
120 /**
121  * When the local device is A2DP sink, get the address of the active peer.
122  */
123 RawAddress btif_av_sink_active_peer(void);
124 
125 /**
126  * Check whether A2DP Sink is enabled.
127  */
128 bool btif_av_is_sink_enabled(void);
129 
130 /**
131  * Check whether A2DP Source is enabled.
132  */
133 bool btif_av_is_source_enabled(void);
134 
135 /**
136  * Start streaming.
137  * @param local_a2dp_type type of local a2dp profile.
138  */
139 void btif_av_stream_start(const A2dpType local_a2dp_type);
140 
141 /**
142  * Start streaming with latency setting.
143  */
144 void btif_av_stream_start_with_latency(bool use_latency_mode);
145 
146 /**
147  * Stop streaming.
148  *
149  * @param peer_address the peer address or RawAddress::kEmpty to stop all peers
150  */
151 void btif_av_stream_stop(const RawAddress& peer_address);
152 
153 /**
154  * Suspend streaming.
155  */
156 void btif_av_stream_suspend(void);
157 
158 /**
159  * Start offload streaming.
160  */
161 void btif_av_stream_start_offload(void);
162 
163 /**
164  * Check whether ready to start the A2DP stream.
165  * @param local_a2dp_type type of local a2dp profile.
166  */
167 bool btif_av_stream_ready(const A2dpType local_a2dp_type);
168 
169 /**
170  * Check whether the A2DP stream is in started state and ready
171  * for media start.
172  * @param local_a2dp_type type of local a2dp profile.
173  */
174 bool btif_av_stream_started_ready(const A2dpType local_a2dp_type);
175 
176 /**
177  * Check whether there is a connected peer (either Source or Sink)
178  * @param local_a2dp_type type of local a2dp profile.
179  */
180 bool btif_av_is_connected(const A2dpType local_a2dp_type);
181 
182 /**
183  * Get the Stream Endpoint Type of the Active peer.
184  * @param local_a2dp_type type of local a2dp profile.
185  *
186  * @return the stream endpoint type: either AVDT_TSEP_SRC or AVDT_TSEP_SNK
187  */
188 uint8_t btif_av_get_peer_sep(const A2dpType local_a2dp_type);
189 
190 /**
191  * Clear the remote suspended flag for the active peer.
192  * @param local_a2dp_type type of local a2dp profile.
193  */
194 void btif_av_clear_remote_suspend_flag(const A2dpType local_a2dp_type);
195 
196 /**
197  * Check whether the connected A2DP peer supports EDR.
198  *
199  * The value can be provided only if the remote peer is connected.
200  * Otherwise, the answer will be always false.
201  *
202  * @param peer_address the peer address
203  * @param local_a2dp_type type of local a2dp profile.
204  * @return true if the remote peer is capable of EDR
205  */
206 bool btif_av_is_peer_edr(const RawAddress& peer_address, const A2dpType local_a2dp_type);
207 
208 /**
209  * Check whether the connected A2DP peer supports 3 Mbps EDR.
210  *
211  * The value can be provided only if the remote peer is connected.
212  * Otherwise, the answer will be always false.
213  *
214  * @param peer_address the peer address
215  * @param local_a2dp_type type of local a2dp profile.
216  * @return true if the remote peer is capable of EDR and supports 3 Mbps
217  */
218 bool btif_av_peer_supports_3mbps(const RawAddress& peer_address, const A2dpType local_a2dp_type);
219 
220 /**
221  * Check whether the mandatory codec is more preferred for this peer.
222  *
223  * @param peer_address the target peer address
224  * @param local_a2dp_type type of local a2dp profile.
225  * @return true if optional codecs are not preferred to be used
226  */
227 bool btif_av_peer_prefers_mandatory_codec(const RawAddress& peer_address,
228                                           const A2dpType local_a2dp_type);
229 
230 /**
231  * Report A2DP Source Codec State for a peer.
232  *
233  * @param peer_address the address of the peer to report
234  * @param codec_config the codec config to report
235  * @param codecs_local_capabilities the codecs local capabilities to report
236  * @param codecs_selectable_capabilities the codecs selectable capabilities
237  * to report
238  */
239 void btif_av_report_source_codec_state(
240         const RawAddress& peer_address, const btav_a2dp_codec_config_t& codec_config,
241         const std::vector<btav_a2dp_codec_config_t>& codecs_local_capabilities,
242         const std::vector<btav_a2dp_codec_config_t>& codecs_selectable_capabilities);
243 
244 /**
245  * Initialize / shut down the A2DP Source service.
246  *
247  * @param enable true to enable the A2DP Source service, false to disable it
248  * @return BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
249  */
250 bt_status_t btif_av_source_execute_service(bool enable);
251 
252 /**
253  * Initialize / shut down the A2DP Sink service.
254  *
255  * @param enable true to enable the A2DP Sink service, false to disable it
256  * @return BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
257  */
258 bt_status_t btif_av_sink_execute_service(bool enable);
259 
260 /**
261  * Peer ACL disconnected.
262  *
263  * @param peer_address the disconnected peer address
264  * @param local_a2dp_type type of local a2dp profile.
265  */
266 void btif_av_acl_disconnected(const RawAddress& peer_address, const A2dpType local_a2dp_type);
267 
268 /**
269  * Dump debug-related information for the BTIF AV module.
270  *
271  * @param fd the file descriptor to use for writing the ASCII formatted
272  * information
273  */
274 void btif_debug_av_dump(int fd);
275 
276 /**
277  * Set the audio delay for the stream.
278  *
279  * @param peer_address the address of the peer to report
280  * @param delay the delay to set in units of 1/10ms
281  * @param local_a2dp_type type of local a2dp profile.
282  */
283 void btif_av_set_audio_delay(const RawAddress& peer_address, uint16_t delay,
284                              const A2dpType local_a2dp_type);
285 
286 /**
287  * Get the audio delay for the stream.
288  * @param local_a2dp_type type of local a2dp profile.
289  */
290 uint16_t btif_av_get_audio_delay(const A2dpType local_a2dp_type);
291 
292 /**
293  *  check A2DP offload support enabled
294  *  @param  none
295  */
296 bool btif_av_is_a2dp_offload_enabled(void);
297 
298 /**
299  *  check A2DP offload enabled and running
300  *  @param  none
301  */
302 bool btif_av_is_a2dp_offload_running(void);
303 
304 /**
305  * Check whether peer device is silenced
306  *
307  * @param peer_address to check
308  *
309  */
310 bool btif_av_is_peer_silenced(const RawAddress& peer_address);
311 
312 /**
313  * check the a2dp connect status
314  *
315  * @param peer_address : checked device address
316  * @param local_a2dp_type type of local a2dp profile.
317  *
318  */
319 bool btif_av_is_connected_addr(const RawAddress& peer_address, const A2dpType local_a2dp_type);
320 
321 /**
322  * Set the dynamic audio buffer size
323  *
324  * @param dynamic_audio_buffer_size to set
325  */
326 void btif_av_set_dynamic_audio_buffer_size(uint8_t dynamic_audio_buffer_size);
327 
328 /**
329  * Enable/disable the low latency
330  *
331  * @param is_low_latency to set
332  */
333 void btif_av_set_low_latency(bool is_low_latency);
334 
335 /**
336  * Initiate an AV connection after 3s timeout to peer audio sink
337  * @param handle bta handle
338  * @param peer_addr peer address
339  */
340 void btif_av_connect_sink_delayed(uint8_t handle, const RawAddress& peer_address);
341 
342 /**
343  * Check whether A2DP Source is enabled.
344  */
345 bool btif_av_both_enable(void);
346 bool btif_av_src_sink_coexist_enabled(void);
347 bool btif_av_peer_is_connected_sink(const RawAddress& peer_address);
348 bool btif_av_peer_is_connected_source(const RawAddress& peer_address);
349 bool btif_av_peer_is_sink(const RawAddress& peer_address);
350 bool btif_av_peer_is_source(const RawAddress& peer_address);
351 const RawAddress& btif_av_find_by_handle(tBTA_AV_HNDL bta_handle);
352 
353 #endif /* BTIF_AV_H */
354