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 aptX-HD Codec Information
     20  *  Element and Media Payload.
     21  *
     22  ******************************************************************************/
     23 
     24 #define LOG_TAG "a2dp_vendor_aptx_hd"
     25 
     26 #include "bt_target.h"
     27 
     28 #include "a2dp_vendor_aptx_hd.h"
     29 
     30 #include <string.h>
     31 
     32 #include <base/logging.h>
     33 #include "a2dp_vendor.h"
     34 #include "a2dp_vendor_aptx_hd_encoder.h"
     35 #include "bt_utils.h"
     36 #include "osi/include/log.h"
     37 #include "osi/include/osi.h"
     38 
     39 // data type for the aptX-HD Codec Information Element */
     40 typedef struct {
     41   uint32_t vendorId;
     42   uint16_t codecId;    /* Codec ID for aptX-HD */
     43   uint8_t sampleRate;  /* Sampling Frequency */
     44   uint8_t channelMode; /* STEREO/DUAL/MONO */
     45   uint8_t acl_sprint_reserved0;
     46   uint8_t acl_sprint_reserved1;
     47   uint8_t acl_sprint_reserved2;
     48   uint8_t acl_sprint_reserved3;
     49   btav_a2dp_codec_bits_per_sample_t bits_per_sample;
     50 } tA2DP_APTX_HD_CIE;
     51 
     52 /* aptX-HD Source codec capabilities */
     53 static const tA2DP_APTX_HD_CIE a2dp_aptx_hd_caps = {
     54     A2DP_APTX_HD_VENDOR_ID,          /* vendorId */
     55     A2DP_APTX_HD_CODEC_ID_BLUETOOTH, /* codecId */
     56     (A2DP_APTX_HD_SAMPLERATE_44100 |
     57      A2DP_APTX_HD_SAMPLERATE_48000),   /* sampleRate */
     58     A2DP_APTX_HD_CHANNELS_STEREO,      /* channelMode */
     59     A2DP_APTX_HD_ACL_SPRINT_RESERVED0, /* acl_sprint_reserved0 */
     60     A2DP_APTX_HD_ACL_SPRINT_RESERVED1, /* acl_sprint_reserved1 */
     61     A2DP_APTX_HD_ACL_SPRINT_RESERVED2, /* acl_sprint_reserved2 */
     62     A2DP_APTX_HD_ACL_SPRINT_RESERVED3, /* acl_sprint_reserved3 */
     63     BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24 /* bits_per_sample */
     64 };
     65 
     66 /* Default aptX-HD codec configuration */
     67 static const tA2DP_APTX_HD_CIE a2dp_aptx_hd_default_config = {
     68     A2DP_APTX_HD_VENDOR_ID,            /* vendorId */
     69     A2DP_APTX_HD_CODEC_ID_BLUETOOTH,   /* codecId */
     70     A2DP_APTX_HD_SAMPLERATE_44100,     /* sampleRate */
     71     A2DP_APTX_HD_CHANNELS_STEREO,      /* channelMode */
     72     A2DP_APTX_HD_ACL_SPRINT_RESERVED0, /* acl_sprint_reserved0 */
     73     A2DP_APTX_HD_ACL_SPRINT_RESERVED1, /* acl_sprint_reserved1 */
     74     A2DP_APTX_HD_ACL_SPRINT_RESERVED2, /* acl_sprint_reserved2 */
     75     A2DP_APTX_HD_ACL_SPRINT_RESERVED3, /* acl_sprint_reserved3 */
     76     BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24 /* bits_per_sample */
     77 };
     78 
     79 static const tA2DP_ENCODER_INTERFACE a2dp_encoder_interface_aptx_hd = {
     80     a2dp_vendor_aptx_hd_encoder_init,
     81     a2dp_vendor_aptx_hd_encoder_cleanup,
     82     a2dp_vendor_aptx_hd_feeding_reset,
     83     a2dp_vendor_aptx_hd_feeding_flush,
     84     a2dp_vendor_aptx_hd_get_encoder_interval_ms,
     85     a2dp_vendor_aptx_hd_send_frames,
     86     nullptr  // set_transmit_queue_length
     87 };
     88 
     89 UNUSED_ATTR static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityAptxHd(
     90     const tA2DP_APTX_HD_CIE* p_cap, const uint8_t* p_codec_info,
     91     bool is_peer_codec_info);
     92 
     93 // Builds the aptX-HD Media Codec Capabilities byte sequence beginning from the
     94 // LOSC octet. |media_type| is the media type |AVDT_MEDIA_TYPE_*|.
     95 // |p_ie| is a pointer to the aptX-HD Codec Information Element information.
     96 // The result is stored in |p_result|. Returns A2DP_SUCCESS on success,
     97 // otherwise the corresponding A2DP error status code.
     98 static tA2DP_STATUS A2DP_BuildInfoAptxHd(uint8_t media_type,
     99                                          const tA2DP_APTX_HD_CIE* p_ie,
    100                                          uint8_t* p_result) {
    101   if (p_ie == NULL || p_result == NULL) {
    102     return A2DP_INVALID_PARAMS;
    103   }
    104 
    105   *p_result++ = A2DP_APTX_HD_CODEC_LEN;
    106   *p_result++ = (media_type << 4);
    107   *p_result++ = A2DP_MEDIA_CT_NON_A2DP;
    108   *p_result++ = (uint8_t)(p_ie->vendorId & 0x000000FF);
    109   *p_result++ = (uint8_t)((p_ie->vendorId & 0x0000FF00) >> 8);
    110   *p_result++ = (uint8_t)((p_ie->vendorId & 0x00FF0000) >> 16);
    111   *p_result++ = (uint8_t)((p_ie->vendorId & 0xFF000000) >> 24);
    112   *p_result++ = (uint8_t)(p_ie->codecId & 0x00FF);
    113   *p_result++ = (uint8_t)((p_ie->codecId & 0xFF00) >> 8);
    114   *p_result++ = p_ie->sampleRate | p_ie->channelMode;
    115   *p_result++ = p_ie->acl_sprint_reserved0;
    116   *p_result++ = p_ie->acl_sprint_reserved1;
    117   *p_result++ = p_ie->acl_sprint_reserved2;
    118   *p_result++ = p_ie->acl_sprint_reserved3;
    119 
    120   return A2DP_SUCCESS;
    121 }
    122 
    123 // Parses the aptX-HD Media Codec Capabilities byte sequence beginning from the
    124 // LOSC octet. The result is stored in |p_ie|. The byte sequence to parse is
    125 // |p_codec_info|. If |is_capability| is true, the byte sequence is
    126 // codec capabilities, otherwise is codec configuration.
    127 // Returns A2DP_SUCCESS on success, otherwise the corresponding A2DP error
    128 // status code.
    129 static tA2DP_STATUS A2DP_ParseInfoAptxHd(tA2DP_APTX_HD_CIE* p_ie,
    130                                          const uint8_t* p_codec_info,
    131                                          bool is_capability) {
    132   uint8_t losc;
    133   uint8_t media_type;
    134   tA2DP_CODEC_TYPE codec_type;
    135 
    136   if (p_ie == NULL || p_codec_info == NULL) return A2DP_INVALID_PARAMS;
    137 
    138   // Check the codec capability length
    139   losc = *p_codec_info++;
    140   if (losc != A2DP_APTX_HD_CODEC_LEN) return A2DP_WRONG_CODEC;
    141 
    142   media_type = (*p_codec_info++) >> 4;
    143   codec_type = *p_codec_info++;
    144   /* Check the Media Type and Media Codec Type */
    145   if (media_type != AVDT_MEDIA_TYPE_AUDIO ||
    146       codec_type != A2DP_MEDIA_CT_NON_A2DP) {
    147     return A2DP_WRONG_CODEC;
    148   }
    149 
    150   // Check the Vendor ID and Codec ID */
    151   p_ie->vendorId = (*p_codec_info & 0x000000FF) |
    152                    (*(p_codec_info + 1) << 8 & 0x0000FF00) |
    153                    (*(p_codec_info + 2) << 16 & 0x00FF0000) |
    154                    (*(p_codec_info + 3) << 24 & 0xFF000000);
    155   p_codec_info += 4;
    156   p_ie->codecId =
    157       (*p_codec_info & 0x00FF) | (*(p_codec_info + 1) << 8 & 0xFF00);
    158   p_codec_info += 2;
    159   if (p_ie->vendorId != A2DP_APTX_HD_VENDOR_ID ||
    160       p_ie->codecId != A2DP_APTX_HD_CODEC_ID_BLUETOOTH) {
    161     return A2DP_WRONG_CODEC;
    162   }
    163 
    164   p_ie->channelMode = *p_codec_info & 0x0F;
    165   p_ie->sampleRate = *p_codec_info & 0xF0;
    166   p_codec_info++;
    167 
    168   p_ie->acl_sprint_reserved0 = *(p_codec_info++);
    169   p_ie->acl_sprint_reserved1 = *(p_codec_info++);
    170   p_ie->acl_sprint_reserved2 = *(p_codec_info++);
    171   p_ie->acl_sprint_reserved3 = *(p_codec_info++);
    172 
    173   if (is_capability) return A2DP_SUCCESS;
    174 
    175   if (A2DP_BitsSet(p_ie->sampleRate) != A2DP_SET_ONE_BIT)
    176     return A2DP_BAD_SAMP_FREQ;
    177   if (A2DP_BitsSet(p_ie->channelMode) != A2DP_SET_ONE_BIT)
    178     return A2DP_BAD_CH_MODE;
    179 
    180   return A2DP_SUCCESS;
    181 }
    182 
    183 bool A2DP_IsVendorSourceCodecValidAptxHd(const uint8_t* p_codec_info) {
    184   tA2DP_APTX_HD_CIE cfg_cie;
    185 
    186   /* Use a liberal check when parsing the codec info */
    187   return (A2DP_ParseInfoAptxHd(&cfg_cie, p_codec_info, false) ==
    188           A2DP_SUCCESS) ||
    189          (A2DP_ParseInfoAptxHd(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
    190 }
    191 
    192 bool A2DP_IsVendorPeerSinkCodecValidAptxHd(const uint8_t* p_codec_info) {
    193   tA2DP_APTX_HD_CIE cfg_cie;
    194 
    195   /* Use a liberal check when parsing the codec info */
    196   return (A2DP_ParseInfoAptxHd(&cfg_cie, p_codec_info, false) ==
    197           A2DP_SUCCESS) ||
    198          (A2DP_ParseInfoAptxHd(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
    199 }
    200 
    201 // Checks whether A2DP aptX-HD codec configuration matches with a device's
    202 // codec capabilities. |p_cap| is the aptX-HD codec configuration.
    203 // |p_codec_info| is the device's codec capabilities.
    204 // If |is_capability| is true, the byte sequence is codec capabilities,
    205 // otherwise is codec configuration.
    206 // |p_codec_info| contains the codec capabilities for a peer device that
    207 // is acting as an A2DP source.
    208 // Returns A2DP_SUCCESS if the codec configuration matches with capabilities,
    209 // otherwise the corresponding A2DP error status code.
    210 static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityAptxHd(
    211     const tA2DP_APTX_HD_CIE* p_cap, const uint8_t* p_codec_info,
    212     bool is_capability) {
    213   tA2DP_STATUS status;
    214   tA2DP_APTX_HD_CIE cfg_cie;
    215 
    216   /* parse configuration */
    217   status = A2DP_ParseInfoAptxHd(&cfg_cie, p_codec_info, is_capability);
    218   if (status != A2DP_SUCCESS) {
    219     LOG_ERROR(LOG_TAG, "%s: parsing failed %d", __func__, status);
    220     return status;
    221   }
    222 
    223   /* verify that each parameter is in range */
    224 
    225   LOG_VERBOSE(LOG_TAG, "%s: FREQ peer: 0x%x, capability 0x%x", __func__,
    226               cfg_cie.sampleRate, p_cap->sampleRate);
    227   LOG_VERBOSE(LOG_TAG, "%s: CH_MODE peer: 0x%x, capability 0x%x", __func__,
    228               cfg_cie.channelMode, p_cap->channelMode);
    229 
    230   /* sampling frequency */
    231   if ((cfg_cie.sampleRate & p_cap->sampleRate) == 0) return A2DP_NS_SAMP_FREQ;
    232 
    233   /* channel mode */
    234   if ((cfg_cie.channelMode & p_cap->channelMode) == 0) return A2DP_NS_CH_MODE;
    235 
    236   return A2DP_SUCCESS;
    237 }
    238 
    239 bool A2DP_VendorUsesRtpHeaderAptxHd(UNUSED_ATTR bool content_protection_enabled,
    240                                     UNUSED_ATTR const uint8_t* p_codec_info) {
    241   return true;
    242 }
    243 
    244 const char* A2DP_VendorCodecNameAptxHd(
    245     UNUSED_ATTR const uint8_t* p_codec_info) {
    246   return "aptX-HD";
    247 }
    248 
    249 bool A2DP_VendorCodecTypeEqualsAptxHd(const uint8_t* p_codec_info_a,
    250                                       const uint8_t* p_codec_info_b) {
    251   tA2DP_APTX_HD_CIE aptx_hd_cie_a;
    252   tA2DP_APTX_HD_CIE aptx_hd_cie_b;
    253 
    254   // Check whether the codec info contains valid data
    255   tA2DP_STATUS a2dp_status =
    256       A2DP_ParseInfoAptxHd(&aptx_hd_cie_a, p_codec_info_a, true);
    257   if (a2dp_status != A2DP_SUCCESS) {
    258     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
    259               a2dp_status);
    260     return false;
    261   }
    262   a2dp_status = A2DP_ParseInfoAptxHd(&aptx_hd_cie_b, p_codec_info_b, true);
    263   if (a2dp_status != A2DP_SUCCESS) {
    264     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
    265               a2dp_status);
    266     return false;
    267   }
    268 
    269   return true;
    270 }
    271 
    272 bool A2DP_VendorCodecEqualsAptxHd(const uint8_t* p_codec_info_a,
    273                                   const uint8_t* p_codec_info_b) {
    274   tA2DP_APTX_HD_CIE aptx_hd_cie_a;
    275   tA2DP_APTX_HD_CIE aptx_hd_cie_b;
    276 
    277   // Check whether the codec info contains valid data
    278   tA2DP_STATUS a2dp_status =
    279       A2DP_ParseInfoAptxHd(&aptx_hd_cie_a, p_codec_info_a, true);
    280   if (a2dp_status != A2DP_SUCCESS) {
    281     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
    282               a2dp_status);
    283     return false;
    284   }
    285   a2dp_status = A2DP_ParseInfoAptxHd(&aptx_hd_cie_b, p_codec_info_b, true);
    286   if (a2dp_status != A2DP_SUCCESS) {
    287     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
    288               a2dp_status);
    289     return false;
    290   }
    291 
    292   return (aptx_hd_cie_a.sampleRate == aptx_hd_cie_b.sampleRate) &&
    293          (aptx_hd_cie_a.channelMode == aptx_hd_cie_b.channelMode);
    294 }
    295 
    296 int A2DP_VendorGetTrackSampleRateAptxHd(const uint8_t* p_codec_info) {
    297   tA2DP_APTX_HD_CIE aptx_hd_cie;
    298 
    299   // Check whether the codec info contains valid data
    300   tA2DP_STATUS a2dp_status =
    301       A2DP_ParseInfoAptxHd(&aptx_hd_cie, p_codec_info, false);
    302   if (a2dp_status != A2DP_SUCCESS) {
    303     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
    304               a2dp_status);
    305     return -1;
    306   }
    307 
    308   if (aptx_hd_cie.sampleRate == A2DP_APTX_HD_SAMPLERATE_44100) return 44100;
    309   if (aptx_hd_cie.sampleRate == A2DP_APTX_HD_SAMPLERATE_48000) return 48000;
    310 
    311   return -1;
    312 }
    313 
    314 int A2DP_VendorGetTrackChannelCountAptxHd(const uint8_t* p_codec_info) {
    315   tA2DP_APTX_HD_CIE aptx_hd_cie;
    316 
    317   // Check whether the codec info contains valid data
    318   tA2DP_STATUS a2dp_status =
    319       A2DP_ParseInfoAptxHd(&aptx_hd_cie, p_codec_info, false);
    320   if (a2dp_status != A2DP_SUCCESS) {
    321     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
    322               a2dp_status);
    323     return -1;
    324   }
    325 
    326   switch (aptx_hd_cie.channelMode) {
    327     case A2DP_APTX_HD_CHANNELS_MONO:
    328       return 1;
    329     case A2DP_APTX_HD_CHANNELS_STEREO:
    330       return 2;
    331   }
    332 
    333   return -1;
    334 }
    335 
    336 bool A2DP_VendorGetPacketTimestampAptxHd(
    337     UNUSED_ATTR const uint8_t* p_codec_info, const uint8_t* p_data,
    338     uint32_t* p_timestamp) {
    339   // TODO: Is this function really codec-specific?
    340   *p_timestamp = *(const uint32_t*)p_data;
    341   return true;
    342 }
    343 
    344 bool A2DP_VendorBuildCodecHeaderAptxHd(UNUSED_ATTR const uint8_t* p_codec_info,
    345                                        UNUSED_ATTR BT_HDR* p_buf,
    346                                        UNUSED_ATTR uint16_t frames_per_packet) {
    347   // Nothing to do
    348   return true;
    349 }
    350 
    351 bool A2DP_VendorDumpCodecInfoAptxHd(const uint8_t* p_codec_info) {
    352   tA2DP_STATUS a2dp_status;
    353   tA2DP_APTX_HD_CIE aptx_hd_cie;
    354 
    355   LOG_VERBOSE(LOG_TAG, "%s", __func__);
    356 
    357   a2dp_status = A2DP_ParseInfoAptxHd(&aptx_hd_cie, p_codec_info, true);
    358   if (a2dp_status != A2DP_SUCCESS) {
    359     LOG_ERROR(LOG_TAG, "%s: A2DP_ParseInfoAptxHd fail:%d", __func__,
    360               a2dp_status);
    361     return false;
    362   }
    363 
    364   LOG_VERBOSE(LOG_TAG, "\tsamp_freq: 0x%x", aptx_hd_cie.sampleRate);
    365   if (aptx_hd_cie.sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
    366     LOG_VERBOSE(LOG_TAG, "\tsamp_freq: (44100)");
    367   }
    368   if (aptx_hd_cie.sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
    369     LOG_VERBOSE(LOG_TAG, "\tsamp_freq: (48000)");
    370   }
    371 
    372   LOG_VERBOSE(LOG_TAG, "\tch_mode: 0x%x", aptx_hd_cie.channelMode);
    373   if (aptx_hd_cie.channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
    374     LOG_VERBOSE(LOG_TAG, "\tch_mode: (Mono)");
    375   }
    376   if (aptx_hd_cie.channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
    377     LOG_VERBOSE(LOG_TAG, "\tch_mode: (Stereo)");
    378   }
    379 
    380   return true;
    381 }
    382 
    383 const tA2DP_ENCODER_INTERFACE* A2DP_VendorGetEncoderInterfaceAptxHd(
    384     const uint8_t* p_codec_info) {
    385   if (!A2DP_IsVendorSourceCodecValidAptxHd(p_codec_info)) return NULL;
    386 
    387   return &a2dp_encoder_interface_aptx_hd;
    388 }
    389 
    390 bool A2DP_VendorAdjustCodecAptxHd(uint8_t* p_codec_info) {
    391   tA2DP_APTX_HD_CIE cfg_cie;
    392 
    393   // Nothing to do: just verify the codec info is valid
    394   if (A2DP_ParseInfoAptxHd(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS)
    395     return false;
    396 
    397   return true;
    398 }
    399 
    400 btav_a2dp_codec_index_t A2DP_VendorSourceCodecIndexAptxHd(
    401     const uint8_t* p_codec_info) {
    402   return BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD;
    403 }
    404 
    405 const char* A2DP_VendorCodecIndexStrAptxHd(void) { return "aptX-HD"; }
    406 
    407 bool A2DP_VendorInitCodecConfigAptxHd(tAVDT_CFG* p_cfg) {
    408   if (A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &a2dp_aptx_hd_caps,
    409                            p_cfg->codec_info) != A2DP_SUCCESS) {
    410     return false;
    411   }
    412 
    413 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
    414   /* Content protection info - support SCMS-T */
    415   uint8_t* p = p_cfg->protect_info;
    416   *p++ = AVDT_CP_LOSC;
    417   UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID);
    418   p_cfg->num_protect = 1;
    419 #endif
    420 
    421   return true;
    422 }
    423 
    424 A2dpCodecConfigAptxHd::A2dpCodecConfigAptxHd(
    425     btav_a2dp_codec_priority_t codec_priority)
    426     : A2dpCodecConfig(BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD, "aptX-HD",
    427                       codec_priority) {
    428   // Compute the local capability
    429   if (a2dp_aptx_hd_caps.sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
    430     codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
    431   }
    432   if (a2dp_aptx_hd_caps.sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
    433     codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
    434   }
    435   codec_local_capability_.bits_per_sample = a2dp_aptx_hd_caps.bits_per_sample;
    436   if (a2dp_aptx_hd_caps.channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
    437     codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
    438   }
    439   if (a2dp_aptx_hd_caps.channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
    440     codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
    441   }
    442 }
    443 
    444 A2dpCodecConfigAptxHd::~A2dpCodecConfigAptxHd() {}
    445 
    446 bool A2dpCodecConfigAptxHd::init() {
    447   if (!isValid()) return false;
    448 
    449   // Load the encoder
    450   if (!A2DP_VendorLoadEncoderAptxHd()) {
    451     LOG_ERROR(LOG_TAG, "%s: cannot load the encoder", __func__);
    452     return false;
    453   }
    454 
    455   return true;
    456 }
    457 
    458 bool A2dpCodecConfigAptxHd::useRtpHeaderMarkerBit() const { return false; }
    459 
    460 //
    461 // Selects the best sample rate from |sampleRate|.
    462 // The result is stored in |p_result| and p_codec_config|.
    463 // Returns true if a selection was made, otherwise false.
    464 //
    465 static bool select_best_sample_rate(uint8_t sampleRate,
    466                                     tA2DP_APTX_HD_CIE* p_result,
    467                                     btav_a2dp_codec_config_t* p_codec_config) {
    468   if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
    469     p_result->sampleRate = A2DP_APTX_HD_SAMPLERATE_48000;
    470     p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
    471     return true;
    472   }
    473   if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
    474     p_result->sampleRate = A2DP_APTX_HD_SAMPLERATE_44100;
    475     p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
    476     return true;
    477   }
    478   return false;
    479 }
    480 
    481 //
    482 // Selects the audio sample rate from |p_codec_audio_config|.
    483 // |sampleRate| contains the capability.
    484 // The result is stored in |p_result| and |p_codec_config|.
    485 // Returns true if a selection was made, otherwise false.
    486 //
    487 static bool select_audio_sample_rate(
    488     const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t sampleRate,
    489     tA2DP_APTX_HD_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
    490   switch (p_codec_audio_config->sample_rate) {
    491     case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
    492       if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
    493         p_result->sampleRate = A2DP_APTX_HD_SAMPLERATE_44100;
    494         p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
    495         return true;
    496       }
    497       break;
    498     case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
    499       if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
    500         p_result->sampleRate = A2DP_APTX_HD_SAMPLERATE_48000;
    501         p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
    502         return true;
    503       }
    504       break;
    505     case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
    506     case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
    507     case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
    508     case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
    509     case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
    510       break;
    511   }
    512   return false;
    513 }
    514 
    515 //
    516 // Selects the best bits per sample.
    517 // The result is stored in |p_codec_config|.
    518 // Returns true if a selection was made, otherwise false.
    519 //
    520 static bool select_best_bits_per_sample(
    521     btav_a2dp_codec_config_t* p_codec_config) {
    522   p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
    523   return true;
    524 }
    525 
    526 //
    527 // Selects the audio bits per sample from |p_codec_audio_config|.
    528 // The result is stored in |p_codec_config|.
    529 // Returns true if a selection was made, otherwise false.
    530 //
    531 static bool select_audio_bits_per_sample(
    532     const btav_a2dp_codec_config_t* p_codec_audio_config,
    533     btav_a2dp_codec_config_t* p_codec_config) {
    534   switch (p_codec_audio_config->bits_per_sample) {
    535     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
    536       p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
    537       return true;
    538     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
    539     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
    540     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
    541       break;
    542   }
    543   return false;
    544 }
    545 
    546 //
    547 // Selects the best channel mode from |channelMode|.
    548 // The result is stored in |p_result| and |p_codec_config|.
    549 // Returns true if a selection was made, otherwise false.
    550 //
    551 static bool select_best_channel_mode(uint8_t channelMode,
    552                                      tA2DP_APTX_HD_CIE* p_result,
    553                                      btav_a2dp_codec_config_t* p_codec_config) {
    554   if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
    555     p_result->channelMode = A2DP_APTX_HD_CHANNELS_STEREO;
    556     p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
    557     return true;
    558   }
    559   if (channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
    560     p_result->channelMode = A2DP_APTX_HD_CHANNELS_MONO;
    561     p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
    562     return true;
    563   }
    564   return false;
    565 }
    566 
    567 //
    568 // Selects the audio channel mode from |p_codec_audio_config|.
    569 // |channelMode| contains the capability.
    570 // The result is stored in |p_result| and |p_codec_config|.
    571 // Returns true if a selection was made, otherwise false.
    572 //
    573 static bool select_audio_channel_mode(
    574     const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t channelMode,
    575     tA2DP_APTX_HD_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
    576   switch (p_codec_audio_config->channel_mode) {
    577     case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
    578       if (channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
    579         p_result->channelMode = A2DP_APTX_HD_CHANNELS_MONO;
    580         p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
    581         return true;
    582       }
    583       break;
    584     case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
    585       if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
    586         p_result->channelMode = A2DP_APTX_HD_CHANNELS_STEREO;
    587         p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
    588         return true;
    589       }
    590       break;
    591     case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
    592       break;
    593   }
    594 
    595   return false;
    596 }
    597 
    598 bool A2dpCodecConfigAptxHd::setCodecConfig(const uint8_t* p_peer_codec_info,
    599                                            bool is_capability,
    600                                            uint8_t* p_result_codec_config) {
    601   std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
    602   tA2DP_APTX_HD_CIE sink_info_cie;
    603   tA2DP_APTX_HD_CIE result_config_cie;
    604   uint8_t sampleRate;
    605   uint8_t channelMode;
    606 
    607   // Save the internal state
    608   btav_a2dp_codec_config_t saved_codec_config = codec_config_;
    609   btav_a2dp_codec_config_t saved_codec_capability = codec_capability_;
    610   btav_a2dp_codec_config_t saved_codec_selectable_capability =
    611       codec_selectable_capability_;
    612   btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_;
    613   btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_;
    614   uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE];
    615   uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
    616   uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE];
    617   memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_));
    618   memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
    619          sizeof(ota_codec_peer_capability_));
    620   memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_,
    621          sizeof(ota_codec_peer_config_));
    622 
    623   tA2DP_STATUS status =
    624       A2DP_ParseInfoAptxHd(&sink_info_cie, p_peer_codec_info, is_capability);
    625   if (status != A2DP_SUCCESS) {
    626     LOG_ERROR(LOG_TAG, "%s: can't parse peer's Sink capabilities: error = %d",
    627               __func__, status);
    628     goto fail;
    629   }
    630 
    631   //
    632   // Build the preferred configuration
    633   //
    634   memset(&result_config_cie, 0, sizeof(result_config_cie));
    635   result_config_cie.vendorId = a2dp_aptx_hd_caps.vendorId;
    636   result_config_cie.codecId = a2dp_aptx_hd_caps.codecId;
    637 
    638   //
    639   // Select the sample frequency
    640   //
    641   sampleRate = a2dp_aptx_hd_caps.sampleRate & sink_info_cie.sampleRate;
    642   codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
    643   switch (codec_user_config_.sample_rate) {
    644     case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
    645       if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
    646         result_config_cie.sampleRate = A2DP_APTX_HD_SAMPLERATE_44100;
    647         codec_capability_.sample_rate = codec_user_config_.sample_rate;
    648         codec_config_.sample_rate = codec_user_config_.sample_rate;
    649       }
    650       break;
    651     case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
    652       if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
    653         result_config_cie.sampleRate = A2DP_APTX_HD_SAMPLERATE_48000;
    654         codec_capability_.sample_rate = codec_user_config_.sample_rate;
    655         codec_config_.sample_rate = codec_user_config_.sample_rate;
    656       }
    657       break;
    658     case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
    659     case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
    660     case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
    661     case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
    662     case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
    663       codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
    664       codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
    665       break;
    666   }
    667 
    668   // Select the sample frequency if there is no user preference
    669   do {
    670     // Compute the selectable capability
    671     if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
    672       codec_selectable_capability_.sample_rate |=
    673           BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
    674     }
    675     if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
    676       codec_selectable_capability_.sample_rate |=
    677           BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
    678     }
    679 
    680     if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break;
    681 
    682     // Compute the common capability
    683     if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100)
    684       codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
    685     if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000)
    686       codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
    687 
    688     // No user preference - try the codec audio config
    689     if (select_audio_sample_rate(&codec_audio_config_, sampleRate,
    690                                  &result_config_cie, &codec_config_)) {
    691       break;
    692     }
    693 
    694     // No user preference - try the default config
    695     if (select_best_sample_rate(
    696             a2dp_aptx_hd_default_config.sampleRate & sink_info_cie.sampleRate,
    697             &result_config_cie, &codec_config_)) {
    698       break;
    699     }
    700 
    701     // No user preference - use the best match
    702     if (select_best_sample_rate(sampleRate, &result_config_cie,
    703                                 &codec_config_)) {
    704       break;
    705     }
    706   } while (false);
    707   if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) {
    708     LOG_ERROR(LOG_TAG,
    709               "%s: cannot match sample frequency: source caps = 0x%x "
    710               "sink info = 0x%x",
    711               __func__, a2dp_aptx_hd_caps.sampleRate, sink_info_cie.sampleRate);
    712     goto fail;
    713   }
    714 
    715   //
    716   // Select the bits per sample
    717   //
    718   // NOTE: this information is NOT included in the aptX-HD A2DP codec
    719   // description that is sent OTA.
    720   codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
    721   switch (codec_user_config_.bits_per_sample) {
    722     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
    723       codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
    724       codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
    725       break;
    726     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
    727     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
    728     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
    729       codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
    730       codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
    731       break;
    732   }
    733 
    734   // Select the bits per sample if there is no user preference
    735   do {
    736     // Compute the selectable capability
    737     codec_selectable_capability_.bits_per_sample =
    738         a2dp_aptx_hd_caps.bits_per_sample;
    739 
    740     if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE)
    741       break;
    742 
    743     // Compute the common capability
    744     codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
    745 
    746     // No user preference - try the codec audio config
    747     if (select_audio_bits_per_sample(&codec_audio_config_, &codec_config_)) {
    748       break;
    749     }
    750 
    751     // No user preference - try the default config
    752     if (select_best_bits_per_sample(&codec_config_)) {
    753       break;
    754     }
    755 
    756     // No user preference - use the best match
    757     // NOTE: no-op - kept here for consistency
    758     if (select_best_bits_per_sample(&codec_config_)) {
    759       break;
    760     }
    761   } while (false);
    762   if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) {
    763     LOG_ERROR(LOG_TAG,
    764               "%s: cannot match bits per sample: user preference = 0x%x",
    765               __func__, codec_user_config_.bits_per_sample);
    766     goto fail;
    767   }
    768 
    769   //
    770   // Select the channel mode
    771   //
    772   channelMode = a2dp_aptx_hd_caps.channelMode & sink_info_cie.channelMode;
    773   codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
    774   switch (codec_user_config_.channel_mode) {
    775     case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
    776       if (channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
    777         result_config_cie.channelMode = A2DP_APTX_HD_CHANNELS_MONO;
    778         codec_capability_.channel_mode = codec_user_config_.channel_mode;
    779         codec_config_.channel_mode = codec_user_config_.channel_mode;
    780       }
    781       break;
    782     case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
    783       if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
    784         result_config_cie.channelMode = A2DP_APTX_HD_CHANNELS_STEREO;
    785         codec_capability_.channel_mode = codec_user_config_.channel_mode;
    786         codec_config_.channel_mode = codec_user_config_.channel_mode;
    787       }
    788       break;
    789     case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
    790       codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
    791       codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
    792       break;
    793   }
    794 
    795   // Select the channel mode if there is no user preference
    796   do {
    797     // Compute the selectable capability
    798     if (channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
    799       codec_selectable_capability_.channel_mode |=
    800           BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
    801     }
    802     if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
    803       codec_selectable_capability_.channel_mode |=
    804           BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
    805     }
    806 
    807     if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break;
    808 
    809     // Compute the common capability
    810     if (channelMode & A2DP_APTX_HD_CHANNELS_MONO)
    811       codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
    812     if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO)
    813       codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
    814 
    815     // No user preference - try the codec audio config
    816     if (select_audio_channel_mode(&codec_audio_config_, channelMode,
    817                                   &result_config_cie, &codec_config_)) {
    818       break;
    819     }
    820 
    821     // No user preference - try the default config
    822     if (select_best_channel_mode(
    823             a2dp_aptx_hd_default_config.channelMode & sink_info_cie.channelMode,
    824             &result_config_cie, &codec_config_)) {
    825       break;
    826     }
    827 
    828     // No user preference - use the best match
    829     if (select_best_channel_mode(channelMode, &result_config_cie,
    830                                  &codec_config_)) {
    831       break;
    832     }
    833   } while (false);
    834   if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) {
    835     LOG_ERROR(LOG_TAG,
    836               "%s: cannot match channel mode: source caps = 0x%x "
    837               "sink info = 0x%x",
    838               __func__, a2dp_aptx_hd_caps.channelMode,
    839               sink_info_cie.channelMode);
    840     goto fail;
    841   }
    842 
    843   //
    844   // Set the rest of the fields as bit-wise AND operation
    845   //
    846   result_config_cie.acl_sprint_reserved0 =
    847       a2dp_aptx_hd_caps.acl_sprint_reserved0 &
    848       sink_info_cie.acl_sprint_reserved0;
    849   result_config_cie.acl_sprint_reserved1 =
    850       a2dp_aptx_hd_caps.acl_sprint_reserved1 &
    851       sink_info_cie.acl_sprint_reserved1;
    852   result_config_cie.acl_sprint_reserved2 =
    853       a2dp_aptx_hd_caps.acl_sprint_reserved2 &
    854       sink_info_cie.acl_sprint_reserved2;
    855   result_config_cie.acl_sprint_reserved3 =
    856       a2dp_aptx_hd_caps.acl_sprint_reserved3 &
    857       sink_info_cie.acl_sprint_reserved3;
    858 
    859   if (A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
    860                            p_result_codec_config) != A2DP_SUCCESS) {
    861     goto fail;
    862   }
    863 
    864   //
    865   // Copy the codec-specific fields if they are not zero
    866   //
    867   if (codec_user_config_.codec_specific_1 != 0)
    868     codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
    869   if (codec_user_config_.codec_specific_2 != 0)
    870     codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2;
    871   if (codec_user_config_.codec_specific_3 != 0)
    872     codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3;
    873   if (codec_user_config_.codec_specific_4 != 0)
    874     codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4;
    875 
    876   // Create a local copy of the peer codec capability/config, and the
    877   // result codec config.
    878   if (is_capability) {
    879     status = A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &sink_info_cie,
    880                                   ota_codec_peer_capability_);
    881   } else {
    882     status = A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &sink_info_cie,
    883                                   ota_codec_peer_config_);
    884   }
    885   CHECK(status == A2DP_SUCCESS);
    886   status = A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
    887                                 ota_codec_config_);
    888   CHECK(status == A2DP_SUCCESS);
    889   return true;
    890 
    891 fail:
    892   // Restore the internal state
    893   codec_config_ = saved_codec_config;
    894   codec_capability_ = saved_codec_capability;
    895   codec_selectable_capability_ = saved_codec_selectable_capability;
    896   codec_user_config_ = saved_codec_user_config;
    897   codec_audio_config_ = saved_codec_audio_config;
    898   memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_));
    899   memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
    900          sizeof(ota_codec_peer_capability_));
    901   memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config,
    902          sizeof(ota_codec_peer_config_));
    903   return false;
    904 }
    905