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