1 /****************************************************************************** 2 * 3 * Copyright 2002-2012 Broadcom Corporation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 /****************************************************************************** 20 * 21 * Utility functions to help build and parse SBC Codec Information Element 22 * and Media Payload. 23 * 24 ******************************************************************************/ 25 26 #define LOG_TAG "a2dp_sbc" 27 28 #include "bt_target.h" 29 30 #include "a2dp_sbc.h" 31 32 #include <string.h> 33 34 #include <base/logging.h> 35 #include "a2dp_sbc_decoder.h" 36 #include "a2dp_sbc_encoder.h" 37 #include "bt_utils.h" 38 #include "embdrv/sbc/encoder/include/sbc_encoder.h" 39 #include "osi/include/log.h" 40 #include "osi/include/osi.h" 41 42 #define A2DP_SBC_MAX_BITPOOL 53 43 44 /* data type for the SBC Codec Information Element */ 45 typedef struct { 46 uint8_t samp_freq; /* Sampling frequency */ 47 uint8_t ch_mode; /* Channel mode */ 48 uint8_t block_len; /* Block length */ 49 uint8_t num_subbands; /* Number of subbands */ 50 uint8_t alloc_method; /* Allocation method */ 51 uint8_t min_bitpool; /* Minimum bitpool */ 52 uint8_t max_bitpool; /* Maximum bitpool */ 53 btav_a2dp_codec_bits_per_sample_t bits_per_sample; 54 } tA2DP_SBC_CIE; 55 56 /* SBC Source codec capabilities */ 57 static const tA2DP_SBC_CIE a2dp_sbc_source_caps = { 58 (A2DP_SBC_IE_SAMP_FREQ_44), /* samp_freq */ 59 (A2DP_SBC_IE_CH_MD_MONO | A2DP_SBC_IE_CH_MD_JOINT), /* ch_mode */ 60 (A2DP_SBC_IE_BLOCKS_16 | A2DP_SBC_IE_BLOCKS_12 | A2DP_SBC_IE_BLOCKS_8 | 61 A2DP_SBC_IE_BLOCKS_4), /* block_len */ 62 A2DP_SBC_IE_SUBBAND_8, /* num_subbands */ 63 A2DP_SBC_IE_ALLOC_MD_L, /* alloc_method */ 64 A2DP_SBC_IE_MIN_BITPOOL, /* min_bitpool */ 65 A2DP_SBC_MAX_BITPOOL, /* max_bitpool */ 66 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 /* bits_per_sample */ 67 }; 68 69 /* SBC Sink codec capabilities */ 70 static const tA2DP_SBC_CIE a2dp_sbc_sink_caps = { 71 (A2DP_SBC_IE_SAMP_FREQ_48 | A2DP_SBC_IE_SAMP_FREQ_44), /* samp_freq */ 72 (A2DP_SBC_IE_CH_MD_MONO | A2DP_SBC_IE_CH_MD_STEREO | 73 A2DP_SBC_IE_CH_MD_JOINT | A2DP_SBC_IE_CH_MD_DUAL), /* ch_mode */ 74 (A2DP_SBC_IE_BLOCKS_16 | A2DP_SBC_IE_BLOCKS_12 | A2DP_SBC_IE_BLOCKS_8 | 75 A2DP_SBC_IE_BLOCKS_4), /* block_len */ 76 (A2DP_SBC_IE_SUBBAND_4 | A2DP_SBC_IE_SUBBAND_8), /* num_subbands */ 77 (A2DP_SBC_IE_ALLOC_MD_L | A2DP_SBC_IE_ALLOC_MD_S), /* alloc_method */ 78 A2DP_SBC_IE_MIN_BITPOOL, /* min_bitpool */ 79 A2DP_SBC_MAX_BITPOOL, /* max_bitpool */ 80 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 /* bits_per_sample */ 81 }; 82 83 /* Default SBC codec configuration */ 84 const tA2DP_SBC_CIE a2dp_sbc_default_config = { 85 A2DP_SBC_IE_SAMP_FREQ_44, /* samp_freq */ 86 A2DP_SBC_IE_CH_MD_JOINT, /* ch_mode */ 87 A2DP_SBC_IE_BLOCKS_16, /* block_len */ 88 A2DP_SBC_IE_SUBBAND_8, /* num_subbands */ 89 A2DP_SBC_IE_ALLOC_MD_L, /* alloc_method */ 90 A2DP_SBC_IE_MIN_BITPOOL, /* min_bitpool */ 91 A2DP_SBC_MAX_BITPOOL, /* max_bitpool */ 92 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 /* bits_per_sample */ 93 }; 94 95 static const tA2DP_ENCODER_INTERFACE a2dp_encoder_interface_sbc = { 96 a2dp_sbc_encoder_init, 97 a2dp_sbc_encoder_cleanup, 98 a2dp_sbc_feeding_reset, 99 a2dp_sbc_feeding_flush, 100 a2dp_sbc_get_encoder_interval_ms, 101 a2dp_sbc_send_frames, 102 nullptr // set_transmit_queue_length 103 }; 104 105 static const tA2DP_DECODER_INTERFACE a2dp_decoder_interface_sbc = { 106 a2dp_sbc_decoder_init, a2dp_sbc_decoder_cleanup, 107 a2dp_sbc_decoder_decode_packet, 108 }; 109 110 static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilitySbc( 111 const tA2DP_SBC_CIE* p_cap, const uint8_t* p_codec_info, 112 bool is_capability); 113 static void A2DP_ParseMplHeaderSbc(uint8_t* p_src, bool* p_frag, bool* p_start, 114 bool* p_last, uint8_t* p_num); 115 116 // Builds the SBC Media Codec Capabilities byte sequence beginning from the 117 // LOSC octet. |media_type| is the media type |AVDT_MEDIA_TYPE_*|. 118 // |p_ie| is a pointer to the SBC Codec Information Element information. 119 // The result is stored in |p_result|. Returns A2DP_SUCCESS on success, 120 // otherwise the corresponding A2DP error status code. 121 static tA2DP_STATUS A2DP_BuildInfoSbc(uint8_t media_type, 122 const tA2DP_SBC_CIE* p_ie, 123 uint8_t* p_result) { 124 if (p_ie == NULL || p_result == NULL || 125 (p_ie->samp_freq & ~A2DP_SBC_IE_SAMP_FREQ_MSK) || 126 (p_ie->ch_mode & ~A2DP_SBC_IE_CH_MD_MSK) || 127 (p_ie->block_len & ~A2DP_SBC_IE_BLOCKS_MSK) || 128 (p_ie->num_subbands & ~A2DP_SBC_IE_SUBBAND_MSK) || 129 (p_ie->alloc_method & ~A2DP_SBC_IE_ALLOC_MD_MSK) || 130 (p_ie->min_bitpool > p_ie->max_bitpool) || 131 (p_ie->min_bitpool < A2DP_SBC_IE_MIN_BITPOOL) || 132 (p_ie->min_bitpool > A2DP_SBC_IE_MAX_BITPOOL) || 133 (p_ie->max_bitpool < A2DP_SBC_IE_MIN_BITPOOL) || 134 (p_ie->max_bitpool > A2DP_SBC_IE_MAX_BITPOOL)) { 135 /* if any unused bit is set */ 136 return A2DP_INVALID_PARAMS; 137 } 138 139 *p_result++ = A2DP_SBC_INFO_LEN; 140 *p_result++ = (media_type << 4); 141 *p_result++ = A2DP_MEDIA_CT_SBC; 142 143 /* Media Codec Specific Information Element */ 144 *p_result++ = p_ie->samp_freq | p_ie->ch_mode; 145 146 *p_result++ = p_ie->block_len | p_ie->num_subbands | p_ie->alloc_method; 147 148 *p_result++ = p_ie->min_bitpool; 149 *p_result = p_ie->max_bitpool; 150 151 return A2DP_SUCCESS; 152 } 153 154 // Parses the SBC Media Codec Capabilities byte sequence beginning from the 155 // LOSC octet. The result is stored in |p_ie|. The byte sequence to parse is 156 // |p_codec_info|. If |is_capability| is true, the byte sequence contains 157 // codec capability. 158 // Returns A2DP_SUCCESS on success, otherwise the corresponding A2DP error 159 // status code. 160 static tA2DP_STATUS A2DP_ParseInfoSbc(tA2DP_SBC_CIE* p_ie, 161 const uint8_t* p_codec_info, 162 bool is_capability) { 163 uint8_t losc; 164 uint8_t media_type; 165 tA2DP_CODEC_TYPE codec_type; 166 167 if (p_ie == NULL || p_codec_info == NULL) return A2DP_INVALID_PARAMS; 168 169 // Check the codec capability length 170 losc = *p_codec_info++; 171 if (losc != A2DP_SBC_INFO_LEN) return A2DP_WRONG_CODEC; 172 173 media_type = (*p_codec_info++) >> 4; 174 codec_type = *p_codec_info++; 175 /* Check the Media Type and Media Codec Type */ 176 if (media_type != AVDT_MEDIA_TYPE_AUDIO || codec_type != A2DP_MEDIA_CT_SBC) { 177 return A2DP_WRONG_CODEC; 178 } 179 180 p_ie->samp_freq = *p_codec_info & A2DP_SBC_IE_SAMP_FREQ_MSK; 181 p_ie->ch_mode = *p_codec_info & A2DP_SBC_IE_CH_MD_MSK; 182 p_codec_info++; 183 p_ie->block_len = *p_codec_info & A2DP_SBC_IE_BLOCKS_MSK; 184 p_ie->num_subbands = *p_codec_info & A2DP_SBC_IE_SUBBAND_MSK; 185 p_ie->alloc_method = *p_codec_info & A2DP_SBC_IE_ALLOC_MD_MSK; 186 p_codec_info++; 187 p_ie->min_bitpool = *p_codec_info++; 188 p_ie->max_bitpool = *p_codec_info++; 189 if (p_ie->min_bitpool < A2DP_SBC_IE_MIN_BITPOOL || 190 p_ie->min_bitpool > A2DP_SBC_IE_MAX_BITPOOL) { 191 return A2DP_BAD_MIN_BITPOOL; 192 } 193 194 if (p_ie->max_bitpool < A2DP_SBC_IE_MIN_BITPOOL || 195 p_ie->max_bitpool > A2DP_SBC_IE_MAX_BITPOOL || 196 p_ie->max_bitpool < p_ie->min_bitpool) { 197 return A2DP_BAD_MAX_BITPOOL; 198 } 199 200 if (is_capability) return A2DP_SUCCESS; 201 202 if (A2DP_BitsSet(p_ie->samp_freq) != A2DP_SET_ONE_BIT) 203 return A2DP_BAD_SAMP_FREQ; 204 if (A2DP_BitsSet(p_ie->ch_mode) != A2DP_SET_ONE_BIT) return A2DP_BAD_CH_MODE; 205 if (A2DP_BitsSet(p_ie->block_len) != A2DP_SET_ONE_BIT) 206 return A2DP_BAD_BLOCK_LEN; 207 if (A2DP_BitsSet(p_ie->num_subbands) != A2DP_SET_ONE_BIT) 208 return A2DP_BAD_SUBBANDS; 209 if (A2DP_BitsSet(p_ie->alloc_method) != A2DP_SET_ONE_BIT) 210 return A2DP_BAD_ALLOC_METHOD; 211 212 return A2DP_SUCCESS; 213 } 214 215 // Build the SBC Media Payload Header. 216 // |p_dst| points to the location where the header should be written to. 217 // If |frag| is true, the media payload frame is fragmented. 218 // |start| is true for the first packet of a fragmented frame. 219 // |last| is true for the last packet of a fragmented frame. 220 // If |frag| is false, |num| is the number of number of frames in the packet, 221 // otherwise is the number of remaining fragments (including this one). 222 static void A2DP_BuildMediaPayloadHeaderSbc(uint8_t* p_dst, bool frag, 223 bool start, bool last, 224 uint8_t num) { 225 if (p_dst == NULL) return; 226 227 *p_dst = 0; 228 if (frag) *p_dst |= A2DP_SBC_HDR_F_MSK; 229 if (start) *p_dst |= A2DP_SBC_HDR_S_MSK; 230 if (last) *p_dst |= A2DP_SBC_HDR_L_MSK; 231 *p_dst |= (A2DP_SBC_HDR_NUM_MSK & num); 232 } 233 234 /****************************************************************************** 235 * 236 * Function A2DP_ParseMplHeaderSbc 237 * 238 * Description This function is called by an application to parse 239 * the SBC Media Payload header. 240 * Input Parameters: 241 * p_src: the byte sequence to parse.. 242 * 243 * Output Parameters: 244 * frag: 1, if fragmented. 0, otherwise. 245 * 246 * start: 1, if the starting packet of a fragmented frame. 247 * 248 * last: 1, if the last packet of a fragmented frame. 249 * 250 * num: If frag is 1, this is the number of remaining 251 * fragments 252 * (including this fragment) of this frame. 253 * If frag is 0, this is the number of frames in 254 * this packet. 255 * 256 * Returns void. 257 *****************************************************************************/ 258 UNUSED_ATTR static void A2DP_ParseMplHeaderSbc(uint8_t* p_src, bool* p_frag, 259 bool* p_start, bool* p_last, 260 uint8_t* p_num) { 261 if (p_src && p_frag && p_start && p_last && p_num) { 262 *p_frag = (*p_src & A2DP_SBC_HDR_F_MSK) ? true : false; 263 *p_start = (*p_src & A2DP_SBC_HDR_S_MSK) ? true : false; 264 *p_last = (*p_src & A2DP_SBC_HDR_L_MSK) ? true : false; 265 *p_num = (*p_src & A2DP_SBC_HDR_NUM_MSK); 266 } 267 } 268 269 const char* A2DP_CodecNameSbc(UNUSED_ATTR const uint8_t* p_codec_info) { 270 return "SBC"; 271 } 272 273 bool A2DP_IsSourceCodecValidSbc(const uint8_t* p_codec_info) { 274 tA2DP_SBC_CIE cfg_cie; 275 276 /* Use a liberal check when parsing the codec info */ 277 return (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) || 278 (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS); 279 } 280 281 bool A2DP_IsSinkCodecValidSbc(const uint8_t* p_codec_info) { 282 tA2DP_SBC_CIE cfg_cie; 283 284 /* Use a liberal check when parsing the codec info */ 285 return (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) || 286 (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS); 287 } 288 289 bool A2DP_IsPeerSourceCodecValidSbc(const uint8_t* p_codec_info) { 290 tA2DP_SBC_CIE cfg_cie; 291 292 /* Use a liberal check when parsing the codec info */ 293 return (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) || 294 (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS); 295 } 296 297 bool A2DP_IsPeerSinkCodecValidSbc(const uint8_t* p_codec_info) { 298 tA2DP_SBC_CIE cfg_cie; 299 300 /* Use a liberal check when parsing the codec info */ 301 return (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) || 302 (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS); 303 } 304 305 bool A2DP_IsSinkCodecSupportedSbc(const uint8_t* p_codec_info) { 306 return (A2DP_CodecInfoMatchesCapabilitySbc(&a2dp_sbc_sink_caps, p_codec_info, 307 false) == A2DP_SUCCESS); 308 } 309 310 bool A2DP_IsPeerSourceCodecSupportedSbc(const uint8_t* p_codec_info) { 311 return (A2DP_CodecInfoMatchesCapabilitySbc(&a2dp_sbc_sink_caps, p_codec_info, 312 true) == A2DP_SUCCESS); 313 } 314 315 void A2DP_InitDefaultCodecSbc(uint8_t* p_codec_info) { 316 if (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &a2dp_sbc_default_config, 317 p_codec_info) != A2DP_SUCCESS) { 318 LOG_ERROR(LOG_TAG, "%s: A2DP_BuildInfoSbc failed", __func__); 319 } 320 } 321 322 // Checks whether A2DP SBC codec configuration matches with a device's codec 323 // capabilities. |p_cap| is the SBC codec configuration. |p_codec_info| is 324 // the device's codec capabilities. |is_capability| is true if 325 // |p_codec_info| contains A2DP codec capability. 326 // Returns A2DP_SUCCESS if the codec configuration matches with capabilities, 327 // otherwise the corresponding A2DP error status code. 328 static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilitySbc( 329 const tA2DP_SBC_CIE* p_cap, const uint8_t* p_codec_info, 330 bool is_capability) { 331 tA2DP_STATUS status; 332 tA2DP_SBC_CIE cfg_cie; 333 334 /* parse configuration */ 335 status = A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, is_capability); 336 if (status != A2DP_SUCCESS) { 337 LOG_ERROR(LOG_TAG, "%s: parsing failed %d", __func__, status); 338 return status; 339 } 340 341 /* verify that each parameter is in range */ 342 343 LOG_VERBOSE(LOG_TAG, "%s: FREQ peer: 0x%x, capability 0x%x", __func__, 344 cfg_cie.samp_freq, p_cap->samp_freq); 345 LOG_VERBOSE(LOG_TAG, "%s: CH_MODE peer: 0x%x, capability 0x%x", __func__, 346 cfg_cie.ch_mode, p_cap->ch_mode); 347 LOG_VERBOSE(LOG_TAG, "%s: BLOCK_LEN peer: 0x%x, capability 0x%x", __func__, 348 cfg_cie.block_len, p_cap->block_len); 349 LOG_VERBOSE(LOG_TAG, "%s: SUB_BAND peer: 0x%x, capability 0x%x", __func__, 350 cfg_cie.num_subbands, p_cap->num_subbands); 351 LOG_VERBOSE(LOG_TAG, "%s: ALLOC_METHOD peer: 0x%x, capability 0x%x", __func__, 352 cfg_cie.alloc_method, p_cap->alloc_method); 353 LOG_VERBOSE(LOG_TAG, "%s: MIN_BitPool peer: 0x%x, capability 0x%x", __func__, 354 cfg_cie.min_bitpool, p_cap->min_bitpool); 355 LOG_VERBOSE(LOG_TAG, "%s: MAX_BitPool peer: 0x%x, capability 0x%x", __func__, 356 cfg_cie.max_bitpool, p_cap->max_bitpool); 357 358 /* sampling frequency */ 359 if ((cfg_cie.samp_freq & p_cap->samp_freq) == 0) return A2DP_NS_SAMP_FREQ; 360 361 /* channel mode */ 362 if ((cfg_cie.ch_mode & p_cap->ch_mode) == 0) return A2DP_NS_CH_MODE; 363 364 /* block length */ 365 if ((cfg_cie.block_len & p_cap->block_len) == 0) return A2DP_BAD_BLOCK_LEN; 366 367 /* subbands */ 368 if ((cfg_cie.num_subbands & p_cap->num_subbands) == 0) 369 return A2DP_NS_SUBBANDS; 370 371 /* allocation method */ 372 if ((cfg_cie.alloc_method & p_cap->alloc_method) == 0) 373 return A2DP_NS_ALLOC_METHOD; 374 375 /* min bitpool */ 376 if (cfg_cie.min_bitpool > p_cap->max_bitpool) return A2DP_NS_MIN_BITPOOL; 377 378 /* max bitpool */ 379 if (cfg_cie.max_bitpool < p_cap->min_bitpool) return A2DP_NS_MAX_BITPOOL; 380 381 return A2DP_SUCCESS; 382 } 383 384 bool A2DP_CodecTypeEqualsSbc(const uint8_t* p_codec_info_a, 385 const uint8_t* p_codec_info_b) { 386 tA2DP_SBC_CIE sbc_cie_a; 387 tA2DP_SBC_CIE sbc_cie_b; 388 389 // Check whether the codec info contains valid data 390 tA2DP_STATUS a2dp_status = 391 A2DP_ParseInfoSbc(&sbc_cie_a, p_codec_info_a, true); 392 if (a2dp_status != A2DP_SUCCESS) { 393 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 394 a2dp_status); 395 return false; 396 } 397 a2dp_status = A2DP_ParseInfoSbc(&sbc_cie_b, p_codec_info_b, true); 398 if (a2dp_status != A2DP_SUCCESS) { 399 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 400 a2dp_status); 401 return false; 402 } 403 404 tA2DP_CODEC_TYPE codec_type_a = A2DP_GetCodecType(p_codec_info_a); 405 tA2DP_CODEC_TYPE codec_type_b = A2DP_GetCodecType(p_codec_info_b); 406 407 return (codec_type_a == codec_type_b) && (codec_type_a == A2DP_MEDIA_CT_SBC); 408 } 409 410 bool A2DP_CodecEqualsSbc(const uint8_t* p_codec_info_a, 411 const uint8_t* p_codec_info_b) { 412 tA2DP_SBC_CIE sbc_cie_a; 413 tA2DP_SBC_CIE sbc_cie_b; 414 415 // Check whether the codec info contains valid data 416 tA2DP_STATUS a2dp_status = 417 A2DP_ParseInfoSbc(&sbc_cie_a, p_codec_info_a, true); 418 if (a2dp_status != A2DP_SUCCESS) { 419 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 420 a2dp_status); 421 return false; 422 } 423 a2dp_status = A2DP_ParseInfoSbc(&sbc_cie_b, p_codec_info_b, true); 424 if (a2dp_status != A2DP_SUCCESS) { 425 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 426 a2dp_status); 427 return false; 428 } 429 430 tA2DP_CODEC_TYPE codec_type_a = A2DP_GetCodecType(p_codec_info_a); 431 tA2DP_CODEC_TYPE codec_type_b = A2DP_GetCodecType(p_codec_info_b); 432 433 if ((codec_type_a != codec_type_b) || (codec_type_a != A2DP_MEDIA_CT_SBC)) 434 return false; 435 436 return (sbc_cie_a.samp_freq == sbc_cie_b.samp_freq) && 437 (sbc_cie_a.ch_mode == sbc_cie_b.ch_mode) && 438 (sbc_cie_a.block_len == sbc_cie_b.block_len) && 439 (sbc_cie_a.num_subbands == sbc_cie_b.num_subbands) && 440 (sbc_cie_a.alloc_method == sbc_cie_b.alloc_method) && 441 (sbc_cie_a.min_bitpool == sbc_cie_b.min_bitpool) && 442 (sbc_cie_a.max_bitpool == sbc_cie_b.max_bitpool); 443 } 444 445 int A2DP_GetTrackSampleRateSbc(const uint8_t* p_codec_info) { 446 tA2DP_SBC_CIE sbc_cie; 447 448 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false); 449 if (a2dp_status != A2DP_SUCCESS) { 450 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 451 a2dp_status); 452 return -1; 453 } 454 455 switch (sbc_cie.samp_freq) { 456 case A2DP_SBC_IE_SAMP_FREQ_16: 457 return 16000; 458 case A2DP_SBC_IE_SAMP_FREQ_32: 459 return 32000; 460 case A2DP_SBC_IE_SAMP_FREQ_44: 461 return 44100; 462 case A2DP_SBC_IE_SAMP_FREQ_48: 463 return 48000; 464 default: 465 break; 466 } 467 468 return -1; 469 } 470 471 int A2DP_GetTrackChannelCountSbc(const uint8_t* p_codec_info) { 472 tA2DP_SBC_CIE sbc_cie; 473 474 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false); 475 if (a2dp_status != A2DP_SUCCESS) { 476 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 477 a2dp_status); 478 return -1; 479 } 480 481 switch (sbc_cie.ch_mode) { 482 case A2DP_SBC_IE_CH_MD_MONO: 483 return 1; 484 case A2DP_SBC_IE_CH_MD_DUAL: 485 case A2DP_SBC_IE_CH_MD_STEREO: 486 case A2DP_SBC_IE_CH_MD_JOINT: 487 return 2; 488 default: 489 break; 490 } 491 492 return -1; 493 } 494 495 int A2DP_GetNumberOfSubbandsSbc(const uint8_t* p_codec_info) { 496 tA2DP_SBC_CIE sbc_cie; 497 498 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false); 499 if (a2dp_status != A2DP_SUCCESS) { 500 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 501 a2dp_status); 502 return -1; 503 } 504 505 switch (sbc_cie.num_subbands) { 506 case A2DP_SBC_IE_SUBBAND_4: 507 return 4; 508 case A2DP_SBC_IE_SUBBAND_8: 509 return 8; 510 default: 511 break; 512 } 513 514 return -1; 515 } 516 517 int A2DP_GetNumberOfBlocksSbc(const uint8_t* p_codec_info) { 518 tA2DP_SBC_CIE sbc_cie; 519 520 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false); 521 if (a2dp_status != A2DP_SUCCESS) { 522 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 523 a2dp_status); 524 return -1; 525 } 526 527 switch (sbc_cie.block_len) { 528 case A2DP_SBC_IE_BLOCKS_4: 529 return 4; 530 case A2DP_SBC_IE_BLOCKS_8: 531 return 8; 532 case A2DP_SBC_IE_BLOCKS_12: 533 return 12; 534 case A2DP_SBC_IE_BLOCKS_16: 535 return 16; 536 default: 537 break; 538 } 539 540 return -1; 541 } 542 543 int A2DP_GetAllocationMethodCodeSbc(const uint8_t* p_codec_info) { 544 tA2DP_SBC_CIE sbc_cie; 545 546 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false); 547 if (a2dp_status != A2DP_SUCCESS) { 548 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 549 a2dp_status); 550 return -1; 551 } 552 553 switch (sbc_cie.alloc_method) { 554 case A2DP_SBC_IE_ALLOC_MD_S: 555 return SBC_SNR; 556 case A2DP_SBC_IE_ALLOC_MD_L: 557 return SBC_LOUDNESS; 558 default: 559 break; 560 } 561 562 return -1; 563 } 564 565 int A2DP_GetChannelModeCodeSbc(const uint8_t* p_codec_info) { 566 tA2DP_SBC_CIE sbc_cie; 567 568 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false); 569 if (a2dp_status != A2DP_SUCCESS) { 570 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 571 a2dp_status); 572 return -1; 573 } 574 575 switch (sbc_cie.ch_mode) { 576 case A2DP_SBC_IE_CH_MD_MONO: 577 return SBC_MONO; 578 case A2DP_SBC_IE_CH_MD_DUAL: 579 return SBC_DUAL; 580 case A2DP_SBC_IE_CH_MD_STEREO: 581 return SBC_STEREO; 582 case A2DP_SBC_IE_CH_MD_JOINT: 583 return SBC_JOINT_STEREO; 584 default: 585 break; 586 } 587 588 return -1; 589 } 590 591 int A2DP_GetSamplingFrequencyCodeSbc(const uint8_t* p_codec_info) { 592 tA2DP_SBC_CIE sbc_cie; 593 594 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false); 595 if (a2dp_status != A2DP_SUCCESS) { 596 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 597 a2dp_status); 598 return -1; 599 } 600 601 switch (sbc_cie.samp_freq) { 602 case A2DP_SBC_IE_SAMP_FREQ_16: 603 return SBC_sf16000; 604 case A2DP_SBC_IE_SAMP_FREQ_32: 605 return SBC_sf32000; 606 case A2DP_SBC_IE_SAMP_FREQ_44: 607 return SBC_sf44100; 608 case A2DP_SBC_IE_SAMP_FREQ_48: 609 return SBC_sf48000; 610 default: 611 break; 612 } 613 614 return -1; 615 } 616 617 int A2DP_GetMinBitpoolSbc(const uint8_t* p_codec_info) { 618 tA2DP_SBC_CIE sbc_cie; 619 620 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, true); 621 if (a2dp_status != A2DP_SUCCESS) { 622 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 623 a2dp_status); 624 return -1; 625 } 626 627 return sbc_cie.min_bitpool; 628 } 629 630 int A2DP_GetMaxBitpoolSbc(const uint8_t* p_codec_info) { 631 tA2DP_SBC_CIE sbc_cie; 632 633 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, true); 634 if (a2dp_status != A2DP_SUCCESS) { 635 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 636 a2dp_status); 637 return -1; 638 } 639 640 return sbc_cie.max_bitpool; 641 } 642 643 uint32_t A2DP_GetBitrateSbc() { return a2dp_sbc_get_bitrate(); } 644 int A2DP_GetSinkTrackChannelTypeSbc(const uint8_t* p_codec_info) { 645 tA2DP_SBC_CIE sbc_cie; 646 647 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false); 648 if (a2dp_status != A2DP_SUCCESS) { 649 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 650 a2dp_status); 651 return -1; 652 } 653 654 switch (sbc_cie.ch_mode) { 655 case A2DP_SBC_IE_CH_MD_MONO: 656 return 1; 657 case A2DP_SBC_IE_CH_MD_DUAL: 658 case A2DP_SBC_IE_CH_MD_STEREO: 659 case A2DP_SBC_IE_CH_MD_JOINT: 660 return 3; 661 default: 662 break; 663 } 664 665 return -1; 666 } 667 668 bool A2DP_GetPacketTimestampSbc(UNUSED_ATTR const uint8_t* p_codec_info, 669 const uint8_t* p_data, uint32_t* p_timestamp) { 670 *p_timestamp = *(const uint32_t*)p_data; 671 return true; 672 } 673 674 bool A2DP_BuildCodecHeaderSbc(UNUSED_ATTR const uint8_t* p_codec_info, 675 BT_HDR* p_buf, uint16_t frames_per_packet) { 676 uint8_t* p; 677 678 p_buf->offset -= A2DP_SBC_MPL_HDR_LEN; 679 p = (uint8_t*)(p_buf + 1) + p_buf->offset; 680 p_buf->len += A2DP_SBC_MPL_HDR_LEN; 681 A2DP_BuildMediaPayloadHeaderSbc(p, false, false, false, 682 (uint8_t)frames_per_packet); 683 684 return true; 685 } 686 687 std::string A2DP_CodecInfoStringSbc(const uint8_t* p_codec_info) { 688 std::stringstream res; 689 std::string field; 690 tA2DP_STATUS a2dp_status; 691 tA2DP_SBC_CIE sbc_cie; 692 693 a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, true); 694 if (a2dp_status != A2DP_SUCCESS) { 695 res << "A2DP_ParseInfoSbc fail: " << loghex(a2dp_status); 696 return res.str(); 697 } 698 699 res << "\tname: SBC\n"; 700 701 // Sample frequency 702 field.clear(); 703 AppendField(&field, (sbc_cie.samp_freq == 0), "NONE"); 704 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_16), "16000"); 705 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_32), "32000"); 706 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_44), "44100"); 707 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_48), "48000"); 708 res << "\tsamp_freq: " << field << " (" << loghex(sbc_cie.samp_freq) << ")\n"; 709 710 // Channel mode 711 field.clear(); 712 AppendField(&field, (sbc_cie.ch_mode == 0), "NONE"); 713 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_MONO), "Mono"); 714 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_DUAL), "Dual"); 715 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_STEREO), "Stereo"); 716 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_JOINT), "Joint"); 717 res << "\tch_mode: " << field << " (" << loghex(sbc_cie.ch_mode) << ")\n"; 718 719 // Block length 720 field.clear(); 721 AppendField(&field, (sbc_cie.block_len == 0), "NONE"); 722 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_4), "4"); 723 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_8), "8"); 724 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_12), "12"); 725 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_16), "16"); 726 res << "\tblock_len: " << field << " (" << loghex(sbc_cie.block_len) << ")\n"; 727 728 // Number of subbands 729 field.clear(); 730 AppendField(&field, (sbc_cie.num_subbands == 0), "NONE"); 731 AppendField(&field, (sbc_cie.num_subbands & A2DP_SBC_IE_SUBBAND_4), "4"); 732 AppendField(&field, (sbc_cie.num_subbands & A2DP_SBC_IE_SUBBAND_8), "8"); 733 res << "\tnum_subbands: " << field << " (" << loghex(sbc_cie.num_subbands) 734 << ")\n"; 735 736 // Allocation method 737 field.clear(); 738 AppendField(&field, (sbc_cie.alloc_method == 0), "NONE"); 739 AppendField(&field, (sbc_cie.alloc_method & A2DP_SBC_IE_ALLOC_MD_S), "SNR"); 740 AppendField(&field, (sbc_cie.alloc_method & A2DP_SBC_IE_ALLOC_MD_L), 741 "Loundess"); 742 res << "\talloc_method: " << field << " (" << loghex(sbc_cie.alloc_method) 743 << ")\n"; 744 745 // Min/max bitloop 746 res << "\tBit pool Min: " << std::to_string(sbc_cie.min_bitpool) 747 << " Max: " << std::to_string(sbc_cie.max_bitpool); 748 749 return res.str(); 750 } 751 752 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterfaceSbc( 753 const uint8_t* p_codec_info) { 754 if (!A2DP_IsSourceCodecValidSbc(p_codec_info)) return NULL; 755 756 return &a2dp_encoder_interface_sbc; 757 } 758 759 const tA2DP_DECODER_INTERFACE* A2DP_GetDecoderInterfaceSbc( 760 const uint8_t* p_codec_info) { 761 if (!A2DP_IsSinkCodecValidSbc(p_codec_info)) return NULL; 762 763 return &a2dp_decoder_interface_sbc; 764 } 765 766 bool A2DP_AdjustCodecSbc(uint8_t* p_codec_info) { 767 tA2DP_SBC_CIE cfg_cie; 768 769 if (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS) 770 return false; 771 772 // Updated the max bitpool 773 if (cfg_cie.max_bitpool > A2DP_SBC_MAX_BITPOOL) { 774 LOG_WARN(LOG_TAG, "%s: Updated the SBC codec max bitpool from %d to %d", 775 __func__, cfg_cie.max_bitpool, A2DP_SBC_MAX_BITPOOL); 776 cfg_cie.max_bitpool = A2DP_SBC_MAX_BITPOOL; 777 } 778 779 return (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &cfg_cie, p_codec_info) == 780 A2DP_SUCCESS); 781 } 782 783 btav_a2dp_codec_index_t A2DP_SourceCodecIndexSbc( 784 UNUSED_ATTR const uint8_t* p_codec_info) { 785 return BTAV_A2DP_CODEC_INDEX_SOURCE_SBC; 786 } 787 788 btav_a2dp_codec_index_t A2DP_SinkCodecIndexSbc( 789 UNUSED_ATTR const uint8_t* p_codec_info) { 790 return BTAV_A2DP_CODEC_INDEX_SINK_SBC; 791 } 792 793 const char* A2DP_CodecIndexStrSbc(void) { return "SBC"; } 794 795 const char* A2DP_CodecIndexStrSbcSink(void) { return "SBC SINK"; } 796 797 bool A2DP_InitCodecConfigSbc(AvdtpSepConfig* p_cfg) { 798 if (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &a2dp_sbc_source_caps, 799 p_cfg->codec_info) != A2DP_SUCCESS) { 800 return false; 801 } 802 803 #if (BTA_AV_CO_CP_SCMS_T == TRUE) 804 /* Content protection info - support SCMS-T */ 805 uint8_t* p = p_cfg->protect_info; 806 *p++ = AVDT_CP_LOSC; 807 UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID); 808 p_cfg->num_protect = 1; 809 #endif 810 811 return true; 812 } 813 814 bool A2DP_InitCodecConfigSbcSink(AvdtpSepConfig* p_cfg) { 815 if (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &a2dp_sbc_sink_caps, 816 p_cfg->codec_info) != A2DP_SUCCESS) { 817 return false; 818 } 819 820 return true; 821 } 822 823 UNUSED_ATTR static void build_codec_config(const tA2DP_SBC_CIE& config_cie, 824 btav_a2dp_codec_config_t* result) { 825 if (config_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) 826 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100; 827 if (config_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) 828 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000; 829 830 result->bits_per_sample = config_cie.bits_per_sample; 831 832 if (config_cie.ch_mode & A2DP_SBC_IE_CH_MD_MONO) 833 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; 834 835 if (config_cie.ch_mode & (A2DP_SBC_IE_CH_MD_STEREO | A2DP_SBC_IE_CH_MD_JOINT | 836 A2DP_SBC_IE_CH_MD_DUAL)) { 837 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 838 } 839 } 840 841 A2dpCodecConfigSbcSource::A2dpCodecConfigSbcSource( 842 btav_a2dp_codec_priority_t codec_priority) 843 : A2dpCodecConfigSbcBase(BTAV_A2DP_CODEC_INDEX_SOURCE_SBC, "SBC", 844 codec_priority, true) { 845 // Compute the local capability 846 if (a2dp_sbc_source_caps.samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) { 847 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100; 848 } 849 if (a2dp_sbc_source_caps.samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) { 850 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000; 851 } 852 codec_local_capability_.bits_per_sample = 853 a2dp_sbc_source_caps.bits_per_sample; 854 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_MONO) { 855 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; 856 } 857 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_JOINT) { 858 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 859 } 860 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_STEREO) { 861 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 862 } 863 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_DUAL) { 864 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 865 } 866 } 867 868 A2dpCodecConfigSbcSource::~A2dpCodecConfigSbcSource() {} 869 870 bool A2dpCodecConfigSbcSource::init() { 871 if (!isValid()) return false; 872 873 // Load the encoder 874 if (!A2DP_LoadEncoderSbc()) { 875 LOG_ERROR(LOG_TAG, "%s: cannot load the encoder", __func__); 876 return false; 877 } 878 879 return true; 880 } 881 882 bool A2dpCodecConfigSbcSource::useRtpHeaderMarkerBit() const { return false; } 883 884 // 885 // Selects the best sample rate from |samp_freq|. 886 // The result is stored in |p_result| and |p_codec_config|. 887 // Returns true if a selection was made, otherwise false. 888 // 889 static bool select_best_sample_rate(uint8_t samp_freq, tA2DP_SBC_CIE* p_result, 890 btav_a2dp_codec_config_t* p_codec_config) { 891 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) { 892 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_48; 893 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000; 894 return true; 895 } 896 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) { 897 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_44; 898 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100; 899 return true; 900 } 901 return false; 902 } 903 904 // 905 // Selects the audio sample rate from |p_codec_audio_config|. 906 // |samp_freq| contains the capability. 907 // The result is stored in |p_result| and |p_codec_config|. 908 // Returns true if a selection was made, otherwise false. 909 // 910 static bool select_audio_sample_rate( 911 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t samp_freq, 912 tA2DP_SBC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) { 913 switch (p_codec_audio_config->sample_rate) { 914 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100: 915 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) { 916 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_44; 917 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100; 918 return true; 919 } 920 break; 921 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000: 922 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) { 923 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_48; 924 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000; 925 return true; 926 } 927 break; 928 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200: 929 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000: 930 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400: 931 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000: 932 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000: 933 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000: 934 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE: 935 break; 936 } 937 938 return false; 939 } 940 941 // 942 // Selects the best bits per sample. 943 // The result is stored in |p_codec_config|. 944 // Returns true if a selection was made, otherwise false. 945 // 946 static bool select_best_bits_per_sample( 947 btav_a2dp_codec_config_t* p_codec_config) { 948 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16; 949 return true; 950 } 951 952 // 953 // Selects the audio bits per sample from |p_codec_audio_config|. 954 // The result is stored in |p_codec_config|. 955 // Returns true if a selection was made, otherwise false. 956 // 957 static bool select_audio_bits_per_sample( 958 const btav_a2dp_codec_config_t* p_codec_audio_config, 959 btav_a2dp_codec_config_t* p_codec_config) { 960 switch (p_codec_audio_config->bits_per_sample) { 961 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16: 962 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16; 963 return true; 964 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24: 965 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32: 966 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE: 967 break; 968 } 969 return false; 970 } 971 972 // 973 // Selects the best channel mode from |ch_mode|. 974 // The result is stored in |p_result| and |p_codec_config|. 975 // Returns true if a selection was made, otherwise false. 976 // 977 static bool select_best_channel_mode(uint8_t ch_mode, tA2DP_SBC_CIE* p_result, 978 btav_a2dp_codec_config_t* p_codec_config) { 979 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) { 980 p_result->ch_mode = A2DP_SBC_IE_CH_MD_JOINT; 981 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 982 return true; 983 } 984 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) { 985 p_result->ch_mode = A2DP_SBC_IE_CH_MD_STEREO; 986 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 987 return true; 988 } 989 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) { 990 p_result->ch_mode = A2DP_SBC_IE_CH_MD_DUAL; 991 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 992 return true; 993 } 994 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) { 995 p_result->ch_mode = A2DP_SBC_IE_CH_MD_MONO; 996 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; 997 return true; 998 } 999 return false; 1000 } 1001 1002 // 1003 // Selects the audio channel mode from |p_codec_audio_config|. 1004 // |ch_mode| contains the capability. 1005 // The result is stored in |p_result| and |p_codec_config|. 1006 // Returns true if a selection was made, otherwise false. 1007 // 1008 static bool select_audio_channel_mode( 1009 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t ch_mode, 1010 tA2DP_SBC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) { 1011 switch (p_codec_audio_config->channel_mode) { 1012 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO: 1013 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) { 1014 p_result->ch_mode = A2DP_SBC_IE_CH_MD_MONO; 1015 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; 1016 return true; 1017 } 1018 break; 1019 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO: 1020 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) { 1021 p_result->ch_mode = A2DP_SBC_IE_CH_MD_JOINT; 1022 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 1023 return true; 1024 } 1025 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) { 1026 p_result->ch_mode = A2DP_SBC_IE_CH_MD_STEREO; 1027 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 1028 return true; 1029 } 1030 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) { 1031 p_result->ch_mode = A2DP_SBC_IE_CH_MD_DUAL; 1032 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 1033 return true; 1034 } 1035 break; 1036 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE: 1037 break; 1038 } 1039 1040 return false; 1041 } 1042 1043 bool A2dpCodecConfigSbcBase::setCodecConfig(const uint8_t* p_peer_codec_info, 1044 bool is_capability, 1045 uint8_t* p_result_codec_config) { 1046 std::lock_guard<std::recursive_mutex> lock(codec_mutex_); 1047 tA2DP_SBC_CIE peer_info_cie; 1048 tA2DP_SBC_CIE result_config_cie; 1049 uint8_t samp_freq; 1050 uint8_t ch_mode; 1051 uint8_t block_len; 1052 uint8_t num_subbands; 1053 uint8_t alloc_method; 1054 const tA2DP_SBC_CIE* p_a2dp_sbc_caps = 1055 (is_source_) ? &a2dp_sbc_source_caps : &a2dp_sbc_sink_caps; 1056 1057 // Save the internal state 1058 btav_a2dp_codec_config_t saved_codec_config = codec_config_; 1059 btav_a2dp_codec_config_t saved_codec_capability = codec_capability_; 1060 btav_a2dp_codec_config_t saved_codec_selectable_capability = 1061 codec_selectable_capability_; 1062 btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_; 1063 btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_; 1064 uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE]; 1065 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE]; 1066 uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE]; 1067 memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_)); 1068 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_, 1069 sizeof(ota_codec_peer_capability_)); 1070 memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_, 1071 sizeof(ota_codec_peer_config_)); 1072 1073 tA2DP_STATUS status = 1074 A2DP_ParseInfoSbc(&peer_info_cie, p_peer_codec_info, is_capability); 1075 if (status != A2DP_SUCCESS) { 1076 LOG_ERROR(LOG_TAG, "%s: can't parse peer's capabilities: error = %d", 1077 __func__, status); 1078 goto fail; 1079 } 1080 // Try using the prefered peer codec config (if valid), instead of the peer 1081 // capability. 1082 if (is_capability) { 1083 if (is_source_) { 1084 if (A2DP_IsPeerSinkCodecValidSbc(ota_codec_peer_config_)) { 1085 status = 1086 A2DP_ParseInfoSbc(&peer_info_cie, ota_codec_peer_config_, false); 1087 } 1088 } else { 1089 if (A2DP_IsPeerSourceCodecValidSbc(ota_codec_peer_config_)) { 1090 status = 1091 A2DP_ParseInfoSbc(&peer_info_cie, ota_codec_peer_config_, false); 1092 } 1093 } 1094 if (status != A2DP_SUCCESS) { 1095 // Use the peer codec capability 1096 status = 1097 A2DP_ParseInfoSbc(&peer_info_cie, p_peer_codec_info, is_capability); 1098 CHECK(status == A2DP_SUCCESS); 1099 } 1100 } 1101 1102 // 1103 // Build the preferred configuration 1104 // 1105 memset(&result_config_cie, 0, sizeof(result_config_cie)); 1106 1107 // 1108 // Select the sample frequency 1109 // 1110 samp_freq = p_a2dp_sbc_caps->samp_freq & peer_info_cie.samp_freq; 1111 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE; 1112 switch (codec_user_config_.sample_rate) { 1113 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100: 1114 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) { 1115 result_config_cie.samp_freq = A2DP_SBC_IE_SAMP_FREQ_44; 1116 codec_capability_.sample_rate = codec_user_config_.sample_rate; 1117 codec_config_.sample_rate = codec_user_config_.sample_rate; 1118 } 1119 break; 1120 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000: 1121 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) { 1122 result_config_cie.samp_freq = A2DP_SBC_IE_SAMP_FREQ_48; 1123 codec_capability_.sample_rate = codec_user_config_.sample_rate; 1124 codec_config_.sample_rate = codec_user_config_.sample_rate; 1125 } 1126 break; 1127 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200: 1128 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000: 1129 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400: 1130 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000: 1131 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000: 1132 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000: 1133 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE: 1134 codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE; 1135 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE; 1136 break; 1137 } 1138 1139 // Select the sample frequency if there is no user preference 1140 do { 1141 // Compute the selectable capability 1142 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) { 1143 codec_selectable_capability_.sample_rate |= 1144 BTAV_A2DP_CODEC_SAMPLE_RATE_44100; 1145 } 1146 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) { 1147 codec_selectable_capability_.sample_rate |= 1148 BTAV_A2DP_CODEC_SAMPLE_RATE_48000; 1149 } 1150 1151 if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break; 1152 1153 // Compute the common capability 1154 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) 1155 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100; 1156 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) 1157 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000; 1158 1159 // No user preference - try the codec audio config 1160 if (select_audio_sample_rate(&codec_audio_config_, samp_freq, 1161 &result_config_cie, &codec_config_)) { 1162 break; 1163 } 1164 1165 // No user preference - try the default config 1166 if (select_best_sample_rate( 1167 a2dp_sbc_default_config.samp_freq & peer_info_cie.samp_freq, 1168 &result_config_cie, &codec_config_)) { 1169 break; 1170 } 1171 1172 // No user preference - use the best match 1173 if (select_best_sample_rate(samp_freq, &result_config_cie, 1174 &codec_config_)) { 1175 break; 1176 } 1177 } while (false); 1178 if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) { 1179 LOG_ERROR(LOG_TAG, 1180 "%s: cannot match sample frequency: local caps = 0x%x " 1181 "peer info = 0x%x", 1182 __func__, p_a2dp_sbc_caps->samp_freq, peer_info_cie.samp_freq); 1183 goto fail; 1184 } 1185 1186 // 1187 // Select the bits per sample 1188 // 1189 // NOTE: this information is NOT included in the SBC A2DP codec description 1190 // that is sent OTA. 1191 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE; 1192 switch (codec_user_config_.bits_per_sample) { 1193 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16: 1194 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample; 1195 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample; 1196 break; 1197 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24: 1198 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32: 1199 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE: 1200 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE; 1201 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE; 1202 break; 1203 } 1204 1205 // Select the bits per sample if there is no user preference 1206 do { 1207 // Compute the selectable capability 1208 codec_selectable_capability_.bits_per_sample = 1209 p_a2dp_sbc_caps->bits_per_sample; 1210 1211 if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) 1212 break; 1213 1214 // Compute the common capability 1215 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16; 1216 1217 // No user preference - try the codec audio config 1218 if (select_audio_bits_per_sample(&codec_audio_config_, &codec_config_)) { 1219 break; 1220 } 1221 1222 // No user preference - try the default config 1223 if (select_best_bits_per_sample(&codec_config_)) { 1224 break; 1225 } 1226 1227 // No user preference - use the best match 1228 // TODO: no-op - temporary kept here for consistency 1229 if (select_best_bits_per_sample(&codec_config_)) { 1230 break; 1231 } 1232 } while (false); 1233 if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) { 1234 LOG_ERROR(LOG_TAG, 1235 "%s: cannot match bits per sample: user preference = 0x%x", 1236 __func__, codec_user_config_.bits_per_sample); 1237 goto fail; 1238 } 1239 1240 // 1241 // Select the channel mode 1242 // 1243 ch_mode = p_a2dp_sbc_caps->ch_mode & peer_info_cie.ch_mode; 1244 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE; 1245 switch (codec_user_config_.channel_mode) { 1246 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO: 1247 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) { 1248 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_MONO; 1249 codec_capability_.channel_mode = codec_user_config_.channel_mode; 1250 codec_config_.channel_mode = codec_user_config_.channel_mode; 1251 } 1252 break; 1253 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO: 1254 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) { 1255 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_JOINT; 1256 codec_capability_.channel_mode = codec_user_config_.channel_mode; 1257 codec_config_.channel_mode = codec_user_config_.channel_mode; 1258 break; 1259 } 1260 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) { 1261 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_STEREO; 1262 codec_capability_.channel_mode = codec_user_config_.channel_mode; 1263 codec_config_.channel_mode = codec_user_config_.channel_mode; 1264 break; 1265 } 1266 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) { 1267 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_DUAL; 1268 codec_capability_.channel_mode = codec_user_config_.channel_mode; 1269 codec_config_.channel_mode = codec_user_config_.channel_mode; 1270 break; 1271 } 1272 break; 1273 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE: 1274 codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE; 1275 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE; 1276 break; 1277 } 1278 1279 // Select the channel mode if there is no user preference 1280 do { 1281 // Compute the selectable capability 1282 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) { 1283 codec_selectable_capability_.channel_mode |= 1284 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; 1285 } 1286 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) { 1287 codec_selectable_capability_.channel_mode |= 1288 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 1289 } 1290 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) { 1291 codec_selectable_capability_.channel_mode |= 1292 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 1293 } 1294 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) { 1295 codec_selectable_capability_.channel_mode |= 1296 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 1297 } 1298 1299 if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break; 1300 1301 // Compute the common capability 1302 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) 1303 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; 1304 if (ch_mode & (A2DP_SBC_IE_CH_MD_JOINT | A2DP_SBC_IE_CH_MD_STEREO | 1305 A2DP_SBC_IE_CH_MD_DUAL)) { 1306 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 1307 } 1308 1309 // No user preference - use the codec audio config 1310 if (select_audio_channel_mode(&codec_audio_config_, ch_mode, 1311 &result_config_cie, &codec_config_)) { 1312 break; 1313 } 1314 1315 // No user preference - try the default config 1316 if (select_best_channel_mode( 1317 a2dp_sbc_default_config.ch_mode & peer_info_cie.ch_mode, 1318 &result_config_cie, &codec_config_)) { 1319 break; 1320 } 1321 1322 // No user preference - use the best match 1323 if (select_best_channel_mode(ch_mode, &result_config_cie, &codec_config_)) { 1324 break; 1325 } 1326 } while (false); 1327 if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) { 1328 LOG_ERROR(LOG_TAG, 1329 "%s: cannot match channel mode: local caps = 0x%x " 1330 "peer info = 0x%x", 1331 __func__, p_a2dp_sbc_caps->ch_mode, peer_info_cie.ch_mode); 1332 goto fail; 1333 } 1334 1335 // 1336 // Select the block length 1337 // 1338 block_len = p_a2dp_sbc_caps->block_len & peer_info_cie.block_len; 1339 if (block_len & A2DP_SBC_IE_BLOCKS_16) { 1340 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_16; 1341 } else if (block_len & A2DP_SBC_IE_BLOCKS_12) { 1342 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_12; 1343 } else if (block_len & A2DP_SBC_IE_BLOCKS_8) { 1344 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_8; 1345 } else if (block_len & A2DP_SBC_IE_BLOCKS_4) { 1346 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_4; 1347 } else { 1348 LOG_ERROR(LOG_TAG, 1349 "%s: cannot match block length: local caps = 0x%x " 1350 "peer info = 0x%x", 1351 __func__, p_a2dp_sbc_caps->block_len, peer_info_cie.block_len); 1352 goto fail; 1353 } 1354 1355 // 1356 // Select the number of sub-bands 1357 // 1358 num_subbands = p_a2dp_sbc_caps->num_subbands & peer_info_cie.num_subbands; 1359 if (num_subbands & A2DP_SBC_IE_SUBBAND_8) { 1360 result_config_cie.num_subbands = A2DP_SBC_IE_SUBBAND_8; 1361 } else if (num_subbands & A2DP_SBC_IE_SUBBAND_4) { 1362 result_config_cie.num_subbands = A2DP_SBC_IE_SUBBAND_4; 1363 } else { 1364 LOG_ERROR(LOG_TAG, 1365 "%s: cannot match number of sub-bands: local caps = 0x%x " 1366 "peer info = 0x%x", 1367 __func__, p_a2dp_sbc_caps->num_subbands, 1368 peer_info_cie.num_subbands); 1369 goto fail; 1370 } 1371 1372 // 1373 // Select the allocation method 1374 // 1375 alloc_method = p_a2dp_sbc_caps->alloc_method & peer_info_cie.alloc_method; 1376 if (alloc_method & A2DP_SBC_IE_ALLOC_MD_L) { 1377 result_config_cie.alloc_method = A2DP_SBC_IE_ALLOC_MD_L; 1378 } else if (alloc_method & A2DP_SBC_IE_ALLOC_MD_S) { 1379 result_config_cie.alloc_method = A2DP_SBC_IE_ALLOC_MD_S; 1380 } else { 1381 LOG_ERROR(LOG_TAG, 1382 "%s: cannot match allocation method: local caps = 0x%x " 1383 "peer info = 0x%x", 1384 __func__, p_a2dp_sbc_caps->alloc_method, 1385 peer_info_cie.alloc_method); 1386 goto fail; 1387 } 1388 1389 // 1390 // Select the min/max bitpool 1391 // 1392 result_config_cie.min_bitpool = p_a2dp_sbc_caps->min_bitpool; 1393 if (result_config_cie.min_bitpool < peer_info_cie.min_bitpool) 1394 result_config_cie.min_bitpool = peer_info_cie.min_bitpool; 1395 result_config_cie.max_bitpool = p_a2dp_sbc_caps->max_bitpool; 1396 if (result_config_cie.max_bitpool > peer_info_cie.max_bitpool) 1397 result_config_cie.max_bitpool = peer_info_cie.max_bitpool; 1398 if (result_config_cie.min_bitpool > result_config_cie.max_bitpool) { 1399 LOG_ERROR(LOG_TAG, 1400 "%s: cannot match min/max bitpool: " 1401 "local caps min/max = 0x%x/0x%x peer info min/max = 0x%x/0x%x", 1402 __func__, p_a2dp_sbc_caps->min_bitpool, 1403 p_a2dp_sbc_caps->max_bitpool, peer_info_cie.min_bitpool, 1404 peer_info_cie.max_bitpool); 1405 goto fail; 1406 } 1407 1408 if (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie, 1409 p_result_codec_config) != A2DP_SUCCESS) { 1410 goto fail; 1411 } 1412 1413 // 1414 // Copy the codec-specific fields if they are not zero 1415 // 1416 if (codec_user_config_.codec_specific_1 != 0) 1417 codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1; 1418 if (codec_user_config_.codec_specific_2 != 0) 1419 codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2; 1420 if (codec_user_config_.codec_specific_3 != 0) 1421 codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3; 1422 if (codec_user_config_.codec_specific_4 != 0) 1423 codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4; 1424 1425 // Create a local copy of the peer codec capability/config, and the 1426 // result codec config. 1427 if (is_capability) { 1428 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie, 1429 ota_codec_peer_capability_); 1430 } else { 1431 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie, 1432 ota_codec_peer_config_); 1433 } 1434 CHECK(status == A2DP_SUCCESS); 1435 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie, 1436 ota_codec_config_); 1437 CHECK(status == A2DP_SUCCESS); 1438 return true; 1439 1440 fail: 1441 // Restore the internal state 1442 codec_config_ = saved_codec_config; 1443 codec_capability_ = saved_codec_capability; 1444 codec_selectable_capability_ = saved_codec_selectable_capability; 1445 codec_user_config_ = saved_codec_user_config; 1446 codec_audio_config_ = saved_codec_audio_config; 1447 memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_)); 1448 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability, 1449 sizeof(ota_codec_peer_capability_)); 1450 memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config, 1451 sizeof(ota_codec_peer_config_)); 1452 return false; 1453 } 1454 1455 bool A2dpCodecConfigSbcBase::setPeerCodecCapabilities( 1456 const uint8_t* p_peer_codec_capabilities) { 1457 std::lock_guard<std::recursive_mutex> lock(codec_mutex_); 1458 tA2DP_SBC_CIE peer_info_cie; 1459 uint8_t samp_freq; 1460 uint8_t ch_mode; 1461 const tA2DP_SBC_CIE* p_a2dp_sbc_caps = 1462 (is_source_) ? &a2dp_sbc_source_caps : &a2dp_sbc_sink_caps; 1463 1464 // Save the internal state 1465 btav_a2dp_codec_config_t saved_codec_selectable_capability = 1466 codec_selectable_capability_; 1467 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE]; 1468 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_, 1469 sizeof(ota_codec_peer_capability_)); 1470 1471 tA2DP_STATUS status = 1472 A2DP_ParseInfoSbc(&peer_info_cie, p_peer_codec_capabilities, true); 1473 if (status != A2DP_SUCCESS) { 1474 LOG_ERROR(LOG_TAG, "%s: can't parse peer's capabilities: error = %d", 1475 __func__, status); 1476 goto fail; 1477 } 1478 1479 // Compute the selectable capability - sample rate 1480 samp_freq = p_a2dp_sbc_caps->samp_freq & peer_info_cie.samp_freq; 1481 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) { 1482 codec_selectable_capability_.sample_rate |= 1483 BTAV_A2DP_CODEC_SAMPLE_RATE_44100; 1484 } 1485 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) { 1486 codec_selectable_capability_.sample_rate |= 1487 BTAV_A2DP_CODEC_SAMPLE_RATE_48000; 1488 } 1489 1490 // Compute the selectable capability - bits per sample 1491 codec_selectable_capability_.bits_per_sample = 1492 p_a2dp_sbc_caps->bits_per_sample; 1493 1494 // Compute the selectable capability - channel mode 1495 ch_mode = p_a2dp_sbc_caps->ch_mode & peer_info_cie.ch_mode; 1496 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) { 1497 codec_selectable_capability_.channel_mode |= 1498 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; 1499 } 1500 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) { 1501 codec_selectable_capability_.channel_mode |= 1502 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 1503 } 1504 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) { 1505 codec_selectable_capability_.channel_mode |= 1506 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 1507 } 1508 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) { 1509 codec_selectable_capability_.channel_mode |= 1510 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 1511 } 1512 1513 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie, 1514 ota_codec_peer_capability_); 1515 CHECK(status == A2DP_SUCCESS); 1516 return true; 1517 1518 fail: 1519 // Restore the internal state 1520 codec_selectable_capability_ = saved_codec_selectable_capability; 1521 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability, 1522 sizeof(ota_codec_peer_capability_)); 1523 return false; 1524 } 1525 1526 A2dpCodecConfigSbcSink::A2dpCodecConfigSbcSink( 1527 btav_a2dp_codec_priority_t codec_priority) 1528 : A2dpCodecConfigSbcBase(BTAV_A2DP_CODEC_INDEX_SINK_SBC, "SBC(Sink)", 1529 codec_priority, false) {} 1530 1531 A2dpCodecConfigSbcSink::~A2dpCodecConfigSbcSink() {} 1532 1533 bool A2dpCodecConfigSbcSink::init() { 1534 if (!isValid()) return false; 1535 1536 // Load the decoder 1537 if (!A2DP_LoadDecoderSbc()) { 1538 LOG_ERROR(LOG_TAG, "%s: cannot load the decoder", __func__); 1539 return false; 1540 } 1541 1542 return true; 1543 } 1544 1545 bool A2dpCodecConfigSbcSink::useRtpHeaderMarkerBit() const { 1546 // TODO: This method applies only to Source codecs 1547 return false; 1548 } 1549 1550 bool A2dpCodecConfigSbcSink::updateEncoderUserConfig( 1551 UNUSED_ATTR const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, 1552 UNUSED_ATTR bool* p_restart_input, UNUSED_ATTR bool* p_restart_output, 1553 UNUSED_ATTR bool* p_config_updated) { 1554 // TODO: This method applies only to Source codecs 1555 return false; 1556 } 1557 1558 period_ms_t A2dpCodecConfigSbcSink::encoderIntervalMs() const { 1559 // TODO: This method applies only to Source codecs 1560 return 0; 1561 } 1562 1563 int A2dpCodecConfigSbcSink::getEffectiveMtu() const { 1564 // TODO: This method applies only to Source codecs 1565 return 0; 1566 } 1567