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