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 aptX-HD Codec Information
20 * Element and Media Payload.
21 *
22 ******************************************************************************/
23
24 #define LOG_TAG "a2dp_vendor_aptx_hd"
25
26 #include "bt_target.h"
27
28 #include "a2dp_vendor_aptx_hd.h"
29
30 #include <string.h>
31
32 #include <base/logging.h>
33 #include "a2dp_vendor.h"
34 #include "a2dp_vendor_aptx_hd_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 aptX-HD Codec Information Element */
41 typedef struct {
42 uint32_t vendorId;
43 uint16_t codecId; /* Codec ID for aptX-HD */
44 uint8_t sampleRate; /* Sampling Frequency */
45 uint8_t channelMode; /* STEREO/DUAL/MONO */
46 uint8_t acl_sprint_reserved0;
47 uint8_t acl_sprint_reserved1;
48 uint8_t acl_sprint_reserved2;
49 uint8_t acl_sprint_reserved3;
50 btav_a2dp_codec_bits_per_sample_t bits_per_sample;
51 } tA2DP_APTX_HD_CIE;
52
53 /* aptX-HD Source codec capabilities */
54 static const tA2DP_APTX_HD_CIE a2dp_aptx_hd_source_caps = {
55 A2DP_APTX_HD_VENDOR_ID, /* vendorId */
56 A2DP_APTX_HD_CODEC_ID_BLUETOOTH, /* codecId */
57 (A2DP_APTX_HD_SAMPLERATE_44100 |
58 A2DP_APTX_HD_SAMPLERATE_48000), /* sampleRate */
59 A2DP_APTX_HD_CHANNELS_STEREO, /* channelMode */
60 A2DP_APTX_HD_ACL_SPRINT_RESERVED0, /* acl_sprint_reserved0 */
61 A2DP_APTX_HD_ACL_SPRINT_RESERVED1, /* acl_sprint_reserved1 */
62 A2DP_APTX_HD_ACL_SPRINT_RESERVED2, /* acl_sprint_reserved2 */
63 A2DP_APTX_HD_ACL_SPRINT_RESERVED3, /* acl_sprint_reserved3 */
64 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24 /* bits_per_sample */
65 };
66
67 /* Default aptX-HD codec configuration */
68 static const tA2DP_APTX_HD_CIE a2dp_aptx_hd_default_config = {
69 A2DP_APTX_HD_VENDOR_ID, /* vendorId */
70 A2DP_APTX_HD_CODEC_ID_BLUETOOTH, /* codecId */
71 A2DP_APTX_HD_SAMPLERATE_48000, /* sampleRate */
72 A2DP_APTX_HD_CHANNELS_STEREO, /* channelMode */
73 A2DP_APTX_HD_ACL_SPRINT_RESERVED0, /* acl_sprint_reserved0 */
74 A2DP_APTX_HD_ACL_SPRINT_RESERVED1, /* acl_sprint_reserved1 */
75 A2DP_APTX_HD_ACL_SPRINT_RESERVED2, /* acl_sprint_reserved2 */
76 A2DP_APTX_HD_ACL_SPRINT_RESERVED3, /* acl_sprint_reserved3 */
77 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24 /* bits_per_sample */
78 };
79
80 static const tA2DP_ENCODER_INTERFACE a2dp_encoder_interface_aptx_hd = {
81 a2dp_vendor_aptx_hd_encoder_init,
82 a2dp_vendor_aptx_hd_encoder_cleanup,
83 a2dp_vendor_aptx_hd_feeding_reset,
84 a2dp_vendor_aptx_hd_feeding_flush,
85 a2dp_vendor_aptx_hd_get_encoder_interval_ms,
86 a2dp_vendor_aptx_hd_send_frames,
87 nullptr // set_transmit_queue_length
88 };
89
90 UNUSED_ATTR static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityAptxHd(
91 const tA2DP_APTX_HD_CIE* p_cap, const uint8_t* p_codec_info,
92 bool is_peer_codec_info);
93
94 // Builds the aptX-HD Media Codec Capabilities byte sequence beginning from the
95 // LOSC octet. |media_type| is the media type |AVDT_MEDIA_TYPE_*|.
96 // |p_ie| is a pointer to the aptX-HD Codec Information Element information.
97 // The result is stored in |p_result|. Returns A2DP_SUCCESS on success,
98 // otherwise the corresponding A2DP error status code.
A2DP_BuildInfoAptxHd(uint8_t media_type,const tA2DP_APTX_HD_CIE * p_ie,uint8_t * p_result)99 static tA2DP_STATUS A2DP_BuildInfoAptxHd(uint8_t media_type,
100 const tA2DP_APTX_HD_CIE* p_ie,
101 uint8_t* p_result) {
102 if (p_ie == NULL || p_result == NULL) {
103 return A2DP_INVALID_PARAMS;
104 }
105
106 *p_result++ = A2DP_APTX_HD_CODEC_LEN;
107 *p_result++ = (media_type << 4);
108 *p_result++ = A2DP_MEDIA_CT_NON_A2DP;
109 *p_result++ = (uint8_t)(p_ie->vendorId & 0x000000FF);
110 *p_result++ = (uint8_t)((p_ie->vendorId & 0x0000FF00) >> 8);
111 *p_result++ = (uint8_t)((p_ie->vendorId & 0x00FF0000) >> 16);
112 *p_result++ = (uint8_t)((p_ie->vendorId & 0xFF000000) >> 24);
113 *p_result++ = (uint8_t)(p_ie->codecId & 0x00FF);
114 *p_result++ = (uint8_t)((p_ie->codecId & 0xFF00) >> 8);
115 *p_result++ = p_ie->sampleRate | p_ie->channelMode;
116 *p_result++ = p_ie->acl_sprint_reserved0;
117 *p_result++ = p_ie->acl_sprint_reserved1;
118 *p_result++ = p_ie->acl_sprint_reserved2;
119 *p_result++ = p_ie->acl_sprint_reserved3;
120
121 return A2DP_SUCCESS;
122 }
123
124 // Parses the aptX-HD Media Codec Capabilities byte sequence beginning from the
125 // LOSC octet. The result is stored in |p_ie|. The byte sequence to parse is
126 // |p_codec_info|. If |is_capability| is true, the byte sequence is
127 // codec capabilities, otherwise is codec configuration.
128 // Returns A2DP_SUCCESS on success, otherwise the corresponding A2DP error
129 // status code.
A2DP_ParseInfoAptxHd(tA2DP_APTX_HD_CIE * p_ie,const uint8_t * p_codec_info,bool is_capability)130 static tA2DP_STATUS A2DP_ParseInfoAptxHd(tA2DP_APTX_HD_CIE* p_ie,
131 const uint8_t* p_codec_info,
132 bool is_capability) {
133 uint8_t losc;
134 uint8_t media_type;
135 tA2DP_CODEC_TYPE codec_type;
136
137 if (p_ie == NULL || p_codec_info == NULL) return A2DP_INVALID_PARAMS;
138
139 // Check the codec capability length
140 losc = *p_codec_info++;
141 if (losc != A2DP_APTX_HD_CODEC_LEN) return A2DP_WRONG_CODEC;
142
143 media_type = (*p_codec_info++) >> 4;
144 codec_type = *p_codec_info++;
145 /* Check the Media Type and Media Codec Type */
146 if (media_type != AVDT_MEDIA_TYPE_AUDIO ||
147 codec_type != A2DP_MEDIA_CT_NON_A2DP) {
148 return A2DP_WRONG_CODEC;
149 }
150
151 // Check the Vendor ID and Codec ID */
152 p_ie->vendorId = (*p_codec_info & 0x000000FF) |
153 (*(p_codec_info + 1) << 8 & 0x0000FF00) |
154 (*(p_codec_info + 2) << 16 & 0x00FF0000) |
155 (*(p_codec_info + 3) << 24 & 0xFF000000);
156 p_codec_info += 4;
157 p_ie->codecId =
158 (*p_codec_info & 0x00FF) | (*(p_codec_info + 1) << 8 & 0xFF00);
159 p_codec_info += 2;
160 if (p_ie->vendorId != A2DP_APTX_HD_VENDOR_ID ||
161 p_ie->codecId != A2DP_APTX_HD_CODEC_ID_BLUETOOTH) {
162 return A2DP_WRONG_CODEC;
163 }
164
165 p_ie->channelMode = *p_codec_info & 0x0F;
166 p_ie->sampleRate = *p_codec_info & 0xF0;
167 p_codec_info++;
168
169 p_ie->acl_sprint_reserved0 = *(p_codec_info++);
170 p_ie->acl_sprint_reserved1 = *(p_codec_info++);
171 p_ie->acl_sprint_reserved2 = *(p_codec_info++);
172 p_ie->acl_sprint_reserved3 = *(p_codec_info++);
173
174 if (is_capability) return A2DP_SUCCESS;
175
176 if (A2DP_BitsSet(p_ie->sampleRate) != A2DP_SET_ONE_BIT)
177 return A2DP_BAD_SAMP_FREQ;
178 if (A2DP_BitsSet(p_ie->channelMode) != A2DP_SET_ONE_BIT)
179 return A2DP_BAD_CH_MODE;
180
181 return A2DP_SUCCESS;
182 }
183
A2DP_IsVendorSourceCodecValidAptxHd(const uint8_t * p_codec_info)184 bool A2DP_IsVendorSourceCodecValidAptxHd(const uint8_t* p_codec_info) {
185 tA2DP_APTX_HD_CIE cfg_cie;
186
187 /* Use a liberal check when parsing the codec info */
188 return (A2DP_ParseInfoAptxHd(&cfg_cie, p_codec_info, false) ==
189 A2DP_SUCCESS) ||
190 (A2DP_ParseInfoAptxHd(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
191 }
192
A2DP_IsVendorPeerSinkCodecValidAptxHd(const uint8_t * p_codec_info)193 bool A2DP_IsVendorPeerSinkCodecValidAptxHd(const uint8_t* p_codec_info) {
194 tA2DP_APTX_HD_CIE cfg_cie;
195
196 /* Use a liberal check when parsing the codec info */
197 return (A2DP_ParseInfoAptxHd(&cfg_cie, p_codec_info, false) ==
198 A2DP_SUCCESS) ||
199 (A2DP_ParseInfoAptxHd(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
200 }
201
202 // Checks whether A2DP aptX-HD codec configuration matches with a device's
203 // codec capabilities. |p_cap| is the aptX-HD codec configuration.
204 // |p_codec_info| is the device's codec capabilities.
205 // If |is_capability| is true, the byte sequence is codec capabilities,
206 // otherwise is codec configuration.
207 // |p_codec_info| contains the codec capabilities for a peer device that
208 // is acting as an A2DP source.
209 // Returns A2DP_SUCCESS if the codec configuration matches with capabilities,
210 // otherwise the corresponding A2DP error status code.
A2DP_CodecInfoMatchesCapabilityAptxHd(const tA2DP_APTX_HD_CIE * p_cap,const uint8_t * p_codec_info,bool is_capability)211 static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityAptxHd(
212 const tA2DP_APTX_HD_CIE* p_cap, const uint8_t* p_codec_info,
213 bool is_capability) {
214 tA2DP_STATUS status;
215 tA2DP_APTX_HD_CIE cfg_cie;
216
217 /* parse configuration */
218 status = A2DP_ParseInfoAptxHd(&cfg_cie, p_codec_info, is_capability);
219 if (status != A2DP_SUCCESS) {
220 LOG_ERROR(LOG_TAG, "%s: parsing failed %d", __func__, status);
221 return status;
222 }
223
224 /* verify that each parameter is in range */
225
226 LOG_VERBOSE(LOG_TAG, "%s: FREQ peer: 0x%x, capability 0x%x", __func__,
227 cfg_cie.sampleRate, p_cap->sampleRate);
228 LOG_VERBOSE(LOG_TAG, "%s: CH_MODE peer: 0x%x, capability 0x%x", __func__,
229 cfg_cie.channelMode, p_cap->channelMode);
230
231 /* sampling frequency */
232 if ((cfg_cie.sampleRate & p_cap->sampleRate) == 0) return A2DP_NS_SAMP_FREQ;
233
234 /* channel mode */
235 if ((cfg_cie.channelMode & p_cap->channelMode) == 0) return A2DP_NS_CH_MODE;
236
237 return A2DP_SUCCESS;
238 }
239
A2DP_VendorUsesRtpHeaderAptxHd(UNUSED_ATTR bool content_protection_enabled,UNUSED_ATTR const uint8_t * p_codec_info)240 bool A2DP_VendorUsesRtpHeaderAptxHd(UNUSED_ATTR bool content_protection_enabled,
241 UNUSED_ATTR const uint8_t* p_codec_info) {
242 return true;
243 }
244
A2DP_VendorCodecNameAptxHd(UNUSED_ATTR const uint8_t * p_codec_info)245 const char* A2DP_VendorCodecNameAptxHd(
246 UNUSED_ATTR const uint8_t* p_codec_info) {
247 return "aptX-HD";
248 }
249
A2DP_VendorCodecTypeEqualsAptxHd(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)250 bool A2DP_VendorCodecTypeEqualsAptxHd(const uint8_t* p_codec_info_a,
251 const uint8_t* p_codec_info_b) {
252 tA2DP_APTX_HD_CIE aptx_hd_cie_a;
253 tA2DP_APTX_HD_CIE aptx_hd_cie_b;
254
255 // Check whether the codec info contains valid data
256 tA2DP_STATUS a2dp_status =
257 A2DP_ParseInfoAptxHd(&aptx_hd_cie_a, p_codec_info_a, true);
258 if (a2dp_status != A2DP_SUCCESS) {
259 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
260 a2dp_status);
261 return false;
262 }
263 a2dp_status = A2DP_ParseInfoAptxHd(&aptx_hd_cie_b, p_codec_info_b, true);
264 if (a2dp_status != A2DP_SUCCESS) {
265 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
266 a2dp_status);
267 return false;
268 }
269
270 return true;
271 }
272
A2DP_VendorCodecEqualsAptxHd(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)273 bool A2DP_VendorCodecEqualsAptxHd(const uint8_t* p_codec_info_a,
274 const uint8_t* p_codec_info_b) {
275 tA2DP_APTX_HD_CIE aptx_hd_cie_a;
276 tA2DP_APTX_HD_CIE aptx_hd_cie_b;
277
278 // Check whether the codec info contains valid data
279 tA2DP_STATUS a2dp_status =
280 A2DP_ParseInfoAptxHd(&aptx_hd_cie_a, p_codec_info_a, true);
281 if (a2dp_status != A2DP_SUCCESS) {
282 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
283 a2dp_status);
284 return false;
285 }
286 a2dp_status = A2DP_ParseInfoAptxHd(&aptx_hd_cie_b, p_codec_info_b, true);
287 if (a2dp_status != A2DP_SUCCESS) {
288 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
289 a2dp_status);
290 return false;
291 }
292
293 return (aptx_hd_cie_a.sampleRate == aptx_hd_cie_b.sampleRate) &&
294 (aptx_hd_cie_a.channelMode == aptx_hd_cie_b.channelMode);
295 }
296
A2DP_VendorGetBitRateAptxHd(const uint8_t * p_codec_info)297 int A2DP_VendorGetBitRateAptxHd(const uint8_t* p_codec_info) {
298 A2dpCodecConfig* CodecConfig = bta_av_get_a2dp_current_codec();
299 tA2DP_BITS_PER_SAMPLE bits_per_sample = CodecConfig->getAudioBitsPerSample();
300 uint16_t samplerate = A2DP_GetTrackSampleRate(p_codec_info);
301 return (samplerate * bits_per_sample * 2) / 4;
302 }
303
A2DP_VendorGetTrackSampleRateAptxHd(const uint8_t * p_codec_info)304 int A2DP_VendorGetTrackSampleRateAptxHd(const uint8_t* p_codec_info) {
305 tA2DP_APTX_HD_CIE aptx_hd_cie;
306
307 // Check whether the codec info contains valid data
308 tA2DP_STATUS a2dp_status =
309 A2DP_ParseInfoAptxHd(&aptx_hd_cie, p_codec_info, false);
310 if (a2dp_status != A2DP_SUCCESS) {
311 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
312 a2dp_status);
313 return -1;
314 }
315
316 if (aptx_hd_cie.sampleRate == A2DP_APTX_HD_SAMPLERATE_44100) return 44100;
317 if (aptx_hd_cie.sampleRate == A2DP_APTX_HD_SAMPLERATE_48000) return 48000;
318
319 return -1;
320 }
321
A2DP_VendorGetTrackChannelCountAptxHd(const uint8_t * p_codec_info)322 int A2DP_VendorGetTrackChannelCountAptxHd(const uint8_t* p_codec_info) {
323 tA2DP_APTX_HD_CIE aptx_hd_cie;
324
325 // Check whether the codec info contains valid data
326 tA2DP_STATUS a2dp_status =
327 A2DP_ParseInfoAptxHd(&aptx_hd_cie, p_codec_info, false);
328 if (a2dp_status != A2DP_SUCCESS) {
329 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
330 a2dp_status);
331 return -1;
332 }
333
334 switch (aptx_hd_cie.channelMode) {
335 case A2DP_APTX_HD_CHANNELS_MONO:
336 return 1;
337 case A2DP_APTX_HD_CHANNELS_STEREO:
338 return 2;
339 }
340
341 return -1;
342 }
343
A2DP_VendorGetPacketTimestampAptxHd(UNUSED_ATTR const uint8_t * p_codec_info,const uint8_t * p_data,uint32_t * p_timestamp)344 bool A2DP_VendorGetPacketTimestampAptxHd(
345 UNUSED_ATTR const uint8_t* p_codec_info, const uint8_t* p_data,
346 uint32_t* p_timestamp) {
347 // TODO: Is this function really codec-specific?
348 *p_timestamp = *(const uint32_t*)p_data;
349 return true;
350 }
351
A2DP_VendorBuildCodecHeaderAptxHd(UNUSED_ATTR const uint8_t * p_codec_info,UNUSED_ATTR BT_HDR * p_buf,UNUSED_ATTR uint16_t frames_per_packet)352 bool A2DP_VendorBuildCodecHeaderAptxHd(UNUSED_ATTR const uint8_t* p_codec_info,
353 UNUSED_ATTR BT_HDR* p_buf,
354 UNUSED_ATTR uint16_t frames_per_packet) {
355 // Nothing to do
356 return true;
357 }
358
A2DP_VendorCodecInfoStringAptxHd(const uint8_t * p_codec_info)359 std::string A2DP_VendorCodecInfoStringAptxHd(const uint8_t* p_codec_info) {
360 std::stringstream res;
361 std::string field;
362 tA2DP_STATUS a2dp_status;
363 tA2DP_APTX_HD_CIE aptx_hd_cie;
364
365 a2dp_status = A2DP_ParseInfoAptxHd(&aptx_hd_cie, p_codec_info, true);
366 if (a2dp_status != A2DP_SUCCESS) {
367 res << "A2DP_ParseInfoAptxHd fail: " << loghex(a2dp_status);
368 return res.str();
369 }
370
371 res << "\tname: aptX-HD\n";
372
373 // Sample frequency
374 field.clear();
375 AppendField(&field, (aptx_hd_cie.sampleRate == 0), "NONE");
376 AppendField(&field, (aptx_hd_cie.sampleRate & A2DP_APTX_HD_SAMPLERATE_44100),
377 "44100");
378 AppendField(&field, (aptx_hd_cie.sampleRate & A2DP_APTX_HD_SAMPLERATE_48000),
379 "48000");
380 res << "\tsamp_freq: " << field << " (" << loghex(aptx_hd_cie.sampleRate)
381 << ")\n";
382
383 // Channel mode
384 field.clear();
385 AppendField(&field, (aptx_hd_cie.channelMode == 0), "NONE");
386 AppendField(&field, (aptx_hd_cie.channelMode & A2DP_APTX_HD_CHANNELS_MONO),
387 "Mono");
388 AppendField(&field, (aptx_hd_cie.channelMode & A2DP_APTX_HD_CHANNELS_STEREO),
389 "Stereo");
390 res << "\tch_mode: " << field << " (" << loghex(aptx_hd_cie.channelMode)
391 << ")\n";
392
393 return res.str();
394 }
395
A2DP_VendorGetEncoderInterfaceAptxHd(const uint8_t * p_codec_info)396 const tA2DP_ENCODER_INTERFACE* A2DP_VendorGetEncoderInterfaceAptxHd(
397 const uint8_t* p_codec_info) {
398 if (!A2DP_IsVendorSourceCodecValidAptxHd(p_codec_info)) return NULL;
399
400 return &a2dp_encoder_interface_aptx_hd;
401 }
402
A2DP_VendorAdjustCodecAptxHd(uint8_t * p_codec_info)403 bool A2DP_VendorAdjustCodecAptxHd(uint8_t* p_codec_info) {
404 tA2DP_APTX_HD_CIE cfg_cie;
405
406 // Nothing to do: just verify the codec info is valid
407 if (A2DP_ParseInfoAptxHd(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS)
408 return false;
409
410 return true;
411 }
412
A2DP_VendorSourceCodecIndexAptxHd(const uint8_t * p_codec_info)413 btav_a2dp_codec_index_t A2DP_VendorSourceCodecIndexAptxHd(
414 const uint8_t* p_codec_info) {
415 return BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD;
416 }
417
A2DP_VendorCodecIndexStrAptxHd(void)418 const char* A2DP_VendorCodecIndexStrAptxHd(void) { return "aptX-HD"; }
419
A2DP_VendorInitCodecConfigAptxHd(AvdtpSepConfig * p_cfg)420 bool A2DP_VendorInitCodecConfigAptxHd(AvdtpSepConfig* p_cfg) {
421 if (A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &a2dp_aptx_hd_source_caps,
422 p_cfg->codec_info) != A2DP_SUCCESS) {
423 return false;
424 }
425
426 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
427 /* Content protection info - support SCMS-T */
428 uint8_t* p = p_cfg->protect_info;
429 *p++ = AVDT_CP_LOSC;
430 UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID);
431 p_cfg->num_protect = 1;
432 #endif
433
434 return true;
435 }
436
A2dpCodecConfigAptxHd(btav_a2dp_codec_priority_t codec_priority)437 A2dpCodecConfigAptxHd::A2dpCodecConfigAptxHd(
438 btav_a2dp_codec_priority_t codec_priority)
439 : A2dpCodecConfig(BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD, "aptX-HD",
440 codec_priority) {
441 // Compute the local capability
442 if (a2dp_aptx_hd_source_caps.sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
443 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
444 }
445 if (a2dp_aptx_hd_source_caps.sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
446 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
447 }
448 codec_local_capability_.bits_per_sample =
449 a2dp_aptx_hd_source_caps.bits_per_sample;
450 if (a2dp_aptx_hd_source_caps.channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
451 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
452 }
453 if (a2dp_aptx_hd_source_caps.channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
454 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
455 }
456 }
457
~A2dpCodecConfigAptxHd()458 A2dpCodecConfigAptxHd::~A2dpCodecConfigAptxHd() {}
459
init()460 bool A2dpCodecConfigAptxHd::init() {
461 if (!isValid()) return false;
462
463 // Load the encoder
464 if (!A2DP_VendorLoadEncoderAptxHd()) {
465 LOG_ERROR(LOG_TAG, "%s: cannot load the encoder", __func__);
466 return false;
467 }
468
469 return true;
470 }
471
useRtpHeaderMarkerBit() const472 bool A2dpCodecConfigAptxHd::useRtpHeaderMarkerBit() const { return false; }
473
474 //
475 // Selects the best sample rate from |sampleRate|.
476 // The result is stored in |p_result| and p_codec_config|.
477 // Returns true if a selection was made, otherwise false.
478 //
select_best_sample_rate(uint8_t sampleRate,tA2DP_APTX_HD_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)479 static bool select_best_sample_rate(uint8_t sampleRate,
480 tA2DP_APTX_HD_CIE* p_result,
481 btav_a2dp_codec_config_t* p_codec_config) {
482 if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
483 p_result->sampleRate = A2DP_APTX_HD_SAMPLERATE_48000;
484 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
485 return true;
486 }
487 if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
488 p_result->sampleRate = A2DP_APTX_HD_SAMPLERATE_44100;
489 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
490 return true;
491 }
492 return false;
493 }
494
495 //
496 // Selects the audio sample rate from |p_codec_audio_config|.
497 // |sampleRate| contains the capability.
498 // The result is stored in |p_result| and |p_codec_config|.
499 // Returns true if a selection was made, otherwise false.
500 //
select_audio_sample_rate(const btav_a2dp_codec_config_t * p_codec_audio_config,uint8_t sampleRate,tA2DP_APTX_HD_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)501 static bool select_audio_sample_rate(
502 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t sampleRate,
503 tA2DP_APTX_HD_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
504 switch (p_codec_audio_config->sample_rate) {
505 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
506 if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
507 p_result->sampleRate = A2DP_APTX_HD_SAMPLERATE_44100;
508 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
509 return true;
510 }
511 break;
512 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
513 if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
514 p_result->sampleRate = A2DP_APTX_HD_SAMPLERATE_48000;
515 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
516 return true;
517 }
518 break;
519 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
520 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
521 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
522 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
523 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
524 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
525 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
526 break;
527 }
528 return false;
529 }
530
531 //
532 // Selects the best bits per sample.
533 // The result is stored in |p_codec_config|.
534 // Returns true if a selection was made, otherwise false.
535 //
select_best_bits_per_sample(btav_a2dp_codec_config_t * p_codec_config)536 static bool select_best_bits_per_sample(
537 btav_a2dp_codec_config_t* p_codec_config) {
538 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
539 return true;
540 }
541
542 //
543 // Selects the audio bits per sample from |p_codec_audio_config|.
544 // The result is stored in |p_codec_config|.
545 // Returns true if a selection was made, otherwise false.
546 //
select_audio_bits_per_sample(const btav_a2dp_codec_config_t * p_codec_audio_config,btav_a2dp_codec_config_t * p_codec_config)547 static bool select_audio_bits_per_sample(
548 const btav_a2dp_codec_config_t* p_codec_audio_config,
549 btav_a2dp_codec_config_t* p_codec_config) {
550 switch (p_codec_audio_config->bits_per_sample) {
551 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
552 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
553 return true;
554 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
555 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
556 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
557 break;
558 }
559 return false;
560 }
561
562 //
563 // Selects the best channel mode from |channelMode|.
564 // The result is stored in |p_result| and |p_codec_config|.
565 // Returns true if a selection was made, otherwise false.
566 //
select_best_channel_mode(uint8_t channelMode,tA2DP_APTX_HD_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)567 static bool select_best_channel_mode(uint8_t channelMode,
568 tA2DP_APTX_HD_CIE* p_result,
569 btav_a2dp_codec_config_t* p_codec_config) {
570 if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
571 p_result->channelMode = A2DP_APTX_HD_CHANNELS_STEREO;
572 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
573 return true;
574 }
575 if (channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
576 p_result->channelMode = A2DP_APTX_HD_CHANNELS_MONO;
577 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
578 return true;
579 }
580 return false;
581 }
582
583 //
584 // Selects the audio channel mode from |p_codec_audio_config|.
585 // |channelMode| contains the capability.
586 // The result is stored in |p_result| and |p_codec_config|.
587 // Returns true if a selection was made, otherwise false.
588 //
select_audio_channel_mode(const btav_a2dp_codec_config_t * p_codec_audio_config,uint8_t channelMode,tA2DP_APTX_HD_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)589 static bool select_audio_channel_mode(
590 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t channelMode,
591 tA2DP_APTX_HD_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
592 switch (p_codec_audio_config->channel_mode) {
593 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
594 if (channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
595 p_result->channelMode = A2DP_APTX_HD_CHANNELS_MONO;
596 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
597 return true;
598 }
599 break;
600 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
601 if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
602 p_result->channelMode = A2DP_APTX_HD_CHANNELS_STEREO;
603 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
604 return true;
605 }
606 break;
607 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
608 break;
609 }
610
611 return false;
612 }
613
setCodecConfig(const uint8_t * p_peer_codec_info,bool is_capability,uint8_t * p_result_codec_config)614 bool A2dpCodecConfigAptxHd::setCodecConfig(const uint8_t* p_peer_codec_info,
615 bool is_capability,
616 uint8_t* p_result_codec_config) {
617 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
618 tA2DP_APTX_HD_CIE peer_info_cie;
619 tA2DP_APTX_HD_CIE result_config_cie;
620 uint8_t sampleRate;
621 uint8_t channelMode;
622
623 // Save the internal state
624 btav_a2dp_codec_config_t saved_codec_config = codec_config_;
625 btav_a2dp_codec_config_t saved_codec_capability = codec_capability_;
626 btav_a2dp_codec_config_t saved_codec_selectable_capability =
627 codec_selectable_capability_;
628 btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_;
629 btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_;
630 uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE];
631 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
632 uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE];
633 memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_));
634 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
635 sizeof(ota_codec_peer_capability_));
636 memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_,
637 sizeof(ota_codec_peer_config_));
638
639 tA2DP_STATUS status =
640 A2DP_ParseInfoAptxHd(&peer_info_cie, p_peer_codec_info, is_capability);
641 if (status != A2DP_SUCCESS) {
642 LOG_ERROR(LOG_TAG, "%s: can't parse peer's capabilities: error = %d",
643 __func__, status);
644 goto fail;
645 }
646
647 //
648 // Build the preferred configuration
649 //
650 memset(&result_config_cie, 0, sizeof(result_config_cie));
651 result_config_cie.vendorId = a2dp_aptx_hd_source_caps.vendorId;
652 result_config_cie.codecId = a2dp_aptx_hd_source_caps.codecId;
653
654 //
655 // Select the sample frequency
656 //
657 sampleRate = a2dp_aptx_hd_source_caps.sampleRate & peer_info_cie.sampleRate;
658 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
659 switch (codec_user_config_.sample_rate) {
660 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
661 if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
662 result_config_cie.sampleRate = A2DP_APTX_HD_SAMPLERATE_44100;
663 codec_capability_.sample_rate = codec_user_config_.sample_rate;
664 codec_config_.sample_rate = codec_user_config_.sample_rate;
665 }
666 break;
667 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
668 if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
669 result_config_cie.sampleRate = A2DP_APTX_HD_SAMPLERATE_48000;
670 codec_capability_.sample_rate = codec_user_config_.sample_rate;
671 codec_config_.sample_rate = codec_user_config_.sample_rate;
672 }
673 break;
674 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
675 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
676 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
677 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
678 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
679 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
680 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
681 codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
682 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
683 break;
684 }
685
686 // Select the sample frequency if there is no user preference
687 do {
688 // Compute the selectable capability
689 if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
690 codec_selectable_capability_.sample_rate |=
691 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
692 }
693 if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
694 codec_selectable_capability_.sample_rate |=
695 BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
696 }
697
698 if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break;
699
700 // Compute the common capability
701 if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100)
702 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
703 if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000)
704 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
705
706 // No user preference - try the codec audio config
707 if (select_audio_sample_rate(&codec_audio_config_, sampleRate,
708 &result_config_cie, &codec_config_)) {
709 break;
710 }
711
712 // No user preference - try the default config
713 if (select_best_sample_rate(
714 a2dp_aptx_hd_default_config.sampleRate & peer_info_cie.sampleRate,
715 &result_config_cie, &codec_config_)) {
716 break;
717 }
718
719 // No user preference - use the best match
720 if (select_best_sample_rate(sampleRate, &result_config_cie,
721 &codec_config_)) {
722 break;
723 }
724 } while (false);
725 if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) {
726 LOG_ERROR(LOG_TAG,
727 "%s: cannot match sample frequency: local caps = 0x%x "
728 "peer info = 0x%x",
729 __func__, a2dp_aptx_hd_source_caps.sampleRate,
730 peer_info_cie.sampleRate);
731 goto fail;
732 }
733
734 //
735 // Select the bits per sample
736 //
737 // NOTE: this information is NOT included in the aptX-HD A2DP codec
738 // description that is sent OTA.
739 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
740 switch (codec_user_config_.bits_per_sample) {
741 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
742 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
743 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
744 break;
745 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
746 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
747 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
748 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
749 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
750 break;
751 }
752
753 // Select the bits per sample if there is no user preference
754 do {
755 // Compute the selectable capability
756 codec_selectable_capability_.bits_per_sample =
757 a2dp_aptx_hd_source_caps.bits_per_sample;
758
759 if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE)
760 break;
761
762 // Compute the common capability
763 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
764
765 // No user preference - try the codec audio config
766 if (select_audio_bits_per_sample(&codec_audio_config_, &codec_config_)) {
767 break;
768 }
769
770 // No user preference - try the default config
771 if (select_best_bits_per_sample(&codec_config_)) {
772 break;
773 }
774
775 // No user preference - use the best match
776 // NOTE: no-op - kept here for consistency
777 if (select_best_bits_per_sample(&codec_config_)) {
778 break;
779 }
780 } while (false);
781 if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) {
782 LOG_ERROR(LOG_TAG,
783 "%s: cannot match bits per sample: user preference = 0x%x",
784 __func__, codec_user_config_.bits_per_sample);
785 goto fail;
786 }
787
788 //
789 // Select the channel mode
790 //
791 channelMode =
792 a2dp_aptx_hd_source_caps.channelMode & peer_info_cie.channelMode;
793 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
794 switch (codec_user_config_.channel_mode) {
795 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
796 if (channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
797 result_config_cie.channelMode = A2DP_APTX_HD_CHANNELS_MONO;
798 codec_capability_.channel_mode = codec_user_config_.channel_mode;
799 codec_config_.channel_mode = codec_user_config_.channel_mode;
800 }
801 break;
802 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
803 if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
804 result_config_cie.channelMode = A2DP_APTX_HD_CHANNELS_STEREO;
805 codec_capability_.channel_mode = codec_user_config_.channel_mode;
806 codec_config_.channel_mode = codec_user_config_.channel_mode;
807 }
808 break;
809 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
810 codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
811 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
812 break;
813 }
814
815 // Select the channel mode if there is no user preference
816 do {
817 // Compute the selectable capability
818 if (channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
819 codec_selectable_capability_.channel_mode |=
820 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
821 }
822 if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
823 codec_selectable_capability_.channel_mode |=
824 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
825 }
826
827 if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break;
828
829 // Compute the common capability
830 if (channelMode & A2DP_APTX_HD_CHANNELS_MONO)
831 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
832 if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO)
833 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
834
835 // No user preference - try the codec audio config
836 if (select_audio_channel_mode(&codec_audio_config_, channelMode,
837 &result_config_cie, &codec_config_)) {
838 break;
839 }
840
841 // No user preference - try the default config
842 if (select_best_channel_mode(
843 a2dp_aptx_hd_default_config.channelMode & peer_info_cie.channelMode,
844 &result_config_cie, &codec_config_)) {
845 break;
846 }
847
848 // No user preference - use the best match
849 if (select_best_channel_mode(channelMode, &result_config_cie,
850 &codec_config_)) {
851 break;
852 }
853 } while (false);
854 if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) {
855 LOG_ERROR(LOG_TAG,
856 "%s: cannot match channel mode: local caps = 0x%x "
857 "peer info = 0x%x",
858 __func__, a2dp_aptx_hd_source_caps.channelMode,
859 peer_info_cie.channelMode);
860 goto fail;
861 }
862
863 //
864 // Set the rest of the fields as bit-wise AND operation
865 //
866 result_config_cie.acl_sprint_reserved0 =
867 a2dp_aptx_hd_source_caps.acl_sprint_reserved0 &
868 peer_info_cie.acl_sprint_reserved0;
869 result_config_cie.acl_sprint_reserved1 =
870 a2dp_aptx_hd_source_caps.acl_sprint_reserved1 &
871 peer_info_cie.acl_sprint_reserved1;
872 result_config_cie.acl_sprint_reserved2 =
873 a2dp_aptx_hd_source_caps.acl_sprint_reserved2 &
874 peer_info_cie.acl_sprint_reserved2;
875 result_config_cie.acl_sprint_reserved3 =
876 a2dp_aptx_hd_source_caps.acl_sprint_reserved3 &
877 peer_info_cie.acl_sprint_reserved3;
878
879 if (A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
880 p_result_codec_config) != A2DP_SUCCESS) {
881 goto fail;
882 }
883
884 //
885 // Copy the codec-specific fields if they are not zero
886 //
887 if (codec_user_config_.codec_specific_1 != 0)
888 codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
889 if (codec_user_config_.codec_specific_2 != 0)
890 codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2;
891 if (codec_user_config_.codec_specific_3 != 0)
892 codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3;
893 if (codec_user_config_.codec_specific_4 != 0)
894 codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4;
895
896 // Create a local copy of the peer codec capability/config, and the
897 // result codec config.
898 if (is_capability) {
899 status = A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
900 ota_codec_peer_capability_);
901 } else {
902 status = A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
903 ota_codec_peer_config_);
904 }
905 CHECK(status == A2DP_SUCCESS);
906 status = A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
907 ota_codec_config_);
908 CHECK(status == A2DP_SUCCESS);
909 return true;
910
911 fail:
912 // Restore the internal state
913 codec_config_ = saved_codec_config;
914 codec_capability_ = saved_codec_capability;
915 codec_selectable_capability_ = saved_codec_selectable_capability;
916 codec_user_config_ = saved_codec_user_config;
917 codec_audio_config_ = saved_codec_audio_config;
918 memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_));
919 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
920 sizeof(ota_codec_peer_capability_));
921 memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config,
922 sizeof(ota_codec_peer_config_));
923 return false;
924 }
925
setPeerCodecCapabilities(const uint8_t * p_peer_codec_capabilities)926 bool A2dpCodecConfigAptxHd::setPeerCodecCapabilities(
927 const uint8_t* p_peer_codec_capabilities) {
928 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
929 tA2DP_APTX_HD_CIE peer_info_cie;
930 uint8_t sampleRate;
931 uint8_t channelMode;
932
933 // Save the internal state
934 btav_a2dp_codec_config_t saved_codec_selectable_capability =
935 codec_selectable_capability_;
936 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
937 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
938 sizeof(ota_codec_peer_capability_));
939
940 tA2DP_STATUS status =
941 A2DP_ParseInfoAptxHd(&peer_info_cie, p_peer_codec_capabilities, true);
942 if (status != A2DP_SUCCESS) {
943 LOG_ERROR(LOG_TAG, "%s: can't parse peer's capabilities: error = %d",
944 __func__, status);
945 goto fail;
946 }
947
948 // Compute the selectable capability - sample rate
949 sampleRate = a2dp_aptx_hd_source_caps.sampleRate & peer_info_cie.sampleRate;
950 if (sampleRate & A2DP_APTX_HD_SAMPLERATE_44100) {
951 codec_selectable_capability_.sample_rate |=
952 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
953 }
954 if (sampleRate & A2DP_APTX_HD_SAMPLERATE_48000) {
955 codec_selectable_capability_.sample_rate |=
956 BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
957 }
958
959 // Compute the selectable capability - bits per sample
960 codec_selectable_capability_.bits_per_sample =
961 a2dp_aptx_hd_source_caps.bits_per_sample;
962
963 // Compute the selectable capability - channel mode
964 channelMode =
965 a2dp_aptx_hd_source_caps.channelMode & peer_info_cie.channelMode;
966 if (channelMode & A2DP_APTX_HD_CHANNELS_MONO) {
967 codec_selectable_capability_.channel_mode |=
968 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
969 }
970 if (channelMode & A2DP_APTX_HD_CHANNELS_STEREO) {
971 codec_selectable_capability_.channel_mode |=
972 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
973 }
974
975 status = A2DP_BuildInfoAptxHd(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
976 ota_codec_peer_capability_);
977 CHECK(status == A2DP_SUCCESS);
978 return true;
979
980 fail:
981 // Restore the internal state
982 codec_selectable_capability_ = saved_codec_selectable_capability;
983 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
984 sizeof(ota_codec_peer_capability_));
985 return false;
986 }
987