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