a2dp_vendor_ldac.cc revision 494e53e904e8aecc28178fda5856071776b2caf0
1/*
2 * Copyright (C) 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_encoder.h"
35#include "bt_utils.h"
36#include "osi/include/log.h"
37#include "osi/include/osi.h"
38
39// data type for the LDAC Codec Information Element */
40// NOTE: bits_per_sample is needed only for LDAC encoder initialization.
41typedef struct {
42  uint32_t vendorId;
43  uint16_t codecId;    /* Codec ID for LDAC */
44  uint8_t sampleRate;  /* Sampling Frequency */
45  uint8_t channelMode; /* STEREO/DUAL/MONO */
46  btav_a2dp_codec_bits_per_sample_t bits_per_sample;
47} tA2DP_LDAC_CIE;
48
49/* LDAC Source codec capabilities */
50static const tA2DP_LDAC_CIE a2dp_ldac_caps = {
51    A2DP_LDAC_VENDOR_ID,  // vendorId
52    A2DP_LDAC_CODEC_ID,   // codecId
53    // sampleRate
54    (A2DP_LDAC_SAMPLING_FREQ_44100 | A2DP_LDAC_SAMPLING_FREQ_48000 |
55     A2DP_LDAC_SAMPLING_FREQ_88200 | A2DP_LDAC_SAMPLING_FREQ_96000),
56    // channelMode
57    (A2DP_LDAC_CHANNEL_MODE_MONO | A2DP_LDAC_CHANNEL_MODE_DUAL |
58     A2DP_LDAC_CHANNEL_MODE_STEREO),
59    // bits_per_sample
60    (BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 | BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24 |
61     BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32)};
62
63/* Default LDAC codec configuration */
64static const tA2DP_LDAC_CIE a2dp_ldac_default_config = {
65    A2DP_LDAC_VENDOR_ID,                // vendorId
66    A2DP_LDAC_CODEC_ID,                 // codecId
67    A2DP_LDAC_SAMPLING_FREQ_96000,      // sampleRate
68    A2DP_LDAC_CHANNEL_MODE_STEREO,      // channelMode
69    BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32  // bits_per_sample
70};
71
72static const tA2DP_ENCODER_INTERFACE a2dp_encoder_interface_ldac = {
73    a2dp_vendor_ldac_encoder_init,
74    a2dp_vendor_ldac_encoder_cleanup,
75    a2dp_vendor_ldac_feeding_reset,
76    a2dp_vendor_ldac_feeding_flush,
77    a2dp_vendor_ldac_get_encoder_interval_ms,
78    a2dp_vendor_ldac_send_frames,
79    a2dp_vendor_ldac_debug_codec_dump};
80
81UNUSED_ATTR static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityLdac(
82    const tA2DP_LDAC_CIE* p_cap, const uint8_t* p_codec_info,
83    bool is_peer_codec_info);
84
85// Builds the LDAC Media Codec Capabilities byte sequence beginning from the
86// LOSC octet. |media_type| is the media type |AVDT_MEDIA_TYPE_*|.
87// |p_ie| is a pointer to the LDAC Codec Information Element information.
88// The result is stored in |p_result|. Returns A2DP_SUCCESS on success,
89// otherwise the corresponding A2DP error status code.
90static tA2DP_STATUS A2DP_BuildInfoLdac(uint8_t media_type,
91                                       const tA2DP_LDAC_CIE* p_ie,
92                                       uint8_t* p_result) {
93  if (p_ie == NULL || p_result == NULL) {
94    return A2DP_INVALID_PARAMS;
95  }
96
97  *p_result++ = A2DP_LDAC_CODEC_LEN;
98  *p_result++ = (media_type << 4);
99  *p_result++ = A2DP_MEDIA_CT_NON_A2DP;
100
101  // Vendor ID and Codec ID
102  *p_result++ = (uint8_t)(p_ie->vendorId & 0x000000FF);
103  *p_result++ = (uint8_t)((p_ie->vendorId & 0x0000FF00) >> 8);
104  *p_result++ = (uint8_t)((p_ie->vendorId & 0x00FF0000) >> 16);
105  *p_result++ = (uint8_t)((p_ie->vendorId & 0xFF000000) >> 24);
106  *p_result++ = (uint8_t)(p_ie->codecId & 0x00FF);
107  *p_result++ = (uint8_t)((p_ie->codecId & 0xFF00) >> 8);
108
109  // Sampling Frequency
110  *p_result = (uint8_t)(p_ie->sampleRate & A2DP_LDAC_SAMPLING_FREQ_MASK);
111  if (*p_result == 0) return A2DP_INVALID_PARAMS;
112  p_result++;
113
114  // Channel Mode
115  *p_result = (uint8_t)(p_ie->channelMode & A2DP_LDAC_CHANNEL_MODE_MASK);
116  if (*p_result == 0) return A2DP_INVALID_PARAMS;
117
118  return A2DP_SUCCESS;
119}
120
121// Parses the LDAC Media Codec Capabilities byte sequence beginning from the
122// LOSC octet. The result is stored in |p_ie|. The byte sequence to parse is
123// |p_codec_info|. If |is_capability| is true, the byte sequence is
124// codec capabilities, otherwise is codec configuration.
125// Returns A2DP_SUCCESS on success, otherwise the corresponding A2DP error
126// status code.
127static tA2DP_STATUS A2DP_ParseInfoLdac(tA2DP_LDAC_CIE* p_ie,
128                                       const uint8_t* p_codec_info,
129                                       bool is_capability) {
130  uint8_t losc;
131  uint8_t media_type;
132  tA2DP_CODEC_TYPE codec_type;
133
134  if (p_ie == NULL || p_codec_info == NULL) return A2DP_INVALID_PARAMS;
135
136  // Check the codec capability length
137  losc = *p_codec_info++;
138  if (losc != A2DP_LDAC_CODEC_LEN) return A2DP_WRONG_CODEC;
139
140  media_type = (*p_codec_info++) >> 4;
141  codec_type = *p_codec_info++;
142  /* Check the Media Type and Media Codec Type */
143  if (media_type != AVDT_MEDIA_TYPE_AUDIO ||
144      codec_type != A2DP_MEDIA_CT_NON_A2DP) {
145    return A2DP_WRONG_CODEC;
146  }
147
148  // Check the Vendor ID and Codec ID */
149  p_ie->vendorId = (*p_codec_info & 0x000000FF) |
150                   (*(p_codec_info + 1) << 8 & 0x0000FF00) |
151                   (*(p_codec_info + 2) << 16 & 0x00FF0000) |
152                   (*(p_codec_info + 3) << 24 & 0xFF000000);
153  p_codec_info += 4;
154  p_ie->codecId =
155      (*p_codec_info & 0x00FF) | (*(p_codec_info + 1) << 8 & 0xFF00);
156  p_codec_info += 2;
157  if (p_ie->vendorId != A2DP_LDAC_VENDOR_ID ||
158      p_ie->codecId != A2DP_LDAC_CODEC_ID) {
159    return A2DP_WRONG_CODEC;
160  }
161
162  p_ie->sampleRate = *p_codec_info++ & A2DP_LDAC_SAMPLING_FREQ_MASK;
163  p_ie->channelMode = *p_codec_info++ & A2DP_LDAC_CHANNEL_MODE_MASK;
164
165  if (is_capability) return A2DP_SUCCESS;
166
167  if (A2DP_BitsSet(p_ie->sampleRate) != A2DP_SET_ONE_BIT)
168    return A2DP_BAD_SAMP_FREQ;
169  if (A2DP_BitsSet(p_ie->channelMode) != A2DP_SET_ONE_BIT)
170    return A2DP_BAD_CH_MODE;
171
172  return A2DP_SUCCESS;
173}
174
175// Build the LDAC Media Payload Header.
176// |p_dst| points to the location where the header should be written to.
177// If |frag| is true, the media payload frame is fragmented.
178// |start| is true for the first packet of a fragmented frame.
179// |last| is true for the last packet of a fragmented frame.
180// If |frag| is false, |num| is the number of number of frames in the packet,
181// otherwise is the number of remaining fragments (including this one).
182static void A2DP_BuildMediaPayloadHeaderLdac(uint8_t* p_dst, bool frag,
183                                             bool start, bool last,
184                                             uint8_t num) {
185  if (p_dst == NULL) return;
186
187  *p_dst = 0;
188  if (frag) *p_dst |= A2DP_LDAC_HDR_F_MSK;
189  if (start) *p_dst |= A2DP_LDAC_HDR_S_MSK;
190  if (last) *p_dst |= A2DP_LDAC_HDR_L_MSK;
191  *p_dst |= (A2DP_LDAC_HDR_NUM_MSK & num);
192}
193
194bool A2DP_IsVendorSourceCodecValidLdac(const uint8_t* p_codec_info) {
195  tA2DP_LDAC_CIE cfg_cie;
196
197  /* Use a liberal check when parsing the codec info */
198  return (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
199         (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
200}
201
202bool A2DP_IsVendorPeerSinkCodecValidLdac(const uint8_t* p_codec_info) {
203  tA2DP_LDAC_CIE cfg_cie;
204
205  /* Use a liberal check when parsing the codec info */
206  return (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
207         (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
208}
209
210// Checks whether A2DP LDAC codec configuration matches with a device's codec
211// capabilities. |p_cap| is the LDAC codec configuration. |p_codec_info| is
212// the device's codec capabilities.
213// If |is_capability| is true, the byte sequence is codec capabilities,
214// otherwise is codec configuration.
215// |p_codec_info| contains the codec capabilities for a peer device that
216// is acting as an A2DP source.
217// Returns A2DP_SUCCESS if the codec configuration matches with capabilities,
218// otherwise the corresponding A2DP error status code.
219static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityLdac(
220    const tA2DP_LDAC_CIE* p_cap, const uint8_t* p_codec_info,
221    bool is_capability) {
222  tA2DP_STATUS status;
223  tA2DP_LDAC_CIE cfg_cie;
224
225  /* parse configuration */
226  status = A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, is_capability);
227  if (status != A2DP_SUCCESS) {
228    LOG_ERROR(LOG_TAG, "%s: parsing failed %d", __func__, status);
229    return status;
230  }
231
232  /* verify that each parameter is in range */
233
234  LOG_DEBUG(LOG_TAG, "%s: FREQ peer: 0x%x, capability 0x%x", __func__,
235            cfg_cie.sampleRate, p_cap->sampleRate);
236  LOG_DEBUG(LOG_TAG, "%s: CH_MODE peer: 0x%x, capability 0x%x", __func__,
237            cfg_cie.channelMode, p_cap->channelMode);
238
239  /* sampling frequency */
240  if ((cfg_cie.sampleRate & p_cap->sampleRate) == 0) return A2DP_NS_SAMP_FREQ;
241
242  /* channel mode */
243  if ((cfg_cie.channelMode & p_cap->channelMode) == 0) return A2DP_NS_CH_MODE;
244
245  return A2DP_SUCCESS;
246}
247
248bool A2DP_VendorUsesRtpHeaderLdac(UNUSED_ATTR bool content_protection_enabled,
249                                  UNUSED_ATTR const uint8_t* p_codec_info) {
250  // TODO: Is this correct? The RTP header is always included?
251  return true;
252}
253
254const char* A2DP_VendorCodecNameLdac(UNUSED_ATTR const uint8_t* p_codec_info) {
255  return "LDAC";
256}
257
258bool A2DP_VendorCodecTypeEqualsLdac(const uint8_t* p_codec_info_a,
259                                    const uint8_t* p_codec_info_b) {
260  tA2DP_LDAC_CIE ldac_cie_a;
261  tA2DP_LDAC_CIE ldac_cie_b;
262
263  // Check whether the codec info contains valid data
264  tA2DP_STATUS a2dp_status =
265      A2DP_ParseInfoLdac(&ldac_cie_a, p_codec_info_a, true);
266  if (a2dp_status != A2DP_SUCCESS) {
267    LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
268              a2dp_status);
269    return false;
270  }
271  a2dp_status = A2DP_ParseInfoLdac(&ldac_cie_b, p_codec_info_b, true);
272  if (a2dp_status != A2DP_SUCCESS) {
273    LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
274              a2dp_status);
275    return false;
276  }
277
278  return true;
279}
280
281bool A2DP_VendorCodecEqualsLdac(const uint8_t* p_codec_info_a,
282                                const uint8_t* p_codec_info_b) {
283  tA2DP_LDAC_CIE ldac_cie_a;
284  tA2DP_LDAC_CIE ldac_cie_b;
285
286  // Check whether the codec info contains valid data
287  tA2DP_STATUS a2dp_status =
288      A2DP_ParseInfoLdac(&ldac_cie_a, p_codec_info_a, true);
289  if (a2dp_status != A2DP_SUCCESS) {
290    LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
291              a2dp_status);
292    return false;
293  }
294  a2dp_status = A2DP_ParseInfoLdac(&ldac_cie_b, p_codec_info_b, true);
295  if (a2dp_status != A2DP_SUCCESS) {
296    LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
297              a2dp_status);
298    return false;
299  }
300
301  return (ldac_cie_a.sampleRate == ldac_cie_b.sampleRate) &&
302         (ldac_cie_a.channelMode == ldac_cie_b.channelMode);
303}
304
305int A2DP_VendorGetTrackSampleRateLdac(const uint8_t* p_codec_info) {
306  tA2DP_LDAC_CIE ldac_cie;
307
308  // Check whether the codec info contains valid data
309  tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false);
310  if (a2dp_status != A2DP_SUCCESS) {
311    LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
312              a2dp_status);
313    return -1;
314  }
315
316  switch (ldac_cie.sampleRate) {
317    case A2DP_LDAC_SAMPLING_FREQ_44100:
318      return 44100;
319    case A2DP_LDAC_SAMPLING_FREQ_48000:
320      return 48000;
321    case A2DP_LDAC_SAMPLING_FREQ_88200:
322      return 88200;
323    case A2DP_LDAC_SAMPLING_FREQ_96000:
324      return 96000;
325    case A2DP_LDAC_SAMPLING_FREQ_176400:
326      return 176400;
327    case A2DP_LDAC_SAMPLING_FREQ_192000:
328      return 192000;
329  }
330
331  return -1;
332}
333
334int A2DP_VendorGetTrackBitsPerSampleLdac(const uint8_t* p_codec_info) {
335  tA2DP_LDAC_CIE ldac_cie;
336
337  // Check whether the codec info contains valid data
338  tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false);
339  if (a2dp_status != A2DP_SUCCESS) {
340    LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
341              a2dp_status);
342    return -1;
343  }
344
345  switch (a2dp_ldac_caps.bits_per_sample) {
346    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
347      return 16;
348    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
349      return 24;
350    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
351      return 32;
352    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
353      break;
354  }
355  return -1;
356}
357
358int A2DP_VendorGetTrackChannelCountLdac(const uint8_t* p_codec_info) {
359  tA2DP_LDAC_CIE ldac_cie;
360
361  // Check whether the codec info contains valid data
362  tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false);
363  if (a2dp_status != A2DP_SUCCESS) {
364    LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
365              a2dp_status);
366    return -1;
367  }
368
369  switch (ldac_cie.channelMode) {
370    case A2DP_LDAC_CHANNEL_MODE_MONO:
371      return 1;
372    case A2DP_LDAC_CHANNEL_MODE_DUAL:
373      return 2;
374    case A2DP_LDAC_CHANNEL_MODE_STEREO:
375      return 2;
376  }
377
378  return -1;
379}
380
381int A2DP_VendorGetChannelModeCodeLdac(const uint8_t* p_codec_info) {
382  tA2DP_LDAC_CIE ldac_cie;
383
384  // Check whether the codec info contains valid data
385  tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false);
386  if (a2dp_status != A2DP_SUCCESS) {
387    LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
388              a2dp_status);
389    return -1;
390  }
391
392  switch (ldac_cie.channelMode) {
393    case A2DP_LDAC_CHANNEL_MODE_MONO:
394    case A2DP_LDAC_CHANNEL_MODE_DUAL:
395    case A2DP_LDAC_CHANNEL_MODE_STEREO:
396      return ldac_cie.channelMode;
397    default:
398      break;
399  }
400
401  return -1;
402}
403
404bool A2DP_VendorGetPacketTimestampLdac(UNUSED_ATTR const uint8_t* p_codec_info,
405                                       const uint8_t* p_data,
406                                       uint32_t* p_timestamp) {
407  // TODO: Is this function really codec-specific?
408  *p_timestamp = *(const uint32_t*)p_data;
409  return true;
410}
411
412bool A2DP_VendorBuildCodecHeaderLdac(UNUSED_ATTR const uint8_t* p_codec_info,
413                                     BT_HDR* p_buf,
414                                     uint16_t frames_per_packet) {
415  uint8_t* p;
416
417  p_buf->offset -= A2DP_LDAC_MPL_HDR_LEN;
418  p = (uint8_t*)(p_buf + 1) + p_buf->offset;
419  p_buf->len += A2DP_LDAC_MPL_HDR_LEN;
420  A2DP_BuildMediaPayloadHeaderLdac(p, false, false, false,
421                                   (uint8_t)frames_per_packet);
422
423  return true;
424}
425
426void A2DP_VendorDumpCodecInfoLdac(const uint8_t* p_codec_info) {
427  tA2DP_STATUS a2dp_status;
428  tA2DP_LDAC_CIE ldac_cie;
429
430  LOG_DEBUG(LOG_TAG, "%s", __func__);
431
432  a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, true);
433  if (a2dp_status != A2DP_SUCCESS) {
434    LOG_ERROR(LOG_TAG, "%s: A2DP_ParseInfoLdac fail:%d", __func__, a2dp_status);
435    return;
436  }
437
438  LOG_DEBUG(LOG_TAG, "\tsamp_freq: 0x%x", ldac_cie.sampleRate);
439  if (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
440    LOG_DEBUG(LOG_TAG, "\tsamp_freq: (44100)");
441  }
442  if (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
443    LOG_DEBUG(LOG_TAG, "\tsamp_freq: (48000)");
444  }
445  if (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
446    LOG_DEBUG(LOG_TAG, "\tsamp_freq: (88200)");
447  }
448  if (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
449    LOG_DEBUG(LOG_TAG, "\tsamp_freq: (96000)");
450  }
451  if (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
452    LOG_DEBUG(LOG_TAG, "\tsamp_freq: (176400)");
453  }
454  if (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
455    LOG_DEBUG(LOG_TAG, "\tsamp_freq: (192000)");
456  }
457
458  LOG_DEBUG(LOG_TAG, "\tch_mode: 0x%x", ldac_cie.channelMode);
459  if (ldac_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
460    LOG_DEBUG(LOG_TAG, "\tch_mode: (Mono)");
461  }
462  if (ldac_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
463    LOG_DEBUG(LOG_TAG, "\tch_mode: (Dual)");
464  }
465  if (ldac_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
466    LOG_DEBUG(LOG_TAG, "\tch_mode: (Stereo)");
467  }
468}
469
470const tA2DP_ENCODER_INTERFACE* A2DP_VendorGetEncoderInterfaceLdac(
471    const uint8_t* p_codec_info) {
472  if (!A2DP_IsVendorSourceCodecValidLdac(p_codec_info)) return NULL;
473
474  return &a2dp_encoder_interface_ldac;
475}
476
477bool A2DP_VendorAdjustCodecLdac(uint8_t* p_codec_info) {
478  tA2DP_LDAC_CIE cfg_cie;
479
480  // Nothing to do: just verify the codec info is valid
481  if (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS)
482    return false;
483
484  return true;
485}
486
487btav_a2dp_codec_index_t A2DP_VendorSourceCodecIndexLdac(
488    UNUSED_ATTR const uint8_t* p_codec_info) {
489  return BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC;
490}
491
492const char* A2DP_VendorCodecIndexStrLdac(void) { return "LDAC"; }
493
494bool A2DP_VendorInitCodecConfigLdac(tAVDT_CFG* p_cfg) {
495  if (A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &a2dp_ldac_caps,
496                         p_cfg->codec_info) != A2DP_SUCCESS) {
497    return false;
498  }
499
500#if (BTA_AV_CO_CP_SCMS_T == TRUE)
501  /* Content protection info - support SCMS-T */
502  uint8_t* p = p_cfg->protect_info;
503  *p++ = AVDT_CP_LOSC;
504  UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID);
505  p_cfg->num_protect = 1;
506#endif
507
508  return true;
509}
510
511UNUSED_ATTR static void build_codec_config(const tA2DP_LDAC_CIE& config_cie,
512                                           btav_a2dp_codec_config_t* result) {
513  if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100)
514    result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
515  if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000)
516    result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
517  if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200)
518    result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
519  if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000)
520    result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
521  if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400)
522    result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
523  if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000)
524    result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
525
526  result->bits_per_sample = config_cie.bits_per_sample;
527
528  if (config_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_MONO)
529    result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
530  if (config_cie.channelMode &
531      (A2DP_LDAC_CHANNEL_MODE_DUAL | A2DP_LDAC_CHANNEL_MODE_STEREO)) {
532    result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
533  }
534}
535
536A2dpCodecConfigLdac::A2dpCodecConfigLdac()
537    : A2dpCodecConfig(BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC, "LDAC") {
538  // Compute the local capability
539  if (a2dp_ldac_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
540    codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
541  }
542  if (a2dp_ldac_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
543    codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
544  }
545  if (a2dp_ldac_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
546    codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
547  }
548  if (a2dp_ldac_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
549    codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
550  }
551  if (a2dp_ldac_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
552    codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
553  }
554  if (a2dp_ldac_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
555    codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
556  }
557  codec_local_capability_.bits_per_sample = a2dp_ldac_caps.bits_per_sample;
558  if (a2dp_ldac_caps.channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
559    codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
560  }
561  if (a2dp_ldac_caps.channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
562    codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
563  }
564  if (a2dp_ldac_caps.channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
565    codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
566  }
567}
568
569A2dpCodecConfigLdac::~A2dpCodecConfigLdac() {}
570
571bool A2dpCodecConfigLdac::init() {
572  if (!isValid()) return false;
573
574  // Load the encoder
575  if (!A2DP_VendorLoadEncoderLdac()) {
576    LOG_ERROR(LOG_TAG, "%s: cannot load the encoder", __func__);
577    return false;
578  }
579
580  return true;
581}
582
583//
584// Selects the best sample rate from |sampleRate|.
585// The result is stored in |p_result| and |p_codec_config|.
586// Returns true if a selection was made, otherwise false.
587//
588static bool select_best_sample_rate(uint8_t sampleRate,
589                                    tA2DP_LDAC_CIE* p_result,
590                                    btav_a2dp_codec_config_t* p_codec_config) {
591  if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
592    p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000;
593    p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
594    return true;
595  }
596  if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
597    p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400;
598    p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
599    return true;
600  }
601  if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
602    p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000;
603    p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
604    return true;
605  }
606  if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
607    p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200;
608    p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
609    return true;
610  }
611  if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
612    p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000;
613    p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
614    return true;
615  }
616  if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
617    p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100;
618    p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
619    return true;
620  }
621  return false;
622}
623
624//
625// Selects the audio sample rate from |p_codec_audio_config|.
626// |sampleRate| contains the capability.
627// The result is stored in |p_result| and |p_codec_config|.
628// Returns true if a selection was made, otherwise false.
629//
630static bool select_audio_sample_rate(
631    const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t sampleRate,
632    tA2DP_LDAC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
633  switch (p_codec_audio_config->sample_rate) {
634    case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
635      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
636        p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100;
637        p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
638        return true;
639      }
640      break;
641    case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
642      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
643        p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000;
644        p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
645        return true;
646      }
647      break;
648    case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
649      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
650        p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200;
651        p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
652        return true;
653      }
654      break;
655    case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
656      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
657        p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000;
658        p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
659        return true;
660      }
661      break;
662    case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
663      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
664        p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400;
665        p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
666        return true;
667      }
668      break;
669    case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
670      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
671        p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000;
672        p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
673        return true;
674      }
675      break;
676    case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
677      break;
678  }
679  return false;
680}
681
682//
683// Selects the best bits per sample from |bits_per_sample|.
684// |bits_per_sample| contains the capability.
685// The result is stored in |p_result| and |p_codec_config|.
686// Returns true if a selection was made, otherwise false.
687//
688static bool select_best_bits_per_sample(
689    btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_LDAC_CIE* p_result,
690    btav_a2dp_codec_config_t* p_codec_config) {
691  if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
692    p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
693    p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
694    return true;
695  }
696  if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
697    p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
698    p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
699    return true;
700  }
701  if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
702    p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
703    p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
704    return true;
705  }
706  return false;
707}
708
709//
710// Selects the audio bits per sample from |p_codec_audio_config|.
711// |bits_per_sample| contains the capability.
712// The result is stored in |p_result| and |p_codec_config|.
713// Returns true if a selection was made, otherwise false.
714//
715static bool select_audio_bits_per_sample(
716    const btav_a2dp_codec_config_t* p_codec_audio_config,
717    btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_LDAC_CIE* p_result,
718    btav_a2dp_codec_config_t* p_codec_config) {
719  switch (p_codec_audio_config->bits_per_sample) {
720    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
721      if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
722        p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
723        p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
724        return true;
725      }
726      break;
727    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
728      if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
729        p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
730        p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
731        return true;
732      }
733      break;
734    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
735      if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
736        p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
737        p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
738        return true;
739      }
740      break;
741    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
742      break;
743  }
744  return false;
745}
746
747//
748// Selects the best channel mode from |channelMode|.
749// The result is stored in |p_result| and |p_codec_config|.
750// Returns true if a selection was made, otherwise false.
751//
752static bool select_best_channel_mode(uint8_t channelMode,
753                                     tA2DP_LDAC_CIE* p_result,
754                                     btav_a2dp_codec_config_t* p_codec_config) {
755  if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
756    p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO;
757    p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
758    return true;
759  }
760  if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
761    p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL;
762    p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
763    return true;
764  }
765  if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
766    p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_MONO;
767    p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
768    return true;
769  }
770  return false;
771}
772
773//
774// Selects the audio channel mode from |p_codec_audio_config|.
775// |channelMode| contains the capability.
776// The result is stored in |p_result| and |p_codec_config|.
777// Returns true if a selection was made, otherwise false.
778//
779static bool select_audio_channel_mode(
780    const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t channelMode,
781    tA2DP_LDAC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
782  switch (p_codec_audio_config->channel_mode) {
783    case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
784      if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
785        p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_MONO;
786        p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
787        return true;
788      }
789      break;
790    case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
791      if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
792        p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO;
793        p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
794        return true;
795      }
796      if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
797        p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL;
798        p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
799        return true;
800      }
801      break;
802    case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
803      break;
804  }
805
806  return false;
807}
808
809bool A2dpCodecConfigLdac::setCodecConfig(const uint8_t* p_peer_codec_info,
810                                         bool is_capability,
811                                         uint8_t* p_result_codec_config) {
812  std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
813  tA2DP_LDAC_CIE sink_info_cie;
814  tA2DP_LDAC_CIE result_config_cie;
815  uint8_t channelMode;
816  uint8_t sampleRate;
817  btav_a2dp_codec_bits_per_sample_t bits_per_sample;
818
819  // Save the internal state
820  btav_a2dp_codec_config_t saved_codec_config = codec_config_;
821  btav_a2dp_codec_config_t saved_codec_capability = codec_capability_;
822  btav_a2dp_codec_config_t saved_codec_selectable_capability =
823      codec_selectable_capability_;
824  btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_;
825  btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_;
826  uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE];
827  uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
828  uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE];
829  memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_));
830  memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
831         sizeof(ota_codec_peer_capability_));
832  memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_,
833         sizeof(ota_codec_peer_config_));
834
835  tA2DP_STATUS status =
836      A2DP_ParseInfoLdac(&sink_info_cie, p_peer_codec_info, is_capability);
837  if (status != A2DP_SUCCESS) {
838    LOG_ERROR(LOG_TAG, "%s: can't parse peer's Sink capabilities: error = %d",
839              __func__, status);
840    goto fail;
841  }
842
843  //
844  // Build the preferred configuration
845  //
846  memset(&result_config_cie, 0, sizeof(result_config_cie));
847  result_config_cie.vendorId = a2dp_ldac_caps.vendorId;
848  result_config_cie.codecId = a2dp_ldac_caps.codecId;
849
850  //
851  // Select the sample frequency
852  //
853  sampleRate = a2dp_ldac_caps.sampleRate & sink_info_cie.sampleRate;
854  codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
855  switch (codec_user_config_.sample_rate) {
856    case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
857      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
858        result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100;
859        codec_capability_.sample_rate = codec_user_config_.sample_rate;
860        codec_config_.sample_rate = codec_user_config_.sample_rate;
861      }
862      break;
863    case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
864      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
865        result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000;
866        codec_capability_.sample_rate = codec_user_config_.sample_rate;
867        codec_config_.sample_rate = codec_user_config_.sample_rate;
868      }
869      break;
870    case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
871      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
872        result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200;
873        codec_capability_.sample_rate = codec_user_config_.sample_rate;
874        codec_config_.sample_rate = codec_user_config_.sample_rate;
875      }
876      break;
877    case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
878      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
879        result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000;
880        codec_capability_.sample_rate = codec_user_config_.sample_rate;
881        codec_config_.sample_rate = codec_user_config_.sample_rate;
882      }
883      break;
884    case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
885      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
886        result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400;
887        codec_capability_.sample_rate = codec_user_config_.sample_rate;
888        codec_config_.sample_rate = codec_user_config_.sample_rate;
889      }
890      break;
891    case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
892      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
893        result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000;
894        codec_capability_.sample_rate = codec_user_config_.sample_rate;
895        codec_config_.sample_rate = codec_user_config_.sample_rate;
896      }
897    case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
898      codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
899      codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
900      break;
901  }
902
903  // Select the sample frequency if there is no user preference
904  do {
905    // Compute the selectable capability
906    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
907      codec_selectable_capability_.sample_rate |=
908          BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
909    }
910    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
911      codec_selectable_capability_.sample_rate |=
912          BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
913    }
914    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
915      codec_selectable_capability_.sample_rate |=
916          BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
917    }
918    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
919      codec_selectable_capability_.sample_rate |=
920          BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
921    }
922    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
923      codec_selectable_capability_.sample_rate |=
924          BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
925    }
926    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
927      codec_selectable_capability_.sample_rate |=
928          BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
929    }
930
931    if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break;
932
933    // Compute the common capability
934    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100)
935      codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
936    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000)
937      codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
938    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200)
939      codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
940    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000)
941      codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
942    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400)
943      codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
944    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000)
945      codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
946
947    // No user preference - try the codec audio config
948    if (select_audio_sample_rate(&codec_audio_config_, sampleRate,
949                                 &result_config_cie, &codec_config_)) {
950      break;
951    }
952
953    // No user preference - try the default config
954    if (select_best_sample_rate(
955            a2dp_ldac_default_config.sampleRate & sink_info_cie.sampleRate,
956            &result_config_cie, &codec_config_)) {
957      break;
958    }
959
960    // No user preference - use the best match
961    if (select_best_sample_rate(sampleRate, &result_config_cie,
962                                &codec_config_)) {
963      break;
964    }
965  } while (false);
966  if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) {
967    LOG_ERROR(LOG_TAG,
968              "%s: cannot match sample frequency: source caps = 0x%x "
969              "sink info = 0x%x",
970              __func__, a2dp_ldac_caps.sampleRate, sink_info_cie.sampleRate);
971    goto fail;
972  }
973
974  //
975  // Select the bits per sample
976  //
977  // NOTE: this information is NOT included in the LDAC A2DP codec description
978  // that is sent OTA.
979  bits_per_sample = a2dp_ldac_caps.bits_per_sample;
980  codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
981  switch (codec_user_config_.bits_per_sample) {
982    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
983      if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
984        result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
985        codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
986        codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
987      }
988      break;
989    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
990      if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
991        result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
992        codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
993        codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
994      }
995      break;
996    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
997      if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
998        result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
999        codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1000        codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1001      }
1002      break;
1003    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
1004      result_config_cie.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1005      codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1006      codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1007      break;
1008  }
1009
1010  // Select the bits per sample if there is no user preference
1011  do {
1012    // Compute the selectable capability
1013    codec_selectable_capability_.bits_per_sample =
1014        a2dp_ldac_caps.bits_per_sample;
1015
1016    if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE)
1017      break;
1018
1019    // Compute the common capability
1020    codec_capability_.bits_per_sample = bits_per_sample;
1021
1022    // No user preference - the the codec audio config
1023    if (select_audio_bits_per_sample(&codec_audio_config_,
1024                                     a2dp_ldac_caps.bits_per_sample,
1025                                     &result_config_cie, &codec_config_)) {
1026      break;
1027    }
1028
1029    // No user preference - try the default config
1030    if (select_best_bits_per_sample(a2dp_ldac_default_config.bits_per_sample,
1031                                    &result_config_cie, &codec_config_)) {
1032      break;
1033    }
1034
1035    // No user preference - use the best match
1036    if (select_best_bits_per_sample(a2dp_ldac_caps.bits_per_sample,
1037                                    &result_config_cie, &codec_config_)) {
1038      break;
1039    }
1040  } while (false);
1041  if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) {
1042    LOG_ERROR(LOG_TAG,
1043              "%s: cannot match bits per sample: default = 0x%x "
1044              "user preference = 0x%x",
1045              __func__, a2dp_ldac_default_config.bits_per_sample,
1046              codec_user_config_.bits_per_sample);
1047    goto fail;
1048  }
1049
1050  //
1051  // Select the channel mode
1052  //
1053  channelMode = a2dp_ldac_caps.channelMode & sink_info_cie.channelMode;
1054  codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1055  switch (codec_user_config_.channel_mode) {
1056    case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
1057      if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
1058        result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_MONO;
1059        codec_capability_.channel_mode = codec_user_config_.channel_mode;
1060        codec_config_.channel_mode = codec_user_config_.channel_mode;
1061      }
1062      break;
1063    case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
1064      if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
1065        result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO;
1066        codec_capability_.channel_mode = codec_user_config_.channel_mode;
1067        codec_config_.channel_mode = codec_user_config_.channel_mode;
1068        break;
1069      }
1070      if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
1071        result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL;
1072        codec_capability_.channel_mode = codec_user_config_.channel_mode;
1073        codec_config_.channel_mode = codec_user_config_.channel_mode;
1074        break;
1075      }
1076      break;
1077    case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1078      codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1079      codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1080      break;
1081  }
1082
1083  // Select the channel mode if there is no user preference
1084  do {
1085    // Compute the selectable capability
1086    if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
1087      codec_selectable_capability_.channel_mode |=
1088          BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1089    }
1090    if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
1091      codec_selectable_capability_.channel_mode |=
1092          BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1093    }
1094    if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
1095      codec_selectable_capability_.channel_mode |=
1096          BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1097    }
1098
1099    if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break;
1100
1101    // Compute the common capability
1102    if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO)
1103      codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1104    if (channelMode &
1105        (A2DP_LDAC_CHANNEL_MODE_STEREO | A2DP_LDAC_CHANNEL_MODE_DUAL)) {
1106      codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1107    }
1108
1109    // No user preference - try the codec audio config
1110    if (select_audio_channel_mode(&codec_audio_config_, channelMode,
1111                                  &result_config_cie, &codec_config_)) {
1112      break;
1113    }
1114
1115    // No user preference - try the default config
1116    if (select_best_channel_mode(
1117            a2dp_ldac_default_config.channelMode & sink_info_cie.channelMode,
1118            &result_config_cie, &codec_config_)) {
1119      break;
1120    }
1121
1122    // No user preference - use the best match
1123    if (select_best_channel_mode(channelMode, &result_config_cie,
1124                                 &codec_config_)) {
1125      break;
1126    }
1127  } while (false);
1128  if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) {
1129    LOG_ERROR(LOG_TAG,
1130              "%s: cannot match channel mode: source caps = 0x%x "
1131              "sink info = 0x%x",
1132              __func__, a2dp_ldac_caps.channelMode, sink_info_cie.channelMode);
1133    goto fail;
1134  }
1135
1136  if (A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1137                         p_result_codec_config) != A2DP_SUCCESS) {
1138    goto fail;
1139  }
1140
1141  //
1142  // Copy the codec-specific fields if they are not zero
1143  //
1144  if (codec_user_config_.codec_specific_1 != 0)
1145    codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
1146  if (codec_user_config_.codec_specific_2 != 0)
1147    codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2;
1148  if (codec_user_config_.codec_specific_3 != 0)
1149    codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3;
1150  if (codec_user_config_.codec_specific_4 != 0)
1151    codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4;
1152
1153  // Create a local copy of the peer codec capability, and the
1154  // result codec config.
1155  if (is_capability) {
1156    status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &sink_info_cie,
1157                                ota_codec_peer_capability_);
1158  } else {
1159    status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &sink_info_cie,
1160                                ota_codec_peer_config_);
1161  }
1162  CHECK(status == A2DP_SUCCESS);
1163  status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1164                              ota_codec_config_);
1165  CHECK(status == A2DP_SUCCESS);
1166  return true;
1167
1168fail:
1169  // Restore the internal state
1170  codec_config_ = saved_codec_config;
1171  codec_capability_ = saved_codec_capability;
1172  codec_selectable_capability_ = saved_codec_selectable_capability;
1173  codec_user_config_ = saved_codec_user_config;
1174  codec_audio_config_ = saved_codec_audio_config;
1175  memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_));
1176  memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1177         sizeof(ota_codec_peer_capability_));
1178  memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config,
1179         sizeof(ota_codec_peer_config_));
1180  return false;
1181}
1182