• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_INCLUDE_BT_AV_H
18 #define ANDROID_INCLUDE_BT_AV_H
19 
20 #include <hardware/bluetooth.h>
21 #include <raw_address.h>
22 
23 #include <optional>
24 #include <vector>
25 
26 __BEGIN_DECLS
27 
28 /* Bluetooth AV connection states */
29 typedef enum {
30   BTAV_CONNECTION_STATE_DISCONNECTED = 0,
31   BTAV_CONNECTION_STATE_CONNECTING,
32   BTAV_CONNECTION_STATE_CONNECTED,
33   BTAV_CONNECTION_STATE_DISCONNECTING
34 } btav_connection_state_t;
35 
36 /* Bluetooth AV datapath states */
37 typedef enum {
38   BTAV_AUDIO_STATE_REMOTE_SUSPEND = 0,
39   BTAV_AUDIO_STATE_STOPPED,
40   BTAV_AUDIO_STATE_STARTED,
41 } btav_audio_state_t;
42 
43 /*
44  * Enum values for each A2DP supported codec.
45  * There should be a separate entry for each A2DP codec that is supported
46  * for encoding (SRC), and for decoding purpose (SINK).
47  */
48 typedef enum {
49   BTAV_A2DP_CODEC_INDEX_SOURCE_MIN = 0,
50 
51   // Add an entry for each source codec here.
52   // NOTE: The values should be same as those listed in the following file:
53   //   BluetoothCodecConfig.java
54   BTAV_A2DP_CODEC_INDEX_SOURCE_SBC = 0,
55   BTAV_A2DP_CODEC_INDEX_SOURCE_AAC,
56   BTAV_A2DP_CODEC_INDEX_SOURCE_APTX,
57   BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD,
58   BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC,
59   BTAV_A2DP_CODEC_INDEX_SOURCE_LC3,
60   BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS,
61 
62   BTAV_A2DP_CODEC_INDEX_SOURCE_MAX,
63 
64   BTAV_A2DP_CODEC_INDEX_SINK_MIN = BTAV_A2DP_CODEC_INDEX_SOURCE_MAX,
65 
66   // Add an entry for each sink codec here
67   BTAV_A2DP_CODEC_INDEX_SINK_SBC = BTAV_A2DP_CODEC_INDEX_SINK_MIN,
68   BTAV_A2DP_CODEC_INDEX_SINK_AAC,
69   BTAV_A2DP_CODEC_INDEX_SINK_LDAC,
70   BTAV_A2DP_CODEC_INDEX_SINK_OPUS,
71 
72   BTAV_A2DP_CODEC_INDEX_SINK_MAX,
73 
74   BTAV_A2DP_CODEC_INDEX_MIN = BTAV_A2DP_CODEC_INDEX_SOURCE_MIN,
75   BTAV_A2DP_CODEC_INDEX_MAX = BTAV_A2DP_CODEC_INDEX_SINK_MAX
76 } btav_a2dp_codec_index_t;
77 
78 typedef enum {
79   // Disable the codec.
80   // NOTE: This value can be used only during initialization when
81   // function btav_source_interface_t::init() is called.
82   BTAV_A2DP_CODEC_PRIORITY_DISABLED = -1,
83 
84   // Reset the codec priority to its default value.
85   BTAV_A2DP_CODEC_PRIORITY_DEFAULT = 0,
86 
87   // Highest codec priority.
88   BTAV_A2DP_CODEC_PRIORITY_HIGHEST = 1000 * 1000
89 } btav_a2dp_codec_priority_t;
90 
91 typedef enum {
92   BTAV_A2DP_CODEC_SAMPLE_RATE_NONE = 0x0,
93   BTAV_A2DP_CODEC_SAMPLE_RATE_44100 = 0x1 << 0,
94   BTAV_A2DP_CODEC_SAMPLE_RATE_48000 = 0x1 << 1,
95   BTAV_A2DP_CODEC_SAMPLE_RATE_88200 = 0x1 << 2,
96   BTAV_A2DP_CODEC_SAMPLE_RATE_96000 = 0x1 << 3,
97   BTAV_A2DP_CODEC_SAMPLE_RATE_176400 = 0x1 << 4,
98   BTAV_A2DP_CODEC_SAMPLE_RATE_192000 = 0x1 << 5,
99   BTAV_A2DP_CODEC_SAMPLE_RATE_16000 = 0x1 << 6,
100   BTAV_A2DP_CODEC_SAMPLE_RATE_24000 = 0x1 << 7
101 } btav_a2dp_codec_sample_rate_t;
102 
103 typedef enum {
104   BTAV_A2DP_CODEC_FRAME_SIZE_NONE = 0x0,
105   BTAV_A2DP_CODEC_FRAME_SIZE_20MS = 0x1 << 0,
106   BTAV_A2DP_CODEC_FRAME_SIZE_15MS = 0x1 << 1,
107   BTAV_A2DP_CODEC_FRAME_SIZE_10MS = 0x1 << 2,
108   BTAV_A2DP_CODEC_FRAME_SIZE_75MS = 0x1 << 3,
109 } btav_a2dp_codec_frame_size_t;
110 
111 typedef enum {
112   BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE = 0x0,
113   BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 = 0x1 << 0,
114   BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24 = 0x1 << 1,
115   BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32 = 0x1 << 2
116 } btav_a2dp_codec_bits_per_sample_t;
117 
118 typedef enum {
119   BTAV_A2DP_CODEC_CHANNEL_MODE_NONE = 0x0,
120   BTAV_A2DP_CODEC_CHANNEL_MODE_MONO = 0x1 << 0,
121   BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO = 0x1 << 1
122 } btav_a2dp_codec_channel_mode_t;
123 
124 typedef enum {
125   BTAV_A2DP_SCMST_DISABLED = 0x00,
126   BTAV_A2DP_SCMST_ENABLED = 0x01
127 } btav_a2dp_scmst_enable_status_t;
128 
129 /*
130  * Structure for representing codec capability or configuration.
131  * It is used for configuring A2DP codec preference, and for reporting back
132  * current configuration or codec capability.
133  * For codec capability, fields "sample_rate", "bits_per_sample" and
134  * "channel_mode" can contain bit-masks with all supported features.
135  */
136 struct btav_a2dp_codec_config_t {
137   btav_a2dp_codec_index_t codec_type;
138   btav_a2dp_codec_priority_t
139       codec_priority;  // Codec selection priority
140                        // relative to other codecs: larger value
141                        // means higher priority. If 0, reset to
142                        // default.
143   btav_a2dp_codec_sample_rate_t sample_rate;
144   btav_a2dp_codec_bits_per_sample_t bits_per_sample;
145   btav_a2dp_codec_channel_mode_t channel_mode;
146   int64_t codec_specific_1;  // Codec-specific value 1
147   int64_t codec_specific_2;  // Codec-specific value 2
148   int64_t codec_specific_3;  // Codec-specific value 3
149   int64_t codec_specific_4;  // Codec-specific value 4
150 
ToStringbtav_a2dp_codec_config_t151   std::string ToString() const {
152     std::string codec_name_str;
153 
154     switch (codec_type) {
155       case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
156         codec_name_str = "SBC";
157         break;
158       case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
159         codec_name_str = "AAC";
160         break;
161       case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX:
162         codec_name_str = "aptX";
163         break;
164       case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD:
165         codec_name_str = "aptX HD";
166         break;
167       case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC:
168         codec_name_str = "LDAC";
169         break;
170       case BTAV_A2DP_CODEC_INDEX_SINK_SBC:
171         codec_name_str = "SBC (Sink)";
172         break;
173       case BTAV_A2DP_CODEC_INDEX_SINK_AAC:
174         codec_name_str = "AAC (Sink)";
175         break;
176       case BTAV_A2DP_CODEC_INDEX_SINK_LDAC:
177         codec_name_str = "LDAC (Sink)";
178         break;
179       case BTAV_A2DP_CODEC_INDEX_SOURCE_LC3:
180         codec_name_str = "LC3";
181         break;
182       case BTAV_A2DP_CODEC_INDEX_SINK_OPUS:
183         codec_name_str = "Opus (Sink)";
184         break;
185       case BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS:
186         codec_name_str = "Opus";
187         break;
188       case BTAV_A2DP_CODEC_INDEX_MAX:
189         codec_name_str = "Unknown(CODEC_INDEX_MAX)";
190         break;
191     }
192 
193     std::string sample_rate_str;
194     AppendCapability(sample_rate_str,
195                      (sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE), "NONE");
196     AppendCapability(sample_rate_str,
197                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_44100),
198                      "44100");
199     AppendCapability(sample_rate_str,
200                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_48000),
201                      "48000");
202     AppendCapability(sample_rate_str,
203                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_88200),
204                      "88200");
205     AppendCapability(sample_rate_str,
206                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_96000),
207                      "96000");
208     AppendCapability(sample_rate_str,
209                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_176400),
210                      "176400");
211     AppendCapability(sample_rate_str,
212                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_192000),
213                      "192000");
214     AppendCapability(sample_rate_str,
215                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_16000),
216                      "16000");
217     AppendCapability(sample_rate_str,
218                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_24000),
219                      "24000");
220 
221     std::string bits_per_sample_str;
222     AppendCapability(bits_per_sample_str,
223                      (bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE),
224                      "NONE");
225     AppendCapability(bits_per_sample_str,
226                      (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16),
227                      "16");
228     AppendCapability(bits_per_sample_str,
229                      (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24),
230                      "24");
231     AppendCapability(bits_per_sample_str,
232                      (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32),
233                      "32");
234 
235     std::string channel_mode_str;
236     AppendCapability(channel_mode_str,
237                      (channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE),
238                      "NONE");
239     AppendCapability(channel_mode_str,
240                      (channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_MONO),
241                      "MONO");
242     AppendCapability(channel_mode_str,
243                      (channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO),
244                      "STEREO");
245 
246     return "codec: " + codec_name_str +
247            " priority: " + std::to_string(codec_priority) +
248            " sample_rate: " + sample_rate_str +
249            " bits_per_sample: " + bits_per_sample_str +
250            " channel_mode: " + channel_mode_str +
251            " codec_specific_1: " + std::to_string(codec_specific_1) +
252            " codec_specific_2: " + std::to_string(codec_specific_2) +
253            " codec_specific_3: " + std::to_string(codec_specific_3) +
254            " codec_specific_4: " + std::to_string(codec_specific_4);
255   }
256 
257  private:
AppendCapabilitybtav_a2dp_codec_config_t258   static std::string AppendCapability(std::string& result, bool append,
259                                       const std::string& name) {
260     if (!append) return result;
261     if (!result.empty()) result += "|";
262     result += name;
263     return result;
264   }
265 };
266 
267 typedef struct {
268   btav_a2dp_scmst_enable_status_t enable_status;
269   uint8_t cp_header;
270 } btav_a2dp_scmst_info_t;
271 
272 typedef struct {
273   bt_status_t status;
274   uint8_t error_code;
275   std::optional<std::string> error_msg;
276 } btav_error_t;
277 
278 /** Callback for connection state change.
279  *  state will have one of the values from btav_connection_state_t
280  */
281 typedef void (*btav_connection_state_callback)(const RawAddress& bd_addr,
282                                                btav_connection_state_t state,
283                                                const btav_error_t& error);
284 
285 /** Callback for audiopath state change.
286  *  state will have one of the values from btav_audio_state_t
287  */
288 typedef void (*btav_audio_state_callback)(const RawAddress& bd_addr,
289                                           btav_audio_state_t state);
290 
291 /** Callback for audio configuration change.
292  *  Used only for the A2DP Source interface.
293  */
294 typedef void (*btav_audio_source_config_callback)(
295     const RawAddress& bd_addr, btav_a2dp_codec_config_t codec_config,
296     std::vector<btav_a2dp_codec_config_t> codecs_local_capabilities,
297     std::vector<btav_a2dp_codec_config_t> codecs_selectable_capabilities);
298 
299 /** Callback for audio configuration change.
300  *  Used only for the A2DP Sink interface.
301  *  sample_rate: sample rate in Hz
302  *  channel_count: number of channels (1 for mono, 2 for stereo)
303  */
304 typedef void (*btav_audio_sink_config_callback)(const RawAddress& bd_addr,
305                                                 uint32_t sample_rate,
306                                                 uint8_t channel_count);
307 
308 /** Callback for querying whether the mandatory codec is more preferred.
309  *  Used only for the A2DP Source interface.
310  *  Return true if optional codecs are not preferred.
311  */
312 typedef bool (*btav_mandatory_codec_preferred_callback)(
313     const RawAddress& bd_addr);
314 
315 /** BT-AV A2DP Source callback structure. */
316 typedef struct {
317   /** set to sizeof(btav_source_callbacks_t) */
318   size_t size;
319   btav_connection_state_callback connection_state_cb;
320   btav_audio_state_callback audio_state_cb;
321   btav_audio_source_config_callback audio_config_cb;
322   btav_mandatory_codec_preferred_callback mandatory_codec_preferred_cb;
323 } btav_source_callbacks_t;
324 
325 /** BT-AV A2DP Sink callback structure. */
326 typedef struct {
327   /** set to sizeof(btav_sink_callbacks_t) */
328   size_t size;
329   btav_connection_state_callback connection_state_cb;
330   btav_audio_state_callback audio_state_cb;
331   btav_audio_sink_config_callback audio_config_cb;
332 } btav_sink_callbacks_t;
333 
334 /**
335  * NOTE:
336  *
337  * 1. AVRCP 1.0 shall be supported initially. AVRCP passthrough commands
338  *    shall be handled internally via uinput
339  *
340  * 2. A2DP data path shall be handled via a socket pipe between the AudioFlinger
341  *    android_audio_hw library and the Bluetooth stack.
342  *
343  */
344 
345 /** Represents the standard BT-AV A2DP Source interface.
346  */
347 typedef struct {
348   /** set to sizeof(btav_source_interface_t) */
349   size_t size;
350   /**
351    * Register the BtAv callbacks.
352    */
353   bt_status_t (*init)(
354       btav_source_callbacks_t* callbacks, int max_connected_audio_devices,
355       const std::vector<btav_a2dp_codec_config_t>& codec_priorities,
356       const std::vector<btav_a2dp_codec_config_t>& offloading_preference);
357 
358   /** connect to headset */
359   bt_status_t (*connect)(const RawAddress& bd_addr);
360 
361   /** dis-connect from headset */
362   bt_status_t (*disconnect)(const RawAddress& bd_addr);
363 
364   /** sets the connected device silence state */
365   bt_status_t (*set_silence_device)(const RawAddress& bd_addr, bool silence);
366 
367   /** sets the connected device as active */
368   bt_status_t (*set_active_device)(const RawAddress& bd_addr);
369 
370   /** configure the codecs settings preferences */
371   bt_status_t (*config_codec)(
372       const RawAddress& bd_addr,
373       std::vector<btav_a2dp_codec_config_t> codec_preferences);
374 
375   /** Closes the interface. */
376   void (*cleanup)(void);
377 
378 } btav_source_interface_t;
379 
380 /** Represents the standard BT-AV A2DP Sink interface.
381  */
382 typedef struct {
383   /** set to sizeof(btav_sink_interface_t) */
384   size_t size;
385   /**
386    * Register the BtAv callbacks
387    */
388   bt_status_t (*init)(btav_sink_callbacks_t* callbacks,
389                       int max_connected_audio_devices);
390 
391   /** connect to headset */
392   bt_status_t (*connect)(const RawAddress& bd_addr);
393 
394   /** dis-connect from headset */
395   bt_status_t (*disconnect)(const RawAddress& bd_addr);
396 
397   /** Closes the interface. */
398   void (*cleanup)(void);
399 
400   /** Sends Audio Focus State. */
401   void (*set_audio_focus_state)(int focus_state);
402 
403   /** Sets the audio track gain. */
404   void (*set_audio_track_gain)(float gain);
405 
406   /** sets the connected device as active */
407   bt_status_t (*set_active_device)(const RawAddress& bd_addr);
408 } btav_sink_interface_t;
409 
410 __END_DECLS
411 
412 #endif /* ANDROID_INCLUDE_BT_AV_H */
413