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