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