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