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