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