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("%s: parsing failed %d", __func__, status);
230     return status;
231   }
232 
233   /* verify that each parameter is in range */
234 
235   LOG_VERBOSE("%s: FREQ peer: 0x%x, capability 0x%x", __func__,
236               cfg_cie.sampleRate, p_cap->sampleRate);
237   LOG_VERBOSE("%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("%s: cannot decode codec information: %d", __func__, a2dp_status);
269     return false;
270   }
271   a2dp_status = A2DP_ParseInfoAptxHd(&aptx_hd_cie_b, p_codec_info_b, true);
272   if (a2dp_status != A2DP_SUCCESS) {
273     LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
274     return false;
275   }
276 
277   return true;
278 }
279 
A2DP_VendorCodecEqualsAptxHd(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)280 bool A2DP_VendorCodecEqualsAptxHd(const uint8_t* p_codec_info_a,
281                                   const uint8_t* p_codec_info_b) {
282   tA2DP_APTX_HD_CIE aptx_hd_cie_a;
283   tA2DP_APTX_HD_CIE aptx_hd_cie_b;
284 
285   // Check whether the codec info contains valid data
286   tA2DP_STATUS a2dp_status =
287       A2DP_ParseInfoAptxHd(&aptx_hd_cie_a, p_codec_info_a, true);
288   if (a2dp_status != A2DP_SUCCESS) {
289     LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
290     return false;
291   }
292   a2dp_status = A2DP_ParseInfoAptxHd(&aptx_hd_cie_b, p_codec_info_b, true);
293   if (a2dp_status != A2DP_SUCCESS) {
294     LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
295     return false;
296   }
297 
298   return (aptx_hd_cie_a.sampleRate == aptx_hd_cie_b.sampleRate) &&
299          (aptx_hd_cie_a.channelMode == aptx_hd_cie_b.channelMode);
300 }
301 
A2DP_VendorGetBitRateAptxHd(const uint8_t * p_codec_info)302 int A2DP_VendorGetBitRateAptxHd(const uint8_t* p_codec_info) {
303   A2dpCodecConfig* CodecConfig = bta_av_get_a2dp_current_codec();
304   tA2DP_BITS_PER_SAMPLE bits_per_sample = CodecConfig->getAudioBitsPerSample();
305   uint16_t samplerate = A2DP_GetTrackSampleRate(p_codec_info);
306   return (samplerate * bits_per_sample * 2) / 4;
307 }
308 
A2DP_VendorGetTrackSampleRateAptxHd(const uint8_t * p_codec_info)309 int A2DP_VendorGetTrackSampleRateAptxHd(const uint8_t* p_codec_info) {
310   tA2DP_APTX_HD_CIE aptx_hd_cie;
311 
312   // Check whether the codec info contains valid data
313   tA2DP_STATUS a2dp_status =
314       A2DP_ParseInfoAptxHd(&aptx_hd_cie, p_codec_info, false);
315   if (a2dp_status != A2DP_SUCCESS) {
316     LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
317     return -1;
318   }
319 
320   if (aptx_hd_cie.sampleRate == A2DP_APTX_HD_SAMPLERATE_44100) return 44100;
321   if (aptx_hd_cie.sampleRate == A2DP_APTX_HD_SAMPLERATE_48000) return 48000;
322 
323   return -1;
324 }
325 
A2DP_VendorGetTrackBitsPerSampleAptxHd(const uint8_t * p_codec_info)326 int A2DP_VendorGetTrackBitsPerSampleAptxHd(const uint8_t* p_codec_info) {
327   tA2DP_APTX_HD_CIE aptx_hd_cie;
328 
329   // Check whether the codec info contains valid data
330   tA2DP_STATUS a2dp_status =
331       A2DP_ParseInfoAptxHd(&aptx_hd_cie, p_codec_info, false);
332   if (a2dp_status != A2DP_SUCCESS) {
333     LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
334     return -1;
335   }
336 
337   // NOTE: The bits per sample never changes for aptX-HD
338   return 24;
339 }
340 
A2DP_VendorGetTrackChannelCountAptxHd(const uint8_t * p_codec_info)341 int A2DP_VendorGetTrackChannelCountAptxHd(const uint8_t* p_codec_info) {
342   tA2DP_APTX_HD_CIE aptx_hd_cie;
343 
344   // Check whether the codec info contains valid data
345   tA2DP_STATUS a2dp_status =
346       A2DP_ParseInfoAptxHd(&aptx_hd_cie, p_codec_info, false);
347   if (a2dp_status != A2DP_SUCCESS) {
348     LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
349     return -1;
350   }
351 
352   switch (aptx_hd_cie.channelMode) {
353     case A2DP_APTX_HD_CHANNELS_MONO:
354       return 1;
355     case A2DP_APTX_HD_CHANNELS_STEREO:
356       return 2;
357   }
358 
359   return -1;
360 }
361 
A2DP_VendorGetPacketTimestampAptxHd(UNUSED_ATTR const uint8_t * p_codec_info,const uint8_t * p_data,uint32_t * p_timestamp)362 bool A2DP_VendorGetPacketTimestampAptxHd(
363     UNUSED_ATTR const uint8_t* p_codec_info, const uint8_t* p_data,
364     uint32_t* p_timestamp) {
365   // TODO: Is this function really codec-specific?
366   *p_timestamp = *(const uint32_t*)p_data;
367   return true;
368 }
369 
A2DP_VendorBuildCodecHeaderAptxHd(UNUSED_ATTR const uint8_t * p_codec_info,UNUSED_ATTR BT_HDR * p_buf,UNUSED_ATTR uint16_t frames_per_packet)370 bool A2DP_VendorBuildCodecHeaderAptxHd(UNUSED_ATTR const uint8_t* p_codec_info,
371                                        UNUSED_ATTR BT_HDR* p_buf,
372                                        UNUSED_ATTR uint16_t frames_per_packet) {
373   // Nothing to do
374   return true;
375 }
376 
A2DP_VendorCodecInfoStringAptxHd(const uint8_t * p_codec_info)377 std::string A2DP_VendorCodecInfoStringAptxHd(const uint8_t* p_codec_info) {
378   std::stringstream res;
379   std::string field;
380   tA2DP_STATUS a2dp_status;
381   tA2DP_APTX_HD_CIE aptx_hd_cie;
382 
383   a2dp_status = A2DP_ParseInfoAptxHd(&aptx_hd_cie, p_codec_info, true);
384   if (a2dp_status != A2DP_SUCCESS) {
385     res << "A2DP_ParseInfoAptxHd fail: " << loghex(a2dp_status);
386     return res.str();
387   }
388 
389   res << "\tname: aptX-HD\n";
390 
391   // Sample frequency
392   field.clear();
393   AppendField(&field, (aptx_hd_cie.sampleRate == 0), "NONE");
394   AppendField(&field, (aptx_hd_cie.sampleRate & A2DP_APTX_HD_SAMPLERATE_44100),
395               "44100");
396   AppendField(&field, (aptx_hd_cie.sampleRate & A2DP_APTX_HD_SAMPLERATE_48000),
397               "48000");
398   res << "\tsamp_freq: " << field << " (" << loghex(aptx_hd_cie.sampleRate)
399       << ")\n";
400 
401   // Channel mode
402   field.clear();
403   AppendField(&field, (aptx_hd_cie.channelMode == 0), "NONE");
404   AppendField(&field, (aptx_hd_cie.channelMode & A2DP_APTX_HD_CHANNELS_MONO),
405               "Mono");
406   AppendField(&field, (aptx_hd_cie.channelMode & A2DP_APTX_HD_CHANNELS_STEREO),
407               "Stereo");
408   res << "\tch_mode: " << field << " (" << loghex(aptx_hd_cie.channelMode)
409       << ")\n";
410 
411   return res.str();
412 }
413 
A2DP_VendorGetEncoderInterfaceAptxHd(const uint8_t * p_codec_info)414 const tA2DP_ENCODER_INTERFACE* A2DP_VendorGetEncoderInterfaceAptxHd(
415     const uint8_t* p_codec_info) {
416   if (!A2DP_IsVendorSourceCodecValidAptxHd(p_codec_info)) return NULL;
417 
418   return &a2dp_encoder_interface_aptx_hd;
419 }
420 
A2DP_VendorAdjustCodecAptxHd(uint8_t * p_codec_info)421 bool A2DP_VendorAdjustCodecAptxHd(uint8_t* p_codec_info) {
422   tA2DP_APTX_HD_CIE cfg_cie;
423 
424   // Nothing to do: just verify the codec info is valid
425   if (A2DP_ParseInfoAptxHd(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS)
426     return false;
427 
428   return true;
429 }
430 
A2DP_VendorSourceCodecIndexAptxHd(const uint8_t * p_codec_info)431 btav_a2dp_codec_index_t A2DP_VendorSourceCodecIndexAptxHd(
432     const uint8_t* p_codec_info) {
433   return BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD;
434 }
435 
A2DP_VendorCodecIndexStrAptxHd(void)436 const char* A2DP_VendorCodecIndexStrAptxHd(void) { return "aptX-HD"; }
437 
A2DP_VendorInitCodecConfigAptxHd(AvdtpSepConfig * p_cfg)438 bool A2DP_VendorInitCodecConfigAptxHd(AvdtpSepConfig* p_cfg) {
439   if (A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &a2dp_aptx_hd_source_caps,
440                            p_cfg->codec_info) != A2DP_SUCCESS) {
441     return false;
442   }
443 
444 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
445   /* Content protection info - support SCMS-T */
446   uint8_t* p = p_cfg->protect_info;
447   *p++ = AVDT_CP_LOSC;
448   UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID);
449   p_cfg->num_protect = 1;
450 #endif
451 
452   return true;
453 }
454 
A2dpCodecConfigAptxHd(btav_a2dp_codec_priority_t codec_priority)455 A2dpCodecConfigAptxHd::A2dpCodecConfigAptxHd(
456     btav_a2dp_codec_priority_t codec_priority)
457     : A2dpCodecConfig(BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD,
458                       A2DP_VendorCodecIndexStrAptxHd(), codec_priority) {
459   // Compute the local capability
460   if (a2dp_aptx_hd_source_caps.sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
461     codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
462   }
463   if (a2dp_aptx_hd_source_caps.sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
464     codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
465   }
466   codec_local_capability_.bits_per_sample =
467       a2dp_aptx_hd_source_caps.bits_per_sample;
468   if (a2dp_aptx_hd_source_caps.channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
469     codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
470   }
471   if (a2dp_aptx_hd_source_caps.channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
472     codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
473   }
474 }
475 
~A2dpCodecConfigAptxHd()476 A2dpCodecConfigAptxHd::~A2dpCodecConfigAptxHd() {}
477 
init()478 bool A2dpCodecConfigAptxHd::init() {
479   if (!isValid()) return false;
480 
481   // Load the encoder
482   if (!A2DP_VendorLoadEncoderAptxHd()) {
483     LOG_ERROR("%s: cannot load the encoder", __func__);
484     return false;
485   }
486 
487   return true;
488 }
489 
useRtpHeaderMarkerBit() const490 bool A2dpCodecConfigAptxHd::useRtpHeaderMarkerBit() const { return false; }
491 
492 //
493 // Selects the best sample rate from |sampleRate|.
494 // The result is stored in |p_result| and p_codec_config|.
495 // Returns true if a selection was made, otherwise false.
496 //
select_best_sample_rate(uint8_t sampleRate,tA2DP_APTX_HD_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)497 static bool select_best_sample_rate(uint8_t sampleRate,
498                                     tA2DP_APTX_HD_CIE* p_result,
499                                     btav_a2dp_codec_config_t* p_codec_config) {
500   if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
501     p_result->sampleRate = A2DP_APTX_HD_SAMPLERATE_48000;
502     p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
503     return true;
504   }
505   if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
506     p_result->sampleRate = A2DP_APTX_HD_SAMPLERATE_44100;
507     p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
508     return true;
509   }
510   return false;
511 }
512 
513 //
514 // Selects the audio sample rate from |p_codec_audio_config|.
515 // |sampleRate| contains the capability.
516 // The result is stored in |p_result| and |p_codec_config|.
517 // Returns true if a selection was made, otherwise false.
518 //
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)519 static bool select_audio_sample_rate(
520     const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t sampleRate,
521     tA2DP_APTX_HD_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
522   switch (p_codec_audio_config->sample_rate) {
523     case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
524       if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
525         p_result->sampleRate = A2DP_APTX_HD_SAMPLERATE_44100;
526         p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
527         return true;
528       }
529       break;
530     case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
531       if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
532         p_result->sampleRate = A2DP_APTX_HD_SAMPLERATE_48000;
533         p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
534         return true;
535       }
536       break;
537     case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
538     case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
539     case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
540     case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
541     case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
542     case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
543     case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
544       break;
545   }
546   return false;
547 }
548 
549 //
550 // Selects the best bits per sample.
551 // The result is stored in |p_codec_config|.
552 // Returns true if a selection was made, otherwise false.
553 //
select_best_bits_per_sample(btav_a2dp_codec_config_t * p_codec_config)554 static bool select_best_bits_per_sample(
555     btav_a2dp_codec_config_t* p_codec_config) {
556   p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
557   return true;
558 }
559 
560 //
561 // Selects the audio bits per sample from |p_codec_audio_config|.
562 // The result is stored in |p_codec_config|.
563 // Returns true if a selection was made, otherwise false.
564 //
select_audio_bits_per_sample(const btav_a2dp_codec_config_t * p_codec_audio_config,btav_a2dp_codec_config_t * p_codec_config)565 static bool select_audio_bits_per_sample(
566     const btav_a2dp_codec_config_t* p_codec_audio_config,
567     btav_a2dp_codec_config_t* p_codec_config) {
568   switch (p_codec_audio_config->bits_per_sample) {
569     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
570       p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
571       return true;
572     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
573     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
574     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
575       break;
576   }
577   return false;
578 }
579 
580 //
581 // Selects the best channel mode from |channelMode|.
582 // The result is stored in |p_result| and |p_codec_config|.
583 // Returns true if a selection was made, otherwise false.
584 //
select_best_channel_mode(uint8_t channelMode,tA2DP_APTX_HD_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)585 static bool select_best_channel_mode(uint8_t channelMode,
586                                      tA2DP_APTX_HD_CIE* p_result,
587                                      btav_a2dp_codec_config_t* p_codec_config) {
588   if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
589     p_result->channelMode = A2DP_APTX_HD_CHANNELS_STEREO;
590     p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
591     return true;
592   }
593   if (channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
594     p_result->channelMode = A2DP_APTX_HD_CHANNELS_MONO;
595     p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
596     return true;
597   }
598   return false;
599 }
600 
601 //
602 // Selects the audio channel mode from |p_codec_audio_config|.
603 // |channelMode| contains the capability.
604 // The result is stored in |p_result| and |p_codec_config|.
605 // Returns true if a selection was made, otherwise false.
606 //
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)607 static bool select_audio_channel_mode(
608     const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t channelMode,
609     tA2DP_APTX_HD_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
610   switch (p_codec_audio_config->channel_mode) {
611     case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
612       if (channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
613         p_result->channelMode = A2DP_APTX_HD_CHANNELS_MONO;
614         p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
615         return true;
616       }
617       break;
618     case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
619       if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
620         p_result->channelMode = A2DP_APTX_HD_CHANNELS_STEREO;
621         p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
622         return true;
623       }
624       break;
625     case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
626       break;
627   }
628 
629   return false;
630 }
631 
setCodecConfig(const uint8_t * p_peer_codec_info,bool is_capability,uint8_t * p_result_codec_config)632 bool A2dpCodecConfigAptxHd::setCodecConfig(const uint8_t* p_peer_codec_info,
633                                            bool is_capability,
634                                            uint8_t* p_result_codec_config) {
635   std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
636   tA2DP_APTX_HD_CIE peer_info_cie;
637   tA2DP_APTX_HD_CIE result_config_cie;
638   uint8_t sampleRate;
639   uint8_t channelMode;
640 
641   // Save the internal state
642   btav_a2dp_codec_config_t saved_codec_config = codec_config_;
643   btav_a2dp_codec_config_t saved_codec_capability = codec_capability_;
644   btav_a2dp_codec_config_t saved_codec_selectable_capability =
645       codec_selectable_capability_;
646   btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_;
647   btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_;
648   uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE];
649   uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
650   uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE];
651   memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_));
652   memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
653          sizeof(ota_codec_peer_capability_));
654   memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_,
655          sizeof(ota_codec_peer_config_));
656 
657   tA2DP_STATUS status =
658       A2DP_ParseInfoAptxHd(&peer_info_cie, p_peer_codec_info, is_capability);
659   if (status != A2DP_SUCCESS) {
660     LOG_ERROR("%s: can't parse peer's capabilities: error = %d", __func__,
661               status);
662     goto fail;
663   }
664 
665   //
666   // Build the preferred configuration
667   //
668   memset(&result_config_cie, 0, sizeof(result_config_cie));
669   result_config_cie.vendorId = a2dp_aptx_hd_source_caps.vendorId;
670   result_config_cie.codecId = a2dp_aptx_hd_source_caps.codecId;
671 
672   //
673   // Select the sample frequency
674   //
675   sampleRate = a2dp_aptx_hd_source_caps.sampleRate & peer_info_cie.sampleRate;
676   codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
677   switch (codec_user_config_.sample_rate) {
678     case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
679       if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
680         result_config_cie.sampleRate = A2DP_APTX_HD_SAMPLERATE_44100;
681         codec_capability_.sample_rate = codec_user_config_.sample_rate;
682         codec_config_.sample_rate = codec_user_config_.sample_rate;
683       }
684       break;
685     case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
686       if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
687         result_config_cie.sampleRate = A2DP_APTX_HD_SAMPLERATE_48000;
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_88200:
693     case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
694     case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
695     case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
696     case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
697     case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
698     case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
699       codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
700       codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
701       break;
702   }
703 
704   // Select the sample frequency if there is no user preference
705   do {
706     // Compute the selectable capability
707     if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
708       codec_selectable_capability_.sample_rate |=
709           BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
710     }
711     if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
712       codec_selectable_capability_.sample_rate |=
713           BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
714     }
715 
716     if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break;
717 
718     // Compute the common capability
719     if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100)
720       codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
721     if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000)
722       codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
723 
724     // No user preference - try the codec audio config
725     if (select_audio_sample_rate(&codec_audio_config_, sampleRate,
726                                  &result_config_cie, &codec_config_)) {
727       break;
728     }
729 
730     // No user preference - try the default config
731     if (select_best_sample_rate(
732             a2dp_aptx_hd_default_config.sampleRate & peer_info_cie.sampleRate,
733             &result_config_cie, &codec_config_)) {
734       break;
735     }
736 
737     // No user preference - use the best match
738     if (select_best_sample_rate(sampleRate, &result_config_cie,
739                                 &codec_config_)) {
740       break;
741     }
742   } while (false);
743   if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) {
744     LOG_ERROR(
745         "%s: cannot match sample frequency: local caps = 0x%x "
746         "peer info = 0x%x",
747         __func__, a2dp_aptx_hd_source_caps.sampleRate,
748         peer_info_cie.sampleRate);
749     goto fail;
750   }
751 
752   //
753   // Select the bits per sample
754   //
755   // NOTE: this information is NOT included in the aptX-HD A2DP codec
756   // description that is sent OTA.
757   codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
758   switch (codec_user_config_.bits_per_sample) {
759     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
760       codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
761       codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
762       break;
763     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
764     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
765     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
766       codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
767       codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
768       break;
769   }
770 
771   // Select the bits per sample if there is no user preference
772   do {
773     // Compute the selectable capability
774     codec_selectable_capability_.bits_per_sample =
775         a2dp_aptx_hd_source_caps.bits_per_sample;
776 
777     if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE)
778       break;
779 
780     // Compute the common capability
781     codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
782 
783     // No user preference - try the codec audio config
784     if (select_audio_bits_per_sample(&codec_audio_config_, &codec_config_)) {
785       break;
786     }
787 
788     // No user preference - try the default config
789     if (select_best_bits_per_sample(&codec_config_)) {
790       break;
791     }
792 
793     // No user preference - use the best match
794     // NOTE: no-op - kept here for consistency
795     if (select_best_bits_per_sample(&codec_config_)) {
796       break;
797     }
798   } while (false);
799   if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) {
800     LOG_ERROR("%s: cannot match bits per sample: user preference = 0x%x",
801               __func__, codec_user_config_.bits_per_sample);
802     goto fail;
803   }
804 
805   //
806   // Select the channel mode
807   //
808   channelMode =
809       a2dp_aptx_hd_source_caps.channelMode & peer_info_cie.channelMode;
810   codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
811   switch (codec_user_config_.channel_mode) {
812     case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
813       if (channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
814         result_config_cie.channelMode = A2DP_APTX_HD_CHANNELS_MONO;
815         codec_capability_.channel_mode = codec_user_config_.channel_mode;
816         codec_config_.channel_mode = codec_user_config_.channel_mode;
817       }
818       break;
819     case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
820       if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
821         result_config_cie.channelMode = A2DP_APTX_HD_CHANNELS_STEREO;
822         codec_capability_.channel_mode = codec_user_config_.channel_mode;
823         codec_config_.channel_mode = codec_user_config_.channel_mode;
824       }
825       break;
826     case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
827       codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
828       codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
829       break;
830   }
831 
832   // Select the channel mode if there is no user preference
833   do {
834     // Compute the selectable capability
835     if (channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
836       codec_selectable_capability_.channel_mode |=
837           BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
838     }
839     if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
840       codec_selectable_capability_.channel_mode |=
841           BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
842     }
843 
844     if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break;
845 
846     // Compute the common capability
847     if (channelMode & A2DP_APTX_HD_CHANNELS_MONO)
848       codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
849     if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO)
850       codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
851 
852     // No user preference - try the codec audio config
853     if (select_audio_channel_mode(&codec_audio_config_, channelMode,
854                                   &result_config_cie, &codec_config_)) {
855       break;
856     }
857 
858     // No user preference - try the default config
859     if (select_best_channel_mode(
860             a2dp_aptx_hd_default_config.channelMode & peer_info_cie.channelMode,
861             &result_config_cie, &codec_config_)) {
862       break;
863     }
864 
865     // No user preference - use the best match
866     if (select_best_channel_mode(channelMode, &result_config_cie,
867                                  &codec_config_)) {
868       break;
869     }
870   } while (false);
871   if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) {
872     LOG_ERROR(
873         "%s: cannot match channel mode: local caps = 0x%x "
874         "peer info = 0x%x",
875         __func__, a2dp_aptx_hd_source_caps.channelMode,
876         peer_info_cie.channelMode);
877     goto fail;
878   }
879 
880   //
881   // Set the rest of the fields as bit-wise AND operation
882   //
883   result_config_cie.acl_sprint_reserved0 =
884       a2dp_aptx_hd_source_caps.acl_sprint_reserved0 &
885       peer_info_cie.acl_sprint_reserved0;
886   result_config_cie.acl_sprint_reserved1 =
887       a2dp_aptx_hd_source_caps.acl_sprint_reserved1 &
888       peer_info_cie.acl_sprint_reserved1;
889   result_config_cie.acl_sprint_reserved2 =
890       a2dp_aptx_hd_source_caps.acl_sprint_reserved2 &
891       peer_info_cie.acl_sprint_reserved2;
892   result_config_cie.acl_sprint_reserved3 =
893       a2dp_aptx_hd_source_caps.acl_sprint_reserved3 &
894       peer_info_cie.acl_sprint_reserved3;
895 
896   if (A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
897                            p_result_codec_config) != A2DP_SUCCESS) {
898     goto fail;
899   }
900 
901   //
902   // Copy the codec-specific fields if they are not zero
903   //
904   if (codec_user_config_.codec_specific_1 != 0)
905     codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
906   if (codec_user_config_.codec_specific_2 != 0)
907     codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2;
908   if (codec_user_config_.codec_specific_3 != 0)
909     codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3;
910   if (codec_user_config_.codec_specific_4 != 0)
911     codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4;
912 
913   // Create a local copy of the peer codec capability/config, and the
914   // result codec config.
915   if (is_capability) {
916     status = A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
917                                   ota_codec_peer_capability_);
918   } else {
919     status = A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
920                                   ota_codec_peer_config_);
921   }
922   CHECK(status == A2DP_SUCCESS);
923   status = A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
924                                 ota_codec_config_);
925   CHECK(status == A2DP_SUCCESS);
926   return true;
927 
928 fail:
929   // Restore the internal state
930   codec_config_ = saved_codec_config;
931   codec_capability_ = saved_codec_capability;
932   codec_selectable_capability_ = saved_codec_selectable_capability;
933   codec_user_config_ = saved_codec_user_config;
934   codec_audio_config_ = saved_codec_audio_config;
935   memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_));
936   memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
937          sizeof(ota_codec_peer_capability_));
938   memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config,
939          sizeof(ota_codec_peer_config_));
940   return false;
941 }
942 
setPeerCodecCapabilities(const uint8_t * p_peer_codec_capabilities)943 bool A2dpCodecConfigAptxHd::setPeerCodecCapabilities(
944     const uint8_t* p_peer_codec_capabilities) {
945   std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
946   tA2DP_APTX_HD_CIE peer_info_cie;
947   uint8_t sampleRate;
948   uint8_t channelMode;
949 
950   // Save the internal state
951   btav_a2dp_codec_config_t saved_codec_selectable_capability =
952       codec_selectable_capability_;
953   uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
954   memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
955          sizeof(ota_codec_peer_capability_));
956 
957   tA2DP_STATUS status =
958       A2DP_ParseInfoAptxHd(&peer_info_cie, p_peer_codec_capabilities, true);
959   if (status != A2DP_SUCCESS) {
960     LOG_ERROR("%s: can't parse peer's capabilities: error = %d", __func__,
961               status);
962     goto fail;
963   }
964 
965   // Compute the selectable capability - sample rate
966   sampleRate = a2dp_aptx_hd_source_caps.sampleRate & peer_info_cie.sampleRate;
967   if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
968     codec_selectable_capability_.sample_rate |=
969         BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
970   }
971   if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
972     codec_selectable_capability_.sample_rate |=
973         BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
974   }
975 
976   // Compute the selectable capability - bits per sample
977   codec_selectable_capability_.bits_per_sample =
978       a2dp_aptx_hd_source_caps.bits_per_sample;
979 
980   // Compute the selectable capability - channel mode
981   channelMode =
982       a2dp_aptx_hd_source_caps.channelMode & peer_info_cie.channelMode;
983   if (channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
984     codec_selectable_capability_.channel_mode |=
985         BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
986   }
987   if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
988     codec_selectable_capability_.channel_mode |=
989         BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
990   }
991 
992   status = A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
993                                 ota_codec_peer_capability_);
994   CHECK(status == A2DP_SUCCESS);
995   return true;
996 
997 fail:
998   // Restore the internal state
999   codec_selectable_capability_ = saved_codec_selectable_capability;
1000   memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1001          sizeof(ota_codec_peer_capability_));
1002   return false;
1003 }
1004