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