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