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