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