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 "bt_target.h"
29
30 #include "a2dp_sbc.h"
31
32 #include <string.h>
33
34 #include <base/logging.h>
35 #include "a2dp_sbc_decoder.h"
36 #include "a2dp_sbc_encoder.h"
37 #include "bt_utils.h"
38 #include "embdrv/sbc/encoder/include/sbc_encoder.h"
39 #include "osi/include/log.h"
40 #include "osi/include/osi.h"
41
42 #define A2DP_SBC_MAX_BITPOOL 53
43
44 /* data type for the SBC Codec Information Element */
45 typedef struct {
46 uint8_t samp_freq; /* Sampling frequency */
47 uint8_t ch_mode; /* Channel mode */
48 uint8_t block_len; /* Block length */
49 uint8_t num_subbands; /* Number of subbands */
50 uint8_t alloc_method; /* Allocation method */
51 uint8_t min_bitpool; /* Minimum bitpool */
52 uint8_t max_bitpool; /* Maximum bitpool */
53 btav_a2dp_codec_bits_per_sample_t bits_per_sample;
54 } tA2DP_SBC_CIE;
55
56 /* SBC Source codec capabilities */
57 static const tA2DP_SBC_CIE a2dp_sbc_source_caps = {
58 (A2DP_SBC_IE_SAMP_FREQ_44), /* samp_freq */
59 (A2DP_SBC_IE_CH_MD_MONO | A2DP_SBC_IE_CH_MD_JOINT), /* ch_mode */
60 (A2DP_SBC_IE_BLOCKS_16 | A2DP_SBC_IE_BLOCKS_12 | A2DP_SBC_IE_BLOCKS_8 |
61 A2DP_SBC_IE_BLOCKS_4), /* block_len */
62 A2DP_SBC_IE_SUBBAND_8, /* num_subbands */
63 A2DP_SBC_IE_ALLOC_MD_L, /* alloc_method */
64 A2DP_SBC_IE_MIN_BITPOOL, /* min_bitpool */
65 A2DP_SBC_MAX_BITPOOL, /* max_bitpool */
66 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 /* bits_per_sample */
67 };
68
69 /* SBC Sink codec capabilities */
70 static const tA2DP_SBC_CIE a2dp_sbc_sink_caps = {
71 (A2DP_SBC_IE_SAMP_FREQ_48 | A2DP_SBC_IE_SAMP_FREQ_44), /* samp_freq */
72 (A2DP_SBC_IE_CH_MD_MONO | A2DP_SBC_IE_CH_MD_STEREO |
73 A2DP_SBC_IE_CH_MD_JOINT | A2DP_SBC_IE_CH_MD_DUAL), /* ch_mode */
74 (A2DP_SBC_IE_BLOCKS_16 | A2DP_SBC_IE_BLOCKS_12 | A2DP_SBC_IE_BLOCKS_8 |
75 A2DP_SBC_IE_BLOCKS_4), /* block_len */
76 (A2DP_SBC_IE_SUBBAND_4 | A2DP_SBC_IE_SUBBAND_8), /* num_subbands */
77 (A2DP_SBC_IE_ALLOC_MD_L | A2DP_SBC_IE_ALLOC_MD_S), /* alloc_method */
78 A2DP_SBC_IE_MIN_BITPOOL, /* min_bitpool */
79 A2DP_SBC_MAX_BITPOOL, /* max_bitpool */
80 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 /* bits_per_sample */
81 };
82
83 /* Default SBC codec configuration */
84 const tA2DP_SBC_CIE a2dp_sbc_default_config = {
85 A2DP_SBC_IE_SAMP_FREQ_44, /* samp_freq */
86 A2DP_SBC_IE_CH_MD_JOINT, /* ch_mode */
87 A2DP_SBC_IE_BLOCKS_16, /* block_len */
88 A2DP_SBC_IE_SUBBAND_8, /* num_subbands */
89 A2DP_SBC_IE_ALLOC_MD_L, /* alloc_method */
90 A2DP_SBC_IE_MIN_BITPOOL, /* min_bitpool */
91 A2DP_SBC_MAX_BITPOOL, /* max_bitpool */
92 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 /* bits_per_sample */
93 };
94
95 static const tA2DP_ENCODER_INTERFACE a2dp_encoder_interface_sbc = {
96 a2dp_sbc_encoder_init,
97 a2dp_sbc_encoder_cleanup,
98 a2dp_sbc_feeding_reset,
99 a2dp_sbc_feeding_flush,
100 a2dp_sbc_get_encoder_interval_ms,
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 uint8_t* p;
695
696 p_buf->offset -= A2DP_SBC_MPL_HDR_LEN;
697 p = (uint8_t*)(p_buf + 1) + p_buf->offset;
698 p_buf->len += A2DP_SBC_MPL_HDR_LEN;
699 A2DP_BuildMediaPayloadHeaderSbc(p, false, false, false,
700 (uint8_t)frames_per_packet);
701
702 return true;
703 }
704
A2DP_CodecInfoStringSbc(const uint8_t * p_codec_info)705 std::string A2DP_CodecInfoStringSbc(const uint8_t* p_codec_info) {
706 std::stringstream res;
707 std::string field;
708 tA2DP_STATUS a2dp_status;
709 tA2DP_SBC_CIE sbc_cie;
710
711 a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, true);
712 if (a2dp_status != A2DP_SUCCESS) {
713 res << "A2DP_ParseInfoSbc fail: " << loghex(a2dp_status);
714 return res.str();
715 }
716
717 res << "\tname: SBC\n";
718
719 // Sample frequency
720 field.clear();
721 AppendField(&field, (sbc_cie.samp_freq == 0), "NONE");
722 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_16), "16000");
723 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_32), "32000");
724 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_44), "44100");
725 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_48), "48000");
726 res << "\tsamp_freq: " << field << " (" << loghex(sbc_cie.samp_freq) << ")\n";
727
728 // Channel mode
729 field.clear();
730 AppendField(&field, (sbc_cie.ch_mode == 0), "NONE");
731 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_MONO), "Mono");
732 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_DUAL), "Dual");
733 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_STEREO), "Stereo");
734 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_JOINT), "Joint");
735 res << "\tch_mode: " << field << " (" << loghex(sbc_cie.ch_mode) << ")\n";
736
737 // Block length
738 field.clear();
739 AppendField(&field, (sbc_cie.block_len == 0), "NONE");
740 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_4), "4");
741 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_8), "8");
742 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_12), "12");
743 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_16), "16");
744 res << "\tblock_len: " << field << " (" << loghex(sbc_cie.block_len) << ")\n";
745
746 // Number of subbands
747 field.clear();
748 AppendField(&field, (sbc_cie.num_subbands == 0), "NONE");
749 AppendField(&field, (sbc_cie.num_subbands & A2DP_SBC_IE_SUBBAND_4), "4");
750 AppendField(&field, (sbc_cie.num_subbands & A2DP_SBC_IE_SUBBAND_8), "8");
751 res << "\tnum_subbands: " << field << " (" << loghex(sbc_cie.num_subbands)
752 << ")\n";
753
754 // Allocation method
755 field.clear();
756 AppendField(&field, (sbc_cie.alloc_method == 0), "NONE");
757 AppendField(&field, (sbc_cie.alloc_method & A2DP_SBC_IE_ALLOC_MD_S), "SNR");
758 AppendField(&field, (sbc_cie.alloc_method & A2DP_SBC_IE_ALLOC_MD_L),
759 "Loundess");
760 res << "\talloc_method: " << field << " (" << loghex(sbc_cie.alloc_method)
761 << ")\n";
762
763 // Min/max bitloop
764 res << "\tBit pool Min: " << std::to_string(sbc_cie.min_bitpool)
765 << " Max: " << std::to_string(sbc_cie.max_bitpool);
766
767 return res.str();
768 }
769
A2DP_GetEncoderInterfaceSbc(const uint8_t * p_codec_info)770 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterfaceSbc(
771 const uint8_t* p_codec_info) {
772 if (!A2DP_IsSourceCodecValidSbc(p_codec_info)) return NULL;
773
774 return &a2dp_encoder_interface_sbc;
775 }
776
A2DP_GetDecoderInterfaceSbc(const uint8_t * p_codec_info)777 const tA2DP_DECODER_INTERFACE* A2DP_GetDecoderInterfaceSbc(
778 const uint8_t* p_codec_info) {
779 if (!A2DP_IsSinkCodecValidSbc(p_codec_info)) return NULL;
780
781 return &a2dp_decoder_interface_sbc;
782 }
783
A2DP_AdjustCodecSbc(uint8_t * p_codec_info)784 bool A2DP_AdjustCodecSbc(uint8_t* p_codec_info) {
785 tA2DP_SBC_CIE cfg_cie;
786
787 if (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS)
788 return false;
789
790 // Updated the max bitpool
791 if (cfg_cie.max_bitpool > A2DP_SBC_MAX_BITPOOL) {
792 LOG_WARN("%s: Updated the SBC codec max bitpool from %d to %d", __func__,
793 cfg_cie.max_bitpool, A2DP_SBC_MAX_BITPOOL);
794 cfg_cie.max_bitpool = A2DP_SBC_MAX_BITPOOL;
795 }
796
797 return (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &cfg_cie, p_codec_info) ==
798 A2DP_SUCCESS);
799 }
800
A2DP_SourceCodecIndexSbc(UNUSED_ATTR const uint8_t * p_codec_info)801 btav_a2dp_codec_index_t A2DP_SourceCodecIndexSbc(
802 UNUSED_ATTR const uint8_t* p_codec_info) {
803 return BTAV_A2DP_CODEC_INDEX_SOURCE_SBC;
804 }
805
A2DP_SinkCodecIndexSbc(UNUSED_ATTR const uint8_t * p_codec_info)806 btav_a2dp_codec_index_t A2DP_SinkCodecIndexSbc(
807 UNUSED_ATTR const uint8_t* p_codec_info) {
808 return BTAV_A2DP_CODEC_INDEX_SINK_SBC;
809 }
810
A2DP_CodecIndexStrSbc(void)811 const char* A2DP_CodecIndexStrSbc(void) { return "SBC"; }
812
A2DP_CodecIndexStrSbcSink(void)813 const char* A2DP_CodecIndexStrSbcSink(void) { return "SBC SINK"; }
814
A2DP_InitCodecConfigSbc(AvdtpSepConfig * p_cfg)815 bool A2DP_InitCodecConfigSbc(AvdtpSepConfig* p_cfg) {
816 if (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &a2dp_sbc_source_caps,
817 p_cfg->codec_info) != A2DP_SUCCESS) {
818 return false;
819 }
820
821 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
822 /* Content protection info - support SCMS-T */
823 uint8_t* p = p_cfg->protect_info;
824 *p++ = AVDT_CP_LOSC;
825 UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID);
826 p_cfg->num_protect = 1;
827 #endif
828
829 return true;
830 }
831
A2DP_InitCodecConfigSbcSink(AvdtpSepConfig * p_cfg)832 bool A2DP_InitCodecConfigSbcSink(AvdtpSepConfig* p_cfg) {
833 if (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &a2dp_sbc_sink_caps,
834 p_cfg->codec_info) != A2DP_SUCCESS) {
835 return false;
836 }
837
838 return true;
839 }
840
build_codec_config(const tA2DP_SBC_CIE & config_cie,btav_a2dp_codec_config_t * result)841 UNUSED_ATTR static void build_codec_config(const tA2DP_SBC_CIE& config_cie,
842 btav_a2dp_codec_config_t* result) {
843 if (config_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_44)
844 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
845 if (config_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_48)
846 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
847
848 result->bits_per_sample = config_cie.bits_per_sample;
849
850 if (config_cie.ch_mode & A2DP_SBC_IE_CH_MD_MONO)
851 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
852
853 if (config_cie.ch_mode & (A2DP_SBC_IE_CH_MD_STEREO | A2DP_SBC_IE_CH_MD_JOINT |
854 A2DP_SBC_IE_CH_MD_DUAL)) {
855 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
856 }
857 }
858
A2dpCodecConfigSbcSource(btav_a2dp_codec_priority_t codec_priority)859 A2dpCodecConfigSbcSource::A2dpCodecConfigSbcSource(
860 btav_a2dp_codec_priority_t codec_priority)
861 : A2dpCodecConfigSbcBase(BTAV_A2DP_CODEC_INDEX_SOURCE_SBC,
862 A2DP_CodecIndexStrSbc(), codec_priority, true) {
863 // Compute the local capability
864 if (a2dp_sbc_source_caps.samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
865 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
866 }
867 if (a2dp_sbc_source_caps.samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
868 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
869 }
870 codec_local_capability_.bits_per_sample =
871 a2dp_sbc_source_caps.bits_per_sample;
872 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
873 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
874 }
875 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
876 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
877 }
878 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
879 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
880 }
881 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
882 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
883 }
884 }
885
~A2dpCodecConfigSbcSource()886 A2dpCodecConfigSbcSource::~A2dpCodecConfigSbcSource() {}
887
init()888 bool A2dpCodecConfigSbcSource::init() {
889 if (!isValid()) return false;
890
891 // Load the encoder
892 if (!A2DP_LoadEncoderSbc()) {
893 LOG_ERROR("%s: cannot load the encoder", __func__);
894 return false;
895 }
896
897 return true;
898 }
899
useRtpHeaderMarkerBit() const900 bool A2dpCodecConfigSbcSource::useRtpHeaderMarkerBit() const { return false; }
901
902 //
903 // Selects the best sample rate from |samp_freq|.
904 // The result is stored in |p_result| and |p_codec_config|.
905 // Returns true if a selection was made, otherwise false.
906 //
select_best_sample_rate(uint8_t samp_freq,tA2DP_SBC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)907 static bool select_best_sample_rate(uint8_t samp_freq, tA2DP_SBC_CIE* p_result,
908 btav_a2dp_codec_config_t* p_codec_config) {
909 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
910 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_48;
911 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
912 return true;
913 }
914 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
915 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_44;
916 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
917 return true;
918 }
919 return false;
920 }
921
922 //
923 // Selects the audio sample rate from |p_codec_audio_config|.
924 // |samp_freq| contains the capability.
925 // The result is stored in |p_result| and |p_codec_config|.
926 // Returns true if a selection was made, otherwise false.
927 //
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)928 static bool select_audio_sample_rate(
929 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t samp_freq,
930 tA2DP_SBC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
931 switch (p_codec_audio_config->sample_rate) {
932 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
933 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
934 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_44;
935 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
936 return true;
937 }
938 break;
939 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
940 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
941 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_48;
942 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
943 return true;
944 }
945 break;
946 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
947 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
948 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
949 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
950 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
951 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
952 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
953 break;
954 }
955
956 return false;
957 }
958
959 //
960 // Selects the best bits per sample.
961 // The result is stored in |p_codec_config|.
962 // Returns true if a selection was made, otherwise false.
963 //
select_best_bits_per_sample(btav_a2dp_codec_config_t * p_codec_config)964 static bool select_best_bits_per_sample(
965 btav_a2dp_codec_config_t* p_codec_config) {
966 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
967 return true;
968 }
969
970 //
971 // Selects the audio bits per sample from |p_codec_audio_config|.
972 // The result is stored in |p_codec_config|.
973 // Returns true if a selection was made, otherwise false.
974 //
select_audio_bits_per_sample(const btav_a2dp_codec_config_t * p_codec_audio_config,btav_a2dp_codec_config_t * p_codec_config)975 static bool select_audio_bits_per_sample(
976 const btav_a2dp_codec_config_t* p_codec_audio_config,
977 btav_a2dp_codec_config_t* p_codec_config) {
978 switch (p_codec_audio_config->bits_per_sample) {
979 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
980 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
981 return true;
982 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
983 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
984 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
985 break;
986 }
987 return false;
988 }
989
990 //
991 // Selects the best channel mode from |ch_mode|.
992 // The result is stored in |p_result| and |p_codec_config|.
993 // Returns true if a selection was made, otherwise false.
994 //
select_best_channel_mode(uint8_t ch_mode,tA2DP_SBC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)995 static bool select_best_channel_mode(uint8_t ch_mode, tA2DP_SBC_CIE* p_result,
996 btav_a2dp_codec_config_t* p_codec_config) {
997 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
998 p_result->ch_mode = A2DP_SBC_IE_CH_MD_JOINT;
999 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1000 return true;
1001 }
1002 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1003 p_result->ch_mode = A2DP_SBC_IE_CH_MD_STEREO;
1004 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1005 return true;
1006 }
1007 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1008 p_result->ch_mode = A2DP_SBC_IE_CH_MD_DUAL;
1009 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1010 return true;
1011 }
1012 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1013 p_result->ch_mode = A2DP_SBC_IE_CH_MD_MONO;
1014 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1015 return true;
1016 }
1017 return false;
1018 }
1019
1020 //
1021 // Selects the audio channel mode from |p_codec_audio_config|.
1022 // |ch_mode| contains the capability.
1023 // The result is stored in |p_result| and |p_codec_config|.
1024 // Returns true if a selection was made, otherwise false.
1025 //
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)1026 static bool select_audio_channel_mode(
1027 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t ch_mode,
1028 tA2DP_SBC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
1029 switch (p_codec_audio_config->channel_mode) {
1030 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
1031 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1032 p_result->ch_mode = A2DP_SBC_IE_CH_MD_MONO;
1033 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1034 return true;
1035 }
1036 break;
1037 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
1038 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
1039 p_result->ch_mode = A2DP_SBC_IE_CH_MD_JOINT;
1040 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1041 return true;
1042 }
1043 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1044 p_result->ch_mode = A2DP_SBC_IE_CH_MD_STEREO;
1045 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1046 return true;
1047 }
1048 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1049 p_result->ch_mode = A2DP_SBC_IE_CH_MD_DUAL;
1050 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1051 return true;
1052 }
1053 break;
1054 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1055 break;
1056 }
1057
1058 return false;
1059 }
1060
setCodecConfig(const uint8_t * p_peer_codec_info,bool is_capability,uint8_t * p_result_codec_config)1061 bool A2dpCodecConfigSbcBase::setCodecConfig(const uint8_t* p_peer_codec_info,
1062 bool is_capability,
1063 uint8_t* p_result_codec_config) {
1064 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
1065 tA2DP_SBC_CIE peer_info_cie;
1066 tA2DP_SBC_CIE result_config_cie;
1067 uint8_t samp_freq;
1068 uint8_t ch_mode;
1069 uint8_t block_len;
1070 uint8_t num_subbands;
1071 uint8_t alloc_method;
1072 const tA2DP_SBC_CIE* p_a2dp_sbc_caps =
1073 (is_source_) ? &a2dp_sbc_source_caps : &a2dp_sbc_sink_caps;
1074
1075 // Save the internal state
1076 btav_a2dp_codec_config_t saved_codec_config = codec_config_;
1077 btav_a2dp_codec_config_t saved_codec_capability = codec_capability_;
1078 btav_a2dp_codec_config_t saved_codec_selectable_capability =
1079 codec_selectable_capability_;
1080 btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_;
1081 btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_;
1082 uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE];
1083 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
1084 uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE];
1085 memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_));
1086 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
1087 sizeof(ota_codec_peer_capability_));
1088 memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_,
1089 sizeof(ota_codec_peer_config_));
1090
1091 tA2DP_STATUS status =
1092 A2DP_ParseInfoSbc(&peer_info_cie, p_peer_codec_info, is_capability);
1093 if (status != A2DP_SUCCESS) {
1094 LOG_ERROR("%s: can't parse peer's capabilities: error = %d", __func__,
1095 status);
1096 goto fail;
1097 }
1098 // Try using the prefered peer codec config (if valid), instead of the peer
1099 // capability.
1100 if (is_capability) {
1101 if (is_source_) {
1102 if (A2DP_IsPeerSinkCodecValidSbc(ota_codec_peer_config_)) {
1103 status =
1104 A2DP_ParseInfoSbc(&peer_info_cie, ota_codec_peer_config_, false);
1105 }
1106 } else {
1107 if (A2DP_IsPeerSourceCodecValidSbc(ota_codec_peer_config_)) {
1108 status =
1109 A2DP_ParseInfoSbc(&peer_info_cie, ota_codec_peer_config_, false);
1110 }
1111 }
1112 if (status != A2DP_SUCCESS) {
1113 // Use the peer codec capability
1114 status =
1115 A2DP_ParseInfoSbc(&peer_info_cie, p_peer_codec_info, is_capability);
1116 CHECK(status == A2DP_SUCCESS);
1117 }
1118 }
1119
1120 //
1121 // Build the preferred configuration
1122 //
1123 memset(&result_config_cie, 0, sizeof(result_config_cie));
1124
1125 //
1126 // Select the sample frequency
1127 //
1128 samp_freq = p_a2dp_sbc_caps->samp_freq & peer_info_cie.samp_freq;
1129 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
1130 switch (codec_user_config_.sample_rate) {
1131 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
1132 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
1133 result_config_cie.samp_freq = A2DP_SBC_IE_SAMP_FREQ_44;
1134 codec_capability_.sample_rate = codec_user_config_.sample_rate;
1135 codec_config_.sample_rate = codec_user_config_.sample_rate;
1136 }
1137 break;
1138 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
1139 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
1140 result_config_cie.samp_freq = A2DP_SBC_IE_SAMP_FREQ_48;
1141 codec_capability_.sample_rate = codec_user_config_.sample_rate;
1142 codec_config_.sample_rate = codec_user_config_.sample_rate;
1143 }
1144 break;
1145 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
1146 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
1147 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
1148 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
1149 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
1150 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
1151 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
1152 codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
1153 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
1154 break;
1155 }
1156
1157 // Select the sample frequency if there is no user preference
1158 do {
1159 // Compute the selectable capability
1160 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
1161 codec_selectable_capability_.sample_rate |=
1162 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1163 }
1164 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
1165 codec_selectable_capability_.sample_rate |=
1166 BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1167 }
1168
1169 if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break;
1170
1171 // Compute the common capability
1172 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44)
1173 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1174 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48)
1175 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1176
1177 // No user preference - try the codec audio config
1178 if (select_audio_sample_rate(&codec_audio_config_, samp_freq,
1179 &result_config_cie, &codec_config_)) {
1180 break;
1181 }
1182
1183 // No user preference - try the default config
1184 if (select_best_sample_rate(
1185 a2dp_sbc_default_config.samp_freq & peer_info_cie.samp_freq,
1186 &result_config_cie, &codec_config_)) {
1187 break;
1188 }
1189
1190 // No user preference - use the best match
1191 if (select_best_sample_rate(samp_freq, &result_config_cie,
1192 &codec_config_)) {
1193 break;
1194 }
1195 } while (false);
1196 if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) {
1197 LOG_ERROR(
1198 "%s: cannot match sample frequency: local caps = 0x%x "
1199 "peer info = 0x%x",
1200 __func__, p_a2dp_sbc_caps->samp_freq, peer_info_cie.samp_freq);
1201 goto fail;
1202 }
1203
1204 //
1205 // Select the bits per sample
1206 //
1207 // NOTE: this information is NOT included in the SBC A2DP codec description
1208 // that is sent OTA.
1209 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1210 switch (codec_user_config_.bits_per_sample) {
1211 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
1212 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1213 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1214 break;
1215 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
1216 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
1217 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
1218 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1219 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1220 break;
1221 }
1222
1223 // Select the bits per sample if there is no user preference
1224 do {
1225 // Compute the selectable capability
1226 codec_selectable_capability_.bits_per_sample =
1227 p_a2dp_sbc_caps->bits_per_sample;
1228
1229 if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE)
1230 break;
1231
1232 // Compute the common capability
1233 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
1234
1235 // No user preference - try the codec audio config
1236 if (select_audio_bits_per_sample(&codec_audio_config_, &codec_config_)) {
1237 break;
1238 }
1239
1240 // No user preference - try the default config
1241 if (select_best_bits_per_sample(&codec_config_)) {
1242 break;
1243 }
1244
1245 // No user preference - use the best match
1246 // TODO: no-op - temporary kept here for consistency
1247 if (select_best_bits_per_sample(&codec_config_)) {
1248 break;
1249 }
1250 } while (false);
1251 if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) {
1252 LOG_ERROR("%s: cannot match bits per sample: user preference = 0x%x",
1253 __func__, codec_user_config_.bits_per_sample);
1254 goto fail;
1255 }
1256
1257 //
1258 // Select the channel mode
1259 //
1260 ch_mode = p_a2dp_sbc_caps->ch_mode & peer_info_cie.ch_mode;
1261 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1262 switch (codec_user_config_.channel_mode) {
1263 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
1264 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1265 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_MONO;
1266 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1267 codec_config_.channel_mode = codec_user_config_.channel_mode;
1268 }
1269 break;
1270 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
1271 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
1272 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_JOINT;
1273 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1274 codec_config_.channel_mode = codec_user_config_.channel_mode;
1275 break;
1276 }
1277 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1278 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_STEREO;
1279 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1280 codec_config_.channel_mode = codec_user_config_.channel_mode;
1281 break;
1282 }
1283 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1284 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_DUAL;
1285 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1286 codec_config_.channel_mode = codec_user_config_.channel_mode;
1287 break;
1288 }
1289 break;
1290 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1291 codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1292 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1293 break;
1294 }
1295
1296 // Select the channel mode if there is no user preference
1297 do {
1298 // Compute the selectable capability
1299 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1300 codec_selectable_capability_.channel_mode |=
1301 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1302 }
1303 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
1304 codec_selectable_capability_.channel_mode |=
1305 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1306 }
1307 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1308 codec_selectable_capability_.channel_mode |=
1309 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1310 }
1311 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1312 codec_selectable_capability_.channel_mode |=
1313 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1314 }
1315
1316 if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break;
1317
1318 // Compute the common capability
1319 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO)
1320 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1321 if (ch_mode & (A2DP_SBC_IE_CH_MD_JOINT | A2DP_SBC_IE_CH_MD_STEREO |
1322 A2DP_SBC_IE_CH_MD_DUAL)) {
1323 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1324 }
1325
1326 // No user preference - use the codec audio config
1327 if (select_audio_channel_mode(&codec_audio_config_, ch_mode,
1328 &result_config_cie, &codec_config_)) {
1329 break;
1330 }
1331
1332 // No user preference - try the default config
1333 if (select_best_channel_mode(
1334 a2dp_sbc_default_config.ch_mode & peer_info_cie.ch_mode,
1335 &result_config_cie, &codec_config_)) {
1336 break;
1337 }
1338
1339 // No user preference - use the best match
1340 if (select_best_channel_mode(ch_mode, &result_config_cie, &codec_config_)) {
1341 break;
1342 }
1343 } while (false);
1344 if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) {
1345 LOG_ERROR(
1346 "%s: cannot match channel mode: local caps = 0x%x "
1347 "peer info = 0x%x",
1348 __func__, p_a2dp_sbc_caps->ch_mode, peer_info_cie.ch_mode);
1349 goto fail;
1350 }
1351
1352 //
1353 // Select the block length
1354 //
1355 block_len = p_a2dp_sbc_caps->block_len & peer_info_cie.block_len;
1356 if (block_len & A2DP_SBC_IE_BLOCKS_16) {
1357 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_16;
1358 } else if (block_len & A2DP_SBC_IE_BLOCKS_12) {
1359 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_12;
1360 } else if (block_len & A2DP_SBC_IE_BLOCKS_8) {
1361 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_8;
1362 } else if (block_len & A2DP_SBC_IE_BLOCKS_4) {
1363 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_4;
1364 } else {
1365 LOG_ERROR(
1366 "%s: cannot match block length: local caps = 0x%x "
1367 "peer info = 0x%x",
1368 __func__, p_a2dp_sbc_caps->block_len, peer_info_cie.block_len);
1369 goto fail;
1370 }
1371
1372 //
1373 // Select the number of sub-bands
1374 //
1375 num_subbands = p_a2dp_sbc_caps->num_subbands & peer_info_cie.num_subbands;
1376 if (num_subbands & A2DP_SBC_IE_SUBBAND_8) {
1377 result_config_cie.num_subbands = A2DP_SBC_IE_SUBBAND_8;
1378 } else if (num_subbands & A2DP_SBC_IE_SUBBAND_4) {
1379 result_config_cie.num_subbands = A2DP_SBC_IE_SUBBAND_4;
1380 } else {
1381 LOG_ERROR(
1382 "%s: cannot match number of sub-bands: local caps = 0x%x "
1383 "peer info = 0x%x",
1384 __func__, p_a2dp_sbc_caps->num_subbands, peer_info_cie.num_subbands);
1385 goto fail;
1386 }
1387
1388 //
1389 // Select the allocation method
1390 //
1391 alloc_method = p_a2dp_sbc_caps->alloc_method & peer_info_cie.alloc_method;
1392 if (alloc_method & A2DP_SBC_IE_ALLOC_MD_L) {
1393 result_config_cie.alloc_method = A2DP_SBC_IE_ALLOC_MD_L;
1394 } else if (alloc_method & A2DP_SBC_IE_ALLOC_MD_S) {
1395 result_config_cie.alloc_method = A2DP_SBC_IE_ALLOC_MD_S;
1396 } else {
1397 LOG_ERROR(
1398 "%s: cannot match allocation method: local caps = 0x%x "
1399 "peer info = 0x%x",
1400 __func__, p_a2dp_sbc_caps->alloc_method, peer_info_cie.alloc_method);
1401 goto fail;
1402 }
1403
1404 //
1405 // Select the min/max bitpool
1406 //
1407 result_config_cie.min_bitpool = p_a2dp_sbc_caps->min_bitpool;
1408 if (result_config_cie.min_bitpool < peer_info_cie.min_bitpool)
1409 result_config_cie.min_bitpool = peer_info_cie.min_bitpool;
1410 result_config_cie.max_bitpool = p_a2dp_sbc_caps->max_bitpool;
1411 if (result_config_cie.max_bitpool > peer_info_cie.max_bitpool)
1412 result_config_cie.max_bitpool = peer_info_cie.max_bitpool;
1413 if (result_config_cie.min_bitpool > result_config_cie.max_bitpool) {
1414 LOG_ERROR(
1415 "%s: cannot match min/max bitpool: "
1416 "local caps min/max = 0x%x/0x%x peer info min/max = 0x%x/0x%x",
1417 __func__, p_a2dp_sbc_caps->min_bitpool, p_a2dp_sbc_caps->max_bitpool,
1418 peer_info_cie.min_bitpool, peer_info_cie.max_bitpool);
1419 goto fail;
1420 }
1421
1422 if (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1423 p_result_codec_config) != A2DP_SUCCESS) {
1424 goto fail;
1425 }
1426
1427 //
1428 // Copy the codec-specific fields if they are not zero
1429 //
1430 if (codec_user_config_.codec_specific_1 != 0)
1431 codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
1432 if (codec_user_config_.codec_specific_2 != 0)
1433 codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2;
1434 if (codec_user_config_.codec_specific_3 != 0)
1435 codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3;
1436 if (codec_user_config_.codec_specific_4 != 0)
1437 codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4;
1438
1439 // Create a local copy of the peer codec capability/config, and the
1440 // result codec config.
1441 if (is_capability) {
1442 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1443 ota_codec_peer_capability_);
1444 } else {
1445 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1446 ota_codec_peer_config_);
1447 }
1448 CHECK(status == A2DP_SUCCESS);
1449 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1450 ota_codec_config_);
1451 CHECK(status == A2DP_SUCCESS);
1452 return true;
1453
1454 fail:
1455 // Restore the internal state
1456 codec_config_ = saved_codec_config;
1457 codec_capability_ = saved_codec_capability;
1458 codec_selectable_capability_ = saved_codec_selectable_capability;
1459 codec_user_config_ = saved_codec_user_config;
1460 codec_audio_config_ = saved_codec_audio_config;
1461 memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_));
1462 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1463 sizeof(ota_codec_peer_capability_));
1464 memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config,
1465 sizeof(ota_codec_peer_config_));
1466 return false;
1467 }
1468
setPeerCodecCapabilities(const uint8_t * p_peer_codec_capabilities)1469 bool A2dpCodecConfigSbcBase::setPeerCodecCapabilities(
1470 const uint8_t* p_peer_codec_capabilities) {
1471 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
1472 tA2DP_SBC_CIE peer_info_cie;
1473 uint8_t samp_freq;
1474 uint8_t ch_mode;
1475 const tA2DP_SBC_CIE* p_a2dp_sbc_caps =
1476 (is_source_) ? &a2dp_sbc_source_caps : &a2dp_sbc_sink_caps;
1477
1478 // Save the internal state
1479 btav_a2dp_codec_config_t saved_codec_selectable_capability =
1480 codec_selectable_capability_;
1481 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
1482 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
1483 sizeof(ota_codec_peer_capability_));
1484
1485 tA2DP_STATUS status =
1486 A2DP_ParseInfoSbc(&peer_info_cie, p_peer_codec_capabilities, true);
1487 if (status != A2DP_SUCCESS) {
1488 LOG_ERROR("%s: can't parse peer's capabilities: error = %d", __func__,
1489 status);
1490 goto fail;
1491 }
1492
1493 // Compute the selectable capability - sample rate
1494 samp_freq = p_a2dp_sbc_caps->samp_freq & peer_info_cie.samp_freq;
1495 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
1496 codec_selectable_capability_.sample_rate |=
1497 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1498 }
1499 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
1500 codec_selectable_capability_.sample_rate |=
1501 BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1502 }
1503
1504 // Compute the selectable capability - bits per sample
1505 codec_selectable_capability_.bits_per_sample =
1506 p_a2dp_sbc_caps->bits_per_sample;
1507
1508 // Compute the selectable capability - channel mode
1509 ch_mode = p_a2dp_sbc_caps->ch_mode & peer_info_cie.ch_mode;
1510 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1511 codec_selectable_capability_.channel_mode |=
1512 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1513 }
1514 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
1515 codec_selectable_capability_.channel_mode |=
1516 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1517 }
1518 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1519 codec_selectable_capability_.channel_mode |=
1520 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1521 }
1522 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1523 codec_selectable_capability_.channel_mode |=
1524 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1525 }
1526
1527 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1528 ota_codec_peer_capability_);
1529 CHECK(status == A2DP_SUCCESS);
1530 return true;
1531
1532 fail:
1533 // Restore the internal state
1534 codec_selectable_capability_ = saved_codec_selectable_capability;
1535 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1536 sizeof(ota_codec_peer_capability_));
1537 return false;
1538 }
1539
A2dpCodecConfigSbcSink(btav_a2dp_codec_priority_t codec_priority)1540 A2dpCodecConfigSbcSink::A2dpCodecConfigSbcSink(
1541 btav_a2dp_codec_priority_t codec_priority)
1542 : A2dpCodecConfigSbcBase(BTAV_A2DP_CODEC_INDEX_SINK_SBC,
1543 A2DP_CodecIndexStrSbcSink(), codec_priority,
1544 false) {}
1545
~A2dpCodecConfigSbcSink()1546 A2dpCodecConfigSbcSink::~A2dpCodecConfigSbcSink() {}
1547
init()1548 bool A2dpCodecConfigSbcSink::init() {
1549 if (!isValid()) return false;
1550
1551 // Load the decoder
1552 if (!A2DP_LoadDecoderSbc()) {
1553 LOG_ERROR("%s: cannot load the decoder", __func__);
1554 return false;
1555 }
1556
1557 return true;
1558 }
1559
useRtpHeaderMarkerBit() const1560 bool A2dpCodecConfigSbcSink::useRtpHeaderMarkerBit() const {
1561 // TODO: This method applies only to Source codecs
1562 return false;
1563 }
1564
updateEncoderUserConfig(UNUSED_ATTR const tA2DP_ENCODER_INIT_PEER_PARAMS * p_peer_params,UNUSED_ATTR bool * p_restart_input,UNUSED_ATTR bool * p_restart_output,UNUSED_ATTR bool * p_config_updated)1565 bool A2dpCodecConfigSbcSink::updateEncoderUserConfig(
1566 UNUSED_ATTR const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
1567 UNUSED_ATTR bool* p_restart_input, UNUSED_ATTR bool* p_restart_output,
1568 UNUSED_ATTR bool* p_config_updated) {
1569 // TODO: This method applies only to Source codecs
1570 return false;
1571 }
1572
encoderIntervalMs() const1573 uint64_t A2dpCodecConfigSbcSink::encoderIntervalMs() const {
1574 // TODO: This method applies only to Source codecs
1575 return 0;
1576 }
1577
getEffectiveMtu() const1578 int A2dpCodecConfigSbcSink::getEffectiveMtu() const {
1579 // TODO: This method applies only to Source codecs
1580 return 0;
1581 }
1582