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