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