1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /****************************************************************************** 18 * 19 * Utility functions to help build and parse the LDAC Codec Information 20 * Element and Media Payload. 21 * 22 ******************************************************************************/ 23 24 #define LOG_TAG "a2dp_vendor_ldac" 25 26 #include "bt_target.h" 27 28 #include "a2dp_vendor_ldac.h" 29 30 #include <string.h> 31 32 #include <base/logging.h> 33 #include "a2dp_vendor.h" 34 #include "a2dp_vendor_ldac_encoder.h" 35 #include "bt_utils.h" 36 #include "osi/include/log.h" 37 #include "osi/include/osi.h" 38 39 // data type for the LDAC Codec Information Element */ 40 // NOTE: bits_per_sample is needed only for LDAC encoder initialization. 41 typedef struct { 42 uint32_t vendorId; 43 uint16_t codecId; /* Codec ID for LDAC */ 44 uint8_t sampleRate; /* Sampling Frequency */ 45 uint8_t channelMode; /* STEREO/DUAL/MONO */ 46 btav_a2dp_codec_bits_per_sample_t bits_per_sample; 47 } tA2DP_LDAC_CIE; 48 49 /* LDAC Source codec capabilities */ 50 static const tA2DP_LDAC_CIE a2dp_ldac_caps = { 51 A2DP_LDAC_VENDOR_ID, // vendorId 52 A2DP_LDAC_CODEC_ID, // codecId 53 // sampleRate 54 (A2DP_LDAC_SAMPLING_FREQ_44100 | A2DP_LDAC_SAMPLING_FREQ_48000 | 55 A2DP_LDAC_SAMPLING_FREQ_88200 | A2DP_LDAC_SAMPLING_FREQ_96000), 56 // channelMode 57 (A2DP_LDAC_CHANNEL_MODE_DUAL | A2DP_LDAC_CHANNEL_MODE_STEREO), 58 // bits_per_sample 59 (BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 | BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24 | 60 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32)}; 61 62 /* Default LDAC codec configuration */ 63 static const tA2DP_LDAC_CIE a2dp_ldac_default_config = { 64 A2DP_LDAC_VENDOR_ID, // vendorId 65 A2DP_LDAC_CODEC_ID, // codecId 66 A2DP_LDAC_SAMPLING_FREQ_96000, // sampleRate 67 A2DP_LDAC_CHANNEL_MODE_STEREO, // channelMode 68 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32 // bits_per_sample 69 }; 70 71 static const tA2DP_ENCODER_INTERFACE a2dp_encoder_interface_ldac = { 72 a2dp_vendor_ldac_encoder_init, 73 a2dp_vendor_ldac_encoder_cleanup, 74 a2dp_vendor_ldac_feeding_reset, 75 a2dp_vendor_ldac_feeding_flush, 76 a2dp_vendor_ldac_get_encoder_interval_ms, 77 a2dp_vendor_ldac_send_frames, 78 a2dp_vendor_ldac_set_transmit_queue_length}; 79 80 UNUSED_ATTR static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityLdac( 81 const tA2DP_LDAC_CIE* p_cap, const uint8_t* p_codec_info, 82 bool is_peer_codec_info); 83 84 // Builds the LDAC Media Codec Capabilities byte sequence beginning from the 85 // LOSC octet. |media_type| is the media type |AVDT_MEDIA_TYPE_*|. 86 // |p_ie| is a pointer to the LDAC Codec Information Element information. 87 // The result is stored in |p_result|. Returns A2DP_SUCCESS on success, 88 // otherwise the corresponding A2DP error status code. 89 static tA2DP_STATUS A2DP_BuildInfoLdac(uint8_t media_type, 90 const tA2DP_LDAC_CIE* p_ie, 91 uint8_t* p_result) { 92 if (p_ie == NULL || p_result == NULL) { 93 return A2DP_INVALID_PARAMS; 94 } 95 96 *p_result++ = A2DP_LDAC_CODEC_LEN; 97 *p_result++ = (media_type << 4); 98 *p_result++ = A2DP_MEDIA_CT_NON_A2DP; 99 100 // Vendor ID and Codec ID 101 *p_result++ = (uint8_t)(p_ie->vendorId & 0x000000FF); 102 *p_result++ = (uint8_t)((p_ie->vendorId & 0x0000FF00) >> 8); 103 *p_result++ = (uint8_t)((p_ie->vendorId & 0x00FF0000) >> 16); 104 *p_result++ = (uint8_t)((p_ie->vendorId & 0xFF000000) >> 24); 105 *p_result++ = (uint8_t)(p_ie->codecId & 0x00FF); 106 *p_result++ = (uint8_t)((p_ie->codecId & 0xFF00) >> 8); 107 108 // Sampling Frequency 109 *p_result = (uint8_t)(p_ie->sampleRate & A2DP_LDAC_SAMPLING_FREQ_MASK); 110 if (*p_result == 0) return A2DP_INVALID_PARAMS; 111 p_result++; 112 113 // Channel Mode 114 *p_result = (uint8_t)(p_ie->channelMode & A2DP_LDAC_CHANNEL_MODE_MASK); 115 if (*p_result == 0) return A2DP_INVALID_PARAMS; 116 117 return A2DP_SUCCESS; 118 } 119 120 // Parses the LDAC Media Codec Capabilities byte sequence beginning from the 121 // LOSC octet. The result is stored in |p_ie|. The byte sequence to parse is 122 // |p_codec_info|. If |is_capability| is true, the byte sequence is 123 // codec capabilities, otherwise is codec configuration. 124 // Returns A2DP_SUCCESS on success, otherwise the corresponding A2DP error 125 // status code. 126 static tA2DP_STATUS A2DP_ParseInfoLdac(tA2DP_LDAC_CIE* p_ie, 127 const uint8_t* p_codec_info, 128 bool is_capability) { 129 uint8_t losc; 130 uint8_t media_type; 131 tA2DP_CODEC_TYPE codec_type; 132 133 if (p_ie == NULL || p_codec_info == NULL) return A2DP_INVALID_PARAMS; 134 135 // Check the codec capability length 136 losc = *p_codec_info++; 137 if (losc != A2DP_LDAC_CODEC_LEN) return A2DP_WRONG_CODEC; 138 139 media_type = (*p_codec_info++) >> 4; 140 codec_type = *p_codec_info++; 141 /* Check the Media Type and Media Codec Type */ 142 if (media_type != AVDT_MEDIA_TYPE_AUDIO || 143 codec_type != A2DP_MEDIA_CT_NON_A2DP) { 144 return A2DP_WRONG_CODEC; 145 } 146 147 // Check the Vendor ID and Codec ID */ 148 p_ie->vendorId = (*p_codec_info & 0x000000FF) | 149 (*(p_codec_info + 1) << 8 & 0x0000FF00) | 150 (*(p_codec_info + 2) << 16 & 0x00FF0000) | 151 (*(p_codec_info + 3) << 24 & 0xFF000000); 152 p_codec_info += 4; 153 p_ie->codecId = 154 (*p_codec_info & 0x00FF) | (*(p_codec_info + 1) << 8 & 0xFF00); 155 p_codec_info += 2; 156 if (p_ie->vendorId != A2DP_LDAC_VENDOR_ID || 157 p_ie->codecId != A2DP_LDAC_CODEC_ID) { 158 return A2DP_WRONG_CODEC; 159 } 160 161 p_ie->sampleRate = *p_codec_info++ & A2DP_LDAC_SAMPLING_FREQ_MASK; 162 p_ie->channelMode = *p_codec_info++ & A2DP_LDAC_CHANNEL_MODE_MASK; 163 164 if (is_capability) return A2DP_SUCCESS; 165 166 if (A2DP_BitsSet(p_ie->sampleRate) != A2DP_SET_ONE_BIT) 167 return A2DP_BAD_SAMP_FREQ; 168 if (A2DP_BitsSet(p_ie->channelMode) != A2DP_SET_ONE_BIT) 169 return A2DP_BAD_CH_MODE; 170 171 return A2DP_SUCCESS; 172 } 173 174 // Build the LDAC Media Payload Header. 175 // |p_dst| points to the location where the header should be written to. 176 // If |frag| is true, the media payload frame is fragmented. 177 // |start| is true for the first packet of a fragmented frame. 178 // |last| is true for the last packet of a fragmented frame. 179 // If |frag| is false, |num| is the number of number of frames in the packet, 180 // otherwise is the number of remaining fragments (including this one). 181 static void A2DP_BuildMediaPayloadHeaderLdac(uint8_t* p_dst, bool frag, 182 bool start, bool last, 183 uint8_t num) { 184 if (p_dst == NULL) return; 185 186 *p_dst = 0; 187 if (frag) *p_dst |= A2DP_LDAC_HDR_F_MSK; 188 if (start) *p_dst |= A2DP_LDAC_HDR_S_MSK; 189 if (last) *p_dst |= A2DP_LDAC_HDR_L_MSK; 190 *p_dst |= (A2DP_LDAC_HDR_NUM_MSK & num); 191 } 192 193 bool A2DP_IsVendorSourceCodecValidLdac(const uint8_t* p_codec_info) { 194 tA2DP_LDAC_CIE cfg_cie; 195 196 /* Use a liberal check when parsing the codec info */ 197 return (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) || 198 (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS); 199 } 200 201 bool A2DP_IsVendorPeerSinkCodecValidLdac(const uint8_t* p_codec_info) { 202 tA2DP_LDAC_CIE cfg_cie; 203 204 /* Use a liberal check when parsing the codec info */ 205 return (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) || 206 (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS); 207 } 208 209 // Checks whether A2DP LDAC codec configuration matches with a device's codec 210 // capabilities. |p_cap| is the LDAC codec configuration. |p_codec_info| is 211 // the device's codec capabilities. 212 // If |is_capability| is true, the byte sequence is codec capabilities, 213 // otherwise is codec configuration. 214 // |p_codec_info| contains the codec capabilities for a peer device that 215 // is acting as an A2DP source. 216 // Returns A2DP_SUCCESS if the codec configuration matches with capabilities, 217 // otherwise the corresponding A2DP error status code. 218 static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityLdac( 219 const tA2DP_LDAC_CIE* p_cap, const uint8_t* p_codec_info, 220 bool is_capability) { 221 tA2DP_STATUS status; 222 tA2DP_LDAC_CIE cfg_cie; 223 224 /* parse configuration */ 225 status = A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, is_capability); 226 if (status != A2DP_SUCCESS) { 227 LOG_ERROR(LOG_TAG, "%s: parsing failed %d", __func__, status); 228 return status; 229 } 230 231 /* verify that each parameter is in range */ 232 233 LOG_DEBUG(LOG_TAG, "%s: FREQ peer: 0x%x, capability 0x%x", __func__, 234 cfg_cie.sampleRate, p_cap->sampleRate); 235 LOG_DEBUG(LOG_TAG, "%s: CH_MODE peer: 0x%x, capability 0x%x", __func__, 236 cfg_cie.channelMode, p_cap->channelMode); 237 238 /* sampling frequency */ 239 if ((cfg_cie.sampleRate & p_cap->sampleRate) == 0) return A2DP_NS_SAMP_FREQ; 240 241 /* channel mode */ 242 if ((cfg_cie.channelMode & p_cap->channelMode) == 0) return A2DP_NS_CH_MODE; 243 244 return A2DP_SUCCESS; 245 } 246 247 bool A2DP_VendorUsesRtpHeaderLdac(UNUSED_ATTR bool content_protection_enabled, 248 UNUSED_ATTR const uint8_t* p_codec_info) { 249 // TODO: Is this correct? The RTP header is always included? 250 return true; 251 } 252 253 const char* A2DP_VendorCodecNameLdac(UNUSED_ATTR const uint8_t* p_codec_info) { 254 return "LDAC"; 255 } 256 257 bool A2DP_VendorCodecTypeEqualsLdac(const uint8_t* p_codec_info_a, 258 const uint8_t* p_codec_info_b) { 259 tA2DP_LDAC_CIE ldac_cie_a; 260 tA2DP_LDAC_CIE ldac_cie_b; 261 262 // Check whether the codec info contains valid data 263 tA2DP_STATUS a2dp_status = 264 A2DP_ParseInfoLdac(&ldac_cie_a, p_codec_info_a, true); 265 if (a2dp_status != A2DP_SUCCESS) { 266 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 267 a2dp_status); 268 return false; 269 } 270 a2dp_status = A2DP_ParseInfoLdac(&ldac_cie_b, p_codec_info_b, true); 271 if (a2dp_status != A2DP_SUCCESS) { 272 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 273 a2dp_status); 274 return false; 275 } 276 277 return true; 278 } 279 280 bool A2DP_VendorCodecEqualsLdac(const uint8_t* p_codec_info_a, 281 const uint8_t* p_codec_info_b) { 282 tA2DP_LDAC_CIE ldac_cie_a; 283 tA2DP_LDAC_CIE ldac_cie_b; 284 285 // Check whether the codec info contains valid data 286 tA2DP_STATUS a2dp_status = 287 A2DP_ParseInfoLdac(&ldac_cie_a, p_codec_info_a, true); 288 if (a2dp_status != A2DP_SUCCESS) { 289 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 290 a2dp_status); 291 return false; 292 } 293 a2dp_status = A2DP_ParseInfoLdac(&ldac_cie_b, p_codec_info_b, true); 294 if (a2dp_status != A2DP_SUCCESS) { 295 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 296 a2dp_status); 297 return false; 298 } 299 300 return (ldac_cie_a.sampleRate == ldac_cie_b.sampleRate) && 301 (ldac_cie_a.channelMode == ldac_cie_b.channelMode); 302 } 303 304 int A2DP_VendorGetTrackSampleRateLdac(const uint8_t* p_codec_info) { 305 tA2DP_LDAC_CIE ldac_cie; 306 307 // Check whether the codec info contains valid data 308 tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false); 309 if (a2dp_status != A2DP_SUCCESS) { 310 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 311 a2dp_status); 312 return -1; 313 } 314 315 switch (ldac_cie.sampleRate) { 316 case A2DP_LDAC_SAMPLING_FREQ_44100: 317 return 44100; 318 case A2DP_LDAC_SAMPLING_FREQ_48000: 319 return 48000; 320 case A2DP_LDAC_SAMPLING_FREQ_88200: 321 return 88200; 322 case A2DP_LDAC_SAMPLING_FREQ_96000: 323 return 96000; 324 case A2DP_LDAC_SAMPLING_FREQ_176400: 325 return 176400; 326 case A2DP_LDAC_SAMPLING_FREQ_192000: 327 return 192000; 328 } 329 330 return -1; 331 } 332 333 int A2DP_VendorGetTrackBitsPerSampleLdac(const uint8_t* p_codec_info) { 334 tA2DP_LDAC_CIE ldac_cie; 335 336 // Check whether the codec info contains valid data 337 tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false); 338 if (a2dp_status != A2DP_SUCCESS) { 339 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 340 a2dp_status); 341 return -1; 342 } 343 344 switch (a2dp_ldac_caps.bits_per_sample) { 345 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16: 346 return 16; 347 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24: 348 return 24; 349 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32: 350 return 32; 351 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE: 352 break; 353 } 354 return -1; 355 } 356 357 int A2DP_VendorGetTrackChannelCountLdac(const uint8_t* p_codec_info) { 358 tA2DP_LDAC_CIE ldac_cie; 359 360 // Check whether the codec info contains valid data 361 tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false); 362 if (a2dp_status != A2DP_SUCCESS) { 363 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 364 a2dp_status); 365 return -1; 366 } 367 368 switch (ldac_cie.channelMode) { 369 case A2DP_LDAC_CHANNEL_MODE_MONO: 370 return 1; 371 case A2DP_LDAC_CHANNEL_MODE_DUAL: 372 return 2; 373 case A2DP_LDAC_CHANNEL_MODE_STEREO: 374 return 2; 375 } 376 377 return -1; 378 } 379 380 int A2DP_VendorGetChannelModeCodeLdac(const uint8_t* p_codec_info) { 381 tA2DP_LDAC_CIE ldac_cie; 382 383 // Check whether the codec info contains valid data 384 tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false); 385 if (a2dp_status != A2DP_SUCCESS) { 386 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__, 387 a2dp_status); 388 return -1; 389 } 390 391 switch (ldac_cie.channelMode) { 392 case A2DP_LDAC_CHANNEL_MODE_MONO: 393 case A2DP_LDAC_CHANNEL_MODE_DUAL: 394 case A2DP_LDAC_CHANNEL_MODE_STEREO: 395 return ldac_cie.channelMode; 396 default: 397 break; 398 } 399 400 return -1; 401 } 402 403 bool A2DP_VendorGetPacketTimestampLdac(UNUSED_ATTR const uint8_t* p_codec_info, 404 const uint8_t* p_data, 405 uint32_t* p_timestamp) { 406 // TODO: Is this function really codec-specific? 407 *p_timestamp = *(const uint32_t*)p_data; 408 return true; 409 } 410 411 bool A2DP_VendorBuildCodecHeaderLdac(UNUSED_ATTR const uint8_t* p_codec_info, 412 BT_HDR* p_buf, 413 uint16_t frames_per_packet) { 414 uint8_t* p; 415 416 p_buf->offset -= A2DP_LDAC_MPL_HDR_LEN; 417 p = (uint8_t*)(p_buf + 1) + p_buf->offset; 418 p_buf->len += A2DP_LDAC_MPL_HDR_LEN; 419 A2DP_BuildMediaPayloadHeaderLdac(p, false, false, false, 420 (uint8_t)frames_per_packet); 421 422 return true; 423 } 424 425 void A2DP_VendorDumpCodecInfoLdac(const uint8_t* p_codec_info) { 426 tA2DP_STATUS a2dp_status; 427 tA2DP_LDAC_CIE ldac_cie; 428 429 LOG_DEBUG(LOG_TAG, "%s", __func__); 430 431 a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, true); 432 if (a2dp_status != A2DP_SUCCESS) { 433 LOG_ERROR(LOG_TAG, "%s: A2DP_ParseInfoLdac fail:%d", __func__, a2dp_status); 434 return; 435 } 436 437 LOG_DEBUG(LOG_TAG, "\tsamp_freq: 0x%x", ldac_cie.sampleRate); 438 if (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) { 439 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (44100)"); 440 } 441 if (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) { 442 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (48000)"); 443 } 444 if (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) { 445 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (88200)"); 446 } 447 if (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) { 448 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (96000)"); 449 } 450 if (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) { 451 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (176400)"); 452 } 453 if (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) { 454 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (192000)"); 455 } 456 457 LOG_DEBUG(LOG_TAG, "\tch_mode: 0x%x", ldac_cie.channelMode); 458 if (ldac_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) { 459 LOG_DEBUG(LOG_TAG, "\tch_mode: (Mono)"); 460 } 461 if (ldac_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) { 462 LOG_DEBUG(LOG_TAG, "\tch_mode: (Dual)"); 463 } 464 if (ldac_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) { 465 LOG_DEBUG(LOG_TAG, "\tch_mode: (Stereo)"); 466 } 467 } 468 469 const tA2DP_ENCODER_INTERFACE* A2DP_VendorGetEncoderInterfaceLdac( 470 const uint8_t* p_codec_info) { 471 if (!A2DP_IsVendorSourceCodecValidLdac(p_codec_info)) return NULL; 472 473 return &a2dp_encoder_interface_ldac; 474 } 475 476 bool A2DP_VendorAdjustCodecLdac(uint8_t* p_codec_info) { 477 tA2DP_LDAC_CIE cfg_cie; 478 479 // Nothing to do: just verify the codec info is valid 480 if (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS) 481 return false; 482 483 return true; 484 } 485 486 btav_a2dp_codec_index_t A2DP_VendorSourceCodecIndexLdac( 487 UNUSED_ATTR const uint8_t* p_codec_info) { 488 return BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC; 489 } 490 491 const char* A2DP_VendorCodecIndexStrLdac(void) { return "LDAC"; } 492 493 bool A2DP_VendorInitCodecConfigLdac(tAVDT_CFG* p_cfg) { 494 if (A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &a2dp_ldac_caps, 495 p_cfg->codec_info) != A2DP_SUCCESS) { 496 return false; 497 } 498 499 #if (BTA_AV_CO_CP_SCMS_T == TRUE) 500 /* Content protection info - support SCMS-T */ 501 uint8_t* p = p_cfg->protect_info; 502 *p++ = AVDT_CP_LOSC; 503 UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID); 504 p_cfg->num_protect = 1; 505 #endif 506 507 return true; 508 } 509 510 UNUSED_ATTR static void build_codec_config(const tA2DP_LDAC_CIE& config_cie, 511 btav_a2dp_codec_config_t* result) { 512 if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) 513 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100; 514 if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) 515 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000; 516 if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) 517 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200; 518 if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) 519 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000; 520 if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) 521 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_176400; 522 if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) 523 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_192000; 524 525 result->bits_per_sample = config_cie.bits_per_sample; 526 527 if (config_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) 528 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; 529 if (config_cie.channelMode & 530 (A2DP_LDAC_CHANNEL_MODE_DUAL | A2DP_LDAC_CHANNEL_MODE_STEREO)) { 531 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 532 } 533 } 534 535 A2dpCodecConfigLdac::A2dpCodecConfigLdac( 536 btav_a2dp_codec_priority_t codec_priority) 537 : A2dpCodecConfig(BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC, "LDAC", 538 codec_priority) { 539 // Compute the local capability 540 if (a2dp_ldac_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) { 541 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100; 542 } 543 if (a2dp_ldac_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) { 544 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000; 545 } 546 if (a2dp_ldac_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) { 547 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200; 548 } 549 if (a2dp_ldac_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) { 550 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000; 551 } 552 if (a2dp_ldac_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) { 553 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_176400; 554 } 555 if (a2dp_ldac_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) { 556 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_192000; 557 } 558 codec_local_capability_.bits_per_sample = a2dp_ldac_caps.bits_per_sample; 559 if (a2dp_ldac_caps.channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) { 560 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; 561 } 562 if (a2dp_ldac_caps.channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) { 563 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 564 } 565 if (a2dp_ldac_caps.channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) { 566 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 567 } 568 } 569 570 A2dpCodecConfigLdac::~A2dpCodecConfigLdac() {} 571 572 bool A2dpCodecConfigLdac::init() { 573 if (!isValid()) return false; 574 575 // Load the encoder 576 if (!A2DP_VendorLoadEncoderLdac()) { 577 LOG_ERROR(LOG_TAG, "%s: cannot load the encoder", __func__); 578 return false; 579 } 580 581 return true; 582 } 583 584 bool A2dpCodecConfigLdac::useRtpHeaderMarkerBit() const { return false; } 585 586 // 587 // Selects the best sample rate from |sampleRate|. 588 // The result is stored in |p_result| and |p_codec_config|. 589 // Returns true if a selection was made, otherwise false. 590 // 591 static bool select_best_sample_rate(uint8_t sampleRate, 592 tA2DP_LDAC_CIE* p_result, 593 btav_a2dp_codec_config_t* p_codec_config) { 594 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) { 595 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000; 596 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_192000; 597 return true; 598 } 599 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) { 600 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400; 601 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_176400; 602 return true; 603 } 604 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) { 605 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000; 606 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000; 607 return true; 608 } 609 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) { 610 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200; 611 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200; 612 return true; 613 } 614 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) { 615 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000; 616 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000; 617 return true; 618 } 619 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) { 620 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100; 621 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100; 622 return true; 623 } 624 return false; 625 } 626 627 // 628 // Selects the audio sample rate from |p_codec_audio_config|. 629 // |sampleRate| contains the capability. 630 // The result is stored in |p_result| and |p_codec_config|. 631 // Returns true if a selection was made, otherwise false. 632 // 633 static bool select_audio_sample_rate( 634 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t sampleRate, 635 tA2DP_LDAC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) { 636 switch (p_codec_audio_config->sample_rate) { 637 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100: 638 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) { 639 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100; 640 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100; 641 return true; 642 } 643 break; 644 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000: 645 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) { 646 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000; 647 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000; 648 return true; 649 } 650 break; 651 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200: 652 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) { 653 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200; 654 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200; 655 return true; 656 } 657 break; 658 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000: 659 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) { 660 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000; 661 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000; 662 return true; 663 } 664 break; 665 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400: 666 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) { 667 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400; 668 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_176400; 669 return true; 670 } 671 break; 672 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000: 673 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) { 674 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000; 675 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_192000; 676 return true; 677 } 678 break; 679 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE: 680 break; 681 } 682 return false; 683 } 684 685 // 686 // Selects the best bits per sample from |bits_per_sample|. 687 // |bits_per_sample| contains the capability. 688 // The result is stored in |p_result| and |p_codec_config|. 689 // Returns true if a selection was made, otherwise false. 690 // 691 static bool select_best_bits_per_sample( 692 btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_LDAC_CIE* p_result, 693 btav_a2dp_codec_config_t* p_codec_config) { 694 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) { 695 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32; 696 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32; 697 return true; 698 } 699 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) { 700 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24; 701 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24; 702 return true; 703 } 704 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) { 705 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16; 706 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16; 707 return true; 708 } 709 return false; 710 } 711 712 // 713 // Selects the audio bits per sample from |p_codec_audio_config|. 714 // |bits_per_sample| contains the capability. 715 // The result is stored in |p_result| and |p_codec_config|. 716 // Returns true if a selection was made, otherwise false. 717 // 718 static bool select_audio_bits_per_sample( 719 const btav_a2dp_codec_config_t* p_codec_audio_config, 720 btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_LDAC_CIE* p_result, 721 btav_a2dp_codec_config_t* p_codec_config) { 722 switch (p_codec_audio_config->bits_per_sample) { 723 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16: 724 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) { 725 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16; 726 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16; 727 return true; 728 } 729 break; 730 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24: 731 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) { 732 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24; 733 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24; 734 return true; 735 } 736 break; 737 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32: 738 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) { 739 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32; 740 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32; 741 return true; 742 } 743 break; 744 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE: 745 break; 746 } 747 return false; 748 } 749 750 // 751 // Selects the best channel mode from |channelMode|. 752 // The result is stored in |p_result| and |p_codec_config|. 753 // Returns true if a selection was made, otherwise false. 754 // 755 static bool select_best_channel_mode(uint8_t channelMode, 756 tA2DP_LDAC_CIE* p_result, 757 btav_a2dp_codec_config_t* p_codec_config) { 758 if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) { 759 p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO; 760 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 761 return true; 762 } 763 if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) { 764 p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL; 765 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 766 return true; 767 } 768 if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) { 769 p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_MONO; 770 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; 771 return true; 772 } 773 return false; 774 } 775 776 // 777 // Selects the audio channel mode from |p_codec_audio_config|. 778 // |channelMode| contains the capability. 779 // The result is stored in |p_result| and |p_codec_config|. 780 // Returns true if a selection was made, otherwise false. 781 // 782 static bool select_audio_channel_mode( 783 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t channelMode, 784 tA2DP_LDAC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) { 785 switch (p_codec_audio_config->channel_mode) { 786 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO: 787 if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) { 788 p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_MONO; 789 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; 790 return true; 791 } 792 break; 793 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO: 794 if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) { 795 p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO; 796 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 797 return true; 798 } 799 if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) { 800 p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL; 801 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 802 return true; 803 } 804 break; 805 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE: 806 break; 807 } 808 809 return false; 810 } 811 812 bool A2dpCodecConfigLdac::setCodecConfig(const uint8_t* p_peer_codec_info, 813 bool is_capability, 814 uint8_t* p_result_codec_config) { 815 std::lock_guard<std::recursive_mutex> lock(codec_mutex_); 816 tA2DP_LDAC_CIE sink_info_cie; 817 tA2DP_LDAC_CIE result_config_cie; 818 uint8_t channelMode; 819 uint8_t sampleRate; 820 btav_a2dp_codec_bits_per_sample_t bits_per_sample; 821 822 // Save the internal state 823 btav_a2dp_codec_config_t saved_codec_config = codec_config_; 824 btav_a2dp_codec_config_t saved_codec_capability = codec_capability_; 825 btav_a2dp_codec_config_t saved_codec_selectable_capability = 826 codec_selectable_capability_; 827 btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_; 828 btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_; 829 uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE]; 830 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE]; 831 uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE]; 832 memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_)); 833 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_, 834 sizeof(ota_codec_peer_capability_)); 835 memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_, 836 sizeof(ota_codec_peer_config_)); 837 838 tA2DP_STATUS status = 839 A2DP_ParseInfoLdac(&sink_info_cie, p_peer_codec_info, is_capability); 840 if (status != A2DP_SUCCESS) { 841 LOG_ERROR(LOG_TAG, "%s: can't parse peer's Sink capabilities: error = %d", 842 __func__, status); 843 goto fail; 844 } 845 846 // 847 // Build the preferred configuration 848 // 849 memset(&result_config_cie, 0, sizeof(result_config_cie)); 850 result_config_cie.vendorId = a2dp_ldac_caps.vendorId; 851 result_config_cie.codecId = a2dp_ldac_caps.codecId; 852 853 // 854 // Select the sample frequency 855 // 856 sampleRate = a2dp_ldac_caps.sampleRate & sink_info_cie.sampleRate; 857 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE; 858 switch (codec_user_config_.sample_rate) { 859 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100: 860 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) { 861 result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100; 862 codec_capability_.sample_rate = codec_user_config_.sample_rate; 863 codec_config_.sample_rate = codec_user_config_.sample_rate; 864 } 865 break; 866 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000: 867 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) { 868 result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000; 869 codec_capability_.sample_rate = codec_user_config_.sample_rate; 870 codec_config_.sample_rate = codec_user_config_.sample_rate; 871 } 872 break; 873 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200: 874 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) { 875 result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200; 876 codec_capability_.sample_rate = codec_user_config_.sample_rate; 877 codec_config_.sample_rate = codec_user_config_.sample_rate; 878 } 879 break; 880 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000: 881 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) { 882 result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000; 883 codec_capability_.sample_rate = codec_user_config_.sample_rate; 884 codec_config_.sample_rate = codec_user_config_.sample_rate; 885 } 886 break; 887 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400: 888 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) { 889 result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400; 890 codec_capability_.sample_rate = codec_user_config_.sample_rate; 891 codec_config_.sample_rate = codec_user_config_.sample_rate; 892 } 893 break; 894 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000: 895 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) { 896 result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000; 897 codec_capability_.sample_rate = codec_user_config_.sample_rate; 898 codec_config_.sample_rate = codec_user_config_.sample_rate; 899 } 900 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE: 901 codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE; 902 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE; 903 break; 904 } 905 906 // Select the sample frequency if there is no user preference 907 do { 908 // Compute the selectable capability 909 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) { 910 codec_selectable_capability_.sample_rate |= 911 BTAV_A2DP_CODEC_SAMPLE_RATE_44100; 912 } 913 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) { 914 codec_selectable_capability_.sample_rate |= 915 BTAV_A2DP_CODEC_SAMPLE_RATE_48000; 916 } 917 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) { 918 codec_selectable_capability_.sample_rate |= 919 BTAV_A2DP_CODEC_SAMPLE_RATE_88200; 920 } 921 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) { 922 codec_selectable_capability_.sample_rate |= 923 BTAV_A2DP_CODEC_SAMPLE_RATE_96000; 924 } 925 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) { 926 codec_selectable_capability_.sample_rate |= 927 BTAV_A2DP_CODEC_SAMPLE_RATE_176400; 928 } 929 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) { 930 codec_selectable_capability_.sample_rate |= 931 BTAV_A2DP_CODEC_SAMPLE_RATE_192000; 932 } 933 934 if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break; 935 936 // Compute the common capability 937 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) 938 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100; 939 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) 940 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000; 941 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) 942 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200; 943 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) 944 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000; 945 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) 946 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_176400; 947 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) 948 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_192000; 949 950 // No user preference - try the codec audio config 951 if (select_audio_sample_rate(&codec_audio_config_, sampleRate, 952 &result_config_cie, &codec_config_)) { 953 break; 954 } 955 956 // No user preference - try the default config 957 if (select_best_sample_rate( 958 a2dp_ldac_default_config.sampleRate & sink_info_cie.sampleRate, 959 &result_config_cie, &codec_config_)) { 960 break; 961 } 962 963 // No user preference - use the best match 964 if (select_best_sample_rate(sampleRate, &result_config_cie, 965 &codec_config_)) { 966 break; 967 } 968 } while (false); 969 if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) { 970 LOG_ERROR(LOG_TAG, 971 "%s: cannot match sample frequency: source caps = 0x%x " 972 "sink info = 0x%x", 973 __func__, a2dp_ldac_caps.sampleRate, sink_info_cie.sampleRate); 974 goto fail; 975 } 976 977 // 978 // Select the bits per sample 979 // 980 // NOTE: this information is NOT included in the LDAC A2DP codec description 981 // that is sent OTA. 982 bits_per_sample = a2dp_ldac_caps.bits_per_sample; 983 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE; 984 switch (codec_user_config_.bits_per_sample) { 985 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16: 986 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) { 987 result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample; 988 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample; 989 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample; 990 } 991 break; 992 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24: 993 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) { 994 result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample; 995 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample; 996 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample; 997 } 998 break; 999 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32: 1000 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) { 1001 result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample; 1002 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample; 1003 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample; 1004 } 1005 break; 1006 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE: 1007 result_config_cie.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE; 1008 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE; 1009 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE; 1010 break; 1011 } 1012 1013 // Select the bits per sample if there is no user preference 1014 do { 1015 // Compute the selectable capability 1016 codec_selectable_capability_.bits_per_sample = 1017 a2dp_ldac_caps.bits_per_sample; 1018 1019 if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) 1020 break; 1021 1022 // Compute the common capability 1023 codec_capability_.bits_per_sample = bits_per_sample; 1024 1025 // No user preference - the the codec audio config 1026 if (select_audio_bits_per_sample(&codec_audio_config_, 1027 a2dp_ldac_caps.bits_per_sample, 1028 &result_config_cie, &codec_config_)) { 1029 break; 1030 } 1031 1032 // No user preference - try the default config 1033 if (select_best_bits_per_sample(a2dp_ldac_default_config.bits_per_sample, 1034 &result_config_cie, &codec_config_)) { 1035 break; 1036 } 1037 1038 // No user preference - use the best match 1039 if (select_best_bits_per_sample(a2dp_ldac_caps.bits_per_sample, 1040 &result_config_cie, &codec_config_)) { 1041 break; 1042 } 1043 } while (false); 1044 if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) { 1045 LOG_ERROR(LOG_TAG, 1046 "%s: cannot match bits per sample: default = 0x%x " 1047 "user preference = 0x%x", 1048 __func__, a2dp_ldac_default_config.bits_per_sample, 1049 codec_user_config_.bits_per_sample); 1050 goto fail; 1051 } 1052 1053 // 1054 // Select the channel mode 1055 // 1056 channelMode = a2dp_ldac_caps.channelMode & sink_info_cie.channelMode; 1057 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE; 1058 switch (codec_user_config_.channel_mode) { 1059 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO: 1060 if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) { 1061 result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_MONO; 1062 codec_capability_.channel_mode = codec_user_config_.channel_mode; 1063 codec_config_.channel_mode = codec_user_config_.channel_mode; 1064 } 1065 break; 1066 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO: 1067 if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) { 1068 result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO; 1069 codec_capability_.channel_mode = codec_user_config_.channel_mode; 1070 codec_config_.channel_mode = codec_user_config_.channel_mode; 1071 break; 1072 } 1073 if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) { 1074 result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL; 1075 codec_capability_.channel_mode = codec_user_config_.channel_mode; 1076 codec_config_.channel_mode = codec_user_config_.channel_mode; 1077 break; 1078 } 1079 break; 1080 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE: 1081 codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE; 1082 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE; 1083 break; 1084 } 1085 1086 // Select the channel mode if there is no user preference 1087 do { 1088 // Compute the selectable capability 1089 if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) { 1090 codec_selectable_capability_.channel_mode |= 1091 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; 1092 } 1093 if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) { 1094 codec_selectable_capability_.channel_mode |= 1095 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 1096 } 1097 if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) { 1098 codec_selectable_capability_.channel_mode |= 1099 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 1100 } 1101 1102 if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break; 1103 1104 // Compute the common capability 1105 if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) 1106 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; 1107 if (channelMode & 1108 (A2DP_LDAC_CHANNEL_MODE_STEREO | A2DP_LDAC_CHANNEL_MODE_DUAL)) { 1109 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; 1110 } 1111 1112 // No user preference - try the codec audio config 1113 if (select_audio_channel_mode(&codec_audio_config_, channelMode, 1114 &result_config_cie, &codec_config_)) { 1115 break; 1116 } 1117 1118 // No user preference - try the default config 1119 if (select_best_channel_mode( 1120 a2dp_ldac_default_config.channelMode & sink_info_cie.channelMode, 1121 &result_config_cie, &codec_config_)) { 1122 break; 1123 } 1124 1125 // No user preference - use the best match 1126 if (select_best_channel_mode(channelMode, &result_config_cie, 1127 &codec_config_)) { 1128 break; 1129 } 1130 } while (false); 1131 if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) { 1132 LOG_ERROR(LOG_TAG, 1133 "%s: cannot match channel mode: source caps = 0x%x " 1134 "sink info = 0x%x", 1135 __func__, a2dp_ldac_caps.channelMode, sink_info_cie.channelMode); 1136 goto fail; 1137 } 1138 1139 if (A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie, 1140 p_result_codec_config) != A2DP_SUCCESS) { 1141 goto fail; 1142 } 1143 1144 // 1145 // Copy the codec-specific fields if they are not zero 1146 // 1147 if (codec_user_config_.codec_specific_1 != 0) 1148 codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1; 1149 if (codec_user_config_.codec_specific_2 != 0) 1150 codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2; 1151 if (codec_user_config_.codec_specific_3 != 0) 1152 codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3; 1153 if (codec_user_config_.codec_specific_4 != 0) 1154 codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4; 1155 1156 // Create a local copy of the peer codec capability, and the 1157 // result codec config. 1158 if (is_capability) { 1159 status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &sink_info_cie, 1160 ota_codec_peer_capability_); 1161 } else { 1162 status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &sink_info_cie, 1163 ota_codec_peer_config_); 1164 } 1165 CHECK(status == A2DP_SUCCESS); 1166 status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie, 1167 ota_codec_config_); 1168 CHECK(status == A2DP_SUCCESS); 1169 return true; 1170 1171 fail: 1172 // Restore the internal state 1173 codec_config_ = saved_codec_config; 1174 codec_capability_ = saved_codec_capability; 1175 codec_selectable_capability_ = saved_codec_selectable_capability; 1176 codec_user_config_ = saved_codec_user_config; 1177 codec_audio_config_ = saved_codec_audio_config; 1178 memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_)); 1179 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability, 1180 sizeof(ota_codec_peer_capability_)); 1181 memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config, 1182 sizeof(ota_codec_peer_config_)); 1183 return false; 1184 } 1185