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