1 /******************************************************************************
2 *
3 * Copyright 2002-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * Utility functions to help build and parse SBC Codec Information Element
22 * and Media Payload.
23 *
24 ******************************************************************************/
25
26 #define LOG_TAG "a2dp_sbc"
27
28 #include "a2dp_sbc.h"
29
30 #include <base/logging.h>
31 #include <string.h>
32
33 #include "a2dp_sbc_decoder.h"
34 #include "a2dp_sbc_encoder.h"
35 #include "bt_utils.h"
36 #include "embdrv/sbc/encoder/include/sbc_encoder.h"
37 #include "osi/include/log.h"
38 #include "osi/include/osi.h"
39 #include "stack/include/bt_hdr.h"
40
41 #define A2DP_SBC_MAX_BITPOOL 53
42
43 /* data type for the SBC Codec Information Element */
44 typedef struct {
45 uint8_t samp_freq; /* Sampling frequency */
46 uint8_t ch_mode; /* Channel mode */
47 uint8_t block_len; /* Block length */
48 uint8_t num_subbands; /* Number of subbands */
49 uint8_t alloc_method; /* Allocation method */
50 uint8_t min_bitpool; /* Minimum bitpool */
51 uint8_t max_bitpool; /* Maximum bitpool */
52 btav_a2dp_codec_bits_per_sample_t bits_per_sample;
53 } tA2DP_SBC_CIE;
54
55 /* SBC Source codec capabilities */
56 static const tA2DP_SBC_CIE a2dp_sbc_source_caps = {
57 (A2DP_SBC_IE_SAMP_FREQ_44), /* samp_freq */
58 (A2DP_SBC_IE_CH_MD_MONO | A2DP_SBC_IE_CH_MD_JOINT), /* ch_mode */
59 (A2DP_SBC_IE_BLOCKS_16 | A2DP_SBC_IE_BLOCKS_12 | A2DP_SBC_IE_BLOCKS_8 |
60 A2DP_SBC_IE_BLOCKS_4), /* block_len */
61 A2DP_SBC_IE_SUBBAND_8, /* num_subbands */
62 A2DP_SBC_IE_ALLOC_MD_L, /* alloc_method */
63 A2DP_SBC_IE_MIN_BITPOOL, /* min_bitpool */
64 A2DP_SBC_MAX_BITPOOL, /* max_bitpool */
65 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 /* bits_per_sample */
66 };
67
68 /* SBC Sink codec capabilities */
69 static const tA2DP_SBC_CIE a2dp_sbc_sink_caps = {
70 (A2DP_SBC_IE_SAMP_FREQ_48 | A2DP_SBC_IE_SAMP_FREQ_44), /* samp_freq */
71 (A2DP_SBC_IE_CH_MD_MONO | A2DP_SBC_IE_CH_MD_STEREO |
72 A2DP_SBC_IE_CH_MD_JOINT | A2DP_SBC_IE_CH_MD_DUAL), /* ch_mode */
73 (A2DP_SBC_IE_BLOCKS_16 | A2DP_SBC_IE_BLOCKS_12 | A2DP_SBC_IE_BLOCKS_8 |
74 A2DP_SBC_IE_BLOCKS_4), /* block_len */
75 (A2DP_SBC_IE_SUBBAND_4 | A2DP_SBC_IE_SUBBAND_8), /* num_subbands */
76 (A2DP_SBC_IE_ALLOC_MD_L | A2DP_SBC_IE_ALLOC_MD_S), /* alloc_method */
77 A2DP_SBC_IE_MIN_BITPOOL, /* min_bitpool */
78 A2DP_SBC_MAX_BITPOOL, /* max_bitpool */
79 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 /* bits_per_sample */
80 };
81
82 /* Default SBC codec configuration */
83 const tA2DP_SBC_CIE a2dp_sbc_default_config = {
84 A2DP_SBC_IE_SAMP_FREQ_44, /* samp_freq */
85 A2DP_SBC_IE_CH_MD_JOINT, /* ch_mode */
86 A2DP_SBC_IE_BLOCKS_16, /* block_len */
87 A2DP_SBC_IE_SUBBAND_8, /* num_subbands */
88 A2DP_SBC_IE_ALLOC_MD_L, /* alloc_method */
89 A2DP_SBC_IE_MIN_BITPOOL, /* min_bitpool */
90 A2DP_SBC_MAX_BITPOOL, /* max_bitpool */
91 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 /* bits_per_sample */
92 };
93
94 static const tA2DP_ENCODER_INTERFACE a2dp_encoder_interface_sbc = {
95 a2dp_sbc_encoder_init,
96 a2dp_sbc_encoder_cleanup,
97 a2dp_sbc_feeding_reset,
98 a2dp_sbc_feeding_flush,
99 a2dp_sbc_get_encoder_interval_ms,
100 a2dp_sbc_get_effective_frame_size,
101 a2dp_sbc_send_frames,
102 nullptr // set_transmit_queue_length
103 };
104
105 static const tA2DP_DECODER_INTERFACE a2dp_decoder_interface_sbc = {
106 a2dp_sbc_decoder_init,
107 a2dp_sbc_decoder_cleanup,
108 a2dp_sbc_decoder_decode_packet,
109 nullptr, // decoder_start
110 nullptr, // decoder_suspend
111 nullptr, // decoder_configure
112 };
113
114 static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilitySbc(
115 const tA2DP_SBC_CIE* p_cap, const uint8_t* p_codec_info,
116 bool is_capability);
117 static void A2DP_ParseMplHeaderSbc(uint8_t* p_src, bool* p_frag, bool* p_start,
118 bool* p_last, uint8_t* p_num);
119
120 // Builds the SBC Media Codec Capabilities byte sequence beginning from the
121 // LOSC octet. |media_type| is the media type |AVDT_MEDIA_TYPE_*|.
122 // |p_ie| is a pointer to the SBC Codec Information Element information.
123 // The result is stored in |p_result|. Returns A2DP_SUCCESS on success,
124 // otherwise the corresponding A2DP error status code.
A2DP_BuildInfoSbc(uint8_t media_type,const tA2DP_SBC_CIE * p_ie,uint8_t * p_result)125 static tA2DP_STATUS A2DP_BuildInfoSbc(uint8_t media_type,
126 const tA2DP_SBC_CIE* p_ie,
127 uint8_t* p_result) {
128 if (p_ie == NULL || p_result == NULL ||
129 (p_ie->samp_freq & ~A2DP_SBC_IE_SAMP_FREQ_MSK) ||
130 (p_ie->ch_mode & ~A2DP_SBC_IE_CH_MD_MSK) ||
131 (p_ie->block_len & ~A2DP_SBC_IE_BLOCKS_MSK) ||
132 (p_ie->num_subbands & ~A2DP_SBC_IE_SUBBAND_MSK) ||
133 (p_ie->alloc_method & ~A2DP_SBC_IE_ALLOC_MD_MSK) ||
134 (p_ie->min_bitpool > p_ie->max_bitpool) ||
135 (p_ie->min_bitpool < A2DP_SBC_IE_MIN_BITPOOL) ||
136 (p_ie->min_bitpool > A2DP_SBC_IE_MAX_BITPOOL) ||
137 (p_ie->max_bitpool < A2DP_SBC_IE_MIN_BITPOOL) ||
138 (p_ie->max_bitpool > A2DP_SBC_IE_MAX_BITPOOL)) {
139 /* if any unused bit is set */
140 return A2DP_INVALID_PARAMS;
141 }
142
143 *p_result++ = A2DP_SBC_INFO_LEN;
144 *p_result++ = (media_type << 4);
145 *p_result++ = A2DP_MEDIA_CT_SBC;
146
147 /* Media Codec Specific Information Element */
148 *p_result++ = p_ie->samp_freq | p_ie->ch_mode;
149
150 *p_result++ = p_ie->block_len | p_ie->num_subbands | p_ie->alloc_method;
151
152 *p_result++ = p_ie->min_bitpool;
153 *p_result = p_ie->max_bitpool;
154
155 return A2DP_SUCCESS;
156 }
157
158 // Parses the SBC Media Codec Capabilities byte sequence beginning from the
159 // LOSC octet. The result is stored in |p_ie|. The byte sequence to parse is
160 // |p_codec_info|. If |is_capability| is true, the byte sequence contains
161 // codec capability.
162 // Returns A2DP_SUCCESS on success, otherwise the corresponding A2DP error
163 // status code.
A2DP_ParseInfoSbc(tA2DP_SBC_CIE * p_ie,const uint8_t * p_codec_info,bool is_capability)164 static tA2DP_STATUS A2DP_ParseInfoSbc(tA2DP_SBC_CIE* p_ie,
165 const uint8_t* p_codec_info,
166 bool is_capability) {
167 uint8_t losc;
168 uint8_t media_type;
169 tA2DP_CODEC_TYPE codec_type;
170
171 if (p_ie == NULL || p_codec_info == NULL) return A2DP_INVALID_PARAMS;
172
173 // Check the codec capability length
174 losc = *p_codec_info++;
175 if (losc != A2DP_SBC_INFO_LEN) return A2DP_WRONG_CODEC;
176
177 media_type = (*p_codec_info++) >> 4;
178 codec_type = *p_codec_info++;
179 /* Check the Media Type and Media Codec Type */
180 if (media_type != AVDT_MEDIA_TYPE_AUDIO || codec_type != A2DP_MEDIA_CT_SBC) {
181 return A2DP_WRONG_CODEC;
182 }
183
184 p_ie->samp_freq = *p_codec_info & A2DP_SBC_IE_SAMP_FREQ_MSK;
185 p_ie->ch_mode = *p_codec_info & A2DP_SBC_IE_CH_MD_MSK;
186 p_codec_info++;
187 p_ie->block_len = *p_codec_info & A2DP_SBC_IE_BLOCKS_MSK;
188 p_ie->num_subbands = *p_codec_info & A2DP_SBC_IE_SUBBAND_MSK;
189 p_ie->alloc_method = *p_codec_info & A2DP_SBC_IE_ALLOC_MD_MSK;
190 p_codec_info++;
191 p_ie->min_bitpool = *p_codec_info++;
192 p_ie->max_bitpool = *p_codec_info++;
193 if (p_ie->min_bitpool < A2DP_SBC_IE_MIN_BITPOOL ||
194 p_ie->min_bitpool > A2DP_SBC_IE_MAX_BITPOOL) {
195 return A2DP_BAD_MIN_BITPOOL;
196 }
197
198 if (p_ie->max_bitpool < A2DP_SBC_IE_MIN_BITPOOL ||
199 p_ie->max_bitpool > A2DP_SBC_IE_MAX_BITPOOL ||
200 p_ie->max_bitpool < p_ie->min_bitpool) {
201 return A2DP_BAD_MAX_BITPOOL;
202 }
203
204 if (is_capability) {
205 // NOTE: The checks here are very liberal. We should be using more
206 // pedantic checks specific to the SRC or SNK as specified in the spec.
207 if (A2DP_BitsSet(p_ie->samp_freq) == A2DP_SET_ZERO_BIT)
208 return A2DP_BAD_SAMP_FREQ;
209 if (A2DP_BitsSet(p_ie->ch_mode) == A2DP_SET_ZERO_BIT)
210 return A2DP_BAD_CH_MODE;
211 if (A2DP_BitsSet(p_ie->block_len) == A2DP_SET_ZERO_BIT)
212 return A2DP_BAD_BLOCK_LEN;
213 if (A2DP_BitsSet(p_ie->num_subbands) == A2DP_SET_ZERO_BIT)
214 return A2DP_BAD_SUBBANDS;
215 if (A2DP_BitsSet(p_ie->alloc_method) == A2DP_SET_ZERO_BIT)
216 return A2DP_BAD_ALLOC_METHOD;
217
218 return A2DP_SUCCESS;
219 }
220
221 if (A2DP_BitsSet(p_ie->samp_freq) != A2DP_SET_ONE_BIT)
222 return A2DP_BAD_SAMP_FREQ;
223 if (A2DP_BitsSet(p_ie->ch_mode) != A2DP_SET_ONE_BIT) return A2DP_BAD_CH_MODE;
224 if (A2DP_BitsSet(p_ie->block_len) != A2DP_SET_ONE_BIT)
225 return A2DP_BAD_BLOCK_LEN;
226 if (A2DP_BitsSet(p_ie->num_subbands) != A2DP_SET_ONE_BIT)
227 return A2DP_BAD_SUBBANDS;
228 if (A2DP_BitsSet(p_ie->alloc_method) != A2DP_SET_ONE_BIT)
229 return A2DP_BAD_ALLOC_METHOD;
230
231 return A2DP_SUCCESS;
232 }
233
234 // Build the SBC Media Payload Header.
235 // |p_dst| points to the location where the header should be written to.
236 // If |frag| is true, the media payload frame is fragmented.
237 // |start| is true for the first packet of a fragmented frame.
238 // |last| is true for the last packet of a fragmented frame.
239 // If |frag| is false, |num| is the number of number of frames in the packet,
240 // otherwise is the number of remaining fragments (including this one).
A2DP_BuildMediaPayloadHeaderSbc(uint8_t * p_dst,bool frag,bool start,bool last,uint8_t num)241 static void A2DP_BuildMediaPayloadHeaderSbc(uint8_t* p_dst, bool frag,
242 bool start, bool last,
243 uint8_t num) {
244 if (p_dst == NULL) return;
245
246 *p_dst = 0;
247 if (frag) *p_dst |= A2DP_SBC_HDR_F_MSK;
248 if (start) *p_dst |= A2DP_SBC_HDR_S_MSK;
249 if (last) *p_dst |= A2DP_SBC_HDR_L_MSK;
250 *p_dst |= (A2DP_SBC_HDR_NUM_MSK & num);
251 }
252
253 /******************************************************************************
254 *
255 * Function A2DP_ParseMplHeaderSbc
256 *
257 * Description This function is called by an application to parse
258 * the SBC Media Payload header.
259 * Input Parameters:
260 * p_src: the byte sequence to parse..
261 *
262 * Output Parameters:
263 * frag: 1, if fragmented. 0, otherwise.
264 *
265 * start: 1, if the starting packet of a fragmented frame.
266 *
267 * last: 1, if the last packet of a fragmented frame.
268 *
269 * num: If frag is 1, this is the number of remaining
270 * fragments
271 * (including this fragment) of this frame.
272 * If frag is 0, this is the number of frames in
273 * this packet.
274 *
275 * Returns void.
276 *****************************************************************************/
A2DP_ParseMplHeaderSbc(uint8_t * p_src,bool * p_frag,bool * p_start,bool * p_last,uint8_t * p_num)277 UNUSED_ATTR static void A2DP_ParseMplHeaderSbc(uint8_t* p_src, bool* p_frag,
278 bool* p_start, bool* p_last,
279 uint8_t* p_num) {
280 if (p_src && p_frag && p_start && p_last && p_num) {
281 *p_frag = (*p_src & A2DP_SBC_HDR_F_MSK) ? true : false;
282 *p_start = (*p_src & A2DP_SBC_HDR_S_MSK) ? true : false;
283 *p_last = (*p_src & A2DP_SBC_HDR_L_MSK) ? true : false;
284 *p_num = (*p_src & A2DP_SBC_HDR_NUM_MSK);
285 }
286 }
287
A2DP_CodecNameSbc(UNUSED_ATTR const uint8_t * p_codec_info)288 const char* A2DP_CodecNameSbc(UNUSED_ATTR const uint8_t* p_codec_info) {
289 return "SBC";
290 }
291
A2DP_IsSourceCodecValidSbc(const uint8_t * p_codec_info)292 bool A2DP_IsSourceCodecValidSbc(const uint8_t* p_codec_info) {
293 tA2DP_SBC_CIE cfg_cie;
294
295 /* Use a liberal check when parsing the codec info */
296 return (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
297 (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
298 }
299
A2DP_IsSinkCodecValidSbc(const uint8_t * p_codec_info)300 bool A2DP_IsSinkCodecValidSbc(const uint8_t* p_codec_info) {
301 tA2DP_SBC_CIE cfg_cie;
302
303 /* Use a liberal check when parsing the codec info */
304 return (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
305 (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
306 }
307
A2DP_IsPeerSourceCodecValidSbc(const uint8_t * p_codec_info)308 bool A2DP_IsPeerSourceCodecValidSbc(const uint8_t* p_codec_info) {
309 tA2DP_SBC_CIE cfg_cie;
310
311 /* Use a liberal check when parsing the codec info */
312 return (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
313 (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
314 }
315
A2DP_IsPeerSinkCodecValidSbc(const uint8_t * p_codec_info)316 bool A2DP_IsPeerSinkCodecValidSbc(const uint8_t* p_codec_info) {
317 tA2DP_SBC_CIE cfg_cie;
318
319 /* Use a liberal check when parsing the codec info */
320 return (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
321 (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
322 }
323
A2DP_IsSinkCodecSupportedSbc(const uint8_t * p_codec_info)324 bool A2DP_IsSinkCodecSupportedSbc(const uint8_t* p_codec_info) {
325 return (A2DP_CodecInfoMatchesCapabilitySbc(&a2dp_sbc_sink_caps, p_codec_info,
326 false) == A2DP_SUCCESS);
327 }
328
A2DP_IsPeerSourceCodecSupportedSbc(const uint8_t * p_codec_info)329 bool A2DP_IsPeerSourceCodecSupportedSbc(const uint8_t* p_codec_info) {
330 return (A2DP_CodecInfoMatchesCapabilitySbc(&a2dp_sbc_sink_caps, p_codec_info,
331 true) == A2DP_SUCCESS);
332 }
333
A2DP_InitDefaultCodecSbc(uint8_t * p_codec_info)334 void A2DP_InitDefaultCodecSbc(uint8_t* p_codec_info) {
335 if (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &a2dp_sbc_default_config,
336 p_codec_info) != A2DP_SUCCESS) {
337 LOG_ERROR("%s: A2DP_BuildInfoSbc failed", __func__);
338 }
339 }
340
341 // Checks whether A2DP SBC codec configuration matches with a device's codec
342 // capabilities. |p_cap| is the SBC codec configuration. |p_codec_info| is
343 // the device's codec capabilities. |is_capability| is true if
344 // |p_codec_info| contains A2DP codec capability.
345 // Returns A2DP_SUCCESS if the codec configuration matches with capabilities,
346 // otherwise the corresponding A2DP error status code.
A2DP_CodecInfoMatchesCapabilitySbc(const tA2DP_SBC_CIE * p_cap,const uint8_t * p_codec_info,bool is_capability)347 static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilitySbc(
348 const tA2DP_SBC_CIE* p_cap, const uint8_t* p_codec_info,
349 bool is_capability) {
350 tA2DP_STATUS status;
351 tA2DP_SBC_CIE cfg_cie;
352
353 /* parse configuration */
354 status = A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, is_capability);
355 if (status != A2DP_SUCCESS) {
356 LOG_ERROR("%s: parsing failed %d", __func__, status);
357 return status;
358 }
359
360 /* verify that each parameter is in range */
361
362 LOG_VERBOSE("%s: FREQ peer: 0x%x, capability 0x%x", __func__,
363 cfg_cie.samp_freq, p_cap->samp_freq);
364 LOG_VERBOSE("%s: CH_MODE peer: 0x%x, capability 0x%x", __func__,
365 cfg_cie.ch_mode, p_cap->ch_mode);
366 LOG_VERBOSE("%s: BLOCK_LEN peer: 0x%x, capability 0x%x", __func__,
367 cfg_cie.block_len, p_cap->block_len);
368 LOG_VERBOSE("%s: SUB_BAND peer: 0x%x, capability 0x%x", __func__,
369 cfg_cie.num_subbands, p_cap->num_subbands);
370 LOG_VERBOSE("%s: ALLOC_METHOD peer: 0x%x, capability 0x%x", __func__,
371 cfg_cie.alloc_method, p_cap->alloc_method);
372 LOG_VERBOSE("%s: MIN_BitPool peer: 0x%x, capability 0x%x", __func__,
373 cfg_cie.min_bitpool, p_cap->min_bitpool);
374 LOG_VERBOSE("%s: MAX_BitPool peer: 0x%x, capability 0x%x", __func__,
375 cfg_cie.max_bitpool, p_cap->max_bitpool);
376
377 /* sampling frequency */
378 if ((cfg_cie.samp_freq & p_cap->samp_freq) == 0) return A2DP_NS_SAMP_FREQ;
379
380 /* channel mode */
381 if ((cfg_cie.ch_mode & p_cap->ch_mode) == 0) return A2DP_NS_CH_MODE;
382
383 /* block length */
384 if ((cfg_cie.block_len & p_cap->block_len) == 0) return A2DP_BAD_BLOCK_LEN;
385
386 /* subbands */
387 if ((cfg_cie.num_subbands & p_cap->num_subbands) == 0)
388 return A2DP_NS_SUBBANDS;
389
390 /* allocation method */
391 if ((cfg_cie.alloc_method & p_cap->alloc_method) == 0)
392 return A2DP_NS_ALLOC_METHOD;
393
394 /* min bitpool */
395 if (cfg_cie.min_bitpool > p_cap->max_bitpool) return A2DP_NS_MIN_BITPOOL;
396
397 /* max bitpool */
398 if (cfg_cie.max_bitpool < p_cap->min_bitpool) return A2DP_NS_MAX_BITPOOL;
399
400 return A2DP_SUCCESS;
401 }
402
A2DP_CodecTypeEqualsSbc(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)403 bool A2DP_CodecTypeEqualsSbc(const uint8_t* p_codec_info_a,
404 const uint8_t* p_codec_info_b) {
405 tA2DP_SBC_CIE sbc_cie_a;
406 tA2DP_SBC_CIE sbc_cie_b;
407
408 // Check whether the codec info contains valid data
409 tA2DP_STATUS a2dp_status =
410 A2DP_ParseInfoSbc(&sbc_cie_a, p_codec_info_a, true);
411 if (a2dp_status != A2DP_SUCCESS) {
412 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
413 return false;
414 }
415 a2dp_status = A2DP_ParseInfoSbc(&sbc_cie_b, p_codec_info_b, true);
416 if (a2dp_status != A2DP_SUCCESS) {
417 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
418 return false;
419 }
420
421 tA2DP_CODEC_TYPE codec_type_a = A2DP_GetCodecType(p_codec_info_a);
422 tA2DP_CODEC_TYPE codec_type_b = A2DP_GetCodecType(p_codec_info_b);
423
424 return (codec_type_a == codec_type_b) && (codec_type_a == A2DP_MEDIA_CT_SBC);
425 }
426
A2DP_CodecEqualsSbc(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)427 bool A2DP_CodecEqualsSbc(const uint8_t* p_codec_info_a,
428 const uint8_t* p_codec_info_b) {
429 tA2DP_SBC_CIE sbc_cie_a;
430 tA2DP_SBC_CIE sbc_cie_b;
431
432 // Check whether the codec info contains valid data
433 tA2DP_STATUS a2dp_status =
434 A2DP_ParseInfoSbc(&sbc_cie_a, p_codec_info_a, true);
435 if (a2dp_status != A2DP_SUCCESS) {
436 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
437 return false;
438 }
439 a2dp_status = A2DP_ParseInfoSbc(&sbc_cie_b, p_codec_info_b, true);
440 if (a2dp_status != A2DP_SUCCESS) {
441 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
442 return false;
443 }
444
445 tA2DP_CODEC_TYPE codec_type_a = A2DP_GetCodecType(p_codec_info_a);
446 tA2DP_CODEC_TYPE codec_type_b = A2DP_GetCodecType(p_codec_info_b);
447
448 if ((codec_type_a != codec_type_b) || (codec_type_a != A2DP_MEDIA_CT_SBC))
449 return false;
450
451 return (sbc_cie_a.samp_freq == sbc_cie_b.samp_freq) &&
452 (sbc_cie_a.ch_mode == sbc_cie_b.ch_mode) &&
453 (sbc_cie_a.block_len == sbc_cie_b.block_len) &&
454 (sbc_cie_a.num_subbands == sbc_cie_b.num_subbands) &&
455 (sbc_cie_a.alloc_method == sbc_cie_b.alloc_method) &&
456 (sbc_cie_a.min_bitpool == sbc_cie_b.min_bitpool) &&
457 (sbc_cie_a.max_bitpool == sbc_cie_b.max_bitpool);
458 }
459
A2DP_GetTrackSampleRateSbc(const uint8_t * p_codec_info)460 int A2DP_GetTrackSampleRateSbc(const uint8_t* p_codec_info) {
461 tA2DP_SBC_CIE sbc_cie;
462
463 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
464 if (a2dp_status != A2DP_SUCCESS) {
465 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
466 return -1;
467 }
468
469 switch (sbc_cie.samp_freq) {
470 case A2DP_SBC_IE_SAMP_FREQ_16:
471 return 16000;
472 case A2DP_SBC_IE_SAMP_FREQ_32:
473 return 32000;
474 case A2DP_SBC_IE_SAMP_FREQ_44:
475 return 44100;
476 case A2DP_SBC_IE_SAMP_FREQ_48:
477 return 48000;
478 default:
479 break;
480 }
481
482 return -1;
483 }
484
A2DP_GetTrackBitsPerSampleSbc(const uint8_t * p_codec_info)485 int A2DP_GetTrackBitsPerSampleSbc(const uint8_t* p_codec_info) {
486 tA2DP_SBC_CIE sbc_cie;
487
488 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
489 if (a2dp_status != A2DP_SUCCESS) {
490 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
491 return -1;
492 }
493
494 // NOTE: The bits per sample never changes for SBC
495 return 16;
496 }
497
A2DP_GetTrackChannelCountSbc(const uint8_t * p_codec_info)498 int A2DP_GetTrackChannelCountSbc(const uint8_t* p_codec_info) {
499 tA2DP_SBC_CIE sbc_cie;
500
501 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
502 if (a2dp_status != A2DP_SUCCESS) {
503 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
504 return -1;
505 }
506
507 switch (sbc_cie.ch_mode) {
508 case A2DP_SBC_IE_CH_MD_MONO:
509 return 1;
510 case A2DP_SBC_IE_CH_MD_DUAL:
511 case A2DP_SBC_IE_CH_MD_STEREO:
512 case A2DP_SBC_IE_CH_MD_JOINT:
513 return 2;
514 default:
515 break;
516 }
517
518 return -1;
519 }
520
A2DP_GetNumberOfSubbandsSbc(const uint8_t * p_codec_info)521 int A2DP_GetNumberOfSubbandsSbc(const uint8_t* p_codec_info) {
522 tA2DP_SBC_CIE sbc_cie;
523
524 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
525 if (a2dp_status != A2DP_SUCCESS) {
526 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
527 return -1;
528 }
529
530 switch (sbc_cie.num_subbands) {
531 case A2DP_SBC_IE_SUBBAND_4:
532 return 4;
533 case A2DP_SBC_IE_SUBBAND_8:
534 return 8;
535 default:
536 break;
537 }
538
539 return -1;
540 }
541
A2DP_GetNumberOfBlocksSbc(const uint8_t * p_codec_info)542 int A2DP_GetNumberOfBlocksSbc(const uint8_t* p_codec_info) {
543 tA2DP_SBC_CIE sbc_cie;
544
545 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
546 if (a2dp_status != A2DP_SUCCESS) {
547 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
548 return -1;
549 }
550
551 switch (sbc_cie.block_len) {
552 case A2DP_SBC_IE_BLOCKS_4:
553 return 4;
554 case A2DP_SBC_IE_BLOCKS_8:
555 return 8;
556 case A2DP_SBC_IE_BLOCKS_12:
557 return 12;
558 case A2DP_SBC_IE_BLOCKS_16:
559 return 16;
560 default:
561 break;
562 }
563
564 return -1;
565 }
566
A2DP_GetAllocationMethodCodeSbc(const uint8_t * p_codec_info)567 int A2DP_GetAllocationMethodCodeSbc(const uint8_t* p_codec_info) {
568 tA2DP_SBC_CIE sbc_cie;
569
570 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
571 if (a2dp_status != A2DP_SUCCESS) {
572 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
573 return -1;
574 }
575
576 switch (sbc_cie.alloc_method) {
577 case A2DP_SBC_IE_ALLOC_MD_S:
578 return SBC_SNR;
579 case A2DP_SBC_IE_ALLOC_MD_L:
580 return SBC_LOUDNESS;
581 default:
582 break;
583 }
584
585 return -1;
586 }
587
A2DP_GetChannelModeCodeSbc(const uint8_t * p_codec_info)588 int A2DP_GetChannelModeCodeSbc(const uint8_t* p_codec_info) {
589 tA2DP_SBC_CIE sbc_cie;
590
591 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
592 if (a2dp_status != A2DP_SUCCESS) {
593 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
594 return -1;
595 }
596
597 switch (sbc_cie.ch_mode) {
598 case A2DP_SBC_IE_CH_MD_MONO:
599 return SBC_MONO;
600 case A2DP_SBC_IE_CH_MD_DUAL:
601 return SBC_DUAL;
602 case A2DP_SBC_IE_CH_MD_STEREO:
603 return SBC_STEREO;
604 case A2DP_SBC_IE_CH_MD_JOINT:
605 return SBC_JOINT_STEREO;
606 default:
607 break;
608 }
609
610 return -1;
611 }
612
A2DP_GetSamplingFrequencyCodeSbc(const uint8_t * p_codec_info)613 int A2DP_GetSamplingFrequencyCodeSbc(const uint8_t* p_codec_info) {
614 tA2DP_SBC_CIE sbc_cie;
615
616 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
617 if (a2dp_status != A2DP_SUCCESS) {
618 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
619 return -1;
620 }
621
622 switch (sbc_cie.samp_freq) {
623 case A2DP_SBC_IE_SAMP_FREQ_16:
624 return SBC_sf16000;
625 case A2DP_SBC_IE_SAMP_FREQ_32:
626 return SBC_sf32000;
627 case A2DP_SBC_IE_SAMP_FREQ_44:
628 return SBC_sf44100;
629 case A2DP_SBC_IE_SAMP_FREQ_48:
630 return SBC_sf48000;
631 default:
632 break;
633 }
634
635 return -1;
636 }
637
A2DP_GetMinBitpoolSbc(const uint8_t * p_codec_info)638 int A2DP_GetMinBitpoolSbc(const uint8_t* p_codec_info) {
639 tA2DP_SBC_CIE sbc_cie;
640
641 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, true);
642 if (a2dp_status != A2DP_SUCCESS) {
643 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
644 return -1;
645 }
646
647 return sbc_cie.min_bitpool;
648 }
649
A2DP_GetMaxBitpoolSbc(const uint8_t * p_codec_info)650 int A2DP_GetMaxBitpoolSbc(const uint8_t* p_codec_info) {
651 tA2DP_SBC_CIE sbc_cie;
652
653 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, true);
654 if (a2dp_status != A2DP_SUCCESS) {
655 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
656 return -1;
657 }
658
659 return sbc_cie.max_bitpool;
660 }
661
A2DP_GetBitrateSbc()662 uint32_t A2DP_GetBitrateSbc() { return a2dp_sbc_get_bitrate(); }
A2DP_GetSinkTrackChannelTypeSbc(const uint8_t * p_codec_info)663 int A2DP_GetSinkTrackChannelTypeSbc(const uint8_t* p_codec_info) {
664 tA2DP_SBC_CIE sbc_cie;
665
666 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
667 if (a2dp_status != A2DP_SUCCESS) {
668 LOG_ERROR("%s: cannot decode codec information: %d", __func__, a2dp_status);
669 return -1;
670 }
671
672 switch (sbc_cie.ch_mode) {
673 case A2DP_SBC_IE_CH_MD_MONO:
674 return 1;
675 case A2DP_SBC_IE_CH_MD_DUAL:
676 case A2DP_SBC_IE_CH_MD_STEREO:
677 case A2DP_SBC_IE_CH_MD_JOINT:
678 return 3;
679 default:
680 break;
681 }
682
683 return -1;
684 }
685
A2DP_GetPacketTimestampSbc(UNUSED_ATTR const uint8_t * p_codec_info,const uint8_t * p_data,uint32_t * p_timestamp)686 bool A2DP_GetPacketTimestampSbc(UNUSED_ATTR const uint8_t* p_codec_info,
687 const uint8_t* p_data, uint32_t* p_timestamp) {
688 *p_timestamp = *(const uint32_t*)p_data;
689 return true;
690 }
691
A2DP_BuildCodecHeaderSbc(UNUSED_ATTR const uint8_t * p_codec_info,BT_HDR * p_buf,uint16_t frames_per_packet)692 bool A2DP_BuildCodecHeaderSbc(UNUSED_ATTR const uint8_t* p_codec_info,
693 BT_HDR* p_buf, uint16_t frames_per_packet) {
694 // this doesn't happen in real life, but keeps fuzzer happy
695 if (p_buf->len - p_buf->offset < A2DP_SBC_MPL_HDR_LEN) {
696 return false;
697 }
698
699 // there is an 4-byte timestamp right following p_buf
700 if (p_buf->offset < 4 + A2DP_SBC_MPL_HDR_LEN) {
701 return false;
702 }
703
704 p_buf->offset -= A2DP_SBC_MPL_HDR_LEN;
705 uint8_t* p = (uint8_t*)(p_buf + 1) + p_buf->offset;
706 p_buf->len += A2DP_SBC_MPL_HDR_LEN;
707 A2DP_BuildMediaPayloadHeaderSbc(p, false, false, false,
708 (uint8_t)frames_per_packet);
709
710 return true;
711 }
712
A2DP_CodecInfoStringSbc(const uint8_t * p_codec_info)713 std::string A2DP_CodecInfoStringSbc(const uint8_t* p_codec_info) {
714 std::stringstream res;
715 std::string field;
716 tA2DP_STATUS a2dp_status;
717 tA2DP_SBC_CIE sbc_cie;
718
719 a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, true);
720 if (a2dp_status != A2DP_SUCCESS) {
721 res << "A2DP_ParseInfoSbc fail: " << loghex(a2dp_status);
722 return res.str();
723 }
724
725 res << "\tname: SBC\n";
726
727 // Sample frequency
728 field.clear();
729 AppendField(&field, (sbc_cie.samp_freq == 0), "NONE");
730 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_16), "16000");
731 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_32), "32000");
732 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_44), "44100");
733 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_48), "48000");
734 res << "\tsamp_freq: " << field << " (" << loghex(sbc_cie.samp_freq) << ")\n";
735
736 // Channel mode
737 field.clear();
738 AppendField(&field, (sbc_cie.ch_mode == 0), "NONE");
739 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_MONO), "Mono");
740 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_DUAL), "Dual");
741 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_STEREO), "Stereo");
742 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_JOINT), "Joint");
743 res << "\tch_mode: " << field << " (" << loghex(sbc_cie.ch_mode) << ")\n";
744
745 // Block length
746 field.clear();
747 AppendField(&field, (sbc_cie.block_len == 0), "NONE");
748 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_4), "4");
749 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_8), "8");
750 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_12), "12");
751 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_16), "16");
752 res << "\tblock_len: " << field << " (" << loghex(sbc_cie.block_len) << ")\n";
753
754 // Number of subbands
755 field.clear();
756 AppendField(&field, (sbc_cie.num_subbands == 0), "NONE");
757 AppendField(&field, (sbc_cie.num_subbands & A2DP_SBC_IE_SUBBAND_4), "4");
758 AppendField(&field, (sbc_cie.num_subbands & A2DP_SBC_IE_SUBBAND_8), "8");
759 res << "\tnum_subbands: " << field << " (" << loghex(sbc_cie.num_subbands)
760 << ")\n";
761
762 // Allocation method
763 field.clear();
764 AppendField(&field, (sbc_cie.alloc_method == 0), "NONE");
765 AppendField(&field, (sbc_cie.alloc_method & A2DP_SBC_IE_ALLOC_MD_S), "SNR");
766 AppendField(&field, (sbc_cie.alloc_method & A2DP_SBC_IE_ALLOC_MD_L),
767 "Loundess");
768 res << "\talloc_method: " << field << " (" << loghex(sbc_cie.alloc_method)
769 << ")\n";
770
771 // Min/max bitloop
772 res << "\tBit pool Min: " << std::to_string(sbc_cie.min_bitpool)
773 << " Max: " << std::to_string(sbc_cie.max_bitpool);
774
775 return res.str();
776 }
777
A2DP_GetEncoderInterfaceSbc(const uint8_t * p_codec_info)778 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterfaceSbc(
779 const uint8_t* p_codec_info) {
780 if (!A2DP_IsSourceCodecValidSbc(p_codec_info)) return NULL;
781
782 return &a2dp_encoder_interface_sbc;
783 }
784
A2DP_GetDecoderInterfaceSbc(const uint8_t * p_codec_info)785 const tA2DP_DECODER_INTERFACE* A2DP_GetDecoderInterfaceSbc(
786 const uint8_t* p_codec_info) {
787 if (!A2DP_IsSinkCodecValidSbc(p_codec_info)) return NULL;
788
789 return &a2dp_decoder_interface_sbc;
790 }
791
A2DP_AdjustCodecSbc(uint8_t * p_codec_info)792 bool A2DP_AdjustCodecSbc(uint8_t* p_codec_info) {
793 tA2DP_SBC_CIE cfg_cie;
794
795 if (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS)
796 return false;
797
798 // Updated the max bitpool
799 if (cfg_cie.max_bitpool > A2DP_SBC_MAX_BITPOOL) {
800 LOG_WARN("%s: Updated the SBC codec max bitpool from %d to %d", __func__,
801 cfg_cie.max_bitpool, A2DP_SBC_MAX_BITPOOL);
802 cfg_cie.max_bitpool = A2DP_SBC_MAX_BITPOOL;
803 }
804
805 return (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &cfg_cie, p_codec_info) ==
806 A2DP_SUCCESS);
807 }
808
A2DP_SourceCodecIndexSbc(UNUSED_ATTR const uint8_t * p_codec_info)809 btav_a2dp_codec_index_t A2DP_SourceCodecIndexSbc(
810 UNUSED_ATTR const uint8_t* p_codec_info) {
811 return BTAV_A2DP_CODEC_INDEX_SOURCE_SBC;
812 }
813
A2DP_SinkCodecIndexSbc(UNUSED_ATTR const uint8_t * p_codec_info)814 btav_a2dp_codec_index_t A2DP_SinkCodecIndexSbc(
815 UNUSED_ATTR const uint8_t* p_codec_info) {
816 return BTAV_A2DP_CODEC_INDEX_SINK_SBC;
817 }
818
A2DP_CodecIndexStrSbc(void)819 const char* A2DP_CodecIndexStrSbc(void) { return "SBC"; }
820
A2DP_CodecIndexStrSbcSink(void)821 const char* A2DP_CodecIndexStrSbcSink(void) { return "SBC SINK"; }
822
A2DP_InitCodecConfigSbc(AvdtpSepConfig * p_cfg)823 bool A2DP_InitCodecConfigSbc(AvdtpSepConfig* p_cfg) {
824 if (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &a2dp_sbc_source_caps,
825 p_cfg->codec_info) != A2DP_SUCCESS) {
826 return false;
827 }
828
829 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
830 /* Content protection info - support SCMS-T */
831 uint8_t* p = p_cfg->protect_info;
832 *p++ = AVDT_CP_LOSC;
833 UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID);
834 p_cfg->num_protect = 1;
835 #endif
836
837 return true;
838 }
839
A2DP_InitCodecConfigSbcSink(AvdtpSepConfig * p_cfg)840 bool A2DP_InitCodecConfigSbcSink(AvdtpSepConfig* p_cfg) {
841 if (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &a2dp_sbc_sink_caps,
842 p_cfg->codec_info) != A2DP_SUCCESS) {
843 return false;
844 }
845
846 return true;
847 }
848
build_codec_config(const tA2DP_SBC_CIE & config_cie,btav_a2dp_codec_config_t * result)849 UNUSED_ATTR static void build_codec_config(const tA2DP_SBC_CIE& config_cie,
850 btav_a2dp_codec_config_t* result) {
851 if (config_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_44)
852 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
853 if (config_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_48)
854 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
855
856 result->bits_per_sample = config_cie.bits_per_sample;
857
858 if (config_cie.ch_mode & A2DP_SBC_IE_CH_MD_MONO)
859 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
860
861 if (config_cie.ch_mode & (A2DP_SBC_IE_CH_MD_STEREO | A2DP_SBC_IE_CH_MD_JOINT |
862 A2DP_SBC_IE_CH_MD_DUAL)) {
863 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
864 }
865 }
866
A2dpCodecConfigSbcSource(btav_a2dp_codec_priority_t codec_priority)867 A2dpCodecConfigSbcSource::A2dpCodecConfigSbcSource(
868 btav_a2dp_codec_priority_t codec_priority)
869 : A2dpCodecConfigSbcBase(BTAV_A2DP_CODEC_INDEX_SOURCE_SBC,
870 A2DP_CodecIndexStrSbc(), codec_priority, true) {
871 // Compute the local capability
872 if (a2dp_sbc_source_caps.samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
873 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
874 }
875 if (a2dp_sbc_source_caps.samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
876 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
877 }
878 codec_local_capability_.bits_per_sample =
879 a2dp_sbc_source_caps.bits_per_sample;
880 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
881 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
882 }
883 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
884 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
885 }
886 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
887 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
888 }
889 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
890 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
891 }
892 }
893
~A2dpCodecConfigSbcSource()894 A2dpCodecConfigSbcSource::~A2dpCodecConfigSbcSource() {}
895
init()896 bool A2dpCodecConfigSbcSource::init() {
897 if (!isValid()) return false;
898
899 // Load the encoder
900 if (!A2DP_LoadEncoderSbc()) {
901 LOG_ERROR("%s: cannot load the encoder", __func__);
902 return false;
903 }
904
905 return true;
906 }
907
useRtpHeaderMarkerBit() const908 bool A2dpCodecConfigSbcSource::useRtpHeaderMarkerBit() const { return false; }
909
910 //
911 // Selects the best sample rate from |samp_freq|.
912 // The result is stored in |p_result| and |p_codec_config|.
913 // Returns true if a selection was made, otherwise false.
914 //
select_best_sample_rate(uint8_t samp_freq,tA2DP_SBC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)915 static bool select_best_sample_rate(uint8_t samp_freq, tA2DP_SBC_CIE* p_result,
916 btav_a2dp_codec_config_t* p_codec_config) {
917 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
918 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_48;
919 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
920 return true;
921 }
922 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
923 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_44;
924 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
925 return true;
926 }
927 return false;
928 }
929
930 //
931 // Selects the audio sample rate from |p_codec_audio_config|.
932 // |samp_freq| contains the capability.
933 // The result is stored in |p_result| and |p_codec_config|.
934 // Returns true if a selection was made, otherwise false.
935 //
select_audio_sample_rate(const btav_a2dp_codec_config_t * p_codec_audio_config,uint8_t samp_freq,tA2DP_SBC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)936 static bool select_audio_sample_rate(
937 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t samp_freq,
938 tA2DP_SBC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
939 switch (p_codec_audio_config->sample_rate) {
940 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
941 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
942 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_44;
943 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
944 return true;
945 }
946 break;
947 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
948 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
949 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_48;
950 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
951 return true;
952 }
953 break;
954 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
955 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
956 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
957 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
958 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
959 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
960 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
961 break;
962 }
963
964 return false;
965 }
966
967 //
968 // Selects the best bits per sample.
969 // The result is stored in |p_codec_config|.
970 // Returns true if a selection was made, otherwise false.
971 //
select_best_bits_per_sample(btav_a2dp_codec_config_t * p_codec_config)972 static bool select_best_bits_per_sample(
973 btav_a2dp_codec_config_t* p_codec_config) {
974 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
975 return true;
976 }
977
978 //
979 // Selects the audio bits per sample from |p_codec_audio_config|.
980 // The result is stored in |p_codec_config|.
981 // Returns true if a selection was made, otherwise false.
982 //
select_audio_bits_per_sample(const btav_a2dp_codec_config_t * p_codec_audio_config,btav_a2dp_codec_config_t * p_codec_config)983 static bool select_audio_bits_per_sample(
984 const btav_a2dp_codec_config_t* p_codec_audio_config,
985 btav_a2dp_codec_config_t* p_codec_config) {
986 switch (p_codec_audio_config->bits_per_sample) {
987 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
988 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
989 return true;
990 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
991 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
992 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
993 break;
994 }
995 return false;
996 }
997
998 //
999 // Selects the best channel mode from |ch_mode|.
1000 // The result is stored in |p_result| and |p_codec_config|.
1001 // Returns true if a selection was made, otherwise false.
1002 //
select_best_channel_mode(uint8_t ch_mode,tA2DP_SBC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)1003 static bool select_best_channel_mode(uint8_t ch_mode, tA2DP_SBC_CIE* p_result,
1004 btav_a2dp_codec_config_t* p_codec_config) {
1005 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
1006 p_result->ch_mode = A2DP_SBC_IE_CH_MD_JOINT;
1007 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1008 return true;
1009 }
1010 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1011 p_result->ch_mode = A2DP_SBC_IE_CH_MD_STEREO;
1012 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1013 return true;
1014 }
1015 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1016 p_result->ch_mode = A2DP_SBC_IE_CH_MD_DUAL;
1017 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1018 return true;
1019 }
1020 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1021 p_result->ch_mode = A2DP_SBC_IE_CH_MD_MONO;
1022 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1023 return true;
1024 }
1025 return false;
1026 }
1027
1028 //
1029 // Selects the audio channel mode from |p_codec_audio_config|.
1030 // |ch_mode| contains the capability.
1031 // The result is stored in |p_result| and |p_codec_config|.
1032 // Returns true if a selection was made, otherwise false.
1033 //
select_audio_channel_mode(const btav_a2dp_codec_config_t * p_codec_audio_config,uint8_t ch_mode,tA2DP_SBC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)1034 static bool select_audio_channel_mode(
1035 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t ch_mode,
1036 tA2DP_SBC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
1037 switch (p_codec_audio_config->channel_mode) {
1038 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
1039 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1040 p_result->ch_mode = A2DP_SBC_IE_CH_MD_MONO;
1041 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1042 return true;
1043 }
1044 break;
1045 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
1046 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
1047 p_result->ch_mode = A2DP_SBC_IE_CH_MD_JOINT;
1048 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1049 return true;
1050 }
1051 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1052 p_result->ch_mode = A2DP_SBC_IE_CH_MD_STEREO;
1053 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1054 return true;
1055 }
1056 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1057 p_result->ch_mode = A2DP_SBC_IE_CH_MD_DUAL;
1058 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1059 return true;
1060 }
1061 break;
1062 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1063 break;
1064 }
1065
1066 return false;
1067 }
1068
setCodecConfig(const uint8_t * p_peer_codec_info,bool is_capability,uint8_t * p_result_codec_config)1069 bool A2dpCodecConfigSbcBase::setCodecConfig(const uint8_t* p_peer_codec_info,
1070 bool is_capability,
1071 uint8_t* p_result_codec_config) {
1072 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
1073 tA2DP_SBC_CIE peer_info_cie;
1074 tA2DP_SBC_CIE result_config_cie;
1075 uint8_t samp_freq;
1076 uint8_t ch_mode;
1077 uint8_t block_len;
1078 uint8_t num_subbands;
1079 uint8_t alloc_method;
1080 const tA2DP_SBC_CIE* p_a2dp_sbc_caps =
1081 (is_source_) ? &a2dp_sbc_source_caps : &a2dp_sbc_sink_caps;
1082
1083 // Save the internal state
1084 btav_a2dp_codec_config_t saved_codec_config = codec_config_;
1085 btav_a2dp_codec_config_t saved_codec_capability = codec_capability_;
1086 btav_a2dp_codec_config_t saved_codec_selectable_capability =
1087 codec_selectable_capability_;
1088 btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_;
1089 btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_;
1090 uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE];
1091 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
1092 uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE];
1093 memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_));
1094 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
1095 sizeof(ota_codec_peer_capability_));
1096 memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_,
1097 sizeof(ota_codec_peer_config_));
1098
1099 tA2DP_STATUS status =
1100 A2DP_ParseInfoSbc(&peer_info_cie, p_peer_codec_info, is_capability);
1101 if (status != A2DP_SUCCESS) {
1102 LOG_ERROR("%s: can't parse peer's capabilities: error = %d", __func__,
1103 status);
1104 goto fail;
1105 }
1106 // Try using the prefered peer codec config (if valid), instead of the peer
1107 // capability.
1108 if (is_capability) {
1109 if (is_source_) {
1110 if (A2DP_IsPeerSinkCodecValidSbc(ota_codec_peer_config_)) {
1111 status =
1112 A2DP_ParseInfoSbc(&peer_info_cie, ota_codec_peer_config_, false);
1113 }
1114 } else {
1115 if (A2DP_IsPeerSourceCodecValidSbc(ota_codec_peer_config_)) {
1116 status =
1117 A2DP_ParseInfoSbc(&peer_info_cie, ota_codec_peer_config_, false);
1118 }
1119 }
1120 if (status != A2DP_SUCCESS) {
1121 // Use the peer codec capability
1122 status =
1123 A2DP_ParseInfoSbc(&peer_info_cie, p_peer_codec_info, is_capability);
1124 CHECK(status == A2DP_SUCCESS);
1125 }
1126 }
1127
1128 //
1129 // Build the preferred configuration
1130 //
1131 memset(&result_config_cie, 0, sizeof(result_config_cie));
1132
1133 //
1134 // Select the sample frequency
1135 //
1136 samp_freq = p_a2dp_sbc_caps->samp_freq & peer_info_cie.samp_freq;
1137 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
1138 switch (codec_user_config_.sample_rate) {
1139 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
1140 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
1141 result_config_cie.samp_freq = A2DP_SBC_IE_SAMP_FREQ_44;
1142 codec_capability_.sample_rate = codec_user_config_.sample_rate;
1143 codec_config_.sample_rate = codec_user_config_.sample_rate;
1144 }
1145 break;
1146 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
1147 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
1148 result_config_cie.samp_freq = A2DP_SBC_IE_SAMP_FREQ_48;
1149 codec_capability_.sample_rate = codec_user_config_.sample_rate;
1150 codec_config_.sample_rate = codec_user_config_.sample_rate;
1151 }
1152 break;
1153 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
1154 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
1155 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
1156 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
1157 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
1158 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
1159 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
1160 codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
1161 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
1162 break;
1163 }
1164
1165 // Select the sample frequency if there is no user preference
1166 do {
1167 // Compute the selectable capability
1168 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
1169 codec_selectable_capability_.sample_rate |=
1170 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1171 }
1172 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
1173 codec_selectable_capability_.sample_rate |=
1174 BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1175 }
1176
1177 if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break;
1178
1179 // Compute the common capability
1180 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44)
1181 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1182 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48)
1183 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1184
1185 // No user preference - try the codec audio config
1186 if (select_audio_sample_rate(&codec_audio_config_, samp_freq,
1187 &result_config_cie, &codec_config_)) {
1188 break;
1189 }
1190
1191 // No user preference - try the default config
1192 if (select_best_sample_rate(
1193 a2dp_sbc_default_config.samp_freq & peer_info_cie.samp_freq,
1194 &result_config_cie, &codec_config_)) {
1195 break;
1196 }
1197
1198 // No user preference - use the best match
1199 if (select_best_sample_rate(samp_freq, &result_config_cie,
1200 &codec_config_)) {
1201 break;
1202 }
1203 } while (false);
1204 if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) {
1205 LOG_ERROR(
1206 "%s: cannot match sample frequency: local caps = 0x%x "
1207 "peer info = 0x%x",
1208 __func__, p_a2dp_sbc_caps->samp_freq, peer_info_cie.samp_freq);
1209 goto fail;
1210 }
1211
1212 //
1213 // Select the bits per sample
1214 //
1215 // NOTE: this information is NOT included in the SBC A2DP codec description
1216 // that is sent OTA.
1217 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1218 switch (codec_user_config_.bits_per_sample) {
1219 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
1220 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1221 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1222 break;
1223 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
1224 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
1225 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
1226 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1227 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1228 break;
1229 }
1230
1231 // Select the bits per sample if there is no user preference
1232 do {
1233 // Compute the selectable capability
1234 codec_selectable_capability_.bits_per_sample =
1235 p_a2dp_sbc_caps->bits_per_sample;
1236
1237 if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE)
1238 break;
1239
1240 // Compute the common capability
1241 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
1242
1243 // No user preference - try the codec audio config
1244 if (select_audio_bits_per_sample(&codec_audio_config_, &codec_config_)) {
1245 break;
1246 }
1247
1248 // No user preference - try the default config
1249 if (select_best_bits_per_sample(&codec_config_)) {
1250 break;
1251 }
1252
1253 // No user preference - use the best match
1254 // TODO: no-op - temporary kept here for consistency
1255 if (select_best_bits_per_sample(&codec_config_)) {
1256 break;
1257 }
1258 } while (false);
1259 if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) {
1260 LOG_ERROR("%s: cannot match bits per sample: user preference = 0x%x",
1261 __func__, codec_user_config_.bits_per_sample);
1262 goto fail;
1263 }
1264
1265 //
1266 // Select the channel mode
1267 //
1268 ch_mode = p_a2dp_sbc_caps->ch_mode & peer_info_cie.ch_mode;
1269 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1270 switch (codec_user_config_.channel_mode) {
1271 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
1272 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1273 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_MONO;
1274 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1275 codec_config_.channel_mode = codec_user_config_.channel_mode;
1276 }
1277 break;
1278 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
1279 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
1280 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_JOINT;
1281 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1282 codec_config_.channel_mode = codec_user_config_.channel_mode;
1283 break;
1284 }
1285 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1286 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_STEREO;
1287 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1288 codec_config_.channel_mode = codec_user_config_.channel_mode;
1289 break;
1290 }
1291 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1292 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_DUAL;
1293 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1294 codec_config_.channel_mode = codec_user_config_.channel_mode;
1295 break;
1296 }
1297 break;
1298 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1299 codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1300 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1301 break;
1302 }
1303
1304 // Select the channel mode if there is no user preference
1305 do {
1306 // Compute the selectable capability
1307 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1308 codec_selectable_capability_.channel_mode |=
1309 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1310 }
1311 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
1312 codec_selectable_capability_.channel_mode |=
1313 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1314 }
1315 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1316 codec_selectable_capability_.channel_mode |=
1317 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1318 }
1319 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1320 codec_selectable_capability_.channel_mode |=
1321 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1322 }
1323
1324 if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break;
1325
1326 // Compute the common capability
1327 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO)
1328 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1329 if (ch_mode & (A2DP_SBC_IE_CH_MD_JOINT | A2DP_SBC_IE_CH_MD_STEREO |
1330 A2DP_SBC_IE_CH_MD_DUAL)) {
1331 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1332 }
1333
1334 // No user preference - use the codec audio config
1335 if (select_audio_channel_mode(&codec_audio_config_, ch_mode,
1336 &result_config_cie, &codec_config_)) {
1337 break;
1338 }
1339
1340 // No user preference - try the default config
1341 if (select_best_channel_mode(
1342 a2dp_sbc_default_config.ch_mode & peer_info_cie.ch_mode,
1343 &result_config_cie, &codec_config_)) {
1344 break;
1345 }
1346
1347 // No user preference - use the best match
1348 if (select_best_channel_mode(ch_mode, &result_config_cie, &codec_config_)) {
1349 break;
1350 }
1351 } while (false);
1352 if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) {
1353 LOG_ERROR(
1354 "%s: cannot match channel mode: local caps = 0x%x "
1355 "peer info = 0x%x",
1356 __func__, p_a2dp_sbc_caps->ch_mode, peer_info_cie.ch_mode);
1357 goto fail;
1358 }
1359
1360 //
1361 // Select the block length
1362 //
1363 block_len = p_a2dp_sbc_caps->block_len & peer_info_cie.block_len;
1364 if (block_len & A2DP_SBC_IE_BLOCKS_16) {
1365 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_16;
1366 } else if (block_len & A2DP_SBC_IE_BLOCKS_12) {
1367 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_12;
1368 } else if (block_len & A2DP_SBC_IE_BLOCKS_8) {
1369 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_8;
1370 } else if (block_len & A2DP_SBC_IE_BLOCKS_4) {
1371 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_4;
1372 } else {
1373 LOG_ERROR(
1374 "%s: cannot match block length: local caps = 0x%x "
1375 "peer info = 0x%x",
1376 __func__, p_a2dp_sbc_caps->block_len, peer_info_cie.block_len);
1377 goto fail;
1378 }
1379
1380 //
1381 // Select the number of sub-bands
1382 //
1383 num_subbands = p_a2dp_sbc_caps->num_subbands & peer_info_cie.num_subbands;
1384 if (num_subbands & A2DP_SBC_IE_SUBBAND_8) {
1385 result_config_cie.num_subbands = A2DP_SBC_IE_SUBBAND_8;
1386 } else if (num_subbands & A2DP_SBC_IE_SUBBAND_4) {
1387 result_config_cie.num_subbands = A2DP_SBC_IE_SUBBAND_4;
1388 } else {
1389 LOG_ERROR(
1390 "%s: cannot match number of sub-bands: local caps = 0x%x "
1391 "peer info = 0x%x",
1392 __func__, p_a2dp_sbc_caps->num_subbands, peer_info_cie.num_subbands);
1393 goto fail;
1394 }
1395
1396 //
1397 // Select the allocation method
1398 //
1399 alloc_method = p_a2dp_sbc_caps->alloc_method & peer_info_cie.alloc_method;
1400 if (alloc_method & A2DP_SBC_IE_ALLOC_MD_L) {
1401 result_config_cie.alloc_method = A2DP_SBC_IE_ALLOC_MD_L;
1402 } else if (alloc_method & A2DP_SBC_IE_ALLOC_MD_S) {
1403 result_config_cie.alloc_method = A2DP_SBC_IE_ALLOC_MD_S;
1404 } else {
1405 LOG_ERROR(
1406 "%s: cannot match allocation method: local caps = 0x%x "
1407 "peer info = 0x%x",
1408 __func__, p_a2dp_sbc_caps->alloc_method, peer_info_cie.alloc_method);
1409 goto fail;
1410 }
1411
1412 //
1413 // Select the min/max bitpool
1414 //
1415 result_config_cie.min_bitpool = p_a2dp_sbc_caps->min_bitpool;
1416 if (result_config_cie.min_bitpool < peer_info_cie.min_bitpool)
1417 result_config_cie.min_bitpool = peer_info_cie.min_bitpool;
1418 result_config_cie.max_bitpool = p_a2dp_sbc_caps->max_bitpool;
1419 if (result_config_cie.max_bitpool > peer_info_cie.max_bitpool)
1420 result_config_cie.max_bitpool = peer_info_cie.max_bitpool;
1421 if (result_config_cie.min_bitpool > result_config_cie.max_bitpool) {
1422 LOG_ERROR(
1423 "%s: cannot match min/max bitpool: "
1424 "local caps min/max = 0x%x/0x%x peer info min/max = 0x%x/0x%x",
1425 __func__, p_a2dp_sbc_caps->min_bitpool, p_a2dp_sbc_caps->max_bitpool,
1426 peer_info_cie.min_bitpool, peer_info_cie.max_bitpool);
1427 goto fail;
1428 }
1429
1430 if (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1431 p_result_codec_config) != A2DP_SUCCESS) {
1432 goto fail;
1433 }
1434
1435 //
1436 // Copy the codec-specific fields if they are not zero
1437 //
1438 if (codec_user_config_.codec_specific_1 != 0)
1439 codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
1440 if (codec_user_config_.codec_specific_2 != 0)
1441 codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2;
1442 if (codec_user_config_.codec_specific_3 != 0)
1443 codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3;
1444 if (codec_user_config_.codec_specific_4 != 0)
1445 codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4;
1446
1447 // Create a local copy of the peer codec capability/config, and the
1448 // result codec config.
1449 if (is_capability) {
1450 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1451 ota_codec_peer_capability_);
1452 } else {
1453 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1454 ota_codec_peer_config_);
1455 }
1456 CHECK(status == A2DP_SUCCESS);
1457 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1458 ota_codec_config_);
1459 CHECK(status == A2DP_SUCCESS);
1460 return true;
1461
1462 fail:
1463 // Restore the internal state
1464 codec_config_ = saved_codec_config;
1465 codec_capability_ = saved_codec_capability;
1466 codec_selectable_capability_ = saved_codec_selectable_capability;
1467 codec_user_config_ = saved_codec_user_config;
1468 codec_audio_config_ = saved_codec_audio_config;
1469 memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_));
1470 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1471 sizeof(ota_codec_peer_capability_));
1472 memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config,
1473 sizeof(ota_codec_peer_config_));
1474 return false;
1475 }
1476
setPeerCodecCapabilities(const uint8_t * p_peer_codec_capabilities)1477 bool A2dpCodecConfigSbcBase::setPeerCodecCapabilities(
1478 const uint8_t* p_peer_codec_capabilities) {
1479 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
1480 tA2DP_SBC_CIE peer_info_cie;
1481 uint8_t samp_freq;
1482 uint8_t ch_mode;
1483 const tA2DP_SBC_CIE* p_a2dp_sbc_caps =
1484 (is_source_) ? &a2dp_sbc_source_caps : &a2dp_sbc_sink_caps;
1485
1486 // Save the internal state
1487 btav_a2dp_codec_config_t saved_codec_selectable_capability =
1488 codec_selectable_capability_;
1489 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
1490 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
1491 sizeof(ota_codec_peer_capability_));
1492
1493 tA2DP_STATUS status =
1494 A2DP_ParseInfoSbc(&peer_info_cie, p_peer_codec_capabilities, true);
1495 if (status != A2DP_SUCCESS) {
1496 LOG_ERROR("%s: can't parse peer's capabilities: error = %d", __func__,
1497 status);
1498 goto fail;
1499 }
1500
1501 // Compute the selectable capability - sample rate
1502 samp_freq = p_a2dp_sbc_caps->samp_freq & peer_info_cie.samp_freq;
1503 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
1504 codec_selectable_capability_.sample_rate |=
1505 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1506 }
1507 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
1508 codec_selectable_capability_.sample_rate |=
1509 BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1510 }
1511
1512 // Compute the selectable capability - bits per sample
1513 codec_selectable_capability_.bits_per_sample =
1514 p_a2dp_sbc_caps->bits_per_sample;
1515
1516 // Compute the selectable capability - channel mode
1517 ch_mode = p_a2dp_sbc_caps->ch_mode & peer_info_cie.ch_mode;
1518 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1519 codec_selectable_capability_.channel_mode |=
1520 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1521 }
1522 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
1523 codec_selectable_capability_.channel_mode |=
1524 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1525 }
1526 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1527 codec_selectable_capability_.channel_mode |=
1528 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1529 }
1530 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1531 codec_selectable_capability_.channel_mode |=
1532 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1533 }
1534
1535 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1536 ota_codec_peer_capability_);
1537 CHECK(status == A2DP_SUCCESS);
1538 return true;
1539
1540 fail:
1541 // Restore the internal state
1542 codec_selectable_capability_ = saved_codec_selectable_capability;
1543 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1544 sizeof(ota_codec_peer_capability_));
1545 return false;
1546 }
1547
A2dpCodecConfigSbcSink(btav_a2dp_codec_priority_t codec_priority)1548 A2dpCodecConfigSbcSink::A2dpCodecConfigSbcSink(
1549 btav_a2dp_codec_priority_t codec_priority)
1550 : A2dpCodecConfigSbcBase(BTAV_A2DP_CODEC_INDEX_SINK_SBC,
1551 A2DP_CodecIndexStrSbcSink(), codec_priority,
1552 false) {}
1553
~A2dpCodecConfigSbcSink()1554 A2dpCodecConfigSbcSink::~A2dpCodecConfigSbcSink() {}
1555
init()1556 bool A2dpCodecConfigSbcSink::init() {
1557 if (!isValid()) return false;
1558
1559 // Load the decoder
1560 if (!A2DP_LoadDecoderSbc()) {
1561 LOG_ERROR("%s: cannot load the decoder", __func__);
1562 return false;
1563 }
1564
1565 return true;
1566 }
1567
useRtpHeaderMarkerBit() const1568 bool A2dpCodecConfigSbcSink::useRtpHeaderMarkerBit() const {
1569 // TODO: This method applies only to Source codecs
1570 return false;
1571 }
1572