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 AAC Codec Information
20 * Element and Media Payload.
21 *
22 ******************************************************************************/
23
24 #define LOG_TAG "a2dp_aac"
25
26 #include "bt_target.h"
27
28 #include "a2dp_aac.h"
29
30 #include <string.h>
31
32 #include <base/logging.h>
33 #include "a2dp_aac_decoder.h"
34 #include "a2dp_aac_encoder.h"
35 #include "bt_utils.h"
36 #include "osi/include/log.h"
37 #include "osi/include/osi.h"
38 #include "osi/include/properties.h"
39
40 #define A2DP_AAC_DEFAULT_BITRATE 320000 // 320 kbps
41 #define A2DP_AAC_MIN_BITRATE 64000 // 64 kbps
42
43 // data type for the AAC Codec Information Element */
44 // NOTE: bits_per_sample is needed only for AAC encoder initialization.
45 typedef struct {
46 uint8_t objectType; /* Object Type */
47 uint16_t sampleRate; /* Sampling Frequency */
48 uint8_t channelMode; /* STEREO/MONO */
49 uint8_t variableBitRateSupport; /* Variable Bit Rate Support*/
50 uint32_t bitRate; /* Bit rate */
51 btav_a2dp_codec_bits_per_sample_t bits_per_sample;
52 } tA2DP_AAC_CIE;
53
54 static bool aac_source_caps_configured = false;
55 static tA2DP_AAC_CIE a2dp_aac_source_caps = {};
56
57 /* AAC Source codec capabilities */
58 static const tA2DP_AAC_CIE a2dp_aac_cbr_source_caps = {
59 // objectType
60 A2DP_AAC_OBJECT_TYPE_MPEG2_LC,
61 // sampleRate
62 // TODO: AAC 48.0kHz sampling rate should be added back - see b/62301376
63 A2DP_AAC_SAMPLING_FREQ_44100,
64 // channelMode
65 A2DP_AAC_CHANNEL_MODE_STEREO,
66 // variableBitRateSupport
67 A2DP_AAC_VARIABLE_BIT_RATE_DISABLED,
68 // bitRate
69 A2DP_AAC_DEFAULT_BITRATE,
70 // bits_per_sample
71 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16};
72
73 /* AAC Source codec capabilities */
74 static const tA2DP_AAC_CIE a2dp_aac_vbr_source_caps = {
75 // objectType
76 A2DP_AAC_OBJECT_TYPE_MPEG2_LC,
77 // sampleRate
78 // TODO: AAC 48.0kHz sampling rate should be added back - see b/62301376
79 A2DP_AAC_SAMPLING_FREQ_44100,
80 // channelMode
81 A2DP_AAC_CHANNEL_MODE_STEREO,
82 // variableBitRateSupport
83 A2DP_AAC_VARIABLE_BIT_RATE_ENABLED,
84 // bitRate
85 A2DP_AAC_DEFAULT_BITRATE,
86 // bits_per_sample
87 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16};
88
89 /* AAC Sink codec capabilities */
90 static const tA2DP_AAC_CIE a2dp_aac_sink_caps = {
91 // objectType
92 A2DP_AAC_OBJECT_TYPE_MPEG2_LC,
93 // sampleRate
94 A2DP_AAC_SAMPLING_FREQ_44100 | A2DP_AAC_SAMPLING_FREQ_48000,
95 // channelMode
96 A2DP_AAC_CHANNEL_MODE_MONO | A2DP_AAC_CHANNEL_MODE_STEREO,
97 // variableBitRateSupport
98 A2DP_AAC_VARIABLE_BIT_RATE_ENABLED,
99 // bitRate
100 A2DP_AAC_DEFAULT_BITRATE,
101 // bits_per_sample
102 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16};
103
104 /* Default AAC codec configuration */
105 static const tA2DP_AAC_CIE a2dp_aac_default_config = {
106 A2DP_AAC_OBJECT_TYPE_MPEG2_LC, // objectType
107 A2DP_AAC_SAMPLING_FREQ_44100, // sampleRate
108 A2DP_AAC_CHANNEL_MODE_STEREO, // channelMode
109 A2DP_AAC_VARIABLE_BIT_RATE_DISABLED, // variableBitRateSupport
110 A2DP_AAC_DEFAULT_BITRATE, // bitRate
111 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 // bits_per_sample
112 };
113
114 static const tA2DP_ENCODER_INTERFACE a2dp_encoder_interface_aac = {
115 a2dp_aac_encoder_init,
116 a2dp_aac_encoder_cleanup,
117 a2dp_aac_feeding_reset,
118 a2dp_aac_feeding_flush,
119 a2dp_aac_get_encoder_interval_ms,
120 a2dp_aac_send_frames,
121 nullptr // set_transmit_queue_length
122 };
123
124 static const tA2DP_DECODER_INTERFACE a2dp_decoder_interface_aac = {
125 a2dp_aac_decoder_init,
126 a2dp_aac_decoder_cleanup,
127 a2dp_aac_decoder_decode_packet,
128 nullptr, // decoder_start
129 nullptr, // decoder_suspend
130 nullptr, // decoder_configure
131 };
132
133 UNUSED_ATTR static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityAac(
134 const tA2DP_AAC_CIE* p_cap, const uint8_t* p_codec_info,
135 bool is_capability);
136
137 // Builds the AAC Media Codec Capabilities byte sequence beginning from the
138 // LOSC octet. |media_type| is the media type |AVDT_MEDIA_TYPE_*|.
139 // |p_ie| is a pointer to the AAC Codec Information Element information.
140 // The result is stored in |p_result|. Returns A2DP_SUCCESS on success,
141 // otherwise the corresponding A2DP error status code.
A2DP_BuildInfoAac(uint8_t media_type,const tA2DP_AAC_CIE * p_ie,uint8_t * p_result)142 static tA2DP_STATUS A2DP_BuildInfoAac(uint8_t media_type,
143 const tA2DP_AAC_CIE* p_ie,
144 uint8_t* p_result) {
145 if (p_ie == NULL || p_result == NULL) {
146 return A2DP_INVALID_PARAMS;
147 }
148
149 *p_result++ = A2DP_AAC_CODEC_LEN;
150 *p_result++ = (media_type << 4);
151 *p_result++ = A2DP_MEDIA_CT_AAC;
152
153 // Object Type
154 if (p_ie->objectType == 0) return A2DP_INVALID_PARAMS;
155 *p_result++ = p_ie->objectType;
156
157 // Sampling Frequency
158 if (p_ie->sampleRate == 0) return A2DP_INVALID_PARAMS;
159 *p_result++ = (uint8_t)(p_ie->sampleRate & A2DP_AAC_SAMPLING_FREQ_MASK0);
160 *p_result = (uint8_t)((p_ie->sampleRate & A2DP_AAC_SAMPLING_FREQ_MASK1) >> 8);
161
162 // Channel Mode
163 if (p_ie->channelMode == 0) return A2DP_INVALID_PARAMS;
164 *p_result++ |= (p_ie->channelMode & A2DP_AAC_CHANNEL_MODE_MASK);
165
166 // Variable Bit Rate Support
167 *p_result = (p_ie->variableBitRateSupport & A2DP_AAC_VARIABLE_BIT_RATE_MASK);
168
169 // Bit Rate
170 *p_result++ |= (uint8_t)((p_ie->bitRate & A2DP_AAC_BIT_RATE_MASK0) >> 16);
171 *p_result++ = (uint8_t)((p_ie->bitRate & A2DP_AAC_BIT_RATE_MASK1) >> 8);
172 *p_result++ = (uint8_t)(p_ie->bitRate & A2DP_AAC_BIT_RATE_MASK2);
173
174 return A2DP_SUCCESS;
175 }
176
177 // Parses the AAC Media Codec Capabilities byte sequence beginning from the
178 // LOSC octet. The result is stored in |p_ie|. The byte sequence to parse is
179 // |p_codec_info|. If |is_capability| is true, the byte sequence is
180 // codec capabilities, otherwise is codec configuration.
181 // Returns A2DP_SUCCESS on success, otherwise the corresponding A2DP error
182 // status code.
A2DP_ParseInfoAac(tA2DP_AAC_CIE * p_ie,const uint8_t * p_codec_info,bool is_capability)183 static tA2DP_STATUS A2DP_ParseInfoAac(tA2DP_AAC_CIE* p_ie,
184 const uint8_t* p_codec_info,
185 bool is_capability) {
186 uint8_t losc;
187 uint8_t media_type;
188 tA2DP_CODEC_TYPE codec_type;
189
190 if (p_ie == NULL || p_codec_info == NULL) return A2DP_INVALID_PARAMS;
191
192 // Check the codec capability length
193 losc = *p_codec_info++;
194 if (losc != A2DP_AAC_CODEC_LEN) return A2DP_WRONG_CODEC;
195
196 media_type = (*p_codec_info++) >> 4;
197 codec_type = *p_codec_info++;
198 /* Check the Media Type and Media Codec Type */
199 if (media_type != AVDT_MEDIA_TYPE_AUDIO || codec_type != A2DP_MEDIA_CT_AAC) {
200 return A2DP_WRONG_CODEC;
201 }
202
203 p_ie->objectType = *p_codec_info++;
204 p_ie->sampleRate = (*p_codec_info & A2DP_AAC_SAMPLING_FREQ_MASK0) |
205 (*(p_codec_info + 1) << 8 & A2DP_AAC_SAMPLING_FREQ_MASK1);
206 p_codec_info++;
207 p_ie->channelMode = *p_codec_info & A2DP_AAC_CHANNEL_MODE_MASK;
208 p_codec_info++;
209
210 p_ie->variableBitRateSupport =
211 *p_codec_info & A2DP_AAC_VARIABLE_BIT_RATE_MASK;
212
213 p_ie->bitRate = ((*p_codec_info) << 16 & A2DP_AAC_BIT_RATE_MASK0) |
214 (*(p_codec_info + 1) << 8 & A2DP_AAC_BIT_RATE_MASK1) |
215 (*(p_codec_info + 2) & A2DP_AAC_BIT_RATE_MASK2);
216 p_codec_info += 3;
217
218 if (is_capability) {
219 // NOTE: The checks here are very liberal. We should be using more
220 // pedantic checks specific to the SRC or SNK as specified in the spec.
221 if (A2DP_BitsSet(p_ie->objectType) == A2DP_SET_ZERO_BIT)
222 return A2DP_BAD_OBJ_TYPE;
223 if (A2DP_BitsSet(p_ie->sampleRate) == A2DP_SET_ZERO_BIT)
224 return A2DP_BAD_SAMP_FREQ;
225 if (A2DP_BitsSet(p_ie->channelMode) == A2DP_SET_ZERO_BIT)
226 return A2DP_BAD_CH_MODE;
227
228 return A2DP_SUCCESS;
229 }
230
231 if (A2DP_BitsSet(p_ie->objectType) != A2DP_SET_ONE_BIT)
232 return A2DP_BAD_OBJ_TYPE;
233 if (A2DP_BitsSet(p_ie->sampleRate) != A2DP_SET_ONE_BIT)
234 return A2DP_BAD_SAMP_FREQ;
235 if (A2DP_BitsSet(p_ie->channelMode) != A2DP_SET_ONE_BIT)
236 return A2DP_BAD_CH_MODE;
237
238 return A2DP_SUCCESS;
239 }
240
A2DP_IsSourceCodecValidAac(const uint8_t * p_codec_info)241 bool A2DP_IsSourceCodecValidAac(const uint8_t* p_codec_info) {
242 tA2DP_AAC_CIE cfg_cie;
243
244 /* Use a liberal check when parsing the codec info */
245 return (A2DP_ParseInfoAac(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
246 (A2DP_ParseInfoAac(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
247 }
248
A2DP_IsSinkCodecValidAac(UNUSED_ATTR const uint8_t * p_codec_info)249 bool A2DP_IsSinkCodecValidAac(UNUSED_ATTR const uint8_t* p_codec_info) {
250 tA2DP_AAC_CIE cfg_cie;
251
252 /* Use a liberal check when parsing the codec info */
253 return (A2DP_ParseInfoAac(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
254 (A2DP_ParseInfoAac(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
255 }
256
A2DP_IsPeerSourceCodecValidAac(UNUSED_ATTR const uint8_t * p_codec_info)257 bool A2DP_IsPeerSourceCodecValidAac(UNUSED_ATTR const uint8_t* p_codec_info) {
258 tA2DP_AAC_CIE cfg_cie;
259
260 /* Use a liberal check when parsing the codec info */
261 return (A2DP_ParseInfoAac(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
262 (A2DP_ParseInfoAac(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
263 }
264
A2DP_IsPeerSinkCodecValidAac(const uint8_t * p_codec_info)265 bool A2DP_IsPeerSinkCodecValidAac(const uint8_t* p_codec_info) {
266 tA2DP_AAC_CIE cfg_cie;
267
268 /* Use a liberal check when parsing the codec info */
269 return (A2DP_ParseInfoAac(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
270 (A2DP_ParseInfoAac(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
271 }
272
A2DP_IsSinkCodecSupportedAac(const uint8_t * p_codec_info)273 bool A2DP_IsSinkCodecSupportedAac(const uint8_t* p_codec_info) {
274 return A2DP_CodecInfoMatchesCapabilityAac(&a2dp_aac_sink_caps, p_codec_info,
275 false) == A2DP_SUCCESS;
276 }
277
A2DP_IsPeerSourceCodecSupportedAac(const uint8_t * p_codec_info)278 bool A2DP_IsPeerSourceCodecSupportedAac(const uint8_t* p_codec_info) {
279 return A2DP_CodecInfoMatchesCapabilityAac(&a2dp_aac_sink_caps, p_codec_info,
280 true) == A2DP_SUCCESS;
281 }
282
283 // Checks whether A2DP AAC codec configuration matches with a device's codec
284 // capabilities. |p_cap| is the AAC codec configuration. |p_codec_info| is
285 // the device's codec capabilities. |is_capability| is true if
286 // |p_codec_info| contains A2DP codec capability.
287 // Returns A2DP_SUCCESS if the codec configuration matches with capabilities,
288 // otherwise the corresponding A2DP error status code.
A2DP_CodecInfoMatchesCapabilityAac(const tA2DP_AAC_CIE * p_cap,const uint8_t * p_codec_info,bool is_capability)289 static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityAac(
290 const tA2DP_AAC_CIE* p_cap, const uint8_t* p_codec_info,
291 bool is_capability) {
292 tA2DP_STATUS status;
293 tA2DP_AAC_CIE cfg_cie;
294
295 /* parse configuration */
296 status = A2DP_ParseInfoAac(&cfg_cie, p_codec_info, is_capability);
297 if (status != A2DP_SUCCESS) {
298 LOG_ERROR(LOG_TAG, "%s: parsing failed %d", __func__, status);
299 return status;
300 }
301
302 /* verify that each parameter is in range */
303
304 LOG_VERBOSE(LOG_TAG, "%s: Object Type peer: 0x%x, capability 0x%x", __func__,
305 cfg_cie.objectType, p_cap->objectType);
306 LOG_VERBOSE(LOG_TAG, "%s: Sample Rate peer: %u, capability %u", __func__,
307 cfg_cie.sampleRate, p_cap->sampleRate);
308 LOG_VERBOSE(LOG_TAG, "%s: Channel Mode peer: 0x%x, capability 0x%x", __func__,
309 cfg_cie.channelMode, p_cap->channelMode);
310 LOG_VERBOSE(
311 LOG_TAG, "%s: Variable Bit Rate Support peer: 0x%x, capability 0x%x",
312 __func__, cfg_cie.variableBitRateSupport, p_cap->variableBitRateSupport);
313 LOG_VERBOSE(LOG_TAG, "%s: Bit Rate peer: %u, capability %u", __func__,
314 cfg_cie.bitRate, p_cap->bitRate);
315
316 /* Object Type */
317 if ((cfg_cie.objectType & p_cap->objectType) == 0) return A2DP_BAD_OBJ_TYPE;
318
319 /* Sample Rate */
320 if ((cfg_cie.sampleRate & p_cap->sampleRate) == 0) return A2DP_BAD_SAMP_FREQ;
321
322 /* Channel Mode */
323 if ((cfg_cie.channelMode & p_cap->channelMode) == 0) return A2DP_NS_CH_MODE;
324
325 return A2DP_SUCCESS;
326 }
327
A2DP_UsesRtpHeaderAac(UNUSED_ATTR bool content_protection_enabled,UNUSED_ATTR const uint8_t * p_codec_info)328 bool A2DP_UsesRtpHeaderAac(UNUSED_ATTR bool content_protection_enabled,
329 UNUSED_ATTR const uint8_t* p_codec_info) {
330 return true;
331 }
332
A2DP_CodecNameAac(UNUSED_ATTR const uint8_t * p_codec_info)333 const char* A2DP_CodecNameAac(UNUSED_ATTR const uint8_t* p_codec_info) {
334 return "AAC";
335 }
336
A2DP_CodecTypeEqualsAac(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)337 bool A2DP_CodecTypeEqualsAac(const uint8_t* p_codec_info_a,
338 const uint8_t* p_codec_info_b) {
339 tA2DP_AAC_CIE aac_cie_a;
340 tA2DP_AAC_CIE aac_cie_b;
341
342 // Check whether the codec info contains valid data
343 tA2DP_STATUS a2dp_status =
344 A2DP_ParseInfoAac(&aac_cie_a, p_codec_info_a, true);
345 if (a2dp_status != A2DP_SUCCESS) {
346 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
347 a2dp_status);
348 return false;
349 }
350 a2dp_status = A2DP_ParseInfoAac(&aac_cie_b, p_codec_info_b, true);
351 if (a2dp_status != A2DP_SUCCESS) {
352 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
353 a2dp_status);
354 return false;
355 }
356
357 return true;
358 }
359
A2DP_CodecEqualsAac(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)360 bool A2DP_CodecEqualsAac(const uint8_t* p_codec_info_a,
361 const uint8_t* p_codec_info_b) {
362 tA2DP_AAC_CIE aac_cie_a;
363 tA2DP_AAC_CIE aac_cie_b;
364
365 // Check whether the codec info contains valid data
366 tA2DP_STATUS a2dp_status =
367 A2DP_ParseInfoAac(&aac_cie_a, p_codec_info_a, true);
368 if (a2dp_status != A2DP_SUCCESS) {
369 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
370 a2dp_status);
371 return false;
372 }
373 a2dp_status = A2DP_ParseInfoAac(&aac_cie_b, p_codec_info_b, true);
374 if (a2dp_status != A2DP_SUCCESS) {
375 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
376 a2dp_status);
377 return false;
378 }
379
380 return (aac_cie_a.objectType == aac_cie_b.objectType) &&
381 (aac_cie_a.sampleRate == aac_cie_b.sampleRate) &&
382 (aac_cie_a.channelMode == aac_cie_b.channelMode) &&
383 (aac_cie_a.variableBitRateSupport ==
384 aac_cie_b.variableBitRateSupport) &&
385 (aac_cie_a.bitRate == aac_cie_b.bitRate);
386 }
387
A2DP_GetTrackSampleRateAac(const uint8_t * p_codec_info)388 int A2DP_GetTrackSampleRateAac(const uint8_t* p_codec_info) {
389 tA2DP_AAC_CIE aac_cie;
390
391 // Check whether the codec info contains valid data
392 tA2DP_STATUS a2dp_status = A2DP_ParseInfoAac(&aac_cie, p_codec_info, false);
393 if (a2dp_status != A2DP_SUCCESS) {
394 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
395 a2dp_status);
396 return -1;
397 }
398
399 switch (aac_cie.sampleRate) {
400 case A2DP_AAC_SAMPLING_FREQ_8000:
401 return 8000;
402 case A2DP_AAC_SAMPLING_FREQ_11025:
403 return 11025;
404 case A2DP_AAC_SAMPLING_FREQ_12000:
405 return 12000;
406 case A2DP_AAC_SAMPLING_FREQ_16000:
407 return 16000;
408 case A2DP_AAC_SAMPLING_FREQ_22050:
409 return 22050;
410 case A2DP_AAC_SAMPLING_FREQ_24000:
411 return 24000;
412 case A2DP_AAC_SAMPLING_FREQ_32000:
413 return 32000;
414 case A2DP_AAC_SAMPLING_FREQ_44100:
415 return 44100;
416 case A2DP_AAC_SAMPLING_FREQ_48000:
417 return 48000;
418 case A2DP_AAC_SAMPLING_FREQ_64000:
419 return 64000;
420 case A2DP_AAC_SAMPLING_FREQ_88200:
421 return 88200;
422 case A2DP_AAC_SAMPLING_FREQ_96000:
423 return 96000;
424 }
425
426 return -1;
427 }
428
A2DP_GetTrackBitsPerSampleAac(const uint8_t * p_codec_info)429 int A2DP_GetTrackBitsPerSampleAac(const uint8_t* p_codec_info) {
430 tA2DP_AAC_CIE aac_cie;
431
432 // Check whether the codec info contains valid data
433 tA2DP_STATUS a2dp_status = A2DP_ParseInfoAac(&aac_cie, p_codec_info, false);
434 if (a2dp_status != A2DP_SUCCESS) {
435 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
436 a2dp_status);
437 return -1;
438 }
439
440 // NOTE: The bits per sample never changes for AAC
441 return 16;
442 }
443
A2DP_GetTrackChannelCountAac(const uint8_t * p_codec_info)444 int A2DP_GetTrackChannelCountAac(const uint8_t* p_codec_info) {
445 tA2DP_AAC_CIE aac_cie;
446
447 // Check whether the codec info contains valid data
448 tA2DP_STATUS a2dp_status = A2DP_ParseInfoAac(&aac_cie, p_codec_info, false);
449 if (a2dp_status != A2DP_SUCCESS) {
450 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
451 a2dp_status);
452 return -1;
453 }
454
455 switch (aac_cie.channelMode) {
456 case A2DP_AAC_CHANNEL_MODE_MONO:
457 return 1;
458 case A2DP_AAC_CHANNEL_MODE_STEREO:
459 return 2;
460 }
461
462 return -1;
463 }
464
A2DP_GetSinkTrackChannelTypeAac(const uint8_t * p_codec_info)465 int A2DP_GetSinkTrackChannelTypeAac(const uint8_t* p_codec_info) {
466 tA2DP_AAC_CIE aac_cie;
467
468 // Check whether the codec info contains valid data
469 tA2DP_STATUS a2dp_status = A2DP_ParseInfoAac(&aac_cie, p_codec_info, false);
470 if (a2dp_status != A2DP_SUCCESS) {
471 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
472 a2dp_status);
473 return -1;
474 }
475
476 switch (aac_cie.channelMode) {
477 case A2DP_AAC_CHANNEL_MODE_MONO:
478 return 1;
479 case A2DP_AAC_CHANNEL_MODE_STEREO:
480 return 3;
481 }
482
483 return -1;
484 }
485
A2DP_GetObjectTypeCodeAac(const uint8_t * p_codec_info)486 int A2DP_GetObjectTypeCodeAac(const uint8_t* p_codec_info) {
487 tA2DP_AAC_CIE aac_cie;
488
489 // Check whether the codec info contains valid data
490 tA2DP_STATUS a2dp_status = A2DP_ParseInfoAac(&aac_cie, p_codec_info, false);
491 if (a2dp_status != A2DP_SUCCESS) {
492 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
493 a2dp_status);
494 return -1;
495 }
496
497 switch (aac_cie.objectType) {
498 case A2DP_AAC_OBJECT_TYPE_MPEG2_LC:
499 case A2DP_AAC_OBJECT_TYPE_MPEG4_LC:
500 case A2DP_AAC_OBJECT_TYPE_MPEG4_LTP:
501 case A2DP_AAC_OBJECT_TYPE_MPEG4_SCALABLE:
502 return aac_cie.objectType;
503 default:
504 break;
505 }
506
507 return -1;
508 }
509
A2DP_GetChannelModeCodeAac(const uint8_t * p_codec_info)510 int A2DP_GetChannelModeCodeAac(const uint8_t* p_codec_info) {
511 tA2DP_AAC_CIE aac_cie;
512
513 // Check whether the codec info contains valid data
514 tA2DP_STATUS a2dp_status = A2DP_ParseInfoAac(&aac_cie, p_codec_info, false);
515 if (a2dp_status != A2DP_SUCCESS) {
516 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
517 a2dp_status);
518 return -1;
519 }
520
521 switch (aac_cie.channelMode) {
522 case A2DP_AAC_CHANNEL_MODE_MONO:
523 case A2DP_AAC_CHANNEL_MODE_STEREO:
524 return aac_cie.channelMode;
525 default:
526 break;
527 }
528
529 return -1;
530 }
531
A2DP_GetVariableBitRateSupportAac(const uint8_t * p_codec_info)532 int A2DP_GetVariableBitRateSupportAac(const uint8_t* p_codec_info) {
533 tA2DP_AAC_CIE aac_cie;
534
535 // Check whether the codec info contains valid data
536 tA2DP_STATUS a2dp_status = A2DP_ParseInfoAac(&aac_cie, p_codec_info, false);
537 if (a2dp_status != A2DP_SUCCESS) {
538 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
539 a2dp_status);
540 return -1;
541 }
542
543 switch (aac_cie.variableBitRateSupport) {
544 case A2DP_AAC_VARIABLE_BIT_RATE_ENABLED:
545 case A2DP_AAC_VARIABLE_BIT_RATE_DISABLED:
546 return aac_cie.variableBitRateSupport;
547 default:
548 break;
549 }
550
551 return -1;
552 }
553
A2DP_GetBitRateAac(const uint8_t * p_codec_info)554 int A2DP_GetBitRateAac(const uint8_t* p_codec_info) {
555 tA2DP_AAC_CIE aac_cie;
556
557 // Check whether the codec info contains valid data
558 tA2DP_STATUS a2dp_status = A2DP_ParseInfoAac(&aac_cie, p_codec_info, false);
559 if (a2dp_status != A2DP_SUCCESS) {
560 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
561 a2dp_status);
562 return -1;
563 }
564
565 return aac_cie.bitRate;
566 }
567
A2DP_ComputeMaxBitRateAac(const uint8_t * p_codec_info,uint16_t mtu)568 int A2DP_ComputeMaxBitRateAac(const uint8_t* p_codec_info, uint16_t mtu) {
569 tA2DP_AAC_CIE aac_cie;
570
571 // Check whether the codec info contains valid data
572 tA2DP_STATUS a2dp_status = A2DP_ParseInfoAac(&aac_cie, p_codec_info, false);
573 if (a2dp_status != A2DP_SUCCESS) {
574 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
575 a2dp_status);
576 return -1;
577 }
578
579 int sampling_freq = A2DP_GetTrackSampleRateAac(p_codec_info);
580 if (sampling_freq == -1) return -1;
581
582 int pcm_channel_samples_per_frame = 0;
583 switch (aac_cie.objectType) {
584 case A2DP_AAC_OBJECT_TYPE_MPEG2_LC:
585 case A2DP_AAC_OBJECT_TYPE_MPEG4_LC:
586 pcm_channel_samples_per_frame = 1024;
587 break;
588 case A2DP_AAC_OBJECT_TYPE_MPEG4_LTP:
589 case A2DP_AAC_OBJECT_TYPE_MPEG4_SCALABLE:
590 // TODO: The MPEG documentation doesn't specify the value.
591 break;
592 default:
593 break;
594 }
595 if (pcm_channel_samples_per_frame == 0) return -1;
596
597 // See Section 3.2.1 Estimating Average Frame Size from
598 // the aacEncoder.pdf document included with the AAC source code.
599 return (8 * mtu * sampling_freq) / pcm_channel_samples_per_frame;
600 }
601
A2DP_GetPacketTimestampAac(const uint8_t * p_codec_info,const uint8_t * p_data,uint32_t * p_timestamp)602 bool A2DP_GetPacketTimestampAac(const uint8_t* p_codec_info,
603 const uint8_t* p_data, uint32_t* p_timestamp) {
604 // TODO: Is this function really codec-specific?
605 *p_timestamp = *(const uint32_t*)p_data;
606 return true;
607 }
608
A2DP_BuildCodecHeaderAac(UNUSED_ATTR const uint8_t * p_codec_info,UNUSED_ATTR BT_HDR * p_buf,UNUSED_ATTR uint16_t frames_per_packet)609 bool A2DP_BuildCodecHeaderAac(UNUSED_ATTR const uint8_t* p_codec_info,
610 UNUSED_ATTR BT_HDR* p_buf,
611 UNUSED_ATTR uint16_t frames_per_packet) {
612 return true;
613 }
614
A2DP_CodecInfoStringAac(const uint8_t * p_codec_info)615 std::string A2DP_CodecInfoStringAac(const uint8_t* p_codec_info) {
616 std::stringstream res;
617 std::string field;
618 tA2DP_STATUS a2dp_status;
619 tA2DP_AAC_CIE aac_cie;
620
621 a2dp_status = A2DP_ParseInfoAac(&aac_cie, p_codec_info, true);
622 if (a2dp_status != A2DP_SUCCESS) {
623 res << "A2DP_ParseInfoAac fail: " << loghex(a2dp_status);
624 return res.str();
625 }
626
627 res << "\tname: AAC\n";
628
629 // Object type
630 field.clear();
631 AppendField(&field, (aac_cie.objectType == 0), "NONE");
632 AppendField(&field, (aac_cie.objectType & A2DP_AAC_OBJECT_TYPE_MPEG2_LC),
633 "(MPEG-2 AAC LC)");
634 AppendField(&field, (aac_cie.objectType & A2DP_AAC_OBJECT_TYPE_MPEG4_LC),
635 "(MPEG-4 AAC LC)");
636 AppendField(&field, (aac_cie.objectType & A2DP_AAC_OBJECT_TYPE_MPEG4_LTP),
637 "(MPEG-4 AAC LTP)");
638 AppendField(&field,
639 (aac_cie.objectType & A2DP_AAC_OBJECT_TYPE_MPEG4_SCALABLE),
640 "(MPEG-4 AAC Scalable)");
641 res << "\tobjectType: " << field << " (" << loghex(aac_cie.objectType)
642 << ")\n";
643
644 // Sample frequency
645 field.clear();
646 AppendField(&field, (aac_cie.sampleRate == 0), "NONE");
647 AppendField(&field, (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_8000),
648 "8000");
649 AppendField(&field, (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_11025),
650 "11025");
651 AppendField(&field, (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_12000),
652 "12000");
653 AppendField(&field, (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_16000),
654 "16000");
655 AppendField(&field, (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_22050),
656 "22050");
657 AppendField(&field, (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_24000),
658 "24000");
659 AppendField(&field, (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_32000),
660 "32000");
661 AppendField(&field, (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_44100),
662 "44100");
663 AppendField(&field, (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_48000),
664 "48000");
665 AppendField(&field, (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_64000),
666 "64000");
667 AppendField(&field, (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_88200),
668 "88200");
669 AppendField(&field, (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_96000),
670 "96000");
671 res << "\tsamp_freq: " << field << " (" << loghex(aac_cie.sampleRate)
672 << ")\n";
673
674 // Channel mode
675 field.clear();
676 AppendField(&field, (aac_cie.channelMode == 0), "NONE");
677 AppendField(&field, (aac_cie.channelMode == A2DP_AAC_CHANNEL_MODE_MONO),
678 "Mono");
679 AppendField(&field, (aac_cie.channelMode == A2DP_AAC_CHANNEL_MODE_STEREO),
680 "Stereo");
681 res << "\tch_mode: " << field << " (" << loghex(aac_cie.channelMode) << ")\n";
682
683 // Variable bit rate support
684 res << "\tvariableBitRateSupport: " << std::boolalpha
685 << (aac_cie.variableBitRateSupport != 0) << "\n";
686
687 // Bit rate
688 res << "\tbitRate: " << std::to_string(aac_cie.bitRate) << "\n";
689
690 return res.str();
691 }
692
A2DP_GetEncoderInterfaceAac(const uint8_t * p_codec_info)693 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterfaceAac(
694 const uint8_t* p_codec_info) {
695 if (!A2DP_IsSourceCodecValidAac(p_codec_info)) return NULL;
696
697 return &a2dp_encoder_interface_aac;
698 }
699
A2DP_GetDecoderInterfaceAac(const uint8_t * p_codec_info)700 const tA2DP_DECODER_INTERFACE* A2DP_GetDecoderInterfaceAac(
701 const uint8_t* p_codec_info) {
702 if (!A2DP_IsSinkCodecValidAac(p_codec_info)) return NULL;
703
704 return &a2dp_decoder_interface_aac;
705 }
706
A2DP_AdjustCodecAac(uint8_t * p_codec_info)707 bool A2DP_AdjustCodecAac(uint8_t* p_codec_info) {
708 tA2DP_AAC_CIE cfg_cie;
709
710 // Nothing to do: just verify the codec info is valid
711 if (A2DP_ParseInfoAac(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS)
712 return false;
713
714 return true;
715 }
716
A2DP_SourceCodecIndexAac(UNUSED_ATTR const uint8_t * p_codec_info)717 btav_a2dp_codec_index_t A2DP_SourceCodecIndexAac(
718 UNUSED_ATTR const uint8_t* p_codec_info) {
719 return BTAV_A2DP_CODEC_INDEX_SOURCE_AAC;
720 }
721
A2DP_SinkCodecIndexAac(UNUSED_ATTR const uint8_t * p_codec_info)722 btav_a2dp_codec_index_t A2DP_SinkCodecIndexAac(
723 UNUSED_ATTR const uint8_t* p_codec_info) {
724 return BTAV_A2DP_CODEC_INDEX_SINK_AAC;
725 }
726
A2DP_CodecIndexStrAac(void)727 const char* A2DP_CodecIndexStrAac(void) { return "AAC"; }
728
A2DP_CodecIndexStrAacSink(void)729 const char* A2DP_CodecIndexStrAacSink(void) { return "AAC SINK"; }
730
aac_source_caps_initialize()731 void aac_source_caps_initialize() {
732 if (aac_source_caps_configured) {
733 return;
734 }
735 a2dp_aac_source_caps =
736 osi_property_get_bool("persist.bluetooth.a2dp_aac.vbr_supported", false)
737 ? a2dp_aac_vbr_source_caps
738 : a2dp_aac_cbr_source_caps;
739 aac_source_caps_configured = true;
740 }
741
A2DP_InitCodecConfigAac(AvdtpSepConfig * p_cfg)742 bool A2DP_InitCodecConfigAac(AvdtpSepConfig* p_cfg) {
743 aac_source_caps_initialize();
744 if (A2DP_BuildInfoAac(AVDT_MEDIA_TYPE_AUDIO, &a2dp_aac_source_caps,
745 p_cfg->codec_info) != A2DP_SUCCESS) {
746 return false;
747 }
748
749 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
750 /* Content protection info - support SCMS-T */
751 uint8_t* p = p_cfg->protect_info;
752 *p++ = AVDT_CP_LOSC;
753 UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID);
754 p_cfg->num_protect = 1;
755 #endif
756
757 return true;
758 }
759
A2DP_InitCodecConfigAacSink(AvdtpSepConfig * p_cfg)760 bool A2DP_InitCodecConfigAacSink(AvdtpSepConfig* p_cfg) {
761 return A2DP_BuildInfoAac(AVDT_MEDIA_TYPE_AUDIO, &a2dp_aac_sink_caps,
762 p_cfg->codec_info) == A2DP_SUCCESS;
763 }
764
build_codec_config(const tA2DP_AAC_CIE & config_cie,btav_a2dp_codec_config_t * result)765 UNUSED_ATTR static void build_codec_config(const tA2DP_AAC_CIE& config_cie,
766 btav_a2dp_codec_config_t* result) {
767 if (config_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_44100)
768 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
769 if (config_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_48000)
770 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
771 if (config_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_88200)
772 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
773 if (config_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_96000)
774 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
775
776 result->bits_per_sample = config_cie.bits_per_sample;
777
778 if (config_cie.channelMode & A2DP_AAC_CHANNEL_MODE_MONO)
779 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
780 if (config_cie.channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
781 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
782 }
783 }
784
A2dpCodecConfigAacSource(btav_a2dp_codec_priority_t codec_priority)785 A2dpCodecConfigAacSource::A2dpCodecConfigAacSource(
786 btav_a2dp_codec_priority_t codec_priority)
787 : A2dpCodecConfigAacBase(BTAV_A2DP_CODEC_INDEX_SOURCE_AAC,
788 A2DP_CodecIndexStrAac(), codec_priority, true) {
789 aac_source_caps_initialize();
790 // Compute the local capability
791 if (a2dp_aac_source_caps.sampleRate & A2DP_AAC_SAMPLING_FREQ_44100) {
792 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
793 }
794 if (a2dp_aac_source_caps.sampleRate & A2DP_AAC_SAMPLING_FREQ_48000) {
795 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
796 }
797 if (a2dp_aac_source_caps.sampleRate & A2DP_AAC_SAMPLING_FREQ_88200) {
798 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
799 }
800 if (a2dp_aac_source_caps.sampleRate & A2DP_AAC_SAMPLING_FREQ_96000) {
801 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
802 }
803 codec_local_capability_.bits_per_sample =
804 a2dp_aac_source_caps.bits_per_sample;
805 if (a2dp_aac_source_caps.channelMode & A2DP_AAC_CHANNEL_MODE_MONO) {
806 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
807 }
808 if (a2dp_aac_source_caps.channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
809 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
810 }
811 }
812
~A2dpCodecConfigAacSource()813 A2dpCodecConfigAacSource::~A2dpCodecConfigAacSource() {}
814
init()815 bool A2dpCodecConfigAacSource::init() {
816 if (!isValid()) return false;
817
818 // Load the encoder
819 if (!A2DP_LoadEncoderAac()) {
820 LOG_ERROR(LOG_TAG, "%s: cannot load the encoder", __func__);
821 return false;
822 }
823
824 return true;
825 }
826
useRtpHeaderMarkerBit() const827 bool A2dpCodecConfigAacSource::useRtpHeaderMarkerBit() const { return true; }
828
829 //
830 // Selects the best sample rate from |sampleRate|.
831 // The result is stored in |p_result| and |p_codec_config|.
832 // Returns true if a selection was made, otherwise false.
833 //
select_best_sample_rate(uint16_t sampleRate,tA2DP_AAC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)834 static bool select_best_sample_rate(uint16_t sampleRate,
835 tA2DP_AAC_CIE* p_result,
836 btav_a2dp_codec_config_t* p_codec_config) {
837 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_96000) {
838 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_96000;
839 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
840 return true;
841 }
842 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_88200) {
843 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_88200;
844 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
845 return true;
846 }
847 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_48000) {
848 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_48000;
849 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
850 return true;
851 }
852 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_44100) {
853 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_44100;
854 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
855 return true;
856 }
857 return false;
858 }
859
860 //
861 // Selects the audio sample rate from |p_codec_audio_config|.
862 // |sampleRate| contains the capability.
863 // The result is stored in |p_result| and |p_codec_config|.
864 // Returns true if a selection was made, otherwise false.
865 //
select_audio_sample_rate(const btav_a2dp_codec_config_t * p_codec_audio_config,uint16_t sampleRate,tA2DP_AAC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)866 static bool select_audio_sample_rate(
867 const btav_a2dp_codec_config_t* p_codec_audio_config, uint16_t sampleRate,
868 tA2DP_AAC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
869 switch (p_codec_audio_config->sample_rate) {
870 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
871 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_44100) {
872 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_44100;
873 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
874 return true;
875 }
876 break;
877 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
878 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_48000) {
879 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_48000;
880 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
881 return true;
882 }
883 break;
884 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
885 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_88200) {
886 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_88200;
887 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
888 return true;
889 }
890 break;
891 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
892 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_96000) {
893 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_96000;
894 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
895 return true;
896 }
897 break;
898 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
899 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
900 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
901 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
902 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
903 break;
904 }
905 return false;
906 }
907
908 //
909 // Selects the best bits per sample from |bits_per_sample|.
910 // |bits_per_sample| contains the capability.
911 // The result is stored in |p_result| and |p_codec_config|.
912 // Returns true if a selection was made, otherwise false.
913 //
select_best_bits_per_sample(btav_a2dp_codec_bits_per_sample_t bits_per_sample,tA2DP_AAC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)914 static bool select_best_bits_per_sample(
915 btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_AAC_CIE* p_result,
916 btav_a2dp_codec_config_t* p_codec_config) {
917 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
918 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
919 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
920 return true;
921 }
922 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
923 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
924 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
925 return true;
926 }
927 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
928 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
929 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
930 return true;
931 }
932 return false;
933 }
934
935 //
936 // Selects the audio bits per sample from |p_codec_audio_config|.
937 // |bits_per_sample| contains the capability.
938 // The result is stored in |p_result| and |p_codec_config|.
939 // Returns true if a selection was made, otherwise false.
940 //
select_audio_bits_per_sample(const btav_a2dp_codec_config_t * p_codec_audio_config,btav_a2dp_codec_bits_per_sample_t bits_per_sample,tA2DP_AAC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)941 static bool select_audio_bits_per_sample(
942 const btav_a2dp_codec_config_t* p_codec_audio_config,
943 btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_AAC_CIE* p_result,
944 btav_a2dp_codec_config_t* p_codec_config) {
945 switch (p_codec_audio_config->bits_per_sample) {
946 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
947 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
948 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
949 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
950 return true;
951 }
952 break;
953 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
954 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
955 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
956 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
957 return true;
958 }
959 break;
960 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
961 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
962 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
963 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
964 return true;
965 }
966 break;
967 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
968 break;
969 }
970 return false;
971 }
972
973 //
974 // Selects the best channel mode from |channelMode|.
975 // The result is stored in |p_result| and |p_codec_config|.
976 // Returns true if a selection was made, otherwise false.
977 //
select_best_channel_mode(uint8_t channelMode,tA2DP_AAC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)978 static bool select_best_channel_mode(uint8_t channelMode,
979 tA2DP_AAC_CIE* p_result,
980 btav_a2dp_codec_config_t* p_codec_config) {
981 if (channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
982 p_result->channelMode = A2DP_AAC_CHANNEL_MODE_STEREO;
983 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
984 return true;
985 }
986 if (channelMode & A2DP_AAC_CHANNEL_MODE_MONO) {
987 p_result->channelMode = A2DP_AAC_CHANNEL_MODE_MONO;
988 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
989 return true;
990 }
991 return false;
992 }
993
994 //
995 // Selects the audio channel mode from |p_codec_audio_config|.
996 // |channelMode| contains the capability.
997 // The result is stored in |p_result| and |p_codec_config|.
998 // Returns true if a selection was made, otherwise false.
999 //
select_audio_channel_mode(const btav_a2dp_codec_config_t * p_codec_audio_config,uint8_t channelMode,tA2DP_AAC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)1000 static bool select_audio_channel_mode(
1001 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t channelMode,
1002 tA2DP_AAC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
1003 switch (p_codec_audio_config->channel_mode) {
1004 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
1005 if (channelMode & A2DP_AAC_CHANNEL_MODE_MONO) {
1006 p_result->channelMode = A2DP_AAC_CHANNEL_MODE_MONO;
1007 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1008 return true;
1009 }
1010 break;
1011 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
1012 if (channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
1013 p_result->channelMode = A2DP_AAC_CHANNEL_MODE_STEREO;
1014 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1015 return true;
1016 }
1017 break;
1018 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1019 break;
1020 }
1021
1022 return false;
1023 }
1024
setCodecConfig(const uint8_t * p_peer_codec_info,bool is_capability,uint8_t * p_result_codec_config)1025 bool A2dpCodecConfigAacBase::setCodecConfig(const uint8_t* p_peer_codec_info,
1026 bool is_capability,
1027 uint8_t* p_result_codec_config) {
1028 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
1029 tA2DP_AAC_CIE peer_info_cie;
1030 tA2DP_AAC_CIE result_config_cie;
1031 uint8_t channelMode;
1032 uint16_t sampleRate;
1033 btav_a2dp_codec_bits_per_sample_t bits_per_sample;
1034 const tA2DP_AAC_CIE* p_a2dp_aac_caps =
1035 (is_source_) ? &a2dp_aac_source_caps : &a2dp_aac_sink_caps;
1036
1037 // Save the internal state
1038 btav_a2dp_codec_config_t saved_codec_config = codec_config_;
1039 btav_a2dp_codec_config_t saved_codec_capability = codec_capability_;
1040 btav_a2dp_codec_config_t saved_codec_selectable_capability =
1041 codec_selectable_capability_;
1042 btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_;
1043 btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_;
1044 uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE];
1045 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
1046 uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE];
1047 memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_));
1048 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
1049 sizeof(ota_codec_peer_capability_));
1050 memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_,
1051 sizeof(ota_codec_peer_config_));
1052
1053 tA2DP_STATUS status =
1054 A2DP_ParseInfoAac(&peer_info_cie, p_peer_codec_info, is_capability);
1055 if (status != A2DP_SUCCESS) {
1056 LOG_ERROR(LOG_TAG, "%s: can't parse peer's capabilities: error = %d",
1057 __func__, status);
1058 goto fail;
1059 }
1060
1061 //
1062 // Build the preferred configuration
1063 //
1064 memset(&result_config_cie, 0, sizeof(result_config_cie));
1065
1066 // NOTE: Always assign the Object Type and Variable Bit Rate Support.
1067 result_config_cie.objectType = p_a2dp_aac_caps->objectType;
1068 // The Variable Bit Rate Support is disabled if either side disables it
1069 result_config_cie.variableBitRateSupport =
1070 p_a2dp_aac_caps->variableBitRateSupport &
1071 peer_info_cie.variableBitRateSupport;
1072 if (result_config_cie.variableBitRateSupport !=
1073 A2DP_AAC_VARIABLE_BIT_RATE_DISABLED) {
1074 codec_config_.codec_specific_1 =
1075 static_cast<int64_t>(AacEncoderBitrateMode::AACENC_BR_MODE_VBR_5);
1076 } else {
1077 codec_config_.codec_specific_1 =
1078 static_cast<int64_t>(AacEncoderBitrateMode::AACENC_BR_MODE_CBR);
1079 }
1080
1081 // Set the bit rate as follows:
1082 // 1. If the remote device reports a bogus bit rate
1083 // (bitRate < A2DP_AAC_MIN_BITRATE), then use the bit rate from our
1084 // configuration. Examples of observed bogus bit rates are zero
1085 // and 24576.
1086 // 2. If the remote device reports valid bit rate
1087 // (bitRate >= A2DP_AAC_MIN_BITRATE), then use the smaller
1088 // of the remote device's bit rate and the bit rate from our configuration.
1089 // In either case, the actual streaming bit rate will also consider the MTU.
1090 if (peer_info_cie.bitRate < A2DP_AAC_MIN_BITRATE) {
1091 // Bogus bit rate
1092 result_config_cie.bitRate = p_a2dp_aac_caps->bitRate;
1093 } else {
1094 result_config_cie.bitRate =
1095 std::min(p_a2dp_aac_caps->bitRate, peer_info_cie.bitRate);
1096 }
1097
1098 //
1099 // Select the sample frequency
1100 //
1101 sampleRate = p_a2dp_aac_caps->sampleRate & peer_info_cie.sampleRate;
1102 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
1103 switch (codec_user_config_.sample_rate) {
1104 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
1105 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_44100) {
1106 result_config_cie.sampleRate = A2DP_AAC_SAMPLING_FREQ_44100;
1107 codec_capability_.sample_rate = codec_user_config_.sample_rate;
1108 codec_config_.sample_rate = codec_user_config_.sample_rate;
1109 }
1110 break;
1111 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
1112 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_48000) {
1113 result_config_cie.sampleRate = A2DP_AAC_SAMPLING_FREQ_48000;
1114 codec_capability_.sample_rate = codec_user_config_.sample_rate;
1115 codec_config_.sample_rate = codec_user_config_.sample_rate;
1116 }
1117 break;
1118 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
1119 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_88200) {
1120 result_config_cie.sampleRate = A2DP_AAC_SAMPLING_FREQ_88200;
1121 codec_capability_.sample_rate = codec_user_config_.sample_rate;
1122 codec_config_.sample_rate = codec_user_config_.sample_rate;
1123 }
1124 break;
1125 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
1126 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_96000) {
1127 result_config_cie.sampleRate = A2DP_AAC_SAMPLING_FREQ_96000;
1128 codec_capability_.sample_rate = codec_user_config_.sample_rate;
1129 codec_config_.sample_rate = codec_user_config_.sample_rate;
1130 }
1131 break;
1132 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
1133 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
1134 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
1135 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
1136 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
1137 codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
1138 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
1139 break;
1140 }
1141
1142 // Select the sample frequency if there is no user preference
1143 do {
1144 // Compute the selectable capability
1145 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_44100) {
1146 codec_selectable_capability_.sample_rate |=
1147 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1148 }
1149 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_48000) {
1150 codec_selectable_capability_.sample_rate |=
1151 BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1152 }
1153 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_88200) {
1154 codec_selectable_capability_.sample_rate |=
1155 BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
1156 }
1157 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_96000) {
1158 codec_selectable_capability_.sample_rate |=
1159 BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
1160 }
1161
1162 if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break;
1163
1164 // Compute the common capability
1165 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_44100)
1166 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1167 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_48000)
1168 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1169 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_88200)
1170 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
1171 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_96000)
1172 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
1173
1174 // No user preference - try the codec audio config
1175 if (select_audio_sample_rate(&codec_audio_config_, sampleRate,
1176 &result_config_cie, &codec_config_)) {
1177 break;
1178 }
1179
1180 // No user preference - try the default config
1181 if (select_best_sample_rate(
1182 a2dp_aac_default_config.sampleRate & peer_info_cie.sampleRate,
1183 &result_config_cie, &codec_config_)) {
1184 break;
1185 }
1186
1187 // No user preference - use the best match
1188 if (select_best_sample_rate(sampleRate, &result_config_cie,
1189 &codec_config_)) {
1190 break;
1191 }
1192 } while (false);
1193 if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) {
1194 LOG_ERROR(LOG_TAG,
1195 "%s: cannot match sample frequency: source caps = 0x%x "
1196 "peer info = 0x%x",
1197 __func__, p_a2dp_aac_caps->sampleRate, peer_info_cie.sampleRate);
1198 goto fail;
1199 }
1200
1201 //
1202 // Select the bits per sample
1203 //
1204 // NOTE: this information is NOT included in the AAC A2DP codec description
1205 // that is sent OTA.
1206 bits_per_sample = p_a2dp_aac_caps->bits_per_sample;
1207 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1208 switch (codec_user_config_.bits_per_sample) {
1209 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
1210 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
1211 result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1212 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1213 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1214 }
1215 break;
1216 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
1217 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
1218 result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1219 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1220 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1221 }
1222 break;
1223 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
1224 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
1225 result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1226 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1227 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1228 }
1229 break;
1230 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
1231 result_config_cie.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1232 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1233 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1234 break;
1235 }
1236
1237 // Select the bits per sample if there is no user preference
1238 do {
1239 // Compute the selectable capability
1240 codec_selectable_capability_.bits_per_sample =
1241 p_a2dp_aac_caps->bits_per_sample;
1242
1243 if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE)
1244 break;
1245
1246 // Compute the common capability
1247 codec_capability_.bits_per_sample = bits_per_sample;
1248
1249 // No user preference - the the codec audio config
1250 if (select_audio_bits_per_sample(&codec_audio_config_,
1251 p_a2dp_aac_caps->bits_per_sample,
1252 &result_config_cie, &codec_config_)) {
1253 break;
1254 }
1255
1256 // No user preference - try the default config
1257 if (select_best_bits_per_sample(a2dp_aac_default_config.bits_per_sample,
1258 &result_config_cie, &codec_config_)) {
1259 break;
1260 }
1261
1262 // No user preference - use the best match
1263 if (select_best_bits_per_sample(p_a2dp_aac_caps->bits_per_sample,
1264 &result_config_cie, &codec_config_)) {
1265 break;
1266 }
1267 } while (false);
1268 if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) {
1269 LOG_ERROR(LOG_TAG,
1270 "%s: cannot match bits per sample: default = 0x%x "
1271 "user preference = 0x%x",
1272 __func__, a2dp_aac_default_config.bits_per_sample,
1273 codec_user_config_.bits_per_sample);
1274 goto fail;
1275 }
1276
1277 //
1278 // Select the channel mode
1279 //
1280 channelMode = p_a2dp_aac_caps->channelMode & peer_info_cie.channelMode;
1281 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1282 switch (codec_user_config_.channel_mode) {
1283 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
1284 if (channelMode & A2DP_AAC_CHANNEL_MODE_MONO) {
1285 result_config_cie.channelMode = A2DP_AAC_CHANNEL_MODE_MONO;
1286 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1287 codec_config_.channel_mode = codec_user_config_.channel_mode;
1288 }
1289 break;
1290 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
1291 if (channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
1292 result_config_cie.channelMode = A2DP_AAC_CHANNEL_MODE_STEREO;
1293 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1294 codec_config_.channel_mode = codec_user_config_.channel_mode;
1295 }
1296 break;
1297 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1298 codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1299 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1300 break;
1301 }
1302
1303 // Select the channel mode if there is no user preference
1304 do {
1305 // Compute the selectable capability
1306 if (channelMode & A2DP_AAC_CHANNEL_MODE_MONO) {
1307 codec_selectable_capability_.channel_mode |=
1308 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1309 }
1310 if (channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
1311 codec_selectable_capability_.channel_mode |=
1312 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1313 }
1314
1315 if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break;
1316
1317 // Compute the common capability
1318 if (channelMode & A2DP_AAC_CHANNEL_MODE_MONO)
1319 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1320 if (channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
1321 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1322 }
1323
1324 // No user preference - try the codec audio config
1325 if (select_audio_channel_mode(&codec_audio_config_, channelMode,
1326 &result_config_cie, &codec_config_)) {
1327 break;
1328 }
1329
1330 // No user preference - try the default config
1331 if (select_best_channel_mode(
1332 a2dp_aac_default_config.channelMode & peer_info_cie.channelMode,
1333 &result_config_cie, &codec_config_)) {
1334 break;
1335 }
1336
1337 // No user preference - use the best match
1338 if (select_best_channel_mode(channelMode, &result_config_cie,
1339 &codec_config_)) {
1340 break;
1341 }
1342 } while (false);
1343 if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) {
1344 LOG_ERROR(LOG_TAG,
1345 "%s: cannot match channel mode: source caps = 0x%x "
1346 "peer info = 0x%x",
1347 __func__, p_a2dp_aac_caps->channelMode,
1348 peer_info_cie.channelMode);
1349 goto fail;
1350 }
1351
1352 //
1353 // Copy the codec-specific fields if they are not zero
1354 //
1355 if (codec_user_config_.codec_specific_1 != 0) {
1356 if (result_config_cie.variableBitRateSupport !=
1357 A2DP_AAC_VARIABLE_BIT_RATE_DISABLED) {
1358 auto user_bitrate_mode = codec_user_config_.codec_specific_1;
1359 switch (static_cast<AacEncoderBitrateMode>(user_bitrate_mode)) {
1360 case AacEncoderBitrateMode::AACENC_BR_MODE_VBR_C:
1361 // VBR is supported, and is disabled by the user preference
1362 result_config_cie.variableBitRateSupport =
1363 A2DP_AAC_VARIABLE_BIT_RATE_DISABLED;
1364 [[fallthrough]];
1365 case AacEncoderBitrateMode::AACENC_BR_MODE_VBR_1:
1366 [[fallthrough]];
1367 case AacEncoderBitrateMode::AACENC_BR_MODE_VBR_2:
1368 [[fallthrough]];
1369 case AacEncoderBitrateMode::AACENC_BR_MODE_VBR_3:
1370 [[fallthrough]];
1371 case AacEncoderBitrateMode::AACENC_BR_MODE_VBR_4:
1372 [[fallthrough]];
1373 case AacEncoderBitrateMode::AACENC_BR_MODE_VBR_5:
1374 codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
1375 break;
1376 default:
1377 codec_config_.codec_specific_1 =
1378 static_cast<int64_t>(AacEncoderBitrateMode::AACENC_BR_MODE_VBR_5);
1379 }
1380 } else {
1381 // It is no needed to check the user preference when Variable Bitrate
1382 // unsupported by one of source or sink
1383 codec_config_.codec_specific_1 =
1384 static_cast<int64_t>(AacEncoderBitrateMode::AACENC_BR_MODE_CBR);
1385 }
1386 }
1387 if (codec_user_config_.codec_specific_2 != 0)
1388 codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2;
1389 if (codec_user_config_.codec_specific_3 != 0)
1390 codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3;
1391 if (codec_user_config_.codec_specific_4 != 0)
1392 codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4;
1393
1394 if (A2DP_BuildInfoAac(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1395 p_result_codec_config) != A2DP_SUCCESS) {
1396 goto fail;
1397 }
1398
1399 // Create a local copy of the peer codec capability/config, and the
1400 // result codec config.
1401 if (is_capability) {
1402 status = A2DP_BuildInfoAac(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1403 ota_codec_peer_capability_);
1404 } else {
1405 status = A2DP_BuildInfoAac(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1406 ota_codec_peer_config_);
1407 }
1408 CHECK(status == A2DP_SUCCESS);
1409 status = A2DP_BuildInfoAac(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1410 ota_codec_config_);
1411 CHECK(status == A2DP_SUCCESS);
1412 return true;
1413
1414 fail:
1415 // Restore the internal state
1416 codec_config_ = saved_codec_config;
1417 codec_capability_ = saved_codec_capability;
1418 codec_selectable_capability_ = saved_codec_selectable_capability;
1419 codec_user_config_ = saved_codec_user_config;
1420 codec_audio_config_ = saved_codec_audio_config;
1421 memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_));
1422 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1423 sizeof(ota_codec_peer_capability_));
1424 memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config,
1425 sizeof(ota_codec_peer_config_));
1426 return false;
1427 }
1428
setPeerCodecCapabilities(const uint8_t * p_peer_codec_capabilities)1429 bool A2dpCodecConfigAacBase::setPeerCodecCapabilities(
1430 const uint8_t* p_peer_codec_capabilities) {
1431 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
1432 tA2DP_AAC_CIE peer_info_cie;
1433 uint8_t channelMode;
1434 uint16_t sampleRate;
1435 uint8_t variableBitRateSupport;
1436 const tA2DP_AAC_CIE* p_a2dp_aac_caps =
1437 (is_source_) ? &a2dp_aac_source_caps : &a2dp_aac_sink_caps;
1438
1439 // Save the internal state
1440 btav_a2dp_codec_config_t saved_codec_selectable_capability =
1441 codec_selectable_capability_;
1442 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
1443 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
1444 sizeof(ota_codec_peer_capability_));
1445
1446 tA2DP_STATUS status =
1447 A2DP_ParseInfoAac(&peer_info_cie, p_peer_codec_capabilities, true);
1448 if (status != A2DP_SUCCESS) {
1449 LOG_ERROR(LOG_TAG, "%s: can't parse peer's capabilities: error = %d",
1450 __func__, status);
1451 goto fail;
1452 }
1453
1454 // Compute the selectable capability - sample rate
1455 sampleRate = p_a2dp_aac_caps->sampleRate & peer_info_cie.sampleRate;
1456 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_44100) {
1457 codec_selectable_capability_.sample_rate |=
1458 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1459 }
1460 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_48000) {
1461 codec_selectable_capability_.sample_rate |=
1462 BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1463 }
1464 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_88200) {
1465 codec_selectable_capability_.sample_rate |=
1466 BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
1467 }
1468 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_96000) {
1469 codec_selectable_capability_.sample_rate |=
1470 BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
1471 }
1472
1473 // Compute the selectable capability - bits per sample
1474 codec_selectable_capability_.bits_per_sample =
1475 p_a2dp_aac_caps->bits_per_sample;
1476
1477 // Compute the selectable capability - channel mode
1478 channelMode = p_a2dp_aac_caps->channelMode & peer_info_cie.channelMode;
1479 if (channelMode & A2DP_AAC_CHANNEL_MODE_MONO) {
1480 codec_selectable_capability_.channel_mode |=
1481 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1482 }
1483 if (channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
1484 codec_selectable_capability_.channel_mode |=
1485 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1486 }
1487
1488 // Compute the selectable capability - variable bitrate mode
1489 variableBitRateSupport = p_a2dp_aac_caps->variableBitRateSupport &
1490 peer_info_cie.variableBitRateSupport;
1491 if (variableBitRateSupport != A2DP_AAC_VARIABLE_BIT_RATE_DISABLED) {
1492 codec_selectable_capability_.codec_specific_1 =
1493 static_cast<int64_t>(AacEncoderBitrateMode::AACENC_BR_MODE_VBR_5);
1494 } else {
1495 codec_selectable_capability_.codec_specific_1 =
1496 static_cast<int64_t>(AacEncoderBitrateMode::AACENC_BR_MODE_CBR);
1497 }
1498
1499 status = A2DP_BuildInfoAac(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1500 ota_codec_peer_capability_);
1501 CHECK(status == A2DP_SUCCESS);
1502 return true;
1503
1504 fail:
1505 // Restore the internal state
1506 codec_selectable_capability_ = saved_codec_selectable_capability;
1507 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1508 sizeof(ota_codec_peer_capability_));
1509 return false;
1510 }
1511
A2dpCodecConfigAacSink(btav_a2dp_codec_priority_t codec_priority)1512 A2dpCodecConfigAacSink::A2dpCodecConfigAacSink(
1513 btav_a2dp_codec_priority_t codec_priority)
1514 : A2dpCodecConfigAacBase(BTAV_A2DP_CODEC_INDEX_SINK_AAC,
1515 A2DP_CodecIndexStrAacSink(), codec_priority,
1516 false) {}
1517
~A2dpCodecConfigAacSink()1518 A2dpCodecConfigAacSink::~A2dpCodecConfigAacSink() {}
1519
init()1520 bool A2dpCodecConfigAacSink::init() {
1521 if (!isValid()) return false;
1522
1523 // Load the decoder
1524 if (!A2DP_LoadDecoderAac()) {
1525 LOG_ERROR(LOG_TAG, "%s: cannot load the decoder", __func__);
1526 return false;
1527 }
1528
1529 return true;
1530 }
1531
encoderIntervalMs() const1532 uint64_t A2dpCodecConfigAacSink::encoderIntervalMs() const {
1533 // TODO: This method applies only to Source codecs
1534 return 0;
1535 }
1536
getEffectiveMtu() const1537 int A2dpCodecConfigAacSink::getEffectiveMtu() const {
1538 // TODO: This method applies only to Source codecs
1539 return 0;
1540 }
1541
useRtpHeaderMarkerBit() const1542 bool A2dpCodecConfigAacSink::useRtpHeaderMarkerBit() const {
1543 // TODO: This method applies only to Source codecs
1544 return false;
1545 }
1546
updateEncoderUserConfig(UNUSED_ATTR const tA2DP_ENCODER_INIT_PEER_PARAMS * p_peer_params,UNUSED_ATTR bool * p_restart_input,UNUSED_ATTR bool * p_restart_output,UNUSED_ATTR bool * p_config_updated)1547 bool A2dpCodecConfigAacSink::updateEncoderUserConfig(
1548 UNUSED_ATTR const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
1549 UNUSED_ATTR bool* p_restart_input, UNUSED_ATTR bool* p_restart_output,
1550 UNUSED_ATTR bool* p_config_updated) {
1551 // TODO: This method applies only to Source codecs
1552 return false;
1553 }
1554