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