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