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