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