blob: 668100883ec3bab5e3ab8edfadd08c45ba85bdaf [file] [log] [blame]
Pavlin Radoslavovd5f49602017-01-03 16:53:18 -08001/*
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
500bool A2DP_GetPacketTimestampAac(const uint8_t* p_codec_info,
501 const uint8_t* p_data, uint32_t* p_timestamp) {
502 // TODO: Is this function really codec-specific?
503 *p_timestamp = *(const uint32_t*)p_data;
504 return true;
505}
506
507bool A2DP_BuildCodecHeaderAac(UNUSED_ATTR const uint8_t* p_codec_info,
508 UNUSED_ATTR BT_HDR* p_buf,
509 UNUSED_ATTR uint16_t frames_per_packet) {
510 return true;
511}
512
513void A2DP_DumpCodecInfoAac(const uint8_t* p_codec_info) {
514 tA2DP_STATUS a2dp_status;
515 tA2DP_AAC_CIE aac_cie;
516
517 LOG_DEBUG(LOG_TAG, "%s", __func__);
518
519 a2dp_status = A2DP_ParseInfoAac(&aac_cie, p_codec_info, true);
520 if (a2dp_status != A2DP_SUCCESS) {
521 LOG_ERROR(LOG_TAG, "%s: A2DP_ParseInfoAac fail:%d", __func__, a2dp_status);
522 return;
523 }
524
525 LOG_DEBUG(LOG_TAG, "\tobjectType: 0x%x", aac_cie.objectType);
526 if (aac_cie.objectType & A2DP_AAC_OBJECT_TYPE_MPEG2_LC) {
527 LOG_DEBUG(LOG_TAG, "\tobjectType: (MPEG-2 AAC LC)");
528 }
529 if (aac_cie.objectType & A2DP_AAC_OBJECT_TYPE_MPEG4_LC) {
530 LOG_DEBUG(LOG_TAG, "\tobjectType: (MPEG-4 AAC LC)");
531 }
532 if (aac_cie.objectType & A2DP_AAC_OBJECT_TYPE_MPEG4_LTP) {
533 LOG_DEBUG(LOG_TAG, "\tobjectType: (MPEG-4 AAC LTP)");
534 }
535 if (aac_cie.objectType & A2DP_AAC_OBJECT_TYPE_MPEG4_SCALABLE) {
536 LOG_DEBUG(LOG_TAG, "\tobjectType: (MPEG-4 AAC Scalable)");
537 }
538
539 LOG_DEBUG(LOG_TAG, "\tsamp_freq: 0x%x", aac_cie.sampleRate);
540 if (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_8000) {
541 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (8000)");
542 }
543 if (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_11025) {
544 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (11025)");
545 }
546 if (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_12000) {
547 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (12000)");
548 }
549 if (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_16000) {
550 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (16000)");
551 }
552 if (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_22050) {
553 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (22050)");
554 }
555 if (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_24000) {
556 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (24000)");
557 }
558 if (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_32000) {
559 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (32000)");
560 }
561 if (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_44100) {
562 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (44100)");
563 }
564 if (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_48000) {
565 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (48000)");
566 }
567 if (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_64000) {
568 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (64000)");
569 }
570 if (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_88200) {
571 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (88200)");
572 }
573 if (aac_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_96000) {
574 LOG_DEBUG(LOG_TAG, "\tsamp_freq: (96000)");
575 }
576
577 LOG_DEBUG(LOG_TAG, "\tch_mode: 0x%x", aac_cie.channelMode);
578 if (aac_cie.channelMode == A2DP_AAC_CHANNEL_MODE_MONO) {
579 LOG_DEBUG(LOG_TAG, "\tch_mode: (Mono)");
580 }
581 if (aac_cie.channelMode == A2DP_AAC_CHANNEL_MODE_STEREO) {
582 LOG_DEBUG(LOG_TAG, "\tch_mode: (Stereo)");
583 }
584
585 LOG_DEBUG(LOG_TAG, "\tvariableBitRateSupport: %s",
586 (aac_cie.variableBitRateSupport != 0) ? "true" : "false");
587
588 LOG_DEBUG(LOG_TAG, "\tbitRate: %u", aac_cie.bitRate);
589}
590
591const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterfaceAac(
592 const uint8_t* p_codec_info) {
593 if (!A2DP_IsSourceCodecValidAac(p_codec_info)) return NULL;
594
595 return &a2dp_encoder_interface_aac;
596}
597
598bool A2DP_AdjustCodecAac(uint8_t* p_codec_info) {
599 tA2DP_AAC_CIE cfg_cie;
600
601 // Nothing to do: just verify the codec info is valid
602 if (A2DP_ParseInfoAac(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS)
603 return false;
604
605 return true;
606}
607
608btav_a2dp_codec_index_t A2DP_SourceCodecIndexAac(
609 UNUSED_ATTR const uint8_t* p_codec_info) {
610 return BTAV_A2DP_CODEC_INDEX_SOURCE_AAC;
611}
612
613const char* A2DP_CodecIndexStrAac(void) { return "AAC"; }
614
615bool A2DP_InitCodecConfigAac(tAVDT_CFG* p_cfg) {
616 if (A2DP_BuildInfoAac(AVDT_MEDIA_TYPE_AUDIO, &a2dp_aac_caps,
617 p_cfg->codec_info) != A2DP_SUCCESS) {
618 return false;
619 }
620
621#if (BTA_AV_CO_CP_SCMS_T == TRUE)
622 /* Content protection info - support SCMS-T */
623 uint8_t* p = p_cfg->protect_info;
624 *p++ = AVDT_CP_LOSC;
625 UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID);
626 p_cfg->num_protect = 1;
627#endif
628
629 return true;
630}
631
632UNUSED_ATTR static void build_codec_config(const tA2DP_AAC_CIE& config_cie,
633 btav_a2dp_codec_config_t* result) {
634 if (config_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_44100)
635 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
636 if (config_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_48000)
637 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
638 if (config_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_88200)
639 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
640 if (config_cie.sampleRate & A2DP_AAC_SAMPLING_FREQ_96000)
641 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
642
643 result->bits_per_sample = config_cie.bits_per_sample;
644
645 if (config_cie.channelMode & A2DP_AAC_CHANNEL_MODE_MONO)
646 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
647 if (config_cie.channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
648 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
649 }
650}
651
Pavlin Radoslavova6ba5ac2017-01-31 20:51:06 -0800652A2dpCodecConfigAac::A2dpCodecConfigAac(
653 btav_a2dp_codec_priority_t codec_priority)
654 : A2dpCodecConfig(BTAV_A2DP_CODEC_INDEX_SOURCE_AAC, "AAC", codec_priority) {
Pavlin Radoslavov494e53e2017-01-25 17:00:23 -0800655 // Compute the local capability
656 if (a2dp_aac_caps.sampleRate & A2DP_AAC_SAMPLING_FREQ_44100) {
657 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
658 }
659 if (a2dp_aac_caps.sampleRate & A2DP_AAC_SAMPLING_FREQ_48000) {
660 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
661 }
662 if (a2dp_aac_caps.sampleRate & A2DP_AAC_SAMPLING_FREQ_88200) {
663 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
664 }
665 if (a2dp_aac_caps.sampleRate & A2DP_AAC_SAMPLING_FREQ_96000) {
666 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
667 }
668 codec_local_capability_.bits_per_sample = a2dp_aac_caps.bits_per_sample;
669 if (a2dp_aac_caps.channelMode & A2DP_AAC_CHANNEL_MODE_MONO) {
670 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
671 }
672 if (a2dp_aac_caps.channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
673 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
674 }
675}
Pavlin Radoslavovd5f49602017-01-03 16:53:18 -0800676
677A2dpCodecConfigAac::~A2dpCodecConfigAac() {}
678
679bool A2dpCodecConfigAac::init() {
680 if (!isValid()) return false;
681
682 // Load the encoder
683 if (!A2DP_LoadEncoderAac()) {
684 LOG_ERROR(LOG_TAG, "%s: cannot load the encoder", __func__);
685 return false;
686 }
687
688 return true;
689}
690
691//
692// Selects the best sample rate from |sampleRate|.
693// The result is stored in |p_result| and |p_codec_config|.
694// Returns true if a selection was made, otherwise false.
695//
696static bool select_best_sample_rate(uint16_t sampleRate,
697 tA2DP_AAC_CIE* p_result,
698 btav_a2dp_codec_config_t* p_codec_config) {
699 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_96000) {
700 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_96000;
701 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
702 return true;
703 }
704 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_88200) {
705 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_88200;
706 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
707 return true;
708 }
709 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_48000) {
710 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_48000;
711 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
712 return true;
713 }
714 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_44100) {
715 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_44100;
716 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
717 return true;
718 }
719 return false;
720}
721
722//
723// Selects the audio sample rate from |p_codec_audio_config|.
724// |sampleRate| contains the capability.
725// The result is stored in |p_result| and |p_codec_config|.
726// Returns true if a selection was made, otherwise false.
727//
728static bool select_audio_sample_rate(
729 const btav_a2dp_codec_config_t* p_codec_audio_config, uint16_t sampleRate,
730 tA2DP_AAC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
731 switch (p_codec_audio_config->sample_rate) {
732 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
733 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_44100) {
734 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_44100;
735 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
736 return true;
737 }
738 break;
739 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
740 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_48000) {
741 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_48000;
742 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
743 return true;
744 }
745 break;
746 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
747 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_88200) {
748 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_88200;
749 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
750 return true;
751 }
752 break;
753 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
754 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_96000) {
755 p_result->sampleRate = A2DP_AAC_SAMPLING_FREQ_96000;
756 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
757 return true;
758 }
759 break;
760 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
761 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
762 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
763 break;
764 }
765 return false;
766}
767
768//
769// Selects the best bits per sample from |bits_per_sample|.
770// |bits_per_sample| contains the capability.
771// The result is stored in |p_result| and |p_codec_config|.
772// Returns true if a selection was made, otherwise false.
773//
774static bool select_best_bits_per_sample(
775 btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_AAC_CIE* p_result,
776 btav_a2dp_codec_config_t* p_codec_config) {
777 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
778 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
779 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
780 return true;
781 }
782 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
783 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
784 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
785 return true;
786 }
787 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
788 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
789 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
790 return true;
791 }
792 return false;
793}
794
795//
796// Selects the audio bits per sample from |p_codec_audio_config|.
797// |bits_per_sample| contains the capability.
798// The result is stored in |p_result| and |p_codec_config|.
799// Returns true if a selection was made, otherwise false.
800//
801static bool select_audio_bits_per_sample(
802 const btav_a2dp_codec_config_t* p_codec_audio_config,
803 btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_AAC_CIE* p_result,
804 btav_a2dp_codec_config_t* p_codec_config) {
805 switch (p_codec_audio_config->bits_per_sample) {
806 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
807 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
808 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
809 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
810 return true;
811 }
812 break;
813 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
814 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
815 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
816 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
817 return true;
818 }
819 break;
820 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
821 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
822 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
823 p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
824 return true;
825 }
826 break;
827 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
828 break;
829 }
830 return false;
831}
832
833//
834// Selects the best channel mode from |channelMode|.
835// The result is stored in |p_result| and |p_codec_config|.
836// Returns true if a selection was made, otherwise false.
837//
838static bool select_best_channel_mode(uint8_t channelMode,
839 tA2DP_AAC_CIE* p_result,
840 btav_a2dp_codec_config_t* p_codec_config) {
841 if (channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
842 p_result->channelMode = A2DP_AAC_CHANNEL_MODE_STEREO;
843 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
844 return true;
845 }
846 if (channelMode & A2DP_AAC_CHANNEL_MODE_MONO) {
847 p_result->channelMode = A2DP_AAC_CHANNEL_MODE_MONO;
848 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
849 return true;
850 }
851 return false;
852}
853
854//
855// Selects the audio channel mode from |p_codec_audio_config|.
856// |channelMode| contains the capability.
857// The result is stored in |p_result| and |p_codec_config|.
858// Returns true if a selection was made, otherwise false.
859//
860static bool select_audio_channel_mode(
861 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t channelMode,
862 tA2DP_AAC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
863 switch (p_codec_audio_config->channel_mode) {
864 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
865 if (channelMode & A2DP_AAC_CHANNEL_MODE_MONO) {
866 p_result->channelMode = A2DP_AAC_CHANNEL_MODE_MONO;
867 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
868 return true;
869 }
870 break;
871 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
872 if (channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
873 p_result->channelMode = A2DP_AAC_CHANNEL_MODE_STEREO;
874 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
875 return true;
876 }
877 break;
878 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
879 break;
880 }
881
882 return false;
883}
884
885bool A2dpCodecConfigAac::setCodecConfig(const uint8_t* p_peer_codec_info,
886 bool is_capability,
887 uint8_t* p_result_codec_config) {
888 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
889 tA2DP_AAC_CIE sink_info_cie;
890 tA2DP_AAC_CIE result_config_cie;
891 uint8_t channelMode;
892 uint16_t sampleRate;
893 btav_a2dp_codec_bits_per_sample_t bits_per_sample;
894
895 // Save the internal state
896 btav_a2dp_codec_config_t saved_codec_config = codec_config_;
897 btav_a2dp_codec_config_t saved_codec_capability = codec_capability_;
Pavlin Radoslavov494e53e2017-01-25 17:00:23 -0800898 btav_a2dp_codec_config_t saved_codec_selectable_capability =
899 codec_selectable_capability_;
Pavlin Radoslavovd5f49602017-01-03 16:53:18 -0800900 btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_;
901 btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_;
902 uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE];
903 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
904 uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE];
905 memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_));
906 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
907 sizeof(ota_codec_peer_capability_));
908 memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_,
909 sizeof(ota_codec_peer_config_));
910
911 tA2DP_STATUS status =
912 A2DP_ParseInfoAac(&sink_info_cie, p_peer_codec_info, is_capability);
913 if (status != A2DP_SUCCESS) {
914 LOG_ERROR(LOG_TAG, "%s: can't parse peer's Sink capabilities: error = %d",
915 __func__, status);
916 goto fail;
917 }
918
919 //
920 // Build the preferred configuration
921 //
922 memset(&result_config_cie, 0, sizeof(result_config_cie));
923
924 // NOTE: Always assign the Object Type and Variable Bit Rate Support.
925 result_config_cie.objectType = a2dp_aac_caps.objectType;
926 result_config_cie.variableBitRateSupport =
927 a2dp_aac_caps.variableBitRateSupport;
928
929 // Set the bit rate to the smaller of the local and peer bit rate
930 // However, make sure the bit rate doesn't go beyond a certain threshold
931 result_config_cie.bitRate =
932 std::min(a2dp_aac_caps.bitRate, sink_info_cie.bitRate);
933 result_config_cie.bitRate = std::max(
934 result_config_cie.bitRate, static_cast<uint32_t>(A2DP_AAC_MIN_BITRATE));
935
936 //
937 // Select the sample frequency
938 //
939 sampleRate = a2dp_aac_caps.sampleRate & sink_info_cie.sampleRate;
940 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
941 switch (codec_user_config_.sample_rate) {
942 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
943 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_44100) {
944 result_config_cie.sampleRate = A2DP_AAC_SAMPLING_FREQ_44100;
945 codec_capability_.sample_rate = codec_user_config_.sample_rate;
946 codec_config_.sample_rate = codec_user_config_.sample_rate;
947 }
948 break;
949 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
950 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_48000) {
951 result_config_cie.sampleRate = A2DP_AAC_SAMPLING_FREQ_48000;
952 codec_capability_.sample_rate = codec_user_config_.sample_rate;
953 codec_config_.sample_rate = codec_user_config_.sample_rate;
954 }
955 break;
956 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
957 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_88200) {
958 result_config_cie.sampleRate = A2DP_AAC_SAMPLING_FREQ_88200;
959 codec_capability_.sample_rate = codec_user_config_.sample_rate;
960 codec_config_.sample_rate = codec_user_config_.sample_rate;
961 }
962 break;
963 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
964 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_96000) {
965 result_config_cie.sampleRate = A2DP_AAC_SAMPLING_FREQ_96000;
966 codec_capability_.sample_rate = codec_user_config_.sample_rate;
967 codec_config_.sample_rate = codec_user_config_.sample_rate;
968 }
969 break;
970 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
971 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
972 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
973 codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
974 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
975 break;
976 }
977
978 // Select the sample frequency if there is no user preference
979 do {
Pavlin Radoslavov494e53e2017-01-25 17:00:23 -0800980 // Compute the selectable capability
981 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_44100) {
982 codec_selectable_capability_.sample_rate |=
983 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
984 }
985 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_48000) {
986 codec_selectable_capability_.sample_rate |=
987 BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
988 }
989 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_88200) {
990 codec_selectable_capability_.sample_rate |=
991 BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
992 }
993 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_96000) {
994 codec_selectable_capability_.sample_rate |=
995 BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
996 }
997
Pavlin Radoslavovd5f49602017-01-03 16:53:18 -0800998 if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break;
999
1000 // Compute the common capability
1001 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_44100)
1002 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1003 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_48000)
1004 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1005 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_88200)
1006 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
1007 if (sampleRate & A2DP_AAC_SAMPLING_FREQ_96000)
1008 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
1009
1010 // No user preference - try the codec audio config
1011 if (select_audio_sample_rate(&codec_audio_config_, sampleRate,
1012 &result_config_cie, &codec_config_)) {
1013 break;
1014 }
1015
1016 // No user preference - try the default config
1017 if (select_best_sample_rate(
1018 a2dp_aac_default_config.sampleRate & sink_info_cie.sampleRate,
1019 &result_config_cie, &codec_config_)) {
1020 break;
1021 }
1022
1023 // No user preference - use the best match
1024 if (select_best_sample_rate(sampleRate, &result_config_cie,
1025 &codec_config_)) {
1026 break;
1027 }
1028 } while (false);
1029 if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) {
1030 LOG_ERROR(LOG_TAG,
1031 "%s: cannot match sample frequency: source caps = 0x%x "
1032 "sink info = 0x%x",
1033 __func__, a2dp_aac_caps.sampleRate, sink_info_cie.sampleRate);
1034 goto fail;
1035 }
1036
1037 //
1038 // Select the bits per sample
1039 //
1040 // NOTE: this information is NOT included in the AAC A2DP codec description
1041 // that is sent OTA.
1042 bits_per_sample = a2dp_aac_caps.bits_per_sample;
1043 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1044 switch (codec_user_config_.bits_per_sample) {
1045 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
1046 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
1047 result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1048 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1049 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1050 }
1051 break;
1052 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
1053 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
1054 result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1055 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1056 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1057 }
1058 break;
1059 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
1060 if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
1061 result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1062 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1063 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1064 }
1065 break;
1066 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
1067 result_config_cie.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1068 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1069 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1070 break;
1071 }
1072
1073 // Select the bits per sample if there is no user preference
1074 do {
Pavlin Radoslavov494e53e2017-01-25 17:00:23 -08001075 // Compute the selectable capability
1076 codec_selectable_capability_.bits_per_sample =
1077 a2dp_aac_caps.bits_per_sample;
1078
Pavlin Radoslavovd5f49602017-01-03 16:53:18 -08001079 if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE)
1080 break;
1081
1082 // Compute the common capability
1083 codec_capability_.bits_per_sample = bits_per_sample;
1084
1085 // No user preference - the the codec audio config
1086 if (select_audio_bits_per_sample(&codec_audio_config_,
1087 a2dp_aac_caps.bits_per_sample,
1088 &result_config_cie, &codec_config_)) {
1089 break;
1090 }
1091
1092 // No user preference - try the default config
1093 if (select_best_bits_per_sample(a2dp_aac_default_config.bits_per_sample,
1094 &result_config_cie, &codec_config_)) {
1095 break;
1096 }
1097
1098 // No user preference - use the best match
1099 if (select_best_bits_per_sample(a2dp_aac_caps.bits_per_sample,
1100 &result_config_cie, &codec_config_)) {
1101 break;
1102 }
1103 } while (false);
1104 if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) {
1105 LOG_ERROR(LOG_TAG,
1106 "%s: cannot match bits per sample: default = 0x%x "
1107 "user preference = 0x%x",
1108 __func__, a2dp_aac_default_config.bits_per_sample,
1109 codec_user_config_.bits_per_sample);
1110 goto fail;
1111 }
1112
1113 //
1114 // Select the channel mode
1115 //
1116 channelMode = a2dp_aac_caps.channelMode & sink_info_cie.channelMode;
1117 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1118 switch (codec_user_config_.channel_mode) {
1119 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
1120 if (channelMode & A2DP_AAC_CHANNEL_MODE_MONO) {
1121 result_config_cie.channelMode = A2DP_AAC_CHANNEL_MODE_MONO;
1122 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1123 codec_config_.channel_mode = codec_user_config_.channel_mode;
1124 }
1125 break;
1126 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
1127 if (channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
1128 result_config_cie.channelMode = A2DP_AAC_CHANNEL_MODE_STEREO;
1129 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1130 codec_config_.channel_mode = codec_user_config_.channel_mode;
1131 }
1132 break;
1133 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1134 codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1135 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1136 break;
1137 }
1138
1139 // Select the channel mode if there is no user preference
1140 do {
Pavlin Radoslavov494e53e2017-01-25 17:00:23 -08001141 // Compute the selectable capability
1142 if (channelMode & A2DP_AAC_CHANNEL_MODE_MONO) {
1143 codec_selectable_capability_.channel_mode |=
1144 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1145 }
1146 if (channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
1147 codec_selectable_capability_.channel_mode |=
1148 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1149 }
1150
Pavlin Radoslavovd5f49602017-01-03 16:53:18 -08001151 if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break;
1152
1153 // Compute the common capability
1154 if (channelMode & A2DP_AAC_CHANNEL_MODE_MONO)
1155 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1156 if (channelMode & A2DP_AAC_CHANNEL_MODE_STEREO) {
1157 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1158 }
1159
1160 // No user preference - try the codec audio config
1161 if (select_audio_channel_mode(&codec_audio_config_, channelMode,
1162 &result_config_cie, &codec_config_)) {
1163 break;
1164 }
1165
1166 // No user preference - try the default config
1167 if (select_best_channel_mode(
1168 a2dp_aac_default_config.channelMode & sink_info_cie.channelMode,
1169 &result_config_cie, &codec_config_)) {
1170 break;
1171 }
1172
1173 // No user preference - use the best match
1174 if (select_best_channel_mode(channelMode, &result_config_cie,
1175 &codec_config_)) {
1176 break;
1177 }
1178 } while (false);
1179 if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) {
1180 LOG_ERROR(LOG_TAG,
1181 "%s: cannot match channel mode: source caps = 0x%x "
1182 "sink info = 0x%x",
1183 __func__, a2dp_aac_caps.channelMode, sink_info_cie.channelMode);
1184 goto fail;
1185 }
1186
1187 if (A2DP_BuildInfoAac(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1188 p_result_codec_config) != A2DP_SUCCESS) {
1189 goto fail;
1190 }
1191
1192 //
1193 // Copy the codec-specific fields if they are not zero
1194 //
1195 if (codec_user_config_.codec_specific_1 != 0)
1196 codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
1197 if (codec_user_config_.codec_specific_2 != 0)
1198 codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2;
1199 if (codec_user_config_.codec_specific_3 != 0)
1200 codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3;
1201 if (codec_user_config_.codec_specific_4 != 0)
1202 codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4;
1203
1204 // Create a local copy of the peer codec capability, and the
1205 // result codec config.
1206 if (is_capability) {
1207 status = A2DP_BuildInfoAac(AVDT_MEDIA_TYPE_AUDIO, &sink_info_cie,
1208 ota_codec_peer_capability_);
1209 } else {
1210 status = A2DP_BuildInfoAac(AVDT_MEDIA_TYPE_AUDIO, &sink_info_cie,
1211 ota_codec_peer_config_);
1212 }
1213 CHECK(status == A2DP_SUCCESS);
1214 status = A2DP_BuildInfoAac(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1215 ota_codec_config_);
1216 CHECK(status == A2DP_SUCCESS);
1217 return true;
1218
1219fail:
1220 // Restore the internal state
1221 codec_config_ = saved_codec_config;
1222 codec_capability_ = saved_codec_capability;
Pavlin Radoslavov494e53e2017-01-25 17:00:23 -08001223 codec_selectable_capability_ = saved_codec_selectable_capability;
Pavlin Radoslavovd5f49602017-01-03 16:53:18 -08001224 codec_user_config_ = saved_codec_user_config;
1225 codec_audio_config_ = saved_codec_audio_config;
1226 memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_));
1227 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1228 sizeof(ota_codec_peer_capability_));
1229 memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config,
1230 sizeof(ota_codec_peer_config_));
1231 return false;
1232}