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