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 23 #include <optional> 24 #include <sstream> 25 #include <vector> 26 27 #include "types/raw_address.h" 28 29 __BEGIN_DECLS 30 31 // Must be kept in sync with BluetoothProfile.java 32 typedef enum { 33 BTAV_CONNECTION_STATE_DISCONNECTED = 0, 34 BTAV_CONNECTION_STATE_CONNECTING, 35 BTAV_CONNECTION_STATE_CONNECTED, 36 BTAV_CONNECTION_STATE_DISCONNECTING 37 } btav_connection_state_t; 38 39 /* Bluetooth AV datapath states */ 40 typedef enum { 41 BTAV_AUDIO_STATE_REMOTE_SUSPEND = 0, 42 BTAV_AUDIO_STATE_STOPPED, 43 BTAV_AUDIO_STATE_STARTED, 44 } btav_audio_state_t; 45 46 /* 47 * Enum values for each A2DP supported codec. 48 * There should be a separate entry for each A2DP codec that is supported 49 * for encoding (SRC), and for decoding purpose (SINK). 50 */ 51 typedef enum { 52 BTAV_A2DP_CODEC_INDEX_SOURCE_MIN = 0, 53 54 // Add an entry for each source codec here. 55 // NOTE: The values should be same as those listed in the following file: 56 // BluetoothCodecConfig.java 57 BTAV_A2DP_CODEC_INDEX_SOURCE_SBC = 0, 58 BTAV_A2DP_CODEC_INDEX_SOURCE_AAC, 59 BTAV_A2DP_CODEC_INDEX_SOURCE_APTX, 60 BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD, 61 BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC, 62 BTAV_A2DP_CODEC_INDEX_SOURCE_LC3, 63 BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS, 64 65 BTAV_A2DP_CODEC_INDEX_SOURCE_MAX, 66 67 // Range of codec indexes reserved for Offload codec extensibility. 68 // Indexes in this range will be allocated for offloaded codecs 69 // that the stack does not recognize. 70 BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MIN = BTAV_A2DP_CODEC_INDEX_SOURCE_MAX, 71 BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MAX = BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MIN + 4, 72 73 BTAV_A2DP_CODEC_INDEX_SINK_MIN = BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MAX, 74 75 // Add an entry for each sink codec here 76 BTAV_A2DP_CODEC_INDEX_SINK_SBC = BTAV_A2DP_CODEC_INDEX_SINK_MIN, 77 BTAV_A2DP_CODEC_INDEX_SINK_AAC, 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 btif_av_source_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 codec_priority; // Codec selection priority 159 // relative to other codecs: larger value 160 // means higher priority. If 0, reset to 161 // default. 162 btav_a2dp_codec_sample_rate_t sample_rate; 163 btav_a2dp_codec_bits_per_sample_t bits_per_sample; 164 btav_a2dp_codec_channel_mode_t channel_mode; 165 int64_t codec_specific_1; // Codec-specific value 1 166 int64_t codec_specific_2; // Codec-specific value 2 167 int64_t codec_specific_3; // Codec-specific value 3 168 int64_t codec_specific_4; // Codec-specific value 4 169 CodecNameStrbtav_a2dp_codec_config_t170 std::string CodecNameStr() const { 171 switch (codec_type) { 172 case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC: 173 return "SBC"; 174 case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC: 175 return "AAC"; 176 case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX: 177 return "aptX"; 178 case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD: 179 return "aptX HD"; 180 case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC: 181 return "LDAC"; 182 case BTAV_A2DP_CODEC_INDEX_SINK_SBC: 183 return "SBC (Sink)"; 184 case BTAV_A2DP_CODEC_INDEX_SINK_AAC: 185 return "AAC (Sink)"; 186 case BTAV_A2DP_CODEC_INDEX_SOURCE_LC3: 187 return "LC3"; 188 case BTAV_A2DP_CODEC_INDEX_SINK_OPUS: 189 return "Opus (Sink)"; 190 case BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS: 191 return "Opus"; 192 case BTAV_A2DP_CODEC_INDEX_MAX: 193 return "Unknown(CODEC_INDEX_MAX)"; 194 case BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MIN: 195 case BTAV_A2DP_CODEC_INDEX_SINK_EXT_MIN: 196 return "Unknown(CODEC_EXT)"; 197 } 198 return "Unknown"; 199 } 200 ToStringbtav_a2dp_codec_config_t201 std::string ToString() const { 202 std::string sample_rate_str; 203 AppendCapability(sample_rate_str, (sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE), "NONE"); 204 AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_44100), "44100"); 205 AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_48000), "48000"); 206 AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_88200), "88200"); 207 AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_96000), "96000"); 208 AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_176400), "176400"); 209 AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_192000), "192000"); 210 AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_16000), "16000"); 211 AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_24000), "24000"); 212 213 std::string bits_per_sample_str; 214 AppendCapability(bits_per_sample_str, (bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE), 215 "NONE"); 216 AppendCapability(bits_per_sample_str, (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16), 217 "16"); 218 AppendCapability(bits_per_sample_str, (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24), 219 "24"); 220 AppendCapability(bits_per_sample_str, (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32), 221 "32"); 222 223 std::string channel_mode_str; 224 AppendCapability(channel_mode_str, (channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE), "NONE"); 225 AppendCapability(channel_mode_str, (channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_MONO), "MONO"); 226 AppendCapability(channel_mode_str, (channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO), 227 "STEREO"); 228 229 return "codec: " + CodecNameStr() + " priority: " + std::to_string(codec_priority) + 230 " sample_rate: " + sample_rate_str + " bits_per_sample: " + bits_per_sample_str + 231 " channel_mode: " + channel_mode_str + 232 " codec_specific_1: " + std::to_string(codec_specific_1) + 233 " codec_specific_2: " + std::to_string(codec_specific_2) + 234 " codec_specific_3: " + std::to_string(codec_specific_3) + 235 " codec_specific_4: " + std::to_string(codec_specific_4); 236 } 237 PrintCodecsbtav_a2dp_codec_config_t238 static std::string PrintCodecs(std::vector<btav_a2dp_codec_config_t> codecs) { 239 std::ostringstream oss; 240 for (size_t i = 0; i < codecs.size(); i++) { 241 oss << codecs[i].CodecNameStr(); 242 if (i != (codecs.size() - 1)) { 243 oss << ", "; 244 } 245 } 246 247 return oss.str(); 248 } 249 250 private: AppendCapabilitybtav_a2dp_codec_config_t251 static std::string AppendCapability(std::string& result, bool append, const std::string& name) { 252 if (!append) { 253 return result; 254 } 255 if (!result.empty()) { 256 result += "|"; 257 } 258 result += name; 259 return result; 260 } 261 }; 262 263 typedef struct { 264 btav_a2dp_scmst_enable_status_t enable_status; 265 uint8_t cp_header; 266 } btav_a2dp_scmst_info_t; 267 268 typedef struct { 269 bt_status_t status; 270 uint8_t error_code; 271 std::optional<std::string> error_msg; 272 } btav_error_t; 273 274 /** 275 * NOTE: 276 * 277 * 1. AVRCP 1.0 shall be supported initially. AVRCP passthrough commands 278 * shall be handled internally via uinput 279 * 280 * 2. A2DP data path shall be handled via a socket pipe between the AudioFlinger 281 * android_audio_hw library and the Bluetooth stack. 282 * 283 */ 284 285 __END_DECLS 286 287 namespace std { 288 template <> 289 struct formatter<btav_connection_state_t> : enum_formatter<btav_connection_state_t> {}; 290 template <> 291 struct formatter<btav_audio_state_t> : enum_formatter<btav_audio_state_t> {}; 292 template <> 293 struct formatter<btav_a2dp_codec_bits_per_sample_t> 294 : enum_formatter<btav_a2dp_codec_bits_per_sample_t> {}; 295 template <> 296 struct formatter<btav_a2dp_codec_priority_t> : enum_formatter<btav_a2dp_codec_priority_t> {}; 297 template <> 298 struct formatter<btav_a2dp_codec_index_t> : enum_formatter<btav_a2dp_codec_index_t> {}; 299 template <> 300 struct formatter<btav_a2dp_codec_sample_rate_t> : enum_formatter<btav_a2dp_codec_sample_rate_t> {}; 301 template <> 302 struct formatter<btav_a2dp_codec_channel_mode_t> : enum_formatter<btav_a2dp_codec_channel_mode_t> { 303 }; 304 template <> 305 struct formatter<btav_a2dp_scmst_enable_status_t> 306 : enum_formatter<btav_a2dp_scmst_enable_status_t> {}; 307 } // namespace std 308 309 #endif /* ANDROID_INCLUDE_BT_AV_H */ 310