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