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) {
159 // NOTE: The checks here are very liberal. We should be using more
160 // pedantic checks specific to the SRC or SNK as specified in the spec.
161 if (A2DP_BitsSet(p_ie->sampleRate) == A2DP_SET_ZERO_BIT)
162 return A2DP_BAD_SAMP_FREQ;
163 if (A2DP_BitsSet(p_ie->channelMode) == A2DP_SET_ZERO_BIT)
164 return A2DP_BAD_CH_MODE;
165
166 return A2DP_SUCCESS;
167 }
168
169 if (A2DP_BitsSet(p_ie->sampleRate) != A2DP_SET_ONE_BIT)
170 return A2DP_BAD_SAMP_FREQ;
171 if (A2DP_BitsSet(p_ie->channelMode) != A2DP_SET_ONE_BIT)
172 return A2DP_BAD_CH_MODE;
173
174 return A2DP_SUCCESS;
175 }
176
A2DP_IsVendorSourceCodecValidAptx(const uint8_t * p_codec_info)177 bool A2DP_IsVendorSourceCodecValidAptx(const uint8_t* p_codec_info) {
178 tA2DP_APTX_CIE cfg_cie;
179
180 /* Use a liberal check when parsing the codec info */
181 return (A2DP_ParseInfoAptx(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
182 (A2DP_ParseInfoAptx(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
183 }
184
A2DP_IsVendorPeerSinkCodecValidAptx(const uint8_t * p_codec_info)185 bool A2DP_IsVendorPeerSinkCodecValidAptx(const uint8_t* p_codec_info) {
186 tA2DP_APTX_CIE cfg_cie;
187
188 /* Use a liberal check when parsing the codec info */
189 return (A2DP_ParseInfoAptx(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
190 (A2DP_ParseInfoAptx(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
191 }
192
193 // Checks whether A2DP aptX codec configuration matches with a device's codec
194 // capabilities. |p_cap| is the aptX codec configuration. |p_codec_info| is
195 // the device's codec capabilities.
196 // If |is_capability| is true, the byte sequence is codec capabilities,
197 // otherwise is codec configuration.
198 // |p_codec_info| contains the codec capabilities for a peer device that
199 // is acting as an A2DP source.
200 // Returns A2DP_SUCCESS if the codec configuration matches with capabilities,
201 // otherwise the corresponding A2DP error status code.
A2DP_CodecInfoMatchesCapabilityAptx(const tA2DP_APTX_CIE * p_cap,const uint8_t * p_codec_info,bool is_capability)202 static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityAptx(
203 const tA2DP_APTX_CIE* p_cap, const uint8_t* p_codec_info,
204 bool is_capability) {
205 tA2DP_STATUS status;
206 tA2DP_APTX_CIE cfg_cie;
207
208 /* parse configuration */
209 status = A2DP_ParseInfoAptx(&cfg_cie, p_codec_info, is_capability);
210 if (status != A2DP_SUCCESS) {
211 LOG_ERROR("%s: parsing failed %d", __func__, status);
212 return status;
213 }
214
215 /* verify that each parameter is in range */
216
217 LOG_VERBOSE("%s: FREQ peer: 0x%x, capability 0x%x", __func__,
218 cfg_cie.sampleRate, p_cap->sampleRate);
219 LOG_VERBOSE("%s: CH_MODE peer: 0x%x, capability 0x%x", __func__,
220 cfg_cie.channelMode, p_cap->channelMode);
221
222 /* sampling frequency */
223 if ((cfg_cie.sampleRate & p_cap->sampleRate) == 0) return A2DP_NS_SAMP_FREQ;
224
225 /* channel mode */
226 if ((cfg_cie.channelMode & p_cap->channelMode) == 0) return A2DP_NS_CH_MODE;
227
228 return A2DP_SUCCESS;
229 }
230
A2DP_VendorUsesRtpHeaderAptx(UNUSED_ATTR bool content_protection_enabled,UNUSED_ATTR const uint8_t * p_codec_info)231 bool A2DP_VendorUsesRtpHeaderAptx(UNUSED_ATTR bool content_protection_enabled,
232 UNUSED_ATTR const uint8_t* p_codec_info) {
233 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
234 return true;
235 #else
236 // no RTP header for aptX classic and no Copy Protection byte
237 return false;
238 #endif
239 }
240
A2DP_VendorCodecNameAptx(UNUSED_ATTR const uint8_t * p_codec_info)241 const char* A2DP_VendorCodecNameAptx(UNUSED_ATTR const uint8_t* p_codec_info) {
242 return "aptX";
243 }
244
A2DP_VendorCodecTypeEqualsAptx(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)245 bool A2DP_VendorCodecTypeEqualsAptx(const uint8_t* p_codec_info_a,
246 const uint8_t* p_codec_info_b) {
247 tA2DP_APTX_CIE aptx_cie_a;
248 tA2DP_APTX_CIE aptx_cie_b;
249
250 // Check whether the codec info contains valid data
251 tA2DP_STATUS a2dp_status =
252 A2DP_ParseInfoAptx(&aptx_cie_a, p_codec_info_a, true);
253 if (a2dp_status != A2DP_SUCCESS) {
254 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
255 return false;
256 }
257 a2dp_status = A2DP_ParseInfoAptx(&aptx_cie_b, p_codec_info_b, true);
258 if (a2dp_status != A2DP_SUCCESS) {
259 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
260 return false;
261 }
262
263 return true;
264 }
265
A2DP_VendorCodecEqualsAptx(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)266 bool A2DP_VendorCodecEqualsAptx(const uint8_t* p_codec_info_a,
267 const uint8_t* p_codec_info_b) {
268 tA2DP_APTX_CIE aptx_cie_a;
269 tA2DP_APTX_CIE aptx_cie_b;
270
271 // Check whether the codec info contains valid data
272 tA2DP_STATUS a2dp_status =
273 A2DP_ParseInfoAptx(&aptx_cie_a, p_codec_info_a, true);
274 if (a2dp_status != A2DP_SUCCESS) {
275 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
276 return false;
277 }
278 a2dp_status = A2DP_ParseInfoAptx(&aptx_cie_b, p_codec_info_b, true);
279 if (a2dp_status != A2DP_SUCCESS) {
280 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
281 return false;
282 }
283
284 return (aptx_cie_a.sampleRate == aptx_cie_b.sampleRate) &&
285 (aptx_cie_a.channelMode == aptx_cie_b.channelMode);
286 }
287
A2DP_VendorGetBitRateAptx(const uint8_t * p_codec_info)288 int A2DP_VendorGetBitRateAptx(const uint8_t* p_codec_info) {
289 A2dpCodecConfig* CodecConfig = bta_av_get_a2dp_current_codec();
290 tA2DP_BITS_PER_SAMPLE bits_per_sample = CodecConfig->getAudioBitsPerSample();
291 uint16_t samplerate = A2DP_GetTrackSampleRate(p_codec_info);
292 return (samplerate * bits_per_sample * 2) / 4;
293 }
294
A2DP_VendorGetTrackSampleRateAptx(const uint8_t * p_codec_info)295 int A2DP_VendorGetTrackSampleRateAptx(const uint8_t* p_codec_info) {
296 tA2DP_APTX_CIE aptx_cie;
297
298 // Check whether the codec info contains valid data
299 tA2DP_STATUS a2dp_status = A2DP_ParseInfoAptx(&aptx_cie, p_codec_info, false);
300 if (a2dp_status != A2DP_SUCCESS) {
301 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
302 return -1;
303 }
304
305 if (aptx_cie.sampleRate == A2DP_APTX_SAMPLERATE_44100) return 44100;
306 if (aptx_cie.sampleRate == A2DP_APTX_SAMPLERATE_48000) return 48000;
307
308 return -1;
309 }
310
A2DP_VendorGetTrackBitsPerSampleAptx(const uint8_t * p_codec_info)311 int A2DP_VendorGetTrackBitsPerSampleAptx(const uint8_t* p_codec_info) {
312 tA2DP_APTX_CIE aptx_cie;
313
314 // Check whether the codec info contains valid data
315 tA2DP_STATUS a2dp_status = A2DP_ParseInfoAptx(&aptx_cie, p_codec_info, false);
316 if (a2dp_status != A2DP_SUCCESS) {
317 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
318 return -1;
319 }
320
321 // NOTE: The bits per sample never changes for aptX
322 return 16;
323 }
324
A2DP_VendorGetTrackChannelCountAptx(const uint8_t * p_codec_info)325 int A2DP_VendorGetTrackChannelCountAptx(const uint8_t* p_codec_info) {
326 tA2DP_APTX_CIE aptx_cie;
327
328 // Check whether the codec info contains valid data
329 tA2DP_STATUS a2dp_status = A2DP_ParseInfoAptx(&aptx_cie, p_codec_info, false);
330 if (a2dp_status != A2DP_SUCCESS) {
331 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
332 return -1;
333 }
334
335 switch (aptx_cie.channelMode) {
336 case A2DP_APTX_CHANNELS_MONO:
337 return 1;
338 case A2DP_APTX_CHANNELS_STEREO:
339 return 2;
340 }
341
342 return -1;
343 }
344
A2DP_VendorGetPacketTimestampAptx(UNUSED_ATTR const uint8_t * p_codec_info,const uint8_t * p_data,uint32_t * p_timestamp)345 bool A2DP_VendorGetPacketTimestampAptx(UNUSED_ATTR const uint8_t* p_codec_info,
346 const uint8_t* p_data,
347 uint32_t* p_timestamp) {
348 // TODO: Is this function really codec-specific?
349 *p_timestamp = *(const uint32_t*)p_data;
350 return true;
351 }
352
A2DP_VendorBuildCodecHeaderAptx(UNUSED_ATTR const uint8_t * p_codec_info,UNUSED_ATTR BT_HDR * p_buf,UNUSED_ATTR uint16_t frames_per_packet)353 bool A2DP_VendorBuildCodecHeaderAptx(UNUSED_ATTR const uint8_t* p_codec_info,
354 UNUSED_ATTR BT_HDR* p_buf,
355 UNUSED_ATTR uint16_t frames_per_packet) {
356 // Nothing to do
357 return true;
358 }
359
A2DP_VendorCodecInfoStringAptx(const uint8_t * p_codec_info)360 std::string A2DP_VendorCodecInfoStringAptx(const uint8_t* p_codec_info) {
361 std::stringstream res;
362 std::string field;
363 tA2DP_STATUS a2dp_status;
364 tA2DP_APTX_CIE aptx_cie;
365
366 a2dp_status = A2DP_ParseInfoAptx(&aptx_cie, p_codec_info, true);
367 if (a2dp_status != A2DP_SUCCESS) {
368 res << "A2DP_ParseInfoAptx fail: " << loghex(a2dp_status);
369 return res.str();
370 }
371
372 res << "\tname: aptX\n";
373
374 // Sample frequency
375 field.clear();
376 AppendField(&field, (aptx_cie.sampleRate == 0), "NONE");
377 AppendField(&field, (aptx_cie.sampleRate & A2DP_APTX_SAMPLERATE_44100),
378 "44100");
379 AppendField(&field, (aptx_cie.sampleRate & A2DP_APTX_SAMPLERATE_48000),
380 "48000");
381 res << "\tsamp_freq: " << field << " (" << loghex(aptx_cie.sampleRate)
382 << ")\n";
383
384 // Channel mode
385 field.clear();
386 AppendField(&field, (aptx_cie.channelMode == 0), "NONE");
387 AppendField(&field, (aptx_cie.channelMode & A2DP_APTX_CHANNELS_MONO), "Mono");
388 AppendField(&field, (aptx_cie.channelMode & A2DP_APTX_CHANNELS_STEREO),
389 "Stereo");
390 res << "\tch_mode: " << field << " (" << loghex(aptx_cie.channelMode)
391 << ")\n";
392
393 return res.str();
394 }
395
A2DP_VendorGetEncoderInterfaceAptx(const uint8_t * p_codec_info)396 const tA2DP_ENCODER_INTERFACE* A2DP_VendorGetEncoderInterfaceAptx(
397 const uint8_t* p_codec_info) {
398 if (!A2DP_IsVendorSourceCodecValidAptx(p_codec_info)) return NULL;
399
400 return &a2dp_encoder_interface_aptx;
401 }
402
A2DP_VendorAdjustCodecAptx(uint8_t * p_codec_info)403 bool A2DP_VendorAdjustCodecAptx(uint8_t* p_codec_info) {
404 tA2DP_APTX_CIE cfg_cie;
405
406 // Nothing to do: just verify the codec info is valid
407 if (A2DP_ParseInfoAptx(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS)
408 return false;
409
410 return true;
411 }
412
A2DP_VendorSourceCodecIndexAptx(UNUSED_ATTR const uint8_t * p_codec_info)413 btav_a2dp_codec_index_t A2DP_VendorSourceCodecIndexAptx(
414 UNUSED_ATTR const uint8_t* p_codec_info) {
415 return BTAV_A2DP_CODEC_INDEX_SOURCE_APTX;
416 }
417
A2DP_VendorCodecIndexStrAptx(void)418 const char* A2DP_VendorCodecIndexStrAptx(void) { return "aptX"; }
419
A2DP_VendorInitCodecConfigAptx(AvdtpSepConfig * p_cfg)420 bool A2DP_VendorInitCodecConfigAptx(AvdtpSepConfig* p_cfg) {
421 if (A2DP_BuildInfoAptx(AVDT_MEDIA_TYPE_AUDIO, &a2dp_aptx_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
A2dpCodecConfigAptx(btav_a2dp_codec_priority_t codec_priority)437 A2dpCodecConfigAptx::A2dpCodecConfigAptx(
438 btav_a2dp_codec_priority_t codec_priority)
439 : A2dpCodecConfig(BTAV_A2DP_CODEC_INDEX_SOURCE_APTX,
440 A2DP_VendorCodecIndexStrAptx(), codec_priority) {
441 // Compute the local capability
442 if (a2dp_aptx_source_caps.sampleRate & A2DP_APTX_SAMPLERATE_44100) {
443 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
444 }
445 if (a2dp_aptx_source_caps.sampleRate & A2DP_APTX_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_source_caps.bits_per_sample;
450 if (a2dp_aptx_source_caps.channelMode & A2DP_APTX_CHANNELS_MONO) {
451 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
452 }
453 if (a2dp_aptx_source_caps.channelMode & A2DP_APTX_CHANNELS_STEREO) {
454 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
455 }
456 }
457
~A2dpCodecConfigAptx()458 A2dpCodecConfigAptx::~A2dpCodecConfigAptx() {}
459
init()460 bool A2dpCodecConfigAptx::init() {
461 if (!isValid()) return false;
462
463 // Load the encoder
464 if (!A2DP_VendorLoadEncoderAptx()) {
465 LOG_ERROR("%s: cannot load the encoder", __func__);
466 return false;
467 }
468
469 return true;
470 }
471
useRtpHeaderMarkerBit() const472 bool A2dpCodecConfigAptx::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_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)479 static bool select_best_sample_rate(uint8_t sampleRate,
480 tA2DP_APTX_CIE* p_result,
481 btav_a2dp_codec_config_t* p_codec_config) {
482 if (sampleRate & A2DP_APTX_SAMPLERATE_48000) {
483 p_result->sampleRate = A2DP_APTX_SAMPLERATE_48000;
484 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
485 return true;
486 }
487 if (sampleRate & A2DP_APTX_SAMPLERATE_44100) {
488 p_result->sampleRate = A2DP_APTX_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_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_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_SAMPLERATE_44100) {
507 p_result->sampleRate = A2DP_APTX_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_SAMPLERATE_48000) {
514 p_result->sampleRate = A2DP_APTX_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_16;
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_16:
552 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
553 return true;
554 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
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_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)567 static bool select_best_channel_mode(uint8_t channelMode,
568 tA2DP_APTX_CIE* p_result,
569 btav_a2dp_codec_config_t* p_codec_config) {
570 if (channelMode & A2DP_APTX_CHANNELS_STEREO) {
571 p_result->channelMode = A2DP_APTX_CHANNELS_STEREO;
572 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
573 return true;
574 }
575 if (channelMode & A2DP_APTX_CHANNELS_MONO) {
576 p_result->channelMode = A2DP_APTX_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_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_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_CHANNELS_MONO) {
595 p_result->channelMode = A2DP_APTX_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_CHANNELS_STEREO) {
602 p_result->channelMode = A2DP_APTX_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 A2dpCodecConfigAptx::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_CIE peer_info_cie;
619 tA2DP_APTX_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_ParseInfoAptx(&peer_info_cie, p_peer_codec_info, is_capability);
641 if (status != A2DP_SUCCESS) {
642 LOG_ERROR("%s: can't parse peer's capabilities: error = %d", __func__,
643 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_source_caps.vendorId;
652 result_config_cie.codecId = a2dp_aptx_source_caps.codecId;
653
654 //
655 // Select the sample frequency
656 //
657 sampleRate = a2dp_aptx_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_SAMPLERATE_44100) {
662 result_config_cie.sampleRate = A2DP_APTX_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_SAMPLERATE_48000) {
669 result_config_cie.sampleRate = A2DP_APTX_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_SAMPLERATE_44100) {
690 codec_selectable_capability_.sample_rate |=
691 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
692 }
693 if (sampleRate & A2DP_APTX_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_SAMPLERATE_44100)
702 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
703 if (sampleRate & A2DP_APTX_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_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(
727 "%s: cannot match sample frequency: local caps = 0x%x "
728 "peer info = 0x%x",
729 __func__, a2dp_aptx_source_caps.sampleRate, peer_info_cie.sampleRate);
730 goto fail;
731 }
732
733 //
734 // Select the bits per sample
735 //
736 // NOTE: this information is NOT included in the aptX A2DP codec
737 // description that is sent OTA.
738 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
739 switch (codec_user_config_.bits_per_sample) {
740 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
741 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
742 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
743 break;
744 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
745 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
746 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
747 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
748 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
749 break;
750 }
751
752 // Select the bits per sample if there is no user preference
753 do {
754 // Compute the selectable capability
755 codec_selectable_capability_.bits_per_sample =
756 a2dp_aptx_source_caps.bits_per_sample;
757
758 if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE)
759 break;
760
761 // Compute the common capability
762 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
763
764 // No user preference - try the codec audio config
765 if (select_audio_bits_per_sample(&codec_audio_config_, &codec_config_)) {
766 break;
767 }
768
769 // No user preference - try the default config
770 if (select_best_bits_per_sample(&codec_config_)) {
771 break;
772 }
773
774 // No user preference - use the best match
775 // NOTE: no-op - kept here for consistency
776 if (select_best_bits_per_sample(&codec_config_)) {
777 break;
778 }
779 } while (false);
780 if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) {
781 LOG_ERROR("%s: cannot match bits per sample: user preference = 0x%x",
782 __func__, codec_user_config_.bits_per_sample);
783 goto fail;
784 }
785
786 //
787 // Select the channel mode
788 //
789 channelMode = a2dp_aptx_source_caps.channelMode & peer_info_cie.channelMode;
790 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
791 switch (codec_user_config_.channel_mode) {
792 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
793 if (channelMode & A2DP_APTX_CHANNELS_MONO) {
794 result_config_cie.channelMode = A2DP_APTX_CHANNELS_MONO;
795 codec_capability_.channel_mode = codec_user_config_.channel_mode;
796 codec_config_.channel_mode = codec_user_config_.channel_mode;
797 }
798 break;
799 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
800 if (channelMode & A2DP_APTX_CHANNELS_STEREO) {
801 result_config_cie.channelMode = A2DP_APTX_CHANNELS_STEREO;
802 codec_capability_.channel_mode = codec_user_config_.channel_mode;
803 codec_config_.channel_mode = codec_user_config_.channel_mode;
804 }
805 break;
806 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
807 codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
808 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
809 break;
810 }
811
812 // Select the channel mode if there is no user preference
813 do {
814 // Compute the selectable capability
815 if (channelMode & A2DP_APTX_CHANNELS_MONO) {
816 codec_selectable_capability_.channel_mode |=
817 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
818 }
819 if (channelMode & A2DP_APTX_CHANNELS_STEREO) {
820 codec_selectable_capability_.channel_mode |=
821 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
822 }
823
824 if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break;
825
826 // Compute the common capability
827 if (channelMode & A2DP_APTX_CHANNELS_MONO)
828 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
829 if (channelMode & A2DP_APTX_CHANNELS_STEREO)
830 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
831
832 // No user preference - try the codec audio config
833 if (select_audio_channel_mode(&codec_audio_config_, channelMode,
834 &result_config_cie, &codec_config_)) {
835 break;
836 }
837
838 // No user preference - try the default config
839 if (select_best_channel_mode(
840 a2dp_aptx_default_config.channelMode & peer_info_cie.channelMode,
841 &result_config_cie, &codec_config_)) {
842 break;
843 }
844
845 // No user preference - use the best match
846 if (select_best_channel_mode(channelMode, &result_config_cie,
847 &codec_config_)) {
848 break;
849 }
850 } while (false);
851 if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) {
852 LOG_ERROR(
853 "%s: cannot match channel mode: local caps = 0x%x "
854 "peer info = 0x%x",
855 __func__, a2dp_aptx_source_caps.channelMode, peer_info_cie.channelMode);
856 goto fail;
857 }
858
859 //
860 // Set the rest of the fields as bit-wise AND operation
861 //
862 result_config_cie.future1 =
863 a2dp_aptx_source_caps.future1 & peer_info_cie.future1;
864 result_config_cie.future2 =
865 a2dp_aptx_source_caps.future2 & peer_info_cie.future2;
866
867 if (A2DP_BuildInfoAptx(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
868 p_result_codec_config) != A2DP_SUCCESS) {
869 goto fail;
870 }
871
872 //
873 // Copy the codec-specific fields if they are not zero
874 //
875 if (codec_user_config_.codec_specific_1 != 0)
876 codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
877 if (codec_user_config_.codec_specific_2 != 0)
878 codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2;
879 if (codec_user_config_.codec_specific_3 != 0)
880 codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3;
881 if (codec_user_config_.codec_specific_4 != 0)
882 codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4;
883
884 // Create a local copy of the peer codec capability/config, and the
885 // result codec config.
886 if (is_capability) {
887 status = A2DP_BuildInfoAptx(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
888 ota_codec_peer_capability_);
889 } else {
890 status = A2DP_BuildInfoAptx(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
891 ota_codec_peer_config_);
892 }
893 CHECK(status == A2DP_SUCCESS);
894 status = A2DP_BuildInfoAptx(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
895 ota_codec_config_);
896 CHECK(status == A2DP_SUCCESS);
897
898 return true;
899
900 fail:
901 // Restore the internal state
902 codec_config_ = saved_codec_config;
903 codec_capability_ = saved_codec_capability;
904 codec_selectable_capability_ = saved_codec_selectable_capability;
905 codec_user_config_ = saved_codec_user_config;
906 codec_audio_config_ = saved_codec_audio_config;
907 memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_));
908 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
909 sizeof(ota_codec_peer_capability_));
910 memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config,
911 sizeof(ota_codec_peer_config_));
912 return false;
913 }
914
setPeerCodecCapabilities(const uint8_t * p_peer_codec_capabilities)915 bool A2dpCodecConfigAptx::setPeerCodecCapabilities(
916 const uint8_t* p_peer_codec_capabilities) {
917 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
918 tA2DP_APTX_CIE peer_info_cie;
919 uint8_t sampleRate;
920 uint8_t channelMode;
921
922 // Save the internal state
923 btav_a2dp_codec_config_t saved_codec_selectable_capability =
924 codec_selectable_capability_;
925 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
926 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
927 sizeof(ota_codec_peer_capability_));
928
929 tA2DP_STATUS status =
930 A2DP_ParseInfoAptx(&peer_info_cie, p_peer_codec_capabilities, true);
931 if (status != A2DP_SUCCESS) {
932 LOG_ERROR("%s: can't parse peer's capabilities: error = %d", __func__,
933 status);
934 goto fail;
935 }
936
937 // Compute the selectable capability - sample rate
938 sampleRate = a2dp_aptx_source_caps.sampleRate & peer_info_cie.sampleRate;
939 if (sampleRate & A2DP_APTX_SAMPLERATE_44100) {
940 codec_selectable_capability_.sample_rate |=
941 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
942 }
943 if (sampleRate & A2DP_APTX_SAMPLERATE_48000) {
944 codec_selectable_capability_.sample_rate |=
945 BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
946 }
947
948 // Compute the selectable capability - bits per sample
949 codec_selectable_capability_.bits_per_sample =
950 a2dp_aptx_source_caps.bits_per_sample;
951
952 // Compute the selectable capability - channel mode
953 channelMode = a2dp_aptx_source_caps.channelMode & peer_info_cie.channelMode;
954 if (channelMode & A2DP_APTX_CHANNELS_MONO) {
955 codec_selectable_capability_.channel_mode |=
956 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
957 }
958 if (channelMode & A2DP_APTX_CHANNELS_STEREO) {
959 codec_selectable_capability_.channel_mode |=
960 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
961 }
962
963 status = A2DP_BuildInfoAptx(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
964 ota_codec_peer_capability_);
965 CHECK(status == A2DP_SUCCESS);
966 return true;
967
968 fail:
969 // Restore the internal state
970 codec_selectable_capability_ = saved_codec_selectable_capability;
971 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
972 sizeof(ota_codec_peer_capability_));
973 return false;
974 }
975