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__anon4fc149f30808133 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__anon4fc149f30808231 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 /** BT-AV A2DP Source callback structure. */ 270 typedef struct { 271 /** set to sizeof(btav_source_callbacks_t) */ 272 size_t size; 273 btav_connection_state_callback connection_state_cb; 274 btav_audio_state_callback audio_state_cb; 275 btav_audio_source_config_callback audio_config_cb; 276 } btav_source_callbacks_t; 277 278 /** BT-AV A2DP Sink callback structure. */ 279 typedef struct { 280 /** set to sizeof(btav_sink_callbacks_t) */ 281 size_t size; 282 btav_connection_state_callback connection_state_cb; 283 btav_audio_state_callback audio_state_cb; 284 btav_audio_sink_config_callback audio_config_cb; 285 } btav_sink_callbacks_t; 286 287 /** 288 * NOTE: 289 * 290 * 1. AVRCP 1.0 shall be supported initially. AVRCP passthrough commands 291 * shall be handled internally via uinput 292 * 293 * 2. A2DP data path shall be handled via a socket pipe between the AudioFlinger 294 * android_audio_hw library and the Bluetooth stack. 295 * 296 */ 297 298 /** Represents the standard BT-AV A2DP Source interface. 299 */ 300 typedef struct { 301 /** set to sizeof(btav_source_interface_t) */ 302 size_t size; 303 /** 304 * Register the BtAv callbacks. 305 */ 306 bt_status_t (*init)(btav_source_callbacks_t* callbacks, 307 int max_connected_audio_devices, 308 std::vector<btav_a2dp_codec_config_t> codec_priorities); 309 310 /** connect to headset */ 311 bt_status_t (*connect)(const RawAddress& bd_addr); 312 313 /** dis-connect from headset */ 314 bt_status_t (*disconnect)(const RawAddress& bd_addr); 315 316 /** sets the connected device silence state */ 317 bt_status_t (*set_silence_device)(const RawAddress& bd_addr, bool silence); 318 319 /** sets the connected device as active */ 320 bt_status_t (*set_active_device)(const RawAddress& bd_addr); 321 322 /** configure the codecs settings preferences */ 323 bt_status_t (*config_codec)( 324 const RawAddress& bd_addr, 325 std::vector<btav_a2dp_codec_config_t> codec_preferences); 326 327 /** Closes the interface. */ 328 void (*cleanup)(void); 329 330 } btav_source_interface_t; 331 332 /** Represents the standard BT-AV A2DP Sink interface. 333 */ 334 typedef struct { 335 /** set to sizeof(btav_sink_interface_t) */ 336 size_t size; 337 /** 338 * Register the BtAv callbacks 339 */ 340 bt_status_t (*init)(btav_sink_callbacks_t* callbacks); 341 342 /** connect to headset */ 343 bt_status_t (*connect)(const RawAddress& bd_addr); 344 345 /** dis-connect from headset */ 346 bt_status_t (*disconnect)(const RawAddress& bd_addr); 347 348 /** Closes the interface. */ 349 void (*cleanup)(void); 350 351 /** Sends Audio Focus State. */ 352 void (*set_audio_focus_state)(int focus_state); 353 354 /** Sets the audio track gain. */ 355 void (*set_audio_track_gain)(float gain); 356 } btav_sink_interface_t; 357 358 __END_DECLS 359 360 #endif /* ANDROID_INCLUDE_BT_AV_H */ 361