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_encoder.h"
35 #include "bt_utils.h"
36 #include "btif_av_co.h"
37 #include "osi/include/log.h"
38 #include "osi/include/osi.h"
39
40 // data type for the LDAC Codec Information Element */
41 // NOTE: bits_per_sample is needed only for LDAC encoder initialization.
42 typedef struct {
43 uint32_t vendorId;
44 uint16_t codecId; /* Codec ID for LDAC */
45 uint8_t sampleRate; /* Sampling Frequency */
46 uint8_t channelMode; /* STEREO/DUAL/MONO */
47 btav_a2dp_codec_bits_per_sample_t bits_per_sample;
48 } tA2DP_LDAC_CIE;
49
50 /* LDAC Source codec capabilities */
51 static const tA2DP_LDAC_CIE a2dp_ldac_source_caps = {
52 A2DP_LDAC_VENDOR_ID, // vendorId
53 A2DP_LDAC_CODEC_ID, // codecId
54 // sampleRate
55 (A2DP_LDAC_SAMPLING_FREQ_44100 | A2DP_LDAC_SAMPLING_FREQ_48000 |
56 A2DP_LDAC_SAMPLING_FREQ_88200 | A2DP_LDAC_SAMPLING_FREQ_96000),
57 // channelMode
58 (A2DP_LDAC_CHANNEL_MODE_DUAL | 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 */
64 static 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
72 static 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_set_transmit_queue_length};
80
81 UNUSED_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.
A2DP_BuildInfoLdac(uint8_t media_type,const tA2DP_LDAC_CIE * p_ie,uint8_t * p_result)90 static 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.
A2DP_ParseInfoLdac(tA2DP_LDAC_CIE * p_ie,const uint8_t * p_codec_info,bool is_capability)127 static 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).
A2DP_BuildMediaPayloadHeaderLdac(uint8_t * p_dst,bool frag,bool start,bool last,uint8_t num)182 static 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
A2DP_IsVendorSourceCodecValidLdac(const uint8_t * p_codec_info)194 bool 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
A2DP_IsVendorPeerSinkCodecValidLdac(const uint8_t * p_codec_info)202 bool 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.
A2DP_CodecInfoMatchesCapabilityLdac(const tA2DP_LDAC_CIE * p_cap,const uint8_t * p_codec_info,bool is_capability)219 static 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_VERBOSE(LOG_TAG, "%s: FREQ peer: 0x%x, capability 0x%x", __func__,
235 cfg_cie.sampleRate, p_cap->sampleRate);
236 LOG_VERBOSE(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
A2DP_VendorUsesRtpHeaderLdac(UNUSED_ATTR bool content_protection_enabled,UNUSED_ATTR const uint8_t * p_codec_info)248 bool 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
A2DP_VendorCodecNameLdac(UNUSED_ATTR const uint8_t * p_codec_info)254 const char* A2DP_VendorCodecNameLdac(UNUSED_ATTR const uint8_t* p_codec_info) {
255 return "LDAC";
256 }
257
A2DP_VendorCodecTypeEqualsLdac(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)258 bool 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
A2DP_VendorCodecEqualsLdac(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)281 bool 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
A2DP_VendorGetBitRateLdac(const uint8_t * p_codec_info)305 int A2DP_VendorGetBitRateLdac(const uint8_t* p_codec_info) {
306 A2dpCodecConfig* current_codec = bta_av_get_a2dp_current_codec();
307 btav_a2dp_codec_config_t codec_config_ = current_codec->getCodecConfig();
308 int samplerate = A2DP_GetTrackSampleRate(p_codec_info);
309 switch (codec_config_.codec_specific_1 % 10) {
310 case 0:
311 if (samplerate == 44100 || samplerate == 88200)
312 return 909000;
313 else
314 return 990000;
315 case 1:
316 if (samplerate == 44100 || samplerate == 88200)
317 return 606000;
318 else
319 return 660000;
320 case 2:
321 if (samplerate == 44100 || samplerate == 88200)
322 return 303000;
323 else
324 return 330000;
325 case 3:
326 default:
327 if (samplerate == 44100 || samplerate == 88200)
328 return 909000;
329 else
330 return 990000;
331 }
332 return 0;
333 }
334
A2DP_VendorGetTrackSampleRateLdac(const uint8_t * p_codec_info)335 int A2DP_VendorGetTrackSampleRateLdac(const uint8_t* p_codec_info) {
336 tA2DP_LDAC_CIE ldac_cie;
337
338 // Check whether the codec info contains valid data
339 tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false);
340 if (a2dp_status != A2DP_SUCCESS) {
341 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
342 a2dp_status);
343 return -1;
344 }
345
346 switch (ldac_cie.sampleRate) {
347 case A2DP_LDAC_SAMPLING_FREQ_44100:
348 return 44100;
349 case A2DP_LDAC_SAMPLING_FREQ_48000:
350 return 48000;
351 case A2DP_LDAC_SAMPLING_FREQ_88200:
352 return 88200;
353 case A2DP_LDAC_SAMPLING_FREQ_96000:
354 return 96000;
355 case A2DP_LDAC_SAMPLING_FREQ_176400:
356 return 176400;
357 case A2DP_LDAC_SAMPLING_FREQ_192000:
358 return 192000;
359 }
360
361 return -1;
362 }
363
A2DP_VendorGetTrackChannelCountLdac(const uint8_t * p_codec_info)364 int A2DP_VendorGetTrackChannelCountLdac(const uint8_t* p_codec_info) {
365 tA2DP_LDAC_CIE ldac_cie;
366
367 // Check whether the codec info contains valid data
368 tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false);
369 if (a2dp_status != A2DP_SUCCESS) {
370 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
371 a2dp_status);
372 return -1;
373 }
374
375 switch (ldac_cie.channelMode) {
376 case A2DP_LDAC_CHANNEL_MODE_MONO:
377 return 1;
378 case A2DP_LDAC_CHANNEL_MODE_DUAL:
379 return 2;
380 case A2DP_LDAC_CHANNEL_MODE_STEREO:
381 return 2;
382 }
383
384 return -1;
385 }
386
A2DP_VendorGetChannelModeCodeLdac(const uint8_t * p_codec_info)387 int A2DP_VendorGetChannelModeCodeLdac(const uint8_t* p_codec_info) {
388 tA2DP_LDAC_CIE ldac_cie;
389
390 // Check whether the codec info contains valid data
391 tA2DP_STATUS a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, false);
392 if (a2dp_status != A2DP_SUCCESS) {
393 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
394 a2dp_status);
395 return -1;
396 }
397
398 switch (ldac_cie.channelMode) {
399 case A2DP_LDAC_CHANNEL_MODE_MONO:
400 case A2DP_LDAC_CHANNEL_MODE_DUAL:
401 case A2DP_LDAC_CHANNEL_MODE_STEREO:
402 return ldac_cie.channelMode;
403 default:
404 break;
405 }
406
407 return -1;
408 }
409
A2DP_VendorGetPacketTimestampLdac(UNUSED_ATTR const uint8_t * p_codec_info,const uint8_t * p_data,uint32_t * p_timestamp)410 bool A2DP_VendorGetPacketTimestampLdac(UNUSED_ATTR const uint8_t* p_codec_info,
411 const uint8_t* p_data,
412 uint32_t* p_timestamp) {
413 // TODO: Is this function really codec-specific?
414 *p_timestamp = *(const uint32_t*)p_data;
415 return true;
416 }
417
A2DP_VendorBuildCodecHeaderLdac(UNUSED_ATTR const uint8_t * p_codec_info,BT_HDR * p_buf,uint16_t frames_per_packet)418 bool A2DP_VendorBuildCodecHeaderLdac(UNUSED_ATTR const uint8_t* p_codec_info,
419 BT_HDR* p_buf,
420 uint16_t frames_per_packet) {
421 uint8_t* p;
422
423 p_buf->offset -= A2DP_LDAC_MPL_HDR_LEN;
424 p = (uint8_t*)(p_buf + 1) + p_buf->offset;
425 p_buf->len += A2DP_LDAC_MPL_HDR_LEN;
426 A2DP_BuildMediaPayloadHeaderLdac(p, false, false, false,
427 (uint8_t)frames_per_packet);
428
429 return true;
430 }
431
A2DP_VendorCodecInfoStringLdac(const uint8_t * p_codec_info)432 std::string A2DP_VendorCodecInfoStringLdac(const uint8_t* p_codec_info) {
433 std::stringstream res;
434 std::string field;
435 tA2DP_STATUS a2dp_status;
436 tA2DP_LDAC_CIE ldac_cie;
437
438 a2dp_status = A2DP_ParseInfoLdac(&ldac_cie, p_codec_info, true);
439 if (a2dp_status != A2DP_SUCCESS) {
440 res << "A2DP_ParseInfoLdac fail: " << loghex(a2dp_status);
441 return res.str();
442 }
443
444 res << "\tname: LDAC\n";
445
446 // Sample frequency
447 field.clear();
448 AppendField(&field, (ldac_cie.sampleRate == 0), "NONE");
449 AppendField(&field, (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100),
450 "44100");
451 AppendField(&field, (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000),
452 "48000");
453 AppendField(&field, (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200),
454 "88200");
455 AppendField(&field, (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000),
456 "96000");
457 AppendField(&field, (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400),
458 "176400");
459 AppendField(&field, (ldac_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000),
460 "192000");
461 res << "\tsamp_freq: " << field << " (" << loghex(ldac_cie.sampleRate)
462 << ")\n";
463
464 // Channel mode
465 field.clear();
466 AppendField(&field, (ldac_cie.channelMode == 0), "NONE");
467 AppendField(&field, (ldac_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_MONO),
468 "Mono");
469 AppendField(&field, (ldac_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL),
470 "Dual");
471 AppendField(&field, (ldac_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO),
472 "Stereo");
473 res << "\tch_mode: " << field << " (" << loghex(ldac_cie.channelMode)
474 << ")\n";
475
476 return res.str();
477 }
478
A2DP_VendorGetEncoderInterfaceLdac(const uint8_t * p_codec_info)479 const tA2DP_ENCODER_INTERFACE* A2DP_VendorGetEncoderInterfaceLdac(
480 const uint8_t* p_codec_info) {
481 if (!A2DP_IsVendorSourceCodecValidLdac(p_codec_info)) return NULL;
482
483 return &a2dp_encoder_interface_ldac;
484 }
485
A2DP_VendorAdjustCodecLdac(uint8_t * p_codec_info)486 bool A2DP_VendorAdjustCodecLdac(uint8_t* p_codec_info) {
487 tA2DP_LDAC_CIE cfg_cie;
488
489 // Nothing to do: just verify the codec info is valid
490 if (A2DP_ParseInfoLdac(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS)
491 return false;
492
493 return true;
494 }
495
A2DP_VendorSourceCodecIndexLdac(UNUSED_ATTR const uint8_t * p_codec_info)496 btav_a2dp_codec_index_t A2DP_VendorSourceCodecIndexLdac(
497 UNUSED_ATTR const uint8_t* p_codec_info) {
498 return BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC;
499 }
500
A2DP_VendorCodecIndexStrLdac(void)501 const char* A2DP_VendorCodecIndexStrLdac(void) { return "LDAC"; }
502
A2DP_VendorInitCodecConfigLdac(AvdtpSepConfig * p_cfg)503 bool A2DP_VendorInitCodecConfigLdac(AvdtpSepConfig* p_cfg) {
504 if (A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &a2dp_ldac_source_caps,
505 p_cfg->codec_info) != A2DP_SUCCESS) {
506 return false;
507 }
508
509 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
510 /* Content protection info - support SCMS-T */
511 uint8_t* p = p_cfg->protect_info;
512 *p++ = AVDT_CP_LOSC;
513 UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID);
514 p_cfg->num_protect = 1;
515 #endif
516
517 return true;
518 }
519
build_codec_config(const tA2DP_LDAC_CIE & config_cie,btav_a2dp_codec_config_t * result)520 UNUSED_ATTR static void build_codec_config(const tA2DP_LDAC_CIE& config_cie,
521 btav_a2dp_codec_config_t* result) {
522 if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100)
523 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
524 if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000)
525 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
526 if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200)
527 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
528 if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000)
529 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
530 if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400)
531 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
532 if (config_cie.sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000)
533 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
534
535 result->bits_per_sample = config_cie.bits_per_sample;
536
537 if (config_cie.channelMode & A2DP_LDAC_CHANNEL_MODE_MONO)
538 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
539 if (config_cie.channelMode &
540 (A2DP_LDAC_CHANNEL_MODE_DUAL | A2DP_LDAC_CHANNEL_MODE_STEREO)) {
541 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
542 }
543 }
544
A2dpCodecConfigLdac(btav_a2dp_codec_priority_t codec_priority)545 A2dpCodecConfigLdac::A2dpCodecConfigLdac(
546 btav_a2dp_codec_priority_t codec_priority)
547 : A2dpCodecConfig(BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC, "LDAC",
548 codec_priority) {
549 // Compute the local capability
550 if (a2dp_ldac_source_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
551 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
552 }
553 if (a2dp_ldac_source_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
554 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
555 }
556 if (a2dp_ldac_source_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
557 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
558 }
559 if (a2dp_ldac_source_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
560 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
561 }
562 if (a2dp_ldac_source_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
563 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
564 }
565 if (a2dp_ldac_source_caps.sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
566 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
567 }
568 codec_local_capability_.bits_per_sample =
569 a2dp_ldac_source_caps.bits_per_sample;
570 if (a2dp_ldac_source_caps.channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
571 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
572 }
573 if (a2dp_ldac_source_caps.channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
574 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
575 }
576 if (a2dp_ldac_source_caps.channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
577 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
578 }
579 }
580
~A2dpCodecConfigLdac()581 A2dpCodecConfigLdac::~A2dpCodecConfigLdac() {}
582
init()583 bool A2dpCodecConfigLdac::init() {
584 if (!isValid()) return false;
585
586 // Load the encoder
587 if (!A2DP_VendorLoadEncoderLdac()) {
588 LOG_ERROR(LOG_TAG, "%s: cannot load the encoder", __func__);
589 return false;
590 }
591
592 return true;
593 }
594
useRtpHeaderMarkerBit() const595 bool A2dpCodecConfigLdac::useRtpHeaderMarkerBit() const { return false; }
596
597 //
598 // Selects the best sample rate from |sampleRate|.
599 // The result is stored in |p_result| and |p_codec_config|.
600 // Returns true if a selection was made, otherwise false.
601 //
select_best_sample_rate(uint8_t sampleRate,tA2DP_LDAC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)602 static bool select_best_sample_rate(uint8_t sampleRate,
603 tA2DP_LDAC_CIE* p_result,
604 btav_a2dp_codec_config_t* p_codec_config) {
605 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
606 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000;
607 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
608 return true;
609 }
610 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
611 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400;
612 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
613 return true;
614 }
615 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
616 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000;
617 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
618 return true;
619 }
620 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
621 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200;
622 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
623 return true;
624 }
625 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
626 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000;
627 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
628 return true;
629 }
630 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
631 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100;
632 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
633 return true;
634 }
635 return false;
636 }
637
638 //
639 // Selects the audio sample rate from |p_codec_audio_config|.
640 // |sampleRate| contains the capability.
641 // The result is stored in |p_result| and |p_codec_config|.
642 // Returns true if a selection was made, otherwise false.
643 //
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)644 static bool select_audio_sample_rate(
645 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t sampleRate,
646 tA2DP_LDAC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
647 switch (p_codec_audio_config->sample_rate) {
648 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
649 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
650 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100;
651 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
652 return true;
653 }
654 break;
655 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
656 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
657 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000;
658 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
659 return true;
660 }
661 break;
662 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
663 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
664 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200;
665 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
666 return true;
667 }
668 break;
669 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
670 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
671 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000;
672 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
673 return true;
674 }
675 break;
676 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
677 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
678 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400;
679 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
680 return true;
681 }
682 break;
683 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
684 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
685 p_result->sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000;
686 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
687 return true;
688 }
689 break;
690 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
691 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
692 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
693 break;
694 }
695 return false;
696 }
697
698 //
699 // Selects the best bits per sample from |bits_per_sample|.
700 // |bits_per_sample| contains the capability.
701 // The result is stored in |p_result| and |p_codec_config|.
702 // Returns true if a selection was made, otherwise false.
703 //
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)704 static bool select_best_bits_per_sample(
705 btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_LDAC_CIE* p_result,
706 btav_a2dp_codec_config_t* p_codec_config) {
707 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
708 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
709 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
710 return true;
711 }
712 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
713 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
714 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
715 return true;
716 }
717 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
718 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
719 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
720 return true;
721 }
722 return false;
723 }
724
725 //
726 // Selects the audio bits per sample from |p_codec_audio_config|.
727 // |bits_per_sample| contains the capability.
728 // The result is stored in |p_result| and |p_codec_config|.
729 // Returns true if a selection was made, otherwise false.
730 //
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)731 static bool select_audio_bits_per_sample(
732 const btav_a2dp_codec_config_t* p_codec_audio_config,
733 btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_LDAC_CIE* p_result,
734 btav_a2dp_codec_config_t* p_codec_config) {
735 switch (p_codec_audio_config->bits_per_sample) {
736 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
737 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
738 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
739 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
740 return true;
741 }
742 break;
743 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
744 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
745 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
746 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
747 return true;
748 }
749 break;
750 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
751 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
752 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
753 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
754 return true;
755 }
756 break;
757 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
758 break;
759 }
760 return false;
761 }
762
763 //
764 // Selects the best channel mode from |channelMode|.
765 // The result is stored in |p_result| and |p_codec_config|.
766 // Returns true if a selection was made, otherwise false.
767 //
select_best_channel_mode(uint8_t channelMode,tA2DP_LDAC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)768 static bool select_best_channel_mode(uint8_t channelMode,
769 tA2DP_LDAC_CIE* p_result,
770 btav_a2dp_codec_config_t* p_codec_config) {
771 if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
772 p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO;
773 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
774 return true;
775 }
776 if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
777 p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL;
778 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
779 return true;
780 }
781 if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
782 p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_MONO;
783 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
784 return true;
785 }
786 return false;
787 }
788
789 //
790 // Selects the audio channel mode from |p_codec_audio_config|.
791 // |channelMode| contains the capability.
792 // The result is stored in |p_result| and |p_codec_config|.
793 // Returns true if a selection was made, otherwise false.
794 //
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)795 static bool select_audio_channel_mode(
796 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t channelMode,
797 tA2DP_LDAC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
798 switch (p_codec_audio_config->channel_mode) {
799 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
800 if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
801 p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_MONO;
802 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
803 return true;
804 }
805 break;
806 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
807 if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
808 p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO;
809 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
810 return true;
811 }
812 if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
813 p_result->channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL;
814 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
815 return true;
816 }
817 break;
818 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
819 break;
820 }
821
822 return false;
823 }
824
setCodecConfig(const uint8_t * p_peer_codec_info,bool is_capability,uint8_t * p_result_codec_config)825 bool A2dpCodecConfigLdac::setCodecConfig(const uint8_t* p_peer_codec_info,
826 bool is_capability,
827 uint8_t* p_result_codec_config) {
828 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
829 tA2DP_LDAC_CIE peer_info_cie;
830 tA2DP_LDAC_CIE result_config_cie;
831 uint8_t channelMode;
832 uint8_t sampleRate;
833 btav_a2dp_codec_bits_per_sample_t bits_per_sample;
834
835 // Save the internal state
836 btav_a2dp_codec_config_t saved_codec_config = codec_config_;
837 btav_a2dp_codec_config_t saved_codec_capability = codec_capability_;
838 btav_a2dp_codec_config_t saved_codec_selectable_capability =
839 codec_selectable_capability_;
840 btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_;
841 btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_;
842 uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE];
843 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
844 uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE];
845 memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_));
846 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
847 sizeof(ota_codec_peer_capability_));
848 memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_,
849 sizeof(ota_codec_peer_config_));
850
851 tA2DP_STATUS status =
852 A2DP_ParseInfoLdac(&peer_info_cie, p_peer_codec_info, is_capability);
853 if (status != A2DP_SUCCESS) {
854 LOG_ERROR(LOG_TAG, "%s: can't parse peer's capabilities: error = %d",
855 __func__, status);
856 goto fail;
857 }
858
859 //
860 // Build the preferred configuration
861 //
862 memset(&result_config_cie, 0, sizeof(result_config_cie));
863 result_config_cie.vendorId = a2dp_ldac_source_caps.vendorId;
864 result_config_cie.codecId = a2dp_ldac_source_caps.codecId;
865
866 //
867 // Select the sample frequency
868 //
869 sampleRate = a2dp_ldac_source_caps.sampleRate & peer_info_cie.sampleRate;
870 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
871 switch (codec_user_config_.sample_rate) {
872 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
873 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
874 result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_44100;
875 codec_capability_.sample_rate = codec_user_config_.sample_rate;
876 codec_config_.sample_rate = codec_user_config_.sample_rate;
877 }
878 break;
879 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
880 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
881 result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_48000;
882 codec_capability_.sample_rate = codec_user_config_.sample_rate;
883 codec_config_.sample_rate = codec_user_config_.sample_rate;
884 }
885 break;
886 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
887 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
888 result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_88200;
889 codec_capability_.sample_rate = codec_user_config_.sample_rate;
890 codec_config_.sample_rate = codec_user_config_.sample_rate;
891 }
892 break;
893 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
894 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
895 result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_96000;
896 codec_capability_.sample_rate = codec_user_config_.sample_rate;
897 codec_config_.sample_rate = codec_user_config_.sample_rate;
898 }
899 break;
900 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
901 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
902 result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_176400;
903 codec_capability_.sample_rate = codec_user_config_.sample_rate;
904 codec_config_.sample_rate = codec_user_config_.sample_rate;
905 }
906 break;
907 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
908 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
909 result_config_cie.sampleRate = A2DP_LDAC_SAMPLING_FREQ_192000;
910 codec_capability_.sample_rate = codec_user_config_.sample_rate;
911 codec_config_.sample_rate = codec_user_config_.sample_rate;
912 }
913 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
914 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
915 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
916 codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
917 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
918 break;
919 }
920
921 // Select the sample frequency if there is no user preference
922 do {
923 // Compute the selectable capability
924 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
925 codec_selectable_capability_.sample_rate |=
926 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
927 }
928 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
929 codec_selectable_capability_.sample_rate |=
930 BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
931 }
932 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
933 codec_selectable_capability_.sample_rate |=
934 BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
935 }
936 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
937 codec_selectable_capability_.sample_rate |=
938 BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
939 }
940 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
941 codec_selectable_capability_.sample_rate |=
942 BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
943 }
944 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
945 codec_selectable_capability_.sample_rate |=
946 BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
947 }
948
949 if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break;
950
951 // Compute the common capability
952 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100)
953 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
954 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000)
955 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
956 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200)
957 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
958 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000)
959 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
960 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400)
961 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
962 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000)
963 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
964
965 // No user preference - try the codec audio config
966 if (select_audio_sample_rate(&codec_audio_config_, sampleRate,
967 &result_config_cie, &codec_config_)) {
968 break;
969 }
970
971 // No user preference - try the default config
972 if (select_best_sample_rate(
973 a2dp_ldac_default_config.sampleRate & peer_info_cie.sampleRate,
974 &result_config_cie, &codec_config_)) {
975 break;
976 }
977
978 // No user preference - use the best match
979 if (select_best_sample_rate(sampleRate, &result_config_cie,
980 &codec_config_)) {
981 break;
982 }
983 } while (false);
984 if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) {
985 LOG_ERROR(LOG_TAG,
986 "%s: cannot match sample frequency: local caps = 0x%x "
987 "peer info = 0x%x",
988 __func__, a2dp_ldac_source_caps.sampleRate,
989 peer_info_cie.sampleRate);
990 goto fail;
991 }
992
993 //
994 // Select the bits per sample
995 //
996 // NOTE: this information is NOT included in the LDAC A2DP codec description
997 // that is sent OTA.
998 bits_per_sample = a2dp_ldac_source_caps.bits_per_sample;
999 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1000 switch (codec_user_config_.bits_per_sample) {
1001 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
1002 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
1003 result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1004 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1005 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1006 }
1007 break;
1008 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
1009 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
1010 result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1011 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1012 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1013 }
1014 break;
1015 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
1016 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
1017 result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1018 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1019 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1020 }
1021 break;
1022 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
1023 result_config_cie.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1024 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1025 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1026 break;
1027 }
1028
1029 // Select the bits per sample if there is no user preference
1030 do {
1031 // Compute the selectable capability
1032 codec_selectable_capability_.bits_per_sample =
1033 a2dp_ldac_source_caps.bits_per_sample;
1034
1035 if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE)
1036 break;
1037
1038 // Compute the common capability
1039 codec_capability_.bits_per_sample = bits_per_sample;
1040
1041 // No user preference - the the codec audio config
1042 if (select_audio_bits_per_sample(&codec_audio_config_,
1043 a2dp_ldac_source_caps.bits_per_sample,
1044 &result_config_cie, &codec_config_)) {
1045 break;
1046 }
1047
1048 // No user preference - try the default config
1049 if (select_best_bits_per_sample(a2dp_ldac_default_config.bits_per_sample,
1050 &result_config_cie, &codec_config_)) {
1051 break;
1052 }
1053
1054 // No user preference - use the best match
1055 if (select_best_bits_per_sample(a2dp_ldac_source_caps.bits_per_sample,
1056 &result_config_cie, &codec_config_)) {
1057 break;
1058 }
1059 } while (false);
1060 if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) {
1061 LOG_ERROR(LOG_TAG,
1062 "%s: cannot match bits per sample: default = 0x%x "
1063 "user preference = 0x%x",
1064 __func__, a2dp_ldac_default_config.bits_per_sample,
1065 codec_user_config_.bits_per_sample);
1066 goto fail;
1067 }
1068
1069 //
1070 // Select the channel mode
1071 //
1072 channelMode = a2dp_ldac_source_caps.channelMode & peer_info_cie.channelMode;
1073 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1074 switch (codec_user_config_.channel_mode) {
1075 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
1076 if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
1077 result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_MONO;
1078 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1079 codec_config_.channel_mode = codec_user_config_.channel_mode;
1080 }
1081 break;
1082 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
1083 if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
1084 result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_STEREO;
1085 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1086 codec_config_.channel_mode = codec_user_config_.channel_mode;
1087 break;
1088 }
1089 if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
1090 result_config_cie.channelMode = A2DP_LDAC_CHANNEL_MODE_DUAL;
1091 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1092 codec_config_.channel_mode = codec_user_config_.channel_mode;
1093 break;
1094 }
1095 break;
1096 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1097 codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1098 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1099 break;
1100 }
1101
1102 // Select the channel mode if there is no user preference
1103 do {
1104 // Compute the selectable capability
1105 if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
1106 codec_selectable_capability_.channel_mode |=
1107 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1108 }
1109 if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
1110 codec_selectable_capability_.channel_mode |=
1111 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1112 }
1113 if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
1114 codec_selectable_capability_.channel_mode |=
1115 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1116 }
1117
1118 if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break;
1119
1120 // Compute the common capability
1121 if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO)
1122 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1123 if (channelMode &
1124 (A2DP_LDAC_CHANNEL_MODE_STEREO | A2DP_LDAC_CHANNEL_MODE_DUAL)) {
1125 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1126 }
1127
1128 // No user preference - try the codec audio config
1129 if (select_audio_channel_mode(&codec_audio_config_, channelMode,
1130 &result_config_cie, &codec_config_)) {
1131 break;
1132 }
1133
1134 // No user preference - try the default config
1135 if (select_best_channel_mode(
1136 a2dp_ldac_default_config.channelMode & peer_info_cie.channelMode,
1137 &result_config_cie, &codec_config_)) {
1138 break;
1139 }
1140
1141 // No user preference - use the best match
1142 if (select_best_channel_mode(channelMode, &result_config_cie,
1143 &codec_config_)) {
1144 break;
1145 }
1146 } while (false);
1147 if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) {
1148 LOG_ERROR(LOG_TAG,
1149 "%s: cannot match channel mode: local caps = 0x%x "
1150 "peer info = 0x%x",
1151 __func__, a2dp_ldac_source_caps.channelMode,
1152 peer_info_cie.channelMode);
1153 goto fail;
1154 }
1155
1156 if (A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1157 p_result_codec_config) != A2DP_SUCCESS) {
1158 goto fail;
1159 }
1160
1161 //
1162 // Copy the codec-specific fields if they are not zero
1163 //
1164 if (codec_user_config_.codec_specific_1 != 0)
1165 codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
1166 if (codec_user_config_.codec_specific_2 != 0)
1167 codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2;
1168 if (codec_user_config_.codec_specific_3 != 0)
1169 codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3;
1170 if (codec_user_config_.codec_specific_4 != 0)
1171 codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4;
1172
1173 // Create a local copy of the peer codec capability, and the
1174 // result codec config.
1175 if (is_capability) {
1176 status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1177 ota_codec_peer_capability_);
1178 } else {
1179 status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1180 ota_codec_peer_config_);
1181 }
1182 CHECK(status == A2DP_SUCCESS);
1183 status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1184 ota_codec_config_);
1185 CHECK(status == A2DP_SUCCESS);
1186 return true;
1187
1188 fail:
1189 // Restore the internal state
1190 codec_config_ = saved_codec_config;
1191 codec_capability_ = saved_codec_capability;
1192 codec_selectable_capability_ = saved_codec_selectable_capability;
1193 codec_user_config_ = saved_codec_user_config;
1194 codec_audio_config_ = saved_codec_audio_config;
1195 memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_));
1196 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1197 sizeof(ota_codec_peer_capability_));
1198 memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config,
1199 sizeof(ota_codec_peer_config_));
1200 return false;
1201 }
1202
setPeerCodecCapabilities(const uint8_t * p_peer_codec_capabilities)1203 bool A2dpCodecConfigLdac::setPeerCodecCapabilities(
1204 const uint8_t* p_peer_codec_capabilities) {
1205 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
1206 tA2DP_LDAC_CIE peer_info_cie;
1207 uint8_t channelMode;
1208 uint8_t sampleRate;
1209
1210 // Save the internal state
1211 btav_a2dp_codec_config_t saved_codec_selectable_capability =
1212 codec_selectable_capability_;
1213 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
1214 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
1215 sizeof(ota_codec_peer_capability_));
1216
1217 tA2DP_STATUS status =
1218 A2DP_ParseInfoLdac(&peer_info_cie, p_peer_codec_capabilities, true);
1219 if (status != A2DP_SUCCESS) {
1220 LOG_ERROR(LOG_TAG, "%s: can't parse peer's capabilities: error = %d",
1221 __func__, status);
1222 goto fail;
1223 }
1224
1225 // Compute the selectable capability - sample rate
1226 sampleRate = a2dp_ldac_source_caps.sampleRate & peer_info_cie.sampleRate;
1227 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_44100) {
1228 codec_selectable_capability_.sample_rate |=
1229 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1230 }
1231 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_48000) {
1232 codec_selectable_capability_.sample_rate |=
1233 BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1234 }
1235 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_88200) {
1236 codec_selectable_capability_.sample_rate |=
1237 BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
1238 }
1239 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_96000) {
1240 codec_selectable_capability_.sample_rate |=
1241 BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
1242 }
1243 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_176400) {
1244 codec_selectable_capability_.sample_rate |=
1245 BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
1246 }
1247 if (sampleRate & A2DP_LDAC_SAMPLING_FREQ_192000) {
1248 codec_selectable_capability_.sample_rate |=
1249 BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
1250 }
1251
1252 // Compute the selectable capability - bits per sample
1253 codec_selectable_capability_.bits_per_sample =
1254 a2dp_ldac_source_caps.bits_per_sample;
1255
1256 // Compute the selectable capability - channel mode
1257 channelMode = a2dp_ldac_source_caps.channelMode & peer_info_cie.channelMode;
1258 if (channelMode & A2DP_LDAC_CHANNEL_MODE_MONO) {
1259 codec_selectable_capability_.channel_mode |=
1260 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1261 }
1262 if (channelMode & A2DP_LDAC_CHANNEL_MODE_STEREO) {
1263 codec_selectable_capability_.channel_mode |=
1264 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1265 }
1266 if (channelMode & A2DP_LDAC_CHANNEL_MODE_DUAL) {
1267 codec_selectable_capability_.channel_mode |=
1268 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1269 }
1270
1271 status = A2DP_BuildInfoLdac(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1272 ota_codec_peer_capability_);
1273 CHECK(status == A2DP_SUCCESS);
1274 return true;
1275
1276 fail:
1277 // Restore the internal state
1278 codec_selectable_capability_ = saved_codec_selectable_capability;
1279 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1280 sizeof(ota_codec_peer_capability_));
1281 return false;
1282 }
1283