a2dp_vendor_ldac.cc revision f8e2670d12ee55ec48635ef55a86ecfa643cc761
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
522  result->bits_per_sample = config_cie.bits_per_sample;
523
524  if (config_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_MONO)
525    result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
526  if (config_cie.channelMode &
527      (A2DP_LDAC_CHANNEL_MODE_DUAL | A2DP_LDAC_CHANNEL_MODE_STEREO)) {
528    result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
529  }
530}
531
532A2dpCodecConfigLdac::A2dpCodecConfigLdac()
533    : A2dpCodecConfig(BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC, "LDAC") {}
534
535A2dpCodecConfigLdac::~A2dpCodecConfigLdac() {}
536
537bool A2dpCodecConfigLdac::init() {
538  if (!isValid()) return false;
539
540  // Load the encoder
541  if (!A2DP_VendorLoadEncoderLdac()) {
542    LOG_ERROR(LOG_TAG, "%s: cannot load the encoder", __func__);
543    return false;
544  }
545
546  return true;
547}
548
549//
550// Selects the best sample rate from |sampleRate|.
551// The result is stored in |p_result| and |p_codec_config|.
552// Returns true if a selection was made, otherwise false.
553//
554static bool select_best_sample_rate(uint8_t sampleRate,
555                                    tA2DP_LDAC_CIE* p_result,
556                                    btav_a2dp_codec_config_t* p_codec_config) {
557  if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
558    p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000;
559    p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
560    return true;
561  }
562  if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
563    p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400;
564    p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
565    return true;
566  }
567  if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
568    p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000;
569    p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
570    return true;
571  }
572  if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
573    p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200;
574    p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
575    return true;
576  }
577  if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
578    p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000;
579    p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
580    return true;
581  }
582  if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
583    p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100;
584    p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
585    return true;
586  }
587  return false;
588}
589
590//
591// Selects the audio sample rate from |p_codec_audio_config|.
592// |sampleRate| contains the capability.
593// The result is stored in |p_result| and |p_codec_config|.
594// Returns true if a selection was made, otherwise false.
595//
596static bool select_audio_sample_rate(
597    const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t sampleRate,
598    tA2DP_LDAC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
599  switch (p_codec_audio_config->sample_rate) {
600    case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
601      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
602        p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100;
603        p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
604        return true;
605      }
606      break;
607    case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
608      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
609        p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000;
610        p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
611        return true;
612      }
613      break;
614    case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
615      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
616        p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200;
617        p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
618        return true;
619      }
620      break;
621    case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
622      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
623        p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000;
624        p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
625        return true;
626      }
627      break;
628    case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
629      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
630        p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400;
631        p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
632        return true;
633      }
634      break;
635    case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
636      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
637        p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000;
638        p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
639        return true;
640      }
641      break;
642    case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
643      break;
644  }
645  return false;
646}
647
648//
649// Selects the best bits per sample from |bits_per_sample|.
650// |bits_per_sample| contains the capability.
651// The result is stored in |p_result| and |p_codec_config|.
652// Returns true if a selection was made, otherwise false.
653//
654static bool select_best_bits_per_sample(
655    btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_LDAC_CIE* p_result,
656    btav_a2dp_codec_config_t* p_codec_config) {
657  if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
658    p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
659    p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
660    return true;
661  }
662  if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
663    p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
664    p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
665    return true;
666  }
667  if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
668    p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
669    p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
670    return true;
671  }
672  return false;
673}
674
675//
676// Selects the audio bits per sample from |p_codec_audio_config|.
677// |bits_per_sample| contains the capability.
678// The result is stored in |p_result| and |p_codec_config|.
679// Returns true if a selection was made, otherwise false.
680//
681static bool select_audio_bits_per_sample(
682    const btav_a2dp_codec_config_t* p_codec_audio_config,
683    btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_LDAC_CIE* p_result,
684    btav_a2dp_codec_config_t* p_codec_config) {
685  switch (p_codec_audio_config->bits_per_sample) {
686    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
687      if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
688        p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
689        p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
690        return true;
691      }
692      break;
693    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
694      if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
695        p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
696        p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
697        return true;
698      }
699      break;
700    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
701      if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
702        p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
703        p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
704        return true;
705      }
706      break;
707    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
708      break;
709  }
710  return false;
711}
712
713//
714// Selects the best channel mode from |channelMode|.
715// The result is stored in |p_result| and |p_codec_config|.
716// Returns true if a selection was made, otherwise false.
717//
718static bool select_best_channel_mode(uint8_t channelMode,
719                                     tA2DP_LDAC_CIE* p_result,
720                                     btav_a2dp_codec_config_t* p_codec_config) {
721  if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
722    p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO;
723    p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
724    return true;
725  }
726  if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
727    p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL;
728    p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
729    return true;
730  }
731  if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
732    p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_MONO;
733    p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
734    return true;
735  }
736  return false;
737}
738
739//
740// Selects the audio channel mode from |p_codec_audio_config|.
741// |channelMode| contains the capability.
742// The result is stored in |p_result| and |p_codec_config|.
743// Returns true if a selection was made, otherwise false.
744//
745static bool select_audio_channel_mode(
746    const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t channelMode,
747    tA2DP_LDAC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
748  switch (p_codec_audio_config->channel_mode) {
749    case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
750      if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
751        p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_MONO;
752        p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
753        return true;
754      }
755      break;
756    case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
757      if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
758        p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO;
759        p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
760        return true;
761      }
762      if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
763        p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL;
764        p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
765        return true;
766      }
767      break;
768    case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
769      break;
770  }
771
772  return false;
773}
774
775bool A2dpCodecConfigLdac::setCodecConfig(const uint8_t* p_peer_codec_info,
776                                         bool is_capability,
777                                         uint8_t* p_result_codec_config) {
778  std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
779  tA2DP_LDAC_CIE sink_info_cie;
780  tA2DP_LDAC_CIE result_config_cie;
781  uint8_t channelMode;
782  uint8_t sampleRate;
783  btav_a2dp_codec_bits_per_sample_t bits_per_sample;
784
785  // Save the internal state
786  btav_a2dp_codec_config_t saved_codec_config = codec_config_;
787  btav_a2dp_codec_config_t saved_codec_capability = codec_capability_;
788  btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_;
789  btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_;
790  uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE];
791  uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
792  uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE];
793  memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_));
794  memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
795         sizeof(ota_codec_peer_capability_));
796  memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_,
797         sizeof(ota_codec_peer_config_));
798
799  tA2DP_STATUS status =
800      A2DP_ParseInfoLdac(&sink_info_cie, p_peer_codec_info, is_capability);
801  if (status != A2DP_SUCCESS) {
802    LOG_ERROR(LOG_TAG, "%s: can't parse peer's Sink capabilities: error = %d",
803              __func__, status);
804    goto fail;
805  }
806
807  //
808  // Build the preferred configuration
809  //
810  memset(&result_config_cie, 0, sizeof(result_config_cie));
811  result_config_cie.vendorId = a2dp_ldac_caps.vendorId;
812  result_config_cie.codecId = a2dp_ldac_caps.codecId;
813
814  //
815  // Select the sample frequency
816  //
817  sampleRate = a2dp_ldac_caps.sampleRate & sink_info_cie.sampleRate;
818  codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
819  switch (codec_user_config_.sample_rate) {
820    case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
821      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
822        result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100;
823        codec_capability_.sample_rate = codec_user_config_.sample_rate;
824        codec_config_.sample_rate = codec_user_config_.sample_rate;
825      }
826      break;
827    case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
828      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
829        result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000;
830        codec_capability_.sample_rate = codec_user_config_.sample_rate;
831        codec_config_.sample_rate = codec_user_config_.sample_rate;
832      }
833      break;
834    case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
835      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
836        result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200;
837        codec_capability_.sample_rate = codec_user_config_.sample_rate;
838        codec_config_.sample_rate = codec_user_config_.sample_rate;
839      }
840      break;
841    case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
842      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
843        result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000;
844        codec_capability_.sample_rate = codec_user_config_.sample_rate;
845        codec_config_.sample_rate = codec_user_config_.sample_rate;
846      }
847      break;
848    case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
849      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
850        result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400;
851        codec_capability_.sample_rate = codec_user_config_.sample_rate;
852        codec_config_.sample_rate = codec_user_config_.sample_rate;
853      }
854      break;
855    case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
856      if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
857        result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000;
858        codec_capability_.sample_rate = codec_user_config_.sample_rate;
859        codec_config_.sample_rate = codec_user_config_.sample_rate;
860      }
861    case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
862      codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
863      codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
864      break;
865  }
866
867  // Select the sample frequency if there is no user preference
868  do {
869    if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break;
870
871    // Compute the common capability
872    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100)
873      codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
874    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000)
875      codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
876    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200)
877      codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
878    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000)
879      codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
880    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400)
881      codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
882    if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000)
883      codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
884
885    // No user preference - try the codec audio config
886    if (select_audio_sample_rate(&codec_audio_config_, sampleRate,
887                                 &result_config_cie, &codec_config_)) {
888      break;
889    }
890
891    // No user preference - try the default config
892    if (select_best_sample_rate(
893            a2dp_ldac_default_config.sampleRate & sink_info_cie.sampleRate,
894            &result_config_cie, &codec_config_)) {
895      break;
896    }
897
898    // No user preference - use the best match
899    if (select_best_sample_rate(sampleRate, &result_config_cie,
900                                &codec_config_)) {
901      break;
902    }
903  } while (false);
904  if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) {
905    LOG_ERROR(LOG_TAG,
906              "%s: cannot match sample frequency: source caps = 0x%x "
907              "sink info = 0x%x",
908              __func__, a2dp_ldac_caps.sampleRate, sink_info_cie.sampleRate);
909    goto fail;
910  }
911
912  //
913  // Select the bits per sample
914  //
915  // NOTE: this information is NOT included in the LDAC A2DP codec description
916  // that is sent OTA.
917  bits_per_sample = a2dp_ldac_caps.bits_per_sample;
918  codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
919  switch (codec_user_config_.bits_per_sample) {
920    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
921      if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
922        result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
923        codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
924        codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
925      }
926      break;
927    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
928      if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
929        result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
930        codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
931        codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
932      }
933      break;
934    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
935      if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
936        result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
937        codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
938        codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
939      }
940      break;
941    case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
942      result_config_cie.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
943      codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
944      codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
945      break;
946  }
947
948  // Select the bits per sample if there is no user preference
949  do {
950    if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE)
951      break;
952
953    // Compute the common capability
954    codec_capability_.bits_per_sample = bits_per_sample;
955
956    // No user preference - the the codec audio config
957    if (select_audio_bits_per_sample(&codec_audio_config_,
958                                     a2dp_ldac_caps.bits_per_sample,
959                                     &result_config_cie, &codec_config_)) {
960      break;
961    }
962
963    // No user preference - try the default config
964    if (select_best_bits_per_sample(a2dp_ldac_default_config.bits_per_sample,
965                                    &result_config_cie, &codec_config_)) {
966      break;
967    }
968
969    // No user preference - use the best match
970    if (select_best_bits_per_sample(a2dp_ldac_caps.bits_per_sample,
971                                    &result_config_cie, &codec_config_)) {
972      break;
973    }
974  } while (false);
975  if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) {
976    LOG_ERROR(LOG_TAG,
977              "%s: cannot match bits per sample: default = 0x%x "
978              "user preference = 0x%x",
979              __func__, a2dp_ldac_default_config.bits_per_sample,
980              codec_user_config_.bits_per_sample);
981    goto fail;
982  }
983
984  //
985  // Select the channel mode
986  //
987  channelMode = a2dp_ldac_caps.channelMode & sink_info_cie.channelMode;
988  codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
989  switch (codec_user_config_.channel_mode) {
990    case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
991      if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
992        result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_MONO;
993        codec_capability_.channel_mode = codec_user_config_.channel_mode;
994        codec_config_.channel_mode = codec_user_config_.channel_mode;
995      }
996      break;
997    case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
998      if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
999        result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO;
1000        codec_capability_.channel_mode = codec_user_config_.channel_mode;
1001        codec_config_.channel_mode = codec_user_config_.channel_mode;
1002        break;
1003      }
1004      if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
1005        result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL;
1006        codec_capability_.channel_mode = codec_user_config_.channel_mode;
1007        codec_config_.channel_mode = codec_user_config_.channel_mode;
1008        break;
1009      }
1010      break;
1011    case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1012      codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1013      codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1014      break;
1015  }
1016
1017  // Select the channel mode if there is no user preference
1018  do {
1019    if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break;
1020
1021    // Compute the common capability
1022    if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO)
1023      codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1024    if (channelMode &
1025        (A2DP_LDAC_CHANNEL_MODE_STEREO | A2DP_LDAC_CHANNEL_MODE_DUAL)) {
1026      codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1027    }
1028
1029    // No user preference - try the codec audio config
1030    if (select_audio_channel_mode(&codec_audio_config_, channelMode,
1031                                  &result_config_cie, &codec_config_)) {
1032      break;
1033    }
1034
1035    // No user preference - try the default config
1036    if (select_best_channel_mode(
1037            a2dp_ldac_default_config.channelMode & sink_info_cie.channelMode,
1038            &result_config_cie, &codec_config_)) {
1039      break;
1040    }
1041
1042    // No user preference - use the best match
1043    if (select_best_channel_mode(channelMode, &result_config_cie,
1044                                 &codec_config_)) {
1045      break;
1046    }
1047  } while (false);
1048  if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) {
1049    LOG_ERROR(LOG_TAG,
1050              "%s: cannot match channel mode: source caps = 0x%x "
1051              "sink info = 0x%x",
1052              __func__, a2dp_ldac_caps.channelMode, sink_info_cie.channelMode);
1053    goto fail;
1054  }
1055
1056  if (A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1057                         p_result_codec_config) != A2DP_SUCCESS) {
1058    goto fail;
1059  }
1060
1061  //
1062  // Copy the codec-specific fields if they are not zero
1063  //
1064  if (codec_user_config_.codec_specific_1 != 0)
1065    codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
1066  if (codec_user_config_.codec_specific_2 != 0)
1067    codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2;
1068  if (codec_user_config_.codec_specific_3 != 0)
1069    codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3;
1070  if (codec_user_config_.codec_specific_4 != 0)
1071    codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4;
1072
1073  // Create a local copy of the peer codec capability, and the
1074  // result codec config.
1075  if (is_capability) {
1076    status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &sink_info_cie,
1077                                ota_codec_peer_capability_);
1078  } else {
1079    status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &sink_info_cie,
1080                                ota_codec_peer_config_);
1081  }
1082  CHECK(status == A2DP_SUCCESS);
1083  status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1084                              ota_codec_config_);
1085  CHECK(status == A2DP_SUCCESS);
1086  return true;
1087
1088fail:
1089  // Restore the internal state
1090  codec_config_ = saved_codec_config;
1091  codec_capability_ = saved_codec_capability;
1092  codec_user_config_ = saved_codec_user_config;
1093  codec_audio_config_ = saved_codec_audio_config;
1094  memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_));
1095  memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1096         sizeof(ota_codec_peer_capability_));
1097  memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config,
1098         sizeof(ota_codec_peer_config_));
1099  return false;
1100}
1101