• 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 LDAC Codec Information
20  *  Element and Media Payload.
21  *
22  ******************************************************************************/
23 
24 #define LOG_TAG "a2dp_vendor_ldac"
25 
26 #include "bt_target.h"
27 
28 #include "a2dp_vendor_ldac.h"
29 
30 #include <string.h>
31 
32 #include <base/logging.h>
33 #include "a2dp_vendor.h"
34 #include "a2dp_vendor_ldac_decoder.h"
35 #include "a2dp_vendor_ldac_encoder.h"
36 #include "bt_utils.h"
37 #include "btif_av_co.h"
38 #include "osi/include/log.h"
39 #include "osi/include/osi.h"
40 
41 // data type for the LDAC Codec Information Element */
42 // NOTE: bits_per_sample is needed only for LDAC encoder initialization.
43 typedef struct {
44   uint32_t vendorId;
45   uint16_t codecId;    /* Codec ID for LDAC */
46   uint8_t sampleRate;  /* Sampling Frequency */
47   uint8_t channelMode; /* STEREO/DUAL/MONO */
48   btav_a2dp_codec_bits_per_sample_t bits_per_sample;
49 } tA2DP_LDAC_CIE;
50 
51 /* LDAC Source codec capabilities */
52 static const tA2DP_LDAC_CIE a2dp_ldac_source_caps = {
53     A2DP_LDAC_VENDOR_ID,  // vendorId
54     A2DP_LDAC_CODEC_ID,   // codecId
55     // sampleRate
56     (A2DP_LDAC_SAMPLING_FREQ_44100 | A2DP_LDAC_SAMPLING_FREQ_48000 |
57      A2DP_LDAC_SAMPLING_FREQ_88200 | A2DP_LDAC_SAMPLING_FREQ_96000),
58     // channelMode
59     (A2DP_LDAC_CHANNEL_MODE_DUAL | A2DP_LDAC_CHANNEL_MODE_STEREO),
60     // bits_per_sample
61     (BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 | BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24 |
62      BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32)};
63 
64 /* LDAC Sink codec capabilities */
65 static const tA2DP_LDAC_CIE a2dp_ldac_sink_caps = {
66     A2DP_LDAC_VENDOR_ID,  // vendorId
67     A2DP_LDAC_CODEC_ID,   // codecId
68     // sampleRate
69     (A2DP_LDAC_SAMPLING_FREQ_44100 | A2DP_LDAC_SAMPLING_FREQ_48000 |
70      A2DP_LDAC_SAMPLING_FREQ_88200 | A2DP_LDAC_SAMPLING_FREQ_96000),
71     // channelMode
72     (A2DP_LDAC_CHANNEL_MODE_DUAL | A2DP_LDAC_CHANNEL_MODE_STEREO),
73     // bits_per_sample
74     (BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 | BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24 |
75      BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32)};
76 
77 /* Default LDAC codec configuration */
78 static const tA2DP_LDAC_CIE a2dp_ldac_default_config = {
79     A2DP_LDAC_VENDOR_ID,                // vendorId
80     A2DP_LDAC_CODEC_ID,                 // codecId
81     A2DP_LDAC_SAMPLING_FREQ_96000,      // sampleRate
82     A2DP_LDAC_CHANNEL_MODE_STEREO,      // channelMode
83     BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32  // bits_per_sample
84 };
85 
86 static const tA2DP_ENCODER_INTERFACE a2dp_encoder_interface_ldac = {
87     a2dp_vendor_ldac_encoder_init,
88     a2dp_vendor_ldac_encoder_cleanup,
89     a2dp_vendor_ldac_feeding_reset,
90     a2dp_vendor_ldac_feeding_flush,
91     a2dp_vendor_ldac_get_encoder_interval_ms,
92     a2dp_vendor_ldac_send_frames,
93     a2dp_vendor_ldac_set_transmit_queue_length};
94 
95 static const tA2DP_DECODER_INTERFACE a2dp_decoder_interface_ldac = {
96     a2dp_vendor_ldac_decoder_init,
97     a2dp_vendor_ldac_decoder_cleanup,
98     a2dp_vendor_ldac_decoder_decode_packet,
99 };
100 
101 UNUSED_ATTR static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityLdac(
102     const tA2DP_LDAC_CIE* p_cap, const uint8_t* p_codec_info,
103     bool is_peer_codec_info);
104 
105 // Builds the LDAC Media Codec Capabilities byte sequence beginning from the
106 // LOSC octet. |media_type| is the media type |AVDT_MEDIA_TYPE_*|.
107 // |p_ie| is a pointer to the LDAC Codec Information Element information.
108 // The result is stored in |p_result|. Returns A2DP_SUCCESS on success,
109 // otherwise the corresponding A2DP error status code.
A2DP_BuildInfoLdac(uint8_t media_type,const tA2DP_LDAC_CIE * p_ie,uint8_t * p_result)110 static tA2DP_STATUS A2DP_BuildInfoLdac(uint8_t media_type,
111                                        const tA2DP_LDAC_CIE* p_ie,
112                                        uint8_t* p_result) {
113   if (p_ie == NULL || p_result == NULL) {
114     return A2DP_INVALID_PARAMS;
115   }
116 
117   *p_result++ = A2DP_LDAC_CODEC_LEN;
118   *p_result++ = (media_type << 4);
119   *p_result++ = A2DP_MEDIA_CT_NON_A2DP;
120 
121   // Vendor ID and Codec ID
122   *p_result++ = (uint8_t)(p_ie->vendorId & 0x000000FF);
123   *p_result++ = (uint8_t)((p_ie->vendorId & 0x0000FF00) >> 8);
124   *p_result++ = (uint8_t)((p_ie->vendorId & 0x00FF0000) >> 16);
125   *p_result++ = (uint8_t)((p_ie->vendorId & 0xFF000000) >> 24);
126   *p_result++ = (uint8_t)(p_ie->codecId & 0x00FF);
127   *p_result++ = (uint8_t)((p_ie->codecId & 0xFF00) >> 8);
128 
129   // Sampling Frequency
130   *p_result = (uint8_t)(p_ie->sampleRate & A2DP_LDAC_SAMPLING_FREQ_MASK);
131   if (*p_result == 0) return A2DP_INVALID_PARAMS;
132   p_result++;
133 
134   // Channel Mode
135   *p_result = (uint8_t)(p_ie->channelMode & A2DP_LDAC_CHANNEL_MODE_MASK);
136   if (*p_result == 0) return A2DP_INVALID_PARAMS;
137 
138   return A2DP_SUCCESS;
139 }
140 
141 // Parses the LDAC Media Codec Capabilities byte sequence beginning from the
142 // LOSC octet. The result is stored in |p_ie|. The byte sequence to parse is
143 // |p_codec_info|. If |is_capability| is true, the byte sequence is
144 // codec capabilities, otherwise is codec configuration.
145 // Returns A2DP_SUCCESS on success, otherwise the corresponding A2DP error
146 // status code.
A2DP_ParseInfoLdac(tA2DP_LDAC_CIE * p_ie,const uint8_t * p_codec_info,bool is_capability)147 static tA2DP_STATUS A2DP_ParseInfoLdac(tA2DP_LDAC_CIE* p_ie,
148                                        const uint8_t* p_codec_info,
149                                        bool is_capability) {
150   uint8_t losc;
151   uint8_t media_type;
152   tA2DP_CODEC_TYPE codec_type;
153 
154   if (p_ie == NULL || p_codec_info == NULL) return A2DP_INVALID_PARAMS;
155 
156   // Check the codec capability length
157   losc = *p_codec_info++;
158   if (losc != A2DP_LDAC_CODEC_LEN) return A2DP_WRONG_CODEC;
159 
160   media_type = (*p_codec_info++) >> 4;
161   codec_type = *p_codec_info++;
162   /* Check the Media Type and Media Codec Type */
163   if (media_type != AVDT_MEDIA_TYPE_AUDIO ||
164       codec_type != A2DP_MEDIA_CT_NON_A2DP) {
165     return A2DP_WRONG_CODEC;
166   }
167 
168   // Check the Vendor ID and Codec ID */
169   p_ie->vendorId = (*p_codec_info & 0x000000FF) |
170                    (*(p_codec_info + 1) << 8 & 0x0000FF00) |
171                    (*(p_codec_info + 2) << 16 & 0x00FF0000) |
172                    (*(p_codec_info + 3) << 24 & 0xFF000000);
173   p_codec_info += 4;
174   p_ie->codecId =
175       (*p_codec_info & 0x00FF) | (*(p_codec_info + 1) << 8 & 0xFF00);
176   p_codec_info += 2;
177   if (p_ie->vendorId != A2DP_LDAC_VENDOR_ID ||
178       p_ie->codecId != A2DP_LDAC_CODEC_ID) {
179     return A2DP_WRONG_CODEC;
180   }
181 
182   p_ie->sampleRate = *p_codec_info++ & A2DP_LDAC_SAMPLING_FREQ_MASK;
183   p_ie->channelMode = *p_codec_info++ & A2DP_LDAC_CHANNEL_MODE_MASK;
184 
185   if (is_capability) {
186     // NOTE: The checks here are very liberal. We should be using more
187     // pedantic checks specific to the SRC or SNK as specified in the spec.
188     if (A2DP_BitsSet(p_ie->sampleRate) == A2DP_SET_ZERO_BIT)
189       return A2DP_BAD_SAMP_FREQ;
190     if (A2DP_BitsSet(p_ie->channelMode) == A2DP_SET_ZERO_BIT)
191       return A2DP_BAD_CH_MODE;
192 
193     return A2DP_SUCCESS;
194   }
195 
196   if (A2DP_BitsSet(p_ie->sampleRate) != A2DP_SET_ONE_BIT)
197     return A2DP_BAD_SAMP_FREQ;
198   if (A2DP_BitsSet(p_ie->channelMode) != A2DP_SET_ONE_BIT)
199     return A2DP_BAD_CH_MODE;
200 
201   return A2DP_SUCCESS;
202 }
203 
204 // Build the LDAC Media Payload Header.
205 // |p_dst| points to the location where the header should be written to.
206 // If |frag| is true, the media payload frame is fragmented.
207 // |start| is true for the first packet of a fragmented frame.
208 // |last| is true for the last packet of a fragmented frame.
209 // If |frag| is false, |num| is the number of number of frames in the packet,
210 // otherwise is the number of remaining fragments (including this one).
A2DP_BuildMediaPayloadHeaderLdac(uint8_t * p_dst,bool frag,bool start,bool last,uint8_t num)211 static void A2DP_BuildMediaPayloadHeaderLdac(uint8_t* p_dst, bool frag,
212                                              bool start, bool last,
213                                              uint8_t num) {
214   if (p_dst == NULL) return;
215 
216   *p_dst = 0;
217   if (frag) *p_dst |= A2DP_LDAC_HDR_F_MSK;
218   if (start) *p_dst |= A2DP_LDAC_HDR_S_MSK;
219   if (last) *p_dst |= A2DP_LDAC_HDR_L_MSK;
220   *p_dst |= (A2DP_LDAC_HDR_NUM_MSK & num);
221 }
222 
A2DP_IsVendorSourceCodecValidLdac(const uint8_t * p_codec_info)223 bool A2DP_IsVendorSourceCodecValidLdac(const uint8_t* p_codec_info) {
224   tA2DP_LDAC_CIE cfg_cie;
225 
226   /* Use a liberal check when parsing the codec info */
227   return (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
228          (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
229 }
230 
A2DP_IsVendorSinkCodecValidLdac(const uint8_t * p_codec_info)231 bool A2DP_IsVendorSinkCodecValidLdac(const uint8_t* p_codec_info) {
232   tA2DP_LDAC_CIE cfg_cie;
233 
234   /* Use a liberal check when parsing the codec info */
235   return (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
236          (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
237 }
238 
A2DP_IsVendorPeerSourceCodecValidLdac(const uint8_t * p_codec_info)239 bool A2DP_IsVendorPeerSourceCodecValidLdac(const uint8_t* p_codec_info) {
240   tA2DP_LDAC_CIE cfg_cie;
241 
242   /* Use a liberal check when parsing the codec info */
243   return (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
244          (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
245 }
246 
A2DP_IsVendorPeerSinkCodecValidLdac(const uint8_t * p_codec_info)247 bool A2DP_IsVendorPeerSinkCodecValidLdac(const uint8_t* p_codec_info) {
248   tA2DP_LDAC_CIE cfg_cie;
249 
250   /* Use a liberal check when parsing the codec info */
251   return (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
252          (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
253 }
254 
A2DP_IsVendorSinkCodecSupportedLdac(const uint8_t * p_codec_info)255 bool A2DP_IsVendorSinkCodecSupportedLdac(const uint8_t* p_codec_info) {
256   return A2DP_CodecInfoMatchesCapabilityLdac(&a2dp_ldac_sink_caps, p_codec_info,
257                                              false) == A2DP_SUCCESS;
258 }
A2DP_IsPeerSourceCodecSupportedLdac(const uint8_t * p_codec_info)259 bool A2DP_IsPeerSourceCodecSupportedLdac(const uint8_t* p_codec_info) {
260   return A2DP_CodecInfoMatchesCapabilityLdac(&a2dp_ldac_sink_caps, p_codec_info,
261                                              true) == A2DP_SUCCESS;
262 }
263 
264 // Checks whether A2DP LDAC codec configuration matches with a device's codec
265 // capabilities. |p_cap| is the LDAC codec configuration. |p_codec_info| is
266 // the device's codec capabilities.
267 // If |is_capability| is true, the byte sequence is codec capabilities,
268 // otherwise is codec configuration.
269 // |p_codec_info| contains the codec capabilities for a peer device that
270 // is acting as an A2DP source.
271 // Returns A2DP_SUCCESS if the codec configuration matches with capabilities,
272 // otherwise the corresponding A2DP error status code.
A2DP_CodecInfoMatchesCapabilityLdac(const tA2DP_LDAC_CIE * p_cap,const uint8_t * p_codec_info,bool is_capability)273 static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityLdac(
274     const tA2DP_LDAC_CIE* p_cap, const uint8_t* p_codec_info,
275     bool is_capability) {
276   tA2DP_STATUS status;
277   tA2DP_LDAC_CIE cfg_cie;
278 
279   /* parse configuration */
280   status = A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, is_capability);
281   if (status != A2DP_SUCCESS) {
282     LOG_ERROR(LOG_TAG, "%s: parsing failed %d", __func__, status);
283     return status;
284   }
285 
286   /* verify that each parameter is in range */
287 
288   LOG_VERBOSE(LOG_TAG, "%s: FREQ peer: 0x%x, capability 0x%x", __func__,
289               cfg_cie.sampleRate, p_cap->sampleRate);
290   LOG_VERBOSE(LOG_TAG, "%s: CH_MODE peer: 0x%x, capability 0x%x", __func__,
291               cfg_cie.channelMode, p_cap->channelMode);
292 
293   /* sampling frequency */
294   if ((cfg_cie.sampleRate & p_cap->sampleRate) == 0) return A2DP_NS_SAMP_FREQ;
295 
296   /* channel mode */
297   if ((cfg_cie.channelMode & p_cap->channelMode) == 0) return A2DP_NS_CH_MODE;
298 
299   return A2DP_SUCCESS;
300 }
301 
A2DP_VendorUsesRtpHeaderLdac(UNUSED_ATTR bool content_protection_enabled,UNUSED_ATTR const uint8_t * p_codec_info)302 bool A2DP_VendorUsesRtpHeaderLdac(UNUSED_ATTR bool content_protection_enabled,
303                                   UNUSED_ATTR const uint8_t* p_codec_info) {
304   // TODO: Is this correct? The RTP header is always included?
305   return true;
306 }
307 
A2DP_VendorCodecNameLdac(UNUSED_ATTR const uint8_t * p_codec_info)308 const char* A2DP_VendorCodecNameLdac(UNUSED_ATTR const uint8_t* p_codec_info) {
309   return "LDAC";
310 }
311 
A2DP_VendorCodecTypeEqualsLdac(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)312 bool A2DP_VendorCodecTypeEqualsLdac(const uint8_t* p_codec_info_a,
313                                     const uint8_t* p_codec_info_b) {
314   tA2DP_LDAC_CIE ldac_cie_a;
315   tA2DP_LDAC_CIE ldac_cie_b;
316 
317   // Check whether the codec info contains valid data
318   tA2DP_STATUS a2dp_status =
319       A2DP_ParseInfoLdac(&ldac_cie_a, p_codec_info_a, true);
320   if (a2dp_status != A2DP_SUCCESS) {
321     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
322               a2dp_status);
323     return false;
324   }
325   a2dp_status = A2DP_ParseInfoLdac(&ldac_cie_b, p_codec_info_b, true);
326   if (a2dp_status != A2DP_SUCCESS) {
327     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
328               a2dp_status);
329     return false;
330   }
331 
332   return true;
333 }
334 
A2DP_VendorCodecEqualsLdac(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)335 bool A2DP_VendorCodecEqualsLdac(const uint8_t* p_codec_info_a,
336                                 const uint8_t* p_codec_info_b) {
337   tA2DP_LDAC_CIE ldac_cie_a;
338   tA2DP_LDAC_CIE ldac_cie_b;
339 
340   // Check whether the codec info contains valid data
341   tA2DP_STATUS a2dp_status =
342       A2DP_ParseInfoLdac(&ldac_cie_a, p_codec_info_a, true);
343   if (a2dp_status != A2DP_SUCCESS) {
344     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
345               a2dp_status);
346     return false;
347   }
348   a2dp_status = A2DP_ParseInfoLdac(&ldac_cie_b, p_codec_info_b, true);
349   if (a2dp_status != A2DP_SUCCESS) {
350     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
351               a2dp_status);
352     return false;
353   }
354 
355   return (ldac_cie_a.sampleRate == ldac_cie_b.sampleRate) &&
356          (ldac_cie_a.channelMode == ldac_cie_b.channelMode);
357 }
358 
A2DP_VendorGetBitRateLdac(const uint8_t * p_codec_info)359 int A2DP_VendorGetBitRateLdac(const uint8_t* p_codec_info) {
360   A2dpCodecConfig* current_codec = bta_av_get_a2dp_current_codec();
361   btav_a2dp_codec_config_t codec_config_ = current_codec->getCodecConfig();
362   int samplerate = A2DP_GetTrackSampleRate(p_codec_info);
363   switch (codec_config_.codec_specific_1 % 10) {
364     case 0:
365       if (samplerate == 44100 || samplerate == 88200)
366         return 909000;
367       else
368         return 990000;
369     case 1:
370       if (samplerate == 44100 || samplerate == 88200)
371         return 606000;
372       else
373         return 660000;
374     case 2:
375       if (samplerate == 44100 || samplerate == 88200)
376         return 303000;
377       else
378         return 330000;
379     case 3:
380     default:
381       if (samplerate == 44100 || samplerate == 88200)
382         return 909000;
383       else
384         return 990000;
385   }
386   return 0;
387 }
388 
A2DP_VendorGetTrackSampleRateLdac(const uint8_t * p_codec_info)389 int A2DP_VendorGetTrackSampleRateLdac(const uint8_t* p_codec_info) {
390   tA2DP_LDAC_CIE ldac_cie;
391 
392   // Check whether the codec info contains valid data
393   tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false);
394   if (a2dp_status != A2DP_SUCCESS) {
395     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
396               a2dp_status);
397     return -1;
398   }
399 
400   switch (ldac_cie.sampleRate) {
401     case A2DP_LDAC_SAMPLING_FREQ_44100:
402       return 44100;
403     case A2DP_LDAC_SAMPLING_FREQ_48000:
404       return 48000;
405     case A2DP_LDAC_SAMPLING_FREQ_88200:
406       return 88200;
407     case A2DP_LDAC_SAMPLING_FREQ_96000:
408       return 96000;
409     case A2DP_LDAC_SAMPLING_FREQ_176400:
410       return 176400;
411     case A2DP_LDAC_SAMPLING_FREQ_192000:
412       return 192000;
413   }
414 
415   return -1;
416 }
417 
A2DP_VendorGetTrackBitsPerSampleLdac(const uint8_t * p_codec_info)418 int A2DP_VendorGetTrackBitsPerSampleLdac(const uint8_t* p_codec_info) {
419   tA2DP_LDAC_CIE ldac_cie;
420 
421   // Check whether the codec info contains valid data
422   tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false);
423   if (a2dp_status != A2DP_SUCCESS) {
424     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
425               a2dp_status);
426     return -1;
427   }
428 
429 #if 1
430   return 32;
431 #else
432   // TODO : Implement proc to care about bit per sample in A2DP_ParseInfoLdac()
433 
434   switch (ldac_cie.bits_per_sample) {
435     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
436       return 16;
437     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
438       return 24;
439     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
440       return 32;
441     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
442       return -1;
443   }
444 #endif
445 }
446 
A2DP_VendorGetTrackChannelCountLdac(const uint8_t * p_codec_info)447 int A2DP_VendorGetTrackChannelCountLdac(const uint8_t* p_codec_info) {
448   tA2DP_LDAC_CIE ldac_cie;
449 
450   // Check whether the codec info contains valid data
451   tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false);
452   if (a2dp_status != A2DP_SUCCESS) {
453     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
454               a2dp_status);
455     return -1;
456   }
457 
458   switch (ldac_cie.channelMode) {
459     case A2DP_LDAC_CHANNEL_MODE_MONO:
460       return 1;
461     case A2DP_LDAC_CHANNEL_MODE_DUAL:
462       return 2;
463     case A2DP_LDAC_CHANNEL_MODE_STEREO:
464       return 2;
465   }
466 
467   return -1;
468 }
469 
A2DP_VendorGetSinkTrackChannelTypeLdac(const uint8_t * p_codec_info)470 int A2DP_VendorGetSinkTrackChannelTypeLdac(const uint8_t* p_codec_info) {
471   tA2DP_LDAC_CIE ldac_cie;
472 
473   // Check whether the codec info contains valid data
474   tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false);
475   if (a2dp_status != A2DP_SUCCESS) {
476     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
477               a2dp_status);
478     return -1;
479   }
480 
481   switch (ldac_cie.channelMode) {
482     case A2DP_LDAC_CHANNEL_MODE_MONO:
483       return 1;
484     case A2DP_LDAC_CHANNEL_MODE_DUAL:
485       return 3;
486     case A2DP_LDAC_CHANNEL_MODE_STEREO:
487       return 3;
488   }
489 
490   return -1;
491 }
492 
A2DP_VendorGetChannelModeCodeLdac(const uint8_t * p_codec_info)493 int A2DP_VendorGetChannelModeCodeLdac(const uint8_t* p_codec_info) {
494   tA2DP_LDAC_CIE ldac_cie;
495 
496   // Check whether the codec info contains valid data
497   tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false);
498   if (a2dp_status != A2DP_SUCCESS) {
499     LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
500               a2dp_status);
501     return -1;
502   }
503 
504   switch (ldac_cie.channelMode) {
505     case A2DP_LDAC_CHANNEL_MODE_MONO:
506     case A2DP_LDAC_CHANNEL_MODE_DUAL:
507     case A2DP_LDAC_CHANNEL_MODE_STEREO:
508       return ldac_cie.channelMode;
509     default:
510       break;
511   }
512 
513   return -1;
514 }
515 
A2DP_VendorGetPacketTimestampLdac(UNUSED_ATTR const uint8_t * p_codec_info,const uint8_t * p_data,uint32_t * p_timestamp)516 bool A2DP_VendorGetPacketTimestampLdac(UNUSED_ATTR const uint8_t* p_codec_info,
517                                        const uint8_t* p_data,
518                                        uint32_t* p_timestamp) {
519   // TODO: Is this function really codec-specific?
520   *p_timestamp = *(const uint32_t*)p_data;
521   return true;
522 }
523 
A2DP_VendorBuildCodecHeaderLdac(UNUSED_ATTR const uint8_t * p_codec_info,BT_HDR * p_buf,uint16_t frames_per_packet)524 bool A2DP_VendorBuildCodecHeaderLdac(UNUSED_ATTR const uint8_t* p_codec_info,
525                                      BT_HDR* p_buf,
526                                      uint16_t frames_per_packet) {
527   uint8_t* p;
528 
529   p_buf->offset -= A2DP_LDAC_MPL_HDR_LEN;
530   p = (uint8_t*)(p_buf + 1) + p_buf->offset;
531   p_buf->len += A2DP_LDAC_MPL_HDR_LEN;
532   A2DP_BuildMediaPayloadHeaderLdac(p, false, false, false,
533                                    (uint8_t)frames_per_packet);
534 
535   return true;
536 }
537 
A2DP_VendorCodecInfoStringLdac(const uint8_t * p_codec_info)538 std::string A2DP_VendorCodecInfoStringLdac(const uint8_t* p_codec_info) {
539   std::stringstream res;
540   std::string field;
541   tA2DP_STATUS a2dp_status;
542   tA2DP_LDAC_CIE ldac_cie;
543 
544   a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, true);
545   if (a2dp_status != A2DP_SUCCESS) {
546     res << "A2DP_ParseInfoLdac fail: " << loghex(a2dp_status);
547     return res.str();
548   }
549 
550   res << "\tname: LDAC\n";
551 
552   // Sample frequency
553   field.clear();
554   AppendField(&field, (ldac_cie.sampleRate == 0), "NONE");
555   AppendField(&field, (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100),
556               "44100");
557   AppendField(&field, (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000),
558               "48000");
559   AppendField(&field, (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200),
560               "88200");
561   AppendField(&field, (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000),
562               "96000");
563   AppendField(&field, (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400),
564               "176400");
565   AppendField(&field, (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000),
566               "192000");
567   res << "\tsamp_freq: " << field << " (" << loghex(ldac_cie.sampleRate)
568       << ")\n";
569 
570   // Channel mode
571   field.clear();
572   AppendField(&field, (ldac_cie.channelMode == 0), "NONE");
573   AppendField(&field, (ldac_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_MONO),
574               "Mono");
575   AppendField(&field, (ldac_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL),
576               "Dual");
577   AppendField(&field, (ldac_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO),
578               "Stereo");
579   res << "\tch_mode: " << field << " (" << loghex(ldac_cie.channelMode)
580       << ")\n";
581 
582   return res.str();
583 }
584 
A2DP_VendorGetEncoderInterfaceLdac(const uint8_t * p_codec_info)585 const tA2DP_ENCODER_INTERFACE* A2DP_VendorGetEncoderInterfaceLdac(
586     const uint8_t* p_codec_info) {
587   if (!A2DP_IsVendorSourceCodecValidLdac(p_codec_info)) return NULL;
588 
589   return &a2dp_encoder_interface_ldac;
590 }
591 
A2DP_VendorGetDecoderInterfaceLdac(const uint8_t * p_codec_info)592 const tA2DP_DECODER_INTERFACE* A2DP_VendorGetDecoderInterfaceLdac(
593     const uint8_t* p_codec_info) {
594   if (!A2DP_IsVendorSinkCodecValidLdac(p_codec_info)) return NULL;
595 
596   return &a2dp_decoder_interface_ldac;
597 }
598 
A2DP_VendorAdjustCodecLdac(uint8_t * p_codec_info)599 bool A2DP_VendorAdjustCodecLdac(uint8_t* p_codec_info) {
600   tA2DP_LDAC_CIE cfg_cie;
601 
602   // Nothing to do: just verify the codec info is valid
603   if (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS)
604     return false;
605 
606   return true;
607 }
608 
A2DP_VendorSourceCodecIndexLdac(UNUSED_ATTR const uint8_t * p_codec_info)609 btav_a2dp_codec_index_t A2DP_VendorSourceCodecIndexLdac(
610     UNUSED_ATTR const uint8_t* p_codec_info) {
611   return BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC;
612 }
613 
A2DP_VendorSinkCodecIndexLdac(UNUSED_ATTR const uint8_t * p_codec_info)614 btav_a2dp_codec_index_t A2DP_VendorSinkCodecIndexLdac(
615     UNUSED_ATTR const uint8_t* p_codec_info) {
616   return BTAV_A2DP_CODEC_INDEX_SINK_LDAC;
617 }
618 
A2DP_VendorCodecIndexStrLdac(void)619 const char* A2DP_VendorCodecIndexStrLdac(void) { return "LDAC"; }
620 
A2DP_VendorCodecIndexStrLdacSink(void)621 const char* A2DP_VendorCodecIndexStrLdacSink(void) { return "LDAC SINK"; }
622 
A2DP_VendorInitCodecConfigLdac(AvdtpSepConfig * p_cfg)623 bool A2DP_VendorInitCodecConfigLdac(AvdtpSepConfig* p_cfg) {
624   if (A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &a2dp_ldac_source_caps,
625                          p_cfg->codec_info) != A2DP_SUCCESS) {
626     return false;
627   }
628 
629 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
630   /* Content protection info - support SCMS-T */
631   uint8_t* p = p_cfg->protect_info;
632   *p++ = AVDT_CP_LOSC;
633   UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID);
634   p_cfg->num_protect = 1;
635 #endif
636 
637   return true;
638 }
639 
A2DP_VendorInitCodecConfigLdacSink(AvdtpSepConfig * p_cfg)640 bool A2DP_VendorInitCodecConfigLdacSink(AvdtpSepConfig* p_cfg) {
641   return A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &a2dp_ldac_sink_caps,
642                             p_cfg->codec_info) == A2DP_SUCCESS;
643 }
644 
build_codec_config(const tA2DP_LDAC_CIE & config_cie,btav_a2dp_codec_config_t * result)645 UNUSED_ATTR static void build_codec_config(const tA2DP_LDAC_CIE& config_cie,
646                                            btav_a2dp_codec_config_t* result) {
647   if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100)
648     result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
649   if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000)
650     result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
651   if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200)
652     result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
653   if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000)
654     result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
655   if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400)
656     result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
657   if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000)
658     result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
659 
660   result->bits_per_sample = config_cie.bits_per_sample;
661 
662   if (config_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_MONO)
663     result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
664   if (config_cie.channelMode &
665       (A2DP_LDAC_CHANNEL_MODE_DUAL | A2DP_LDAC_CHANNEL_MODE_STEREO)) {
666     result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
667   }
668 }
669 
A2dpCodecConfigLdacSource(btav_a2dp_codec_priority_t codec_priority)670 A2dpCodecConfigLdacSource::A2dpCodecConfigLdacSource(
671     btav_a2dp_codec_priority_t codec_priority)
672     : A2dpCodecConfigLdacBase(BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC,
673                               A2DP_VendorCodecIndexStrLdac(), codec_priority,
674                               true) {
675   // Compute the local capability
676   if (a2dp_ldac_source_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
677     codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
678   }
679   if (a2dp_ldac_source_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
680     codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
681   }
682   if (a2dp_ldac_source_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
683     codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
684   }
685   if (a2dp_ldac_source_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
686     codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
687   }
688   if (a2dp_ldac_source_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
689     codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
690   }
691   if (a2dp_ldac_source_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
692     codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
693   }
694   codec_local_capability_.bits_per_sample =
695       a2dp_ldac_source_caps.bits_per_sample;
696   if (a2dp_ldac_source_caps.channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
697     codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
698   }
699   if (a2dp_ldac_source_caps.channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
700     codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
701   }
702   if (a2dp_ldac_source_caps.channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
703     codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
704   }
705 }
706 
~A2dpCodecConfigLdacSource()707 A2dpCodecConfigLdacSource::~A2dpCodecConfigLdacSource() {}
708 
init()709 bool A2dpCodecConfigLdacSource::init() {
710   if (!isValid()) return false;
711 
712   // Load the encoder
713   if (!A2DP_VendorLoadEncoderLdac()) {
714     LOG_ERROR(LOG_TAG, "%s: cannot load the encoder", __func__);
715     return false;
716   }
717 
718   return true;
719 }
720 
useRtpHeaderMarkerBit() const721 bool A2dpCodecConfigLdacSource::useRtpHeaderMarkerBit() const { return false; }
722 
723 //
724 // Selects the best sample rate from |sampleRate|.
725 // The result is stored in |p_result| and |p_codec_config|.
726 // Returns true if a selection was made, otherwise false.
727 //
select_best_sample_rate(uint8_t sampleRate,tA2DP_LDAC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)728 static bool select_best_sample_rate(uint8_t sampleRate,
729                                     tA2DP_LDAC_CIE* p_result,
730                                     btav_a2dp_codec_config_t* p_codec_config) {
731   if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
732     p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000;
733     p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
734     return true;
735   }
736   if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
737     p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400;
738     p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
739     return true;
740   }
741   if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
742     p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000;
743     p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
744     return true;
745   }
746   if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
747     p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200;
748     p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
749     return true;
750   }
751   if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
752     p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000;
753     p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
754     return true;
755   }
756   if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
757     p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100;
758     p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
759     return true;
760   }
761   return false;
762 }
763 
764 //
765 // Selects the audio sample rate from |p_codec_audio_config|.
766 // |sampleRate| contains the capability.
767 // The result is stored in |p_result| and |p_codec_config|.
768 // Returns true if a selection was made, otherwise false.
769 //
select_audio_sample_rate(const btav_a2dp_codec_config_t * p_codec_audio_config,uint8_t sampleRate,tA2DP_LDAC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)770 static bool select_audio_sample_rate(
771     const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t sampleRate,
772     tA2DP_LDAC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
773   switch (p_codec_audio_config->sample_rate) {
774     case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
775       if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
776         p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100;
777         p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
778         return true;
779       }
780       break;
781     case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
782       if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
783         p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000;
784         p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
785         return true;
786       }
787       break;
788     case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
789       if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
790         p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200;
791         p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
792         return true;
793       }
794       break;
795     case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
796       if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
797         p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000;
798         p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
799         return true;
800       }
801       break;
802     case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
803       if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
804         p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400;
805         p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
806         return true;
807       }
808       break;
809     case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
810       if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
811         p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000;
812         p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
813         return true;
814       }
815       break;
816     case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
817     case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
818     case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
819       break;
820   }
821   return false;
822 }
823 
824 //
825 // Selects the best bits per sample from |bits_per_sample|.
826 // |bits_per_sample| contains the capability.
827 // The result is stored in |p_result| and |p_codec_config|.
828 // Returns true if a selection was made, otherwise false.
829 //
select_best_bits_per_sample(btav_a2dp_codec_bits_per_sample_t bits_per_sample,tA2DP_LDAC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)830 static bool select_best_bits_per_sample(
831     btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_LDAC_CIE* p_result,
832     btav_a2dp_codec_config_t* p_codec_config) {
833   if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
834     p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
835     p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
836     return true;
837   }
838   if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
839     p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
840     p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
841     return true;
842   }
843   if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
844     p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
845     p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
846     return true;
847   }
848   return false;
849 }
850 
851 //
852 // Selects the audio bits per sample from |p_codec_audio_config|.
853 // |bits_per_sample| contains the capability.
854 // The result is stored in |p_result| and |p_codec_config|.
855 // Returns true if a selection was made, otherwise false.
856 //
select_audio_bits_per_sample(const btav_a2dp_codec_config_t * p_codec_audio_config,btav_a2dp_codec_bits_per_sample_t bits_per_sample,tA2DP_LDAC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)857 static bool select_audio_bits_per_sample(
858     const btav_a2dp_codec_config_t* p_codec_audio_config,
859     btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_LDAC_CIE* p_result,
860     btav_a2dp_codec_config_t* p_codec_config) {
861   switch (p_codec_audio_config->bits_per_sample) {
862     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
863       if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
864         p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
865         p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
866         return true;
867       }
868       break;
869     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
870       if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
871         p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
872         p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
873         return true;
874       }
875       break;
876     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
877       if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
878         p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
879         p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
880         return true;
881       }
882       break;
883     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
884       break;
885   }
886   return false;
887 }
888 
889 //
890 // Selects the best channel mode from |channelMode|.
891 // The result is stored in |p_result| and |p_codec_config|.
892 // Returns true if a selection was made, otherwise false.
893 //
select_best_channel_mode(uint8_t channelMode,tA2DP_LDAC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)894 static bool select_best_channel_mode(uint8_t channelMode,
895                                      tA2DP_LDAC_CIE* p_result,
896                                      btav_a2dp_codec_config_t* p_codec_config) {
897   if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
898     p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO;
899     p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
900     return true;
901   }
902   if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
903     p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL;
904     p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
905     return true;
906   }
907   if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
908     p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_MONO;
909     p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
910     return true;
911   }
912   return false;
913 }
914 
915 //
916 // Selects the audio channel mode from |p_codec_audio_config|.
917 // |channelMode| contains the capability.
918 // The result is stored in |p_result| and |p_codec_config|.
919 // Returns true if a selection was made, otherwise false.
920 //
select_audio_channel_mode(const btav_a2dp_codec_config_t * p_codec_audio_config,uint8_t channelMode,tA2DP_LDAC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)921 static bool select_audio_channel_mode(
922     const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t channelMode,
923     tA2DP_LDAC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
924   switch (p_codec_audio_config->channel_mode) {
925     case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
926       if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
927         p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_MONO;
928         p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
929         return true;
930       }
931       break;
932     case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
933       if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
934         p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO;
935         p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
936         return true;
937       }
938       if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
939         p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL;
940         p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
941         return true;
942       }
943       break;
944     case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
945       break;
946   }
947 
948   return false;
949 }
950 
setCodecConfig(const uint8_t * p_peer_codec_info,bool is_capability,uint8_t * p_result_codec_config)951 bool A2dpCodecConfigLdacBase::setCodecConfig(const uint8_t* p_peer_codec_info,
952                                              bool is_capability,
953                                              uint8_t* p_result_codec_config) {
954   std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
955   tA2DP_LDAC_CIE peer_info_cie;
956   tA2DP_LDAC_CIE result_config_cie;
957   uint8_t channelMode;
958   uint8_t sampleRate;
959   btav_a2dp_codec_bits_per_sample_t bits_per_sample;
960   const tA2DP_LDAC_CIE* p_a2dp_ldac_caps =
961       (is_source_) ? &a2dp_ldac_source_caps : &a2dp_ldac_sink_caps;
962 
963   // Save the internal state
964   btav_a2dp_codec_config_t saved_codec_config = codec_config_;
965   btav_a2dp_codec_config_t saved_codec_capability = codec_capability_;
966   btav_a2dp_codec_config_t saved_codec_selectable_capability =
967       codec_selectable_capability_;
968   btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_;
969   btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_;
970   uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE];
971   uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
972   uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE];
973   memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_));
974   memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
975          sizeof(ota_codec_peer_capability_));
976   memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_,
977          sizeof(ota_codec_peer_config_));
978 
979   tA2DP_STATUS status =
980       A2DP_ParseInfoLdac(&peer_info_cie, p_peer_codec_info, is_capability);
981   if (status != A2DP_SUCCESS) {
982     LOG_ERROR(LOG_TAG, "%s: can't parse peer's capabilities: error = %d",
983               __func__, status);
984     goto fail;
985   }
986 
987   //
988   // Build the preferred configuration
989   //
990   memset(&result_config_cie, 0, sizeof(result_config_cie));
991   result_config_cie.vendorId = p_a2dp_ldac_caps->vendorId;
992   result_config_cie.codecId = p_a2dp_ldac_caps->codecId;
993 
994   //
995   // Select the sample frequency
996   //
997   sampleRate = p_a2dp_ldac_caps->sampleRate & peer_info_cie.sampleRate;
998   codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
999   switch (codec_user_config_.sample_rate) {
1000     case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
1001       if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
1002         result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100;
1003         codec_capability_.sample_rate = codec_user_config_.sample_rate;
1004         codec_config_.sample_rate = codec_user_config_.sample_rate;
1005       }
1006       break;
1007     case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
1008       if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
1009         result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000;
1010         codec_capability_.sample_rate = codec_user_config_.sample_rate;
1011         codec_config_.sample_rate = codec_user_config_.sample_rate;
1012       }
1013       break;
1014     case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
1015       if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
1016         result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200;
1017         codec_capability_.sample_rate = codec_user_config_.sample_rate;
1018         codec_config_.sample_rate = codec_user_config_.sample_rate;
1019       }
1020       break;
1021     case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
1022       if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
1023         result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000;
1024         codec_capability_.sample_rate = codec_user_config_.sample_rate;
1025         codec_config_.sample_rate = codec_user_config_.sample_rate;
1026       }
1027       break;
1028     case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
1029       if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
1030         result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400;
1031         codec_capability_.sample_rate = codec_user_config_.sample_rate;
1032         codec_config_.sample_rate = codec_user_config_.sample_rate;
1033       }
1034       break;
1035     case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
1036       if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
1037         result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000;
1038         codec_capability_.sample_rate = codec_user_config_.sample_rate;
1039         codec_config_.sample_rate = codec_user_config_.sample_rate;
1040       }
1041       break;
1042     case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
1043     case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
1044     case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
1045       codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
1046       codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
1047       break;
1048   }
1049 
1050   // Select the sample frequency if there is no user preference
1051   do {
1052     // Compute the selectable capability
1053     if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
1054       codec_selectable_capability_.sample_rate |=
1055           BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1056     }
1057     if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
1058       codec_selectable_capability_.sample_rate |=
1059           BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1060     }
1061     if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
1062       codec_selectable_capability_.sample_rate |=
1063           BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
1064     }
1065     if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
1066       codec_selectable_capability_.sample_rate |=
1067           BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
1068     }
1069     if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
1070       codec_selectable_capability_.sample_rate |=
1071           BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
1072     }
1073     if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
1074       codec_selectable_capability_.sample_rate |=
1075           BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
1076     }
1077 
1078     if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break;
1079 
1080     // Compute the common capability
1081     if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100)
1082       codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1083     if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000)
1084       codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1085     if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200)
1086       codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
1087     if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000)
1088       codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
1089     if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400)
1090       codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
1091     if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000)
1092       codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
1093 
1094     // No user preference - try the codec audio config
1095     if (select_audio_sample_rate(&codec_audio_config_, sampleRate,
1096                                  &result_config_cie, &codec_config_)) {
1097       break;
1098     }
1099 
1100     // No user preference - try the default config
1101     if (select_best_sample_rate(
1102             a2dp_ldac_default_config.sampleRate & peer_info_cie.sampleRate,
1103             &result_config_cie, &codec_config_)) {
1104       break;
1105     }
1106 
1107     // No user preference - use the best match
1108     if (select_best_sample_rate(sampleRate, &result_config_cie,
1109                                 &codec_config_)) {
1110       break;
1111     }
1112   } while (false);
1113   if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) {
1114     LOG_ERROR(LOG_TAG,
1115               "%s: cannot match sample frequency: local caps = 0x%x "
1116               "peer info = 0x%x",
1117               __func__, p_a2dp_ldac_caps->sampleRate, peer_info_cie.sampleRate);
1118     goto fail;
1119   }
1120 
1121   //
1122   // Select the bits per sample
1123   //
1124   // NOTE: this information is NOT included in the LDAC A2DP codec description
1125   // that is sent OTA.
1126   bits_per_sample = p_a2dp_ldac_caps->bits_per_sample;
1127   codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1128   switch (codec_user_config_.bits_per_sample) {
1129     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
1130       if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
1131         result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1132         codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1133         codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1134       }
1135       break;
1136     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
1137       if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
1138         result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1139         codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1140         codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1141       }
1142       break;
1143     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
1144       if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
1145         result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1146         codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1147         codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1148       }
1149       break;
1150     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
1151       result_config_cie.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1152       codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1153       codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1154       break;
1155   }
1156 
1157   // Select the bits per sample if there is no user preference
1158   do {
1159     // Compute the selectable capability
1160     codec_selectable_capability_.bits_per_sample =
1161         p_a2dp_ldac_caps->bits_per_sample;
1162 
1163     if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE)
1164       break;
1165 
1166     // Compute the common capability
1167     codec_capability_.bits_per_sample = bits_per_sample;
1168 
1169     // No user preference - the the codec audio config
1170     if (select_audio_bits_per_sample(&codec_audio_config_,
1171                                      p_a2dp_ldac_caps->bits_per_sample,
1172                                      &result_config_cie, &codec_config_)) {
1173       break;
1174     }
1175 
1176     // No user preference - try the default config
1177     if (select_best_bits_per_sample(a2dp_ldac_default_config.bits_per_sample,
1178                                     &result_config_cie, &codec_config_)) {
1179       break;
1180     }
1181 
1182     // No user preference - use the best match
1183     if (select_best_bits_per_sample(p_a2dp_ldac_caps->bits_per_sample,
1184                                     &result_config_cie, &codec_config_)) {
1185       break;
1186     }
1187   } while (false);
1188   if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) {
1189     LOG_ERROR(LOG_TAG,
1190               "%s: cannot match bits per sample: default = 0x%x "
1191               "user preference = 0x%x",
1192               __func__, a2dp_ldac_default_config.bits_per_sample,
1193               codec_user_config_.bits_per_sample);
1194     goto fail;
1195   }
1196 
1197   //
1198   // Select the channel mode
1199   //
1200   channelMode = p_a2dp_ldac_caps->channelMode & peer_info_cie.channelMode;
1201   codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1202   switch (codec_user_config_.channel_mode) {
1203     case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
1204       if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
1205         result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_MONO;
1206         codec_capability_.channel_mode = codec_user_config_.channel_mode;
1207         codec_config_.channel_mode = codec_user_config_.channel_mode;
1208       }
1209       break;
1210     case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
1211       if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
1212         result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO;
1213         codec_capability_.channel_mode = codec_user_config_.channel_mode;
1214         codec_config_.channel_mode = codec_user_config_.channel_mode;
1215         break;
1216       }
1217       if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
1218         result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL;
1219         codec_capability_.channel_mode = codec_user_config_.channel_mode;
1220         codec_config_.channel_mode = codec_user_config_.channel_mode;
1221         break;
1222       }
1223       break;
1224     case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1225       codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1226       codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1227       break;
1228   }
1229 
1230   // Select the channel mode if there is no user preference
1231   do {
1232     // Compute the selectable capability
1233     if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
1234       codec_selectable_capability_.channel_mode |=
1235           BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1236     }
1237     if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
1238       codec_selectable_capability_.channel_mode |=
1239           BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1240     }
1241     if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
1242       codec_selectable_capability_.channel_mode |=
1243           BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1244     }
1245 
1246     if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break;
1247 
1248     // Compute the common capability
1249     if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO)
1250       codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1251     if (channelMode &
1252         (A2DP_LDAC_CHANNEL_MODE_STEREO | A2DP_LDAC_CHANNEL_MODE_DUAL)) {
1253       codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1254     }
1255 
1256     // No user preference - try the codec audio config
1257     if (select_audio_channel_mode(&codec_audio_config_, channelMode,
1258                                   &result_config_cie, &codec_config_)) {
1259       break;
1260     }
1261 
1262     // No user preference - try the default config
1263     if (select_best_channel_mode(
1264             a2dp_ldac_default_config.channelMode & peer_info_cie.channelMode,
1265             &result_config_cie, &codec_config_)) {
1266       break;
1267     }
1268 
1269     // No user preference - use the best match
1270     if (select_best_channel_mode(channelMode, &result_config_cie,
1271                                  &codec_config_)) {
1272       break;
1273     }
1274   } while (false);
1275   if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) {
1276     LOG_ERROR(LOG_TAG,
1277               "%s: cannot match channel mode: local caps = 0x%x "
1278               "peer info = 0x%x",
1279               __func__, p_a2dp_ldac_caps->channelMode,
1280               peer_info_cie.channelMode);
1281     goto fail;
1282   }
1283 
1284   if (A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1285                          p_result_codec_config) != A2DP_SUCCESS) {
1286     goto fail;
1287   }
1288 
1289   //
1290   // Copy the codec-specific fields if they are not zero
1291   //
1292   if (codec_user_config_.codec_specific_1 != 0)
1293     codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
1294   if (codec_user_config_.codec_specific_2 != 0)
1295     codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2;
1296   if (codec_user_config_.codec_specific_3 != 0)
1297     codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3;
1298   if (codec_user_config_.codec_specific_4 != 0)
1299     codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4;
1300 
1301   // Create a local copy of the peer codec capability, and the
1302   // result codec config.
1303   if (is_capability) {
1304     status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1305                                 ota_codec_peer_capability_);
1306   } else {
1307     status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1308                                 ota_codec_peer_config_);
1309   }
1310   CHECK(status == A2DP_SUCCESS);
1311   status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1312                               ota_codec_config_);
1313   CHECK(status == A2DP_SUCCESS);
1314   return true;
1315 
1316 fail:
1317   // Restore the internal state
1318   codec_config_ = saved_codec_config;
1319   codec_capability_ = saved_codec_capability;
1320   codec_selectable_capability_ = saved_codec_selectable_capability;
1321   codec_user_config_ = saved_codec_user_config;
1322   codec_audio_config_ = saved_codec_audio_config;
1323   memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_));
1324   memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1325          sizeof(ota_codec_peer_capability_));
1326   memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config,
1327          sizeof(ota_codec_peer_config_));
1328   return false;
1329 }
1330 
setPeerCodecCapabilities(const uint8_t * p_peer_codec_capabilities)1331 bool A2dpCodecConfigLdacBase::setPeerCodecCapabilities(
1332     const uint8_t* p_peer_codec_capabilities) {
1333   std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
1334   tA2DP_LDAC_CIE peer_info_cie;
1335   uint8_t channelMode;
1336   uint8_t sampleRate;
1337   const tA2DP_LDAC_CIE* p_a2dp_ldac_caps =
1338       (is_source_) ? &a2dp_ldac_source_caps : &a2dp_ldac_sink_caps;
1339 
1340   // Save the internal state
1341   btav_a2dp_codec_config_t saved_codec_selectable_capability =
1342       codec_selectable_capability_;
1343   uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
1344   memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
1345          sizeof(ota_codec_peer_capability_));
1346 
1347   tA2DP_STATUS status =
1348       A2DP_ParseInfoLdac(&peer_info_cie, p_peer_codec_capabilities, true);
1349   if (status != A2DP_SUCCESS) {
1350     LOG_ERROR(LOG_TAG, "%s: can't parse peer's capabilities: error = %d",
1351               __func__, status);
1352     goto fail;
1353   }
1354 
1355   // Compute the selectable capability - sample rate
1356   sampleRate = p_a2dp_ldac_caps->sampleRate & peer_info_cie.sampleRate;
1357   if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
1358     codec_selectable_capability_.sample_rate |=
1359         BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1360   }
1361   if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
1362     codec_selectable_capability_.sample_rate |=
1363         BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1364   }
1365   if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
1366     codec_selectable_capability_.sample_rate |=
1367         BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
1368   }
1369   if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
1370     codec_selectable_capability_.sample_rate |=
1371         BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
1372   }
1373   if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
1374     codec_selectable_capability_.sample_rate |=
1375         BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
1376   }
1377   if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
1378     codec_selectable_capability_.sample_rate |=
1379         BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
1380   }
1381 
1382   // Compute the selectable capability - bits per sample
1383   codec_selectable_capability_.bits_per_sample =
1384       p_a2dp_ldac_caps->bits_per_sample;
1385 
1386   // Compute the selectable capability - channel mode
1387   channelMode = p_a2dp_ldac_caps->channelMode & peer_info_cie.channelMode;
1388   if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
1389     codec_selectable_capability_.channel_mode |=
1390         BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1391   }
1392   if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
1393     codec_selectable_capability_.channel_mode |=
1394         BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1395   }
1396   if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
1397     codec_selectable_capability_.channel_mode |=
1398         BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1399   }
1400 
1401   status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1402                               ota_codec_peer_capability_);
1403   CHECK(status == A2DP_SUCCESS);
1404   return true;
1405 
1406 fail:
1407   // Restore the internal state
1408   codec_selectable_capability_ = saved_codec_selectable_capability;
1409   memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1410          sizeof(ota_codec_peer_capability_));
1411   return false;
1412 }
1413 
A2dpCodecConfigLdacSink(btav_a2dp_codec_priority_t codec_priority)1414 A2dpCodecConfigLdacSink::A2dpCodecConfigLdacSink(
1415     btav_a2dp_codec_priority_t codec_priority)
1416     : A2dpCodecConfigLdacBase(BTAV_A2DP_CODEC_INDEX_SINK_LDAC,
1417                               A2DP_VendorCodecIndexStrLdacSink(),
1418                               codec_priority, false) {}
1419 
~A2dpCodecConfigLdacSink()1420 A2dpCodecConfigLdacSink::~A2dpCodecConfigLdacSink() {}
1421 
init()1422 bool A2dpCodecConfigLdacSink::init() {
1423   if (!isValid()) return false;
1424 
1425   // Load the decoder
1426   if (!A2DP_VendorLoadDecoderLdac()) {
1427     LOG_ERROR(LOG_TAG, "%s: cannot load the decoder", __func__);
1428     return false;
1429   }
1430 
1431   return true;
1432 }
1433 
encoderIntervalMs() const1434 uint64_t A2dpCodecConfigLdacSink::encoderIntervalMs() const {
1435   // TODO: This method applies only to Source codecs
1436   return 0;
1437 }
1438 
getEffectiveMtu() const1439 int A2dpCodecConfigLdacSink::getEffectiveMtu() const {
1440   // TODO: This method applies only to Source codecs
1441   return 0;
1442 }
1443 
useRtpHeaderMarkerBit() const1444 bool A2dpCodecConfigLdacSink::useRtpHeaderMarkerBit() const {
1445   // TODO: This method applies only to Source codecs
1446   return false;
1447 }
1448 
updateEncoderUserConfig(UNUSED_ATTR const tA2DP_ENCODER_INIT_PEER_PARAMS * p_peer_params,UNUSED_ATTR bool * p_restart_input,UNUSED_ATTR bool * p_restart_output,UNUSED_ATTR bool * p_config_updated)1449 bool A2dpCodecConfigLdacSink::updateEncoderUserConfig(
1450     UNUSED_ATTR const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
1451     UNUSED_ATTR bool* p_restart_input, UNUSED_ATTR bool* p_restart_output,
1452     UNUSED_ATTR bool* p_config_updated) {
1453   // TODO: This method applies only to Source codecs
1454   return false;
1455 }
1456