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