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