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 /*
112  * Structure for representing codec capability or configuration.
113  * It is used for configuring A2DP codec preference, and for reporting back
114  * current configuration or codec capability.
115  * For codec capability, fields "sample_rate", "bits_per_sample" and
116  * "channel_mode" can contain bit-masks with all supported features.
117  */
118 typedef struct {
119   btav_a2dp_codec_index_t codec_type;
120   btav_a2dp_codec_priority_t
121       codec_priority;  // Codec selection priority
122                        // relative to other codecs: larger value
123                        // means higher priority. If 0, reset to
124                        // default.
125   btav_a2dp_codec_sample_rate_t sample_rate;
126   btav_a2dp_codec_bits_per_sample_t bits_per_sample;
127   btav_a2dp_codec_channel_mode_t channel_mode;
128   int64_t codec_specific_1;  // Codec-specific value 1
129   int64_t codec_specific_2;  // Codec-specific value 2
130   int64_t codec_specific_3;  // Codec-specific value 3
131   int64_t codec_specific_4;  // Codec-specific value 4
132 
ToString__anona323586a0808133   std::string ToString() const {
134     std::string codec_name_str;
135 
136     switch (codec_type) {
137       case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
138         codec_name_str = "SBC";
139         break;
140       case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
141         codec_name_str = "AAC";
142         break;
143       case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX:
144         codec_name_str = "aptX";
145         break;
146       case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD:
147         codec_name_str = "aptX HD";
148         break;
149       case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC:
150         codec_name_str = "LDAC";
151         break;
152       case BTAV_A2DP_CODEC_INDEX_SINK_SBC:
153         codec_name_str = "SBC (Sink)";
154         break;
155       case BTAV_A2DP_CODEC_INDEX_SINK_AAC:
156         codec_name_str = "AAC (Sink)";
157         break;
158       case BTAV_A2DP_CODEC_INDEX_SINK_LDAC:
159         codec_name_str = "LDAC (Sink)";
160         break;
161       case BTAV_A2DP_CODEC_INDEX_MAX:
162         codec_name_str = "Unknown(CODEC_INDEX_MAX)";
163         break;
164     }
165 
166     std::string sample_rate_str;
167     AppendCapability(sample_rate_str,
168                      (sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE), "NONE");
169     AppendCapability(sample_rate_str,
170                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_44100),
171                      "44100");
172     AppendCapability(sample_rate_str,
173                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_48000),
174                      "48000");
175     AppendCapability(sample_rate_str,
176                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_88200),
177                      "88200");
178     AppendCapability(sample_rate_str,
179                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_96000),
180                      "96000");
181     AppendCapability(sample_rate_str,
182                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_176400),
183                      "176400");
184     AppendCapability(sample_rate_str,
185                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_192000),
186                      "192000");
187     AppendCapability(sample_rate_str,
188                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_16000),
189                      "16000");
190     AppendCapability(sample_rate_str,
191                      (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_24000),
192                      "24000");
193 
194     std::string bits_per_sample_str;
195     AppendCapability(bits_per_sample_str,
196                      (bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE),
197                      "NONE");
198     AppendCapability(bits_per_sample_str,
199                      (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16),
200                      "16");
201     AppendCapability(bits_per_sample_str,
202                      (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24),
203                      "24");
204     AppendCapability(bits_per_sample_str,
205                      (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32),
206                      "32");
207 
208     std::string channel_mode_str;
209     AppendCapability(channel_mode_str,
210                      (channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE),
211                      "NONE");
212     AppendCapability(channel_mode_str,
213                      (channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_MONO),
214                      "MONO");
215     AppendCapability(channel_mode_str,
216                      (channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO),
217                      "STEREO");
218 
219     return "codec: " + codec_name_str +
220            " priority: " + std::to_string(codec_priority) +
221            " sample_rate: " + sample_rate_str +
222            " bits_per_sample: " + bits_per_sample_str +
223            " channel_mode: " + channel_mode_str +
224            " codec_specific_1: " + std::to_string(codec_specific_1) +
225            " codec_specific_2: " + std::to_string(codec_specific_2) +
226            " codec_specific_3: " + std::to_string(codec_specific_3) +
227            " codec_specific_4: " + std::to_string(codec_specific_4);
228   }
229 
230  private:
AppendCapability__anona323586a0808231   static std::string AppendCapability(std::string& result, bool append,
232                                       const std::string& name) {
233     if (!append) return result;
234     if (!result.empty()) result += "|";
235     result += name;
236     return result;
237   }
238 } btav_a2dp_codec_config_t;
239 
240 /** Callback for connection state change.
241  *  state will have one of the values from btav_connection_state_t
242  */
243 typedef void (*btav_connection_state_callback)(const RawAddress& bd_addr,
244                                                btav_connection_state_t state);
245 
246 /** Callback for audiopath state change.
247  *  state will have one of the values from btav_audio_state_t
248  */
249 typedef void (*btav_audio_state_callback)(const RawAddress& bd_addr,
250                                           btav_audio_state_t state);
251 
252 /** Callback for audio configuration change.
253  *  Used only for the A2DP Source interface.
254  */
255 typedef void (*btav_audio_source_config_callback)(
256     const RawAddress& bd_addr, btav_a2dp_codec_config_t codec_config,
257     std::vector<btav_a2dp_codec_config_t> codecs_local_capabilities,
258     std::vector<btav_a2dp_codec_config_t> codecs_selectable_capabilities);
259 
260 /** Callback for audio configuration change.
261  *  Used only for the A2DP Sink interface.
262  *  sample_rate: sample rate in Hz
263  *  channel_count: number of channels (1 for mono, 2 for stereo)
264  */
265 typedef void (*btav_audio_sink_config_callback)(const RawAddress& bd_addr,
266                                                 uint32_t sample_rate,
267                                                 uint8_t channel_count);
268 
269 /** Callback for querying whether the mandatory codec is more preferred.
270  *  Used only for the A2DP Source interface.
271  *  Return true if optional codecs are not preferred.
272  */
273 typedef bool (*btav_mandatory_codec_preferred_callback)(
274     const RawAddress& bd_addr);
275 
276 /** BT-AV A2DP Source callback structure. */
277 typedef struct {
278   /** set to sizeof(btav_source_callbacks_t) */
279   size_t size;
280   btav_connection_state_callback connection_state_cb;
281   btav_audio_state_callback audio_state_cb;
282   btav_audio_source_config_callback audio_config_cb;
283   btav_mandatory_codec_preferred_callback mandatory_codec_preferred_cb;
284 } btav_source_callbacks_t;
285 
286 /** BT-AV A2DP Sink callback structure. */
287 typedef struct {
288   /** set to sizeof(btav_sink_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_sink_config_callback audio_config_cb;
293 } btav_sink_callbacks_t;
294 
295 /**
296  * NOTE:
297  *
298  * 1. AVRCP 1.0 shall be supported initially. AVRCP passthrough commands
299  *    shall be handled internally via uinput
300  *
301  * 2. A2DP data path shall be handled via a socket pipe between the AudioFlinger
302  *    android_audio_hw library and the Bluetooth stack.
303  *
304  */
305 
306 /** Represents the standard BT-AV A2DP Source interface.
307  */
308 typedef struct {
309   /** set to sizeof(btav_source_interface_t) */
310   size_t size;
311   /**
312    * Register the BtAv callbacks.
313    */
314   bt_status_t (*init)(
315       btav_source_callbacks_t* callbacks, int max_connected_audio_devices,
316       const std::vector<btav_a2dp_codec_config_t>& codec_priorities,
317       const std::vector<btav_a2dp_codec_config_t>& offloading_preference);
318 
319   /** connect to headset */
320   bt_status_t (*connect)(const RawAddress& bd_addr);
321 
322   /** dis-connect from headset */
323   bt_status_t (*disconnect)(const RawAddress& bd_addr);
324 
325   /** sets the connected device silence state */
326   bt_status_t (*set_silence_device)(const RawAddress& bd_addr, bool silence);
327 
328   /** sets the connected device as active */
329   bt_status_t (*set_active_device)(const RawAddress& bd_addr);
330 
331   /** configure the codecs settings preferences */
332   bt_status_t (*config_codec)(
333       const RawAddress& bd_addr,
334       std::vector<btav_a2dp_codec_config_t> codec_preferences);
335 
336   /** Closes the interface. */
337   void (*cleanup)(void);
338 
339 } btav_source_interface_t;
340 
341 /** Represents the standard BT-AV A2DP Sink interface.
342  */
343 typedef struct {
344   /** set to sizeof(btav_sink_interface_t) */
345   size_t size;
346   /**
347    * Register the BtAv callbacks
348    */
349   bt_status_t (*init)(btav_sink_callbacks_t* callbacks);
350 
351   /** connect to headset */
352   bt_status_t (*connect)(const RawAddress& bd_addr);
353 
354   /** dis-connect from headset */
355   bt_status_t (*disconnect)(const RawAddress& bd_addr);
356 
357   /** Closes the interface. */
358   void (*cleanup)(void);
359 
360   /** Sends Audio Focus State. */
361   void (*set_audio_focus_state)(int focus_state);
362 
363   /** Sets the audio track gain. */
364   void (*set_audio_track_gain)(float gain);
365 
366   /** sets the connected device as active */
367   bt_status_t (*set_active_device)(const RawAddress& bd_addr);
368 } btav_sink_interface_t;
369 
370 __END_DECLS
371 
372 #endif /* ANDROID_INCLUDE_BT_AV_H */
373