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(LOG_TAG, "%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(LOG_TAG, "%s: parsing failed %d", __func__, status);
357 return status;
358 }
359
360 /* verify that each parameter is in range */
361
362 LOG_VERBOSE(LOG_TAG, "%s: FREQ peer: 0x%x, capability 0x%x", __func__,
363 cfg_cie.samp_freq, p_cap->samp_freq);
364 LOG_VERBOSE(LOG_TAG, "%s: CH_MODE peer: 0x%x, capability 0x%x", __func__,
365 cfg_cie.ch_mode, p_cap->ch_mode);
366 LOG_VERBOSE(LOG_TAG, "%s: BLOCK_LEN peer: 0x%x, capability 0x%x", __func__,
367 cfg_cie.block_len, p_cap->block_len);
368 LOG_VERBOSE(LOG_TAG, "%s: SUB_BAND peer: 0x%x, capability 0x%x", __func__,
369 cfg_cie.num_subbands, p_cap->num_subbands);
370 LOG_VERBOSE(LOG_TAG, "%s: ALLOC_METHOD peer: 0x%x, capability 0x%x", __func__,
371 cfg_cie.alloc_method, p_cap->alloc_method);
372 LOG_VERBOSE(LOG_TAG, "%s: MIN_BitPool peer: 0x%x, capability 0x%x", __func__,
373 cfg_cie.min_bitpool, p_cap->min_bitpool);
374 LOG_VERBOSE(LOG_TAG, "%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(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
413 a2dp_status);
414 return false;
415 }
416 a2dp_status = A2DP_ParseInfoSbc(&sbc_cie_b, p_codec_info_b, true);
417 if (a2dp_status != A2DP_SUCCESS) {
418 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
419 a2dp_status);
420 return false;
421 }
422
423 tA2DP_CODEC_TYPE codec_type_a = A2DP_GetCodecType(p_codec_info_a);
424 tA2DP_CODEC_TYPE codec_type_b = A2DP_GetCodecType(p_codec_info_b);
425
426 return (codec_type_a == codec_type_b) && (codec_type_a == A2DP_MEDIA_CT_SBC);
427 }
428
A2DP_CodecEqualsSbc(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)429 bool A2DP_CodecEqualsSbc(const uint8_t* p_codec_info_a,
430 const uint8_t* p_codec_info_b) {
431 tA2DP_SBC_CIE sbc_cie_a;
432 tA2DP_SBC_CIE sbc_cie_b;
433
434 // Check whether the codec info contains valid data
435 tA2DP_STATUS a2dp_status =
436 A2DP_ParseInfoSbc(&sbc_cie_a, p_codec_info_a, true);
437 if (a2dp_status != A2DP_SUCCESS) {
438 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
439 a2dp_status);
440 return false;
441 }
442 a2dp_status = A2DP_ParseInfoSbc(&sbc_cie_b, p_codec_info_b, true);
443 if (a2dp_status != A2DP_SUCCESS) {
444 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
445 a2dp_status);
446 return false;
447 }
448
449 tA2DP_CODEC_TYPE codec_type_a = A2DP_GetCodecType(p_codec_info_a);
450 tA2DP_CODEC_TYPE codec_type_b = A2DP_GetCodecType(p_codec_info_b);
451
452 if ((codec_type_a != codec_type_b) || (codec_type_a != A2DP_MEDIA_CT_SBC))
453 return false;
454
455 return (sbc_cie_a.samp_freq == sbc_cie_b.samp_freq) &&
456 (sbc_cie_a.ch_mode == sbc_cie_b.ch_mode) &&
457 (sbc_cie_a.block_len == sbc_cie_b.block_len) &&
458 (sbc_cie_a.num_subbands == sbc_cie_b.num_subbands) &&
459 (sbc_cie_a.alloc_method == sbc_cie_b.alloc_method) &&
460 (sbc_cie_a.min_bitpool == sbc_cie_b.min_bitpool) &&
461 (sbc_cie_a.max_bitpool == sbc_cie_b.max_bitpool);
462 }
463
A2DP_GetTrackSampleRateSbc(const uint8_t * p_codec_info)464 int A2DP_GetTrackSampleRateSbc(const uint8_t* p_codec_info) {
465 tA2DP_SBC_CIE sbc_cie;
466
467 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
468 if (a2dp_status != A2DP_SUCCESS) {
469 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
470 a2dp_status);
471 return -1;
472 }
473
474 switch (sbc_cie.samp_freq) {
475 case A2DP_SBC_IE_SAMP_FREQ_16:
476 return 16000;
477 case A2DP_SBC_IE_SAMP_FREQ_32:
478 return 32000;
479 case A2DP_SBC_IE_SAMP_FREQ_44:
480 return 44100;
481 case A2DP_SBC_IE_SAMP_FREQ_48:
482 return 48000;
483 default:
484 break;
485 }
486
487 return -1;
488 }
489
A2DP_GetTrackBitsPerSampleSbc(const uint8_t * p_codec_info)490 int A2DP_GetTrackBitsPerSampleSbc(const uint8_t* p_codec_info) {
491 tA2DP_SBC_CIE sbc_cie;
492
493 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
494 if (a2dp_status != A2DP_SUCCESS) {
495 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
496 a2dp_status);
497 return -1;
498 }
499
500 // NOTE: The bits per sample never changes for SBC
501 return 16;
502 }
503
A2DP_GetTrackChannelCountSbc(const uint8_t * p_codec_info)504 int A2DP_GetTrackChannelCountSbc(const uint8_t* p_codec_info) {
505 tA2DP_SBC_CIE sbc_cie;
506
507 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
508 if (a2dp_status != A2DP_SUCCESS) {
509 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
510 a2dp_status);
511 return -1;
512 }
513
514 switch (sbc_cie.ch_mode) {
515 case A2DP_SBC_IE_CH_MD_MONO:
516 return 1;
517 case A2DP_SBC_IE_CH_MD_DUAL:
518 case A2DP_SBC_IE_CH_MD_STEREO:
519 case A2DP_SBC_IE_CH_MD_JOINT:
520 return 2;
521 default:
522 break;
523 }
524
525 return -1;
526 }
527
A2DP_GetNumberOfSubbandsSbc(const uint8_t * p_codec_info)528 int A2DP_GetNumberOfSubbandsSbc(const uint8_t* p_codec_info) {
529 tA2DP_SBC_CIE sbc_cie;
530
531 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
532 if (a2dp_status != A2DP_SUCCESS) {
533 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
534 a2dp_status);
535 return -1;
536 }
537
538 switch (sbc_cie.num_subbands) {
539 case A2DP_SBC_IE_SUBBAND_4:
540 return 4;
541 case A2DP_SBC_IE_SUBBAND_8:
542 return 8;
543 default:
544 break;
545 }
546
547 return -1;
548 }
549
A2DP_GetNumberOfBlocksSbc(const uint8_t * p_codec_info)550 int A2DP_GetNumberOfBlocksSbc(const uint8_t* p_codec_info) {
551 tA2DP_SBC_CIE sbc_cie;
552
553 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
554 if (a2dp_status != A2DP_SUCCESS) {
555 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
556 a2dp_status);
557 return -1;
558 }
559
560 switch (sbc_cie.block_len) {
561 case A2DP_SBC_IE_BLOCKS_4:
562 return 4;
563 case A2DP_SBC_IE_BLOCKS_8:
564 return 8;
565 case A2DP_SBC_IE_BLOCKS_12:
566 return 12;
567 case A2DP_SBC_IE_BLOCKS_16:
568 return 16;
569 default:
570 break;
571 }
572
573 return -1;
574 }
575
A2DP_GetAllocationMethodCodeSbc(const uint8_t * p_codec_info)576 int A2DP_GetAllocationMethodCodeSbc(const uint8_t* p_codec_info) {
577 tA2DP_SBC_CIE sbc_cie;
578
579 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
580 if (a2dp_status != A2DP_SUCCESS) {
581 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
582 a2dp_status);
583 return -1;
584 }
585
586 switch (sbc_cie.alloc_method) {
587 case A2DP_SBC_IE_ALLOC_MD_S:
588 return SBC_SNR;
589 case A2DP_SBC_IE_ALLOC_MD_L:
590 return SBC_LOUDNESS;
591 default:
592 break;
593 }
594
595 return -1;
596 }
597
A2DP_GetChannelModeCodeSbc(const uint8_t * p_codec_info)598 int A2DP_GetChannelModeCodeSbc(const uint8_t* p_codec_info) {
599 tA2DP_SBC_CIE sbc_cie;
600
601 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
602 if (a2dp_status != A2DP_SUCCESS) {
603 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
604 a2dp_status);
605 return -1;
606 }
607
608 switch (sbc_cie.ch_mode) {
609 case A2DP_SBC_IE_CH_MD_MONO:
610 return SBC_MONO;
611 case A2DP_SBC_IE_CH_MD_DUAL:
612 return SBC_DUAL;
613 case A2DP_SBC_IE_CH_MD_STEREO:
614 return SBC_STEREO;
615 case A2DP_SBC_IE_CH_MD_JOINT:
616 return SBC_JOINT_STEREO;
617 default:
618 break;
619 }
620
621 return -1;
622 }
623
A2DP_GetSamplingFrequencyCodeSbc(const uint8_t * p_codec_info)624 int A2DP_GetSamplingFrequencyCodeSbc(const uint8_t* p_codec_info) {
625 tA2DP_SBC_CIE sbc_cie;
626
627 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
628 if (a2dp_status != A2DP_SUCCESS) {
629 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
630 a2dp_status);
631 return -1;
632 }
633
634 switch (sbc_cie.samp_freq) {
635 case A2DP_SBC_IE_SAMP_FREQ_16:
636 return SBC_sf16000;
637 case A2DP_SBC_IE_SAMP_FREQ_32:
638 return SBC_sf32000;
639 case A2DP_SBC_IE_SAMP_FREQ_44:
640 return SBC_sf44100;
641 case A2DP_SBC_IE_SAMP_FREQ_48:
642 return SBC_sf48000;
643 default:
644 break;
645 }
646
647 return -1;
648 }
649
A2DP_GetMinBitpoolSbc(const uint8_t * p_codec_info)650 int A2DP_GetMinBitpoolSbc(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(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
656 a2dp_status);
657 return -1;
658 }
659
660 return sbc_cie.min_bitpool;
661 }
662
A2DP_GetMaxBitpoolSbc(const uint8_t * p_codec_info)663 int A2DP_GetMaxBitpoolSbc(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, true);
667 if (a2dp_status != A2DP_SUCCESS) {
668 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
669 a2dp_status);
670 return -1;
671 }
672
673 return sbc_cie.max_bitpool;
674 }
675
A2DP_GetBitrateSbc()676 uint32_t A2DP_GetBitrateSbc() { return a2dp_sbc_get_bitrate(); }
A2DP_GetSinkTrackChannelTypeSbc(const uint8_t * p_codec_info)677 int A2DP_GetSinkTrackChannelTypeSbc(const uint8_t* p_codec_info) {
678 tA2DP_SBC_CIE sbc_cie;
679
680 tA2DP_STATUS a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, false);
681 if (a2dp_status != A2DP_SUCCESS) {
682 LOG_ERROR(LOG_TAG, "%s: cannot decode codec information: %d", __func__,
683 a2dp_status);
684 return -1;
685 }
686
687 switch (sbc_cie.ch_mode) {
688 case A2DP_SBC_IE_CH_MD_MONO:
689 return 1;
690 case A2DP_SBC_IE_CH_MD_DUAL:
691 case A2DP_SBC_IE_CH_MD_STEREO:
692 case A2DP_SBC_IE_CH_MD_JOINT:
693 return 3;
694 default:
695 break;
696 }
697
698 return -1;
699 }
700
A2DP_GetPacketTimestampSbc(UNUSED_ATTR const uint8_t * p_codec_info,const uint8_t * p_data,uint32_t * p_timestamp)701 bool A2DP_GetPacketTimestampSbc(UNUSED_ATTR const uint8_t* p_codec_info,
702 const uint8_t* p_data, uint32_t* p_timestamp) {
703 *p_timestamp = *(const uint32_t*)p_data;
704 return true;
705 }
706
A2DP_BuildCodecHeaderSbc(UNUSED_ATTR const uint8_t * p_codec_info,BT_HDR * p_buf,uint16_t frames_per_packet)707 bool A2DP_BuildCodecHeaderSbc(UNUSED_ATTR const uint8_t* p_codec_info,
708 BT_HDR* p_buf, uint16_t frames_per_packet) {
709 uint8_t* p;
710
711 p_buf->offset -= A2DP_SBC_MPL_HDR_LEN;
712 p = (uint8_t*)(p_buf + 1) + p_buf->offset;
713 p_buf->len += A2DP_SBC_MPL_HDR_LEN;
714 A2DP_BuildMediaPayloadHeaderSbc(p, false, false, false,
715 (uint8_t)frames_per_packet);
716
717 return true;
718 }
719
A2DP_CodecInfoStringSbc(const uint8_t * p_codec_info)720 std::string A2DP_CodecInfoStringSbc(const uint8_t* p_codec_info) {
721 std::stringstream res;
722 std::string field;
723 tA2DP_STATUS a2dp_status;
724 tA2DP_SBC_CIE sbc_cie;
725
726 a2dp_status = A2DP_ParseInfoSbc(&sbc_cie, p_codec_info, true);
727 if (a2dp_status != A2DP_SUCCESS) {
728 res << "A2DP_ParseInfoSbc fail: " << loghex(a2dp_status);
729 return res.str();
730 }
731
732 res << "\tname: SBC\n";
733
734 // Sample frequency
735 field.clear();
736 AppendField(&field, (sbc_cie.samp_freq == 0), "NONE");
737 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_16), "16000");
738 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_32), "32000");
739 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_44), "44100");
740 AppendField(&field, (sbc_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_48), "48000");
741 res << "\tsamp_freq: " << field << " (" << loghex(sbc_cie.samp_freq) << ")\n";
742
743 // Channel mode
744 field.clear();
745 AppendField(&field, (sbc_cie.ch_mode == 0), "NONE");
746 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_MONO), "Mono");
747 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_DUAL), "Dual");
748 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_STEREO), "Stereo");
749 AppendField(&field, (sbc_cie.ch_mode & A2DP_SBC_IE_CH_MD_JOINT), "Joint");
750 res << "\tch_mode: " << field << " (" << loghex(sbc_cie.ch_mode) << ")\n";
751
752 // Block length
753 field.clear();
754 AppendField(&field, (sbc_cie.block_len == 0), "NONE");
755 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_4), "4");
756 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_8), "8");
757 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_12), "12");
758 AppendField(&field, (sbc_cie.block_len & A2DP_SBC_IE_BLOCKS_16), "16");
759 res << "\tblock_len: " << field << " (" << loghex(sbc_cie.block_len) << ")\n";
760
761 // Number of subbands
762 field.clear();
763 AppendField(&field, (sbc_cie.num_subbands == 0), "NONE");
764 AppendField(&field, (sbc_cie.num_subbands & A2DP_SBC_IE_SUBBAND_4), "4");
765 AppendField(&field, (sbc_cie.num_subbands & A2DP_SBC_IE_SUBBAND_8), "8");
766 res << "\tnum_subbands: " << field << " (" << loghex(sbc_cie.num_subbands)
767 << ")\n";
768
769 // Allocation method
770 field.clear();
771 AppendField(&field, (sbc_cie.alloc_method == 0), "NONE");
772 AppendField(&field, (sbc_cie.alloc_method & A2DP_SBC_IE_ALLOC_MD_S), "SNR");
773 AppendField(&field, (sbc_cie.alloc_method & A2DP_SBC_IE_ALLOC_MD_L),
774 "Loundess");
775 res << "\talloc_method: " << field << " (" << loghex(sbc_cie.alloc_method)
776 << ")\n";
777
778 // Min/max bitloop
779 res << "\tBit pool Min: " << std::to_string(sbc_cie.min_bitpool)
780 << " Max: " << std::to_string(sbc_cie.max_bitpool);
781
782 return res.str();
783 }
784
A2DP_GetEncoderInterfaceSbc(const uint8_t * p_codec_info)785 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterfaceSbc(
786 const uint8_t* p_codec_info) {
787 if (!A2DP_IsSourceCodecValidSbc(p_codec_info)) return NULL;
788
789 return &a2dp_encoder_interface_sbc;
790 }
791
A2DP_GetDecoderInterfaceSbc(const uint8_t * p_codec_info)792 const tA2DP_DECODER_INTERFACE* A2DP_GetDecoderInterfaceSbc(
793 const uint8_t* p_codec_info) {
794 if (!A2DP_IsSinkCodecValidSbc(p_codec_info)) return NULL;
795
796 return &a2dp_decoder_interface_sbc;
797 }
798
A2DP_AdjustCodecSbc(uint8_t * p_codec_info)799 bool A2DP_AdjustCodecSbc(uint8_t* p_codec_info) {
800 tA2DP_SBC_CIE cfg_cie;
801
802 if (A2DP_ParseInfoSbc(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS)
803 return false;
804
805 // Updated the max bitpool
806 if (cfg_cie.max_bitpool > A2DP_SBC_MAX_BITPOOL) {
807 LOG_WARN(LOG_TAG, "%s: Updated the SBC codec max bitpool from %d to %d",
808 __func__, cfg_cie.max_bitpool, A2DP_SBC_MAX_BITPOOL);
809 cfg_cie.max_bitpool = A2DP_SBC_MAX_BITPOOL;
810 }
811
812 return (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &cfg_cie, p_codec_info) ==
813 A2DP_SUCCESS);
814 }
815
A2DP_SourceCodecIndexSbc(UNUSED_ATTR const uint8_t * p_codec_info)816 btav_a2dp_codec_index_t A2DP_SourceCodecIndexSbc(
817 UNUSED_ATTR const uint8_t* p_codec_info) {
818 return BTAV_A2DP_CODEC_INDEX_SOURCE_SBC;
819 }
820
A2DP_SinkCodecIndexSbc(UNUSED_ATTR const uint8_t * p_codec_info)821 btav_a2dp_codec_index_t A2DP_SinkCodecIndexSbc(
822 UNUSED_ATTR const uint8_t* p_codec_info) {
823 return BTAV_A2DP_CODEC_INDEX_SINK_SBC;
824 }
825
A2DP_CodecIndexStrSbc(void)826 const char* A2DP_CodecIndexStrSbc(void) { return "SBC"; }
827
A2DP_CodecIndexStrSbcSink(void)828 const char* A2DP_CodecIndexStrSbcSink(void) { return "SBC SINK"; }
829
A2DP_InitCodecConfigSbc(AvdtpSepConfig * p_cfg)830 bool A2DP_InitCodecConfigSbc(AvdtpSepConfig* p_cfg) {
831 if (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &a2dp_sbc_source_caps,
832 p_cfg->codec_info) != A2DP_SUCCESS) {
833 return false;
834 }
835
836 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
837 /* Content protection info - support SCMS-T */
838 uint8_t* p = p_cfg->protect_info;
839 *p++ = AVDT_CP_LOSC;
840 UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID);
841 p_cfg->num_protect = 1;
842 #endif
843
844 return true;
845 }
846
A2DP_InitCodecConfigSbcSink(AvdtpSepConfig * p_cfg)847 bool A2DP_InitCodecConfigSbcSink(AvdtpSepConfig* p_cfg) {
848 if (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &a2dp_sbc_sink_caps,
849 p_cfg->codec_info) != A2DP_SUCCESS) {
850 return false;
851 }
852
853 return true;
854 }
855
build_codec_config(const tA2DP_SBC_CIE & config_cie,btav_a2dp_codec_config_t * result)856 UNUSED_ATTR static void build_codec_config(const tA2DP_SBC_CIE& config_cie,
857 btav_a2dp_codec_config_t* result) {
858 if (config_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_44)
859 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
860 if (config_cie.samp_freq & A2DP_SBC_IE_SAMP_FREQ_48)
861 result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
862
863 result->bits_per_sample = config_cie.bits_per_sample;
864
865 if (config_cie.ch_mode & A2DP_SBC_IE_CH_MD_MONO)
866 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
867
868 if (config_cie.ch_mode & (A2DP_SBC_IE_CH_MD_STEREO | A2DP_SBC_IE_CH_MD_JOINT |
869 A2DP_SBC_IE_CH_MD_DUAL)) {
870 result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
871 }
872 }
873
A2dpCodecConfigSbcSource(btav_a2dp_codec_priority_t codec_priority)874 A2dpCodecConfigSbcSource::A2dpCodecConfigSbcSource(
875 btav_a2dp_codec_priority_t codec_priority)
876 : A2dpCodecConfigSbcBase(BTAV_A2DP_CODEC_INDEX_SOURCE_SBC,
877 A2DP_CodecIndexStrSbc(), codec_priority, true) {
878 // Compute the local capability
879 if (a2dp_sbc_source_caps.samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
880 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
881 }
882 if (a2dp_sbc_source_caps.samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
883 codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
884 }
885 codec_local_capability_.bits_per_sample =
886 a2dp_sbc_source_caps.bits_per_sample;
887 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
888 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
889 }
890 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
891 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
892 }
893 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
894 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
895 }
896 if (a2dp_sbc_source_caps.ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
897 codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
898 }
899 }
900
~A2dpCodecConfigSbcSource()901 A2dpCodecConfigSbcSource::~A2dpCodecConfigSbcSource() {}
902
init()903 bool A2dpCodecConfigSbcSource::init() {
904 if (!isValid()) return false;
905
906 // Load the encoder
907 if (!A2DP_LoadEncoderSbc()) {
908 LOG_ERROR(LOG_TAG, "%s: cannot load the encoder", __func__);
909 return false;
910 }
911
912 return true;
913 }
914
useRtpHeaderMarkerBit() const915 bool A2dpCodecConfigSbcSource::useRtpHeaderMarkerBit() const { return false; }
916
917 //
918 // Selects the best sample rate from |samp_freq|.
919 // The result is stored in |p_result| and |p_codec_config|.
920 // Returns true if a selection was made, otherwise false.
921 //
select_best_sample_rate(uint8_t samp_freq,tA2DP_SBC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)922 static bool select_best_sample_rate(uint8_t samp_freq, tA2DP_SBC_CIE* p_result,
923 btav_a2dp_codec_config_t* p_codec_config) {
924 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
925 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_48;
926 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
927 return true;
928 }
929 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
930 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_44;
931 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
932 return true;
933 }
934 return false;
935 }
936
937 //
938 // Selects the audio sample rate from |p_codec_audio_config|.
939 // |samp_freq| contains the capability.
940 // The result is stored in |p_result| and |p_codec_config|.
941 // Returns true if a selection was made, otherwise false.
942 //
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)943 static bool select_audio_sample_rate(
944 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t samp_freq,
945 tA2DP_SBC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
946 switch (p_codec_audio_config->sample_rate) {
947 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
948 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
949 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_44;
950 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
951 return true;
952 }
953 break;
954 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
955 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
956 p_result->samp_freq = A2DP_SBC_IE_SAMP_FREQ_48;
957 p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
958 return true;
959 }
960 break;
961 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
962 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
963 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
964 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
965 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
966 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
967 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
968 break;
969 }
970
971 return false;
972 }
973
974 //
975 // Selects the best bits per sample.
976 // The result is stored in |p_codec_config|.
977 // Returns true if a selection was made, otherwise false.
978 //
select_best_bits_per_sample(btav_a2dp_codec_config_t * p_codec_config)979 static bool select_best_bits_per_sample(
980 btav_a2dp_codec_config_t* p_codec_config) {
981 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
982 return true;
983 }
984
985 //
986 // Selects the audio bits per sample from |p_codec_audio_config|.
987 // The result is stored in |p_codec_config|.
988 // Returns true if a selection was made, otherwise false.
989 //
select_audio_bits_per_sample(const btav_a2dp_codec_config_t * p_codec_audio_config,btav_a2dp_codec_config_t * p_codec_config)990 static bool select_audio_bits_per_sample(
991 const btav_a2dp_codec_config_t* p_codec_audio_config,
992 btav_a2dp_codec_config_t* p_codec_config) {
993 switch (p_codec_audio_config->bits_per_sample) {
994 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
995 p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
996 return true;
997 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
998 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
999 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
1000 break;
1001 }
1002 return false;
1003 }
1004
1005 //
1006 // Selects the best channel mode from |ch_mode|.
1007 // The result is stored in |p_result| and |p_codec_config|.
1008 // Returns true if a selection was made, otherwise false.
1009 //
select_best_channel_mode(uint8_t ch_mode,tA2DP_SBC_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)1010 static bool select_best_channel_mode(uint8_t ch_mode, tA2DP_SBC_CIE* p_result,
1011 btav_a2dp_codec_config_t* p_codec_config) {
1012 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
1013 p_result->ch_mode = A2DP_SBC_IE_CH_MD_JOINT;
1014 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1015 return true;
1016 }
1017 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1018 p_result->ch_mode = A2DP_SBC_IE_CH_MD_STEREO;
1019 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1020 return true;
1021 }
1022 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1023 p_result->ch_mode = A2DP_SBC_IE_CH_MD_DUAL;
1024 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1025 return true;
1026 }
1027 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1028 p_result->ch_mode = A2DP_SBC_IE_CH_MD_MONO;
1029 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1030 return true;
1031 }
1032 return false;
1033 }
1034
1035 //
1036 // Selects the audio channel mode from |p_codec_audio_config|.
1037 // |ch_mode| contains the capability.
1038 // The result is stored in |p_result| and |p_codec_config|.
1039 // Returns true if a selection was made, otherwise false.
1040 //
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)1041 static bool select_audio_channel_mode(
1042 const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t ch_mode,
1043 tA2DP_SBC_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
1044 switch (p_codec_audio_config->channel_mode) {
1045 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
1046 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1047 p_result->ch_mode = A2DP_SBC_IE_CH_MD_MONO;
1048 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1049 return true;
1050 }
1051 break;
1052 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
1053 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
1054 p_result->ch_mode = A2DP_SBC_IE_CH_MD_JOINT;
1055 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1056 return true;
1057 }
1058 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1059 p_result->ch_mode = A2DP_SBC_IE_CH_MD_STEREO;
1060 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1061 return true;
1062 }
1063 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1064 p_result->ch_mode = A2DP_SBC_IE_CH_MD_DUAL;
1065 p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1066 return true;
1067 }
1068 break;
1069 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1070 break;
1071 }
1072
1073 return false;
1074 }
1075
setCodecConfig(const uint8_t * p_peer_codec_info,bool is_capability,uint8_t * p_result_codec_config)1076 bool A2dpCodecConfigSbcBase::setCodecConfig(const uint8_t* p_peer_codec_info,
1077 bool is_capability,
1078 uint8_t* p_result_codec_config) {
1079 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
1080 tA2DP_SBC_CIE peer_info_cie;
1081 tA2DP_SBC_CIE result_config_cie;
1082 uint8_t samp_freq;
1083 uint8_t ch_mode;
1084 uint8_t block_len;
1085 uint8_t num_subbands;
1086 uint8_t alloc_method;
1087 const tA2DP_SBC_CIE* p_a2dp_sbc_caps =
1088 (is_source_) ? &a2dp_sbc_source_caps : &a2dp_sbc_sink_caps;
1089
1090 // Save the internal state
1091 btav_a2dp_codec_config_t saved_codec_config = codec_config_;
1092 btav_a2dp_codec_config_t saved_codec_capability = codec_capability_;
1093 btav_a2dp_codec_config_t saved_codec_selectable_capability =
1094 codec_selectable_capability_;
1095 btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_;
1096 btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_;
1097 uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE];
1098 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
1099 uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE];
1100 memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_));
1101 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
1102 sizeof(ota_codec_peer_capability_));
1103 memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_,
1104 sizeof(ota_codec_peer_config_));
1105
1106 tA2DP_STATUS status =
1107 A2DP_ParseInfoSbc(&peer_info_cie, p_peer_codec_info, is_capability);
1108 if (status != A2DP_SUCCESS) {
1109 LOG_ERROR(LOG_TAG, "%s: can't parse peer's capabilities: error = %d",
1110 __func__, status);
1111 goto fail;
1112 }
1113 // Try using the prefered peer codec config (if valid), instead of the peer
1114 // capability.
1115 if (is_capability) {
1116 if (is_source_) {
1117 if (A2DP_IsPeerSinkCodecValidSbc(ota_codec_peer_config_)) {
1118 status =
1119 A2DP_ParseInfoSbc(&peer_info_cie, ota_codec_peer_config_, false);
1120 }
1121 } else {
1122 if (A2DP_IsPeerSourceCodecValidSbc(ota_codec_peer_config_)) {
1123 status =
1124 A2DP_ParseInfoSbc(&peer_info_cie, ota_codec_peer_config_, false);
1125 }
1126 }
1127 if (status != A2DP_SUCCESS) {
1128 // Use the peer codec capability
1129 status =
1130 A2DP_ParseInfoSbc(&peer_info_cie, p_peer_codec_info, is_capability);
1131 CHECK(status == A2DP_SUCCESS);
1132 }
1133 }
1134
1135 //
1136 // Build the preferred configuration
1137 //
1138 memset(&result_config_cie, 0, sizeof(result_config_cie));
1139
1140 //
1141 // Select the sample frequency
1142 //
1143 samp_freq = p_a2dp_sbc_caps->samp_freq & peer_info_cie.samp_freq;
1144 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
1145 switch (codec_user_config_.sample_rate) {
1146 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
1147 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
1148 result_config_cie.samp_freq = A2DP_SBC_IE_SAMP_FREQ_44;
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_48000:
1154 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
1155 result_config_cie.samp_freq = A2DP_SBC_IE_SAMP_FREQ_48;
1156 codec_capability_.sample_rate = codec_user_config_.sample_rate;
1157 codec_config_.sample_rate = codec_user_config_.sample_rate;
1158 }
1159 break;
1160 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
1161 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
1162 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
1163 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
1164 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
1165 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
1166 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
1167 codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
1168 codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
1169 break;
1170 }
1171
1172 // Select the sample frequency if there is no user preference
1173 do {
1174 // Compute the selectable capability
1175 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
1176 codec_selectable_capability_.sample_rate |=
1177 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1178 }
1179 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
1180 codec_selectable_capability_.sample_rate |=
1181 BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1182 }
1183
1184 if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break;
1185
1186 // Compute the common capability
1187 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44)
1188 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1189 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48)
1190 codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1191
1192 // No user preference - try the codec audio config
1193 if (select_audio_sample_rate(&codec_audio_config_, samp_freq,
1194 &result_config_cie, &codec_config_)) {
1195 break;
1196 }
1197
1198 // No user preference - try the default config
1199 if (select_best_sample_rate(
1200 a2dp_sbc_default_config.samp_freq & peer_info_cie.samp_freq,
1201 &result_config_cie, &codec_config_)) {
1202 break;
1203 }
1204
1205 // No user preference - use the best match
1206 if (select_best_sample_rate(samp_freq, &result_config_cie,
1207 &codec_config_)) {
1208 break;
1209 }
1210 } while (false);
1211 if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) {
1212 LOG_ERROR(LOG_TAG,
1213 "%s: cannot match sample frequency: local caps = 0x%x "
1214 "peer info = 0x%x",
1215 __func__, p_a2dp_sbc_caps->samp_freq, peer_info_cie.samp_freq);
1216 goto fail;
1217 }
1218
1219 //
1220 // Select the bits per sample
1221 //
1222 // NOTE: this information is NOT included in the SBC A2DP codec description
1223 // that is sent OTA.
1224 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1225 switch (codec_user_config_.bits_per_sample) {
1226 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
1227 codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1228 codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1229 break;
1230 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
1231 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
1232 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
1233 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1234 codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1235 break;
1236 }
1237
1238 // Select the bits per sample if there is no user preference
1239 do {
1240 // Compute the selectable capability
1241 codec_selectable_capability_.bits_per_sample =
1242 p_a2dp_sbc_caps->bits_per_sample;
1243
1244 if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE)
1245 break;
1246
1247 // Compute the common capability
1248 codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
1249
1250 // No user preference - try the codec audio config
1251 if (select_audio_bits_per_sample(&codec_audio_config_, &codec_config_)) {
1252 break;
1253 }
1254
1255 // No user preference - try the default config
1256 if (select_best_bits_per_sample(&codec_config_)) {
1257 break;
1258 }
1259
1260 // No user preference - use the best match
1261 // TODO: no-op - temporary kept here for consistency
1262 if (select_best_bits_per_sample(&codec_config_)) {
1263 break;
1264 }
1265 } while (false);
1266 if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) {
1267 LOG_ERROR(LOG_TAG,
1268 "%s: cannot match bits per sample: user preference = 0x%x",
1269 __func__, codec_user_config_.bits_per_sample);
1270 goto fail;
1271 }
1272
1273 //
1274 // Select the channel mode
1275 //
1276 ch_mode = p_a2dp_sbc_caps->ch_mode & peer_info_cie.ch_mode;
1277 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1278 switch (codec_user_config_.channel_mode) {
1279 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
1280 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1281 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_MONO;
1282 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1283 codec_config_.channel_mode = codec_user_config_.channel_mode;
1284 }
1285 break;
1286 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
1287 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
1288 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_JOINT;
1289 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1290 codec_config_.channel_mode = codec_user_config_.channel_mode;
1291 break;
1292 }
1293 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1294 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_STEREO;
1295 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1296 codec_config_.channel_mode = codec_user_config_.channel_mode;
1297 break;
1298 }
1299 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1300 result_config_cie.ch_mode = A2DP_SBC_IE_CH_MD_DUAL;
1301 codec_capability_.channel_mode = codec_user_config_.channel_mode;
1302 codec_config_.channel_mode = codec_user_config_.channel_mode;
1303 break;
1304 }
1305 break;
1306 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1307 codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1308 codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1309 break;
1310 }
1311
1312 // Select the channel mode if there is no user preference
1313 do {
1314 // Compute the selectable capability
1315 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1316 codec_selectable_capability_.channel_mode |=
1317 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1318 }
1319 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
1320 codec_selectable_capability_.channel_mode |=
1321 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1322 }
1323 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1324 codec_selectable_capability_.channel_mode |=
1325 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1326 }
1327 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1328 codec_selectable_capability_.channel_mode |=
1329 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1330 }
1331
1332 if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break;
1333
1334 // Compute the common capability
1335 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO)
1336 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1337 if (ch_mode & (A2DP_SBC_IE_CH_MD_JOINT | A2DP_SBC_IE_CH_MD_STEREO |
1338 A2DP_SBC_IE_CH_MD_DUAL)) {
1339 codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1340 }
1341
1342 // No user preference - use the codec audio config
1343 if (select_audio_channel_mode(&codec_audio_config_, ch_mode,
1344 &result_config_cie, &codec_config_)) {
1345 break;
1346 }
1347
1348 // No user preference - try the default config
1349 if (select_best_channel_mode(
1350 a2dp_sbc_default_config.ch_mode & peer_info_cie.ch_mode,
1351 &result_config_cie, &codec_config_)) {
1352 break;
1353 }
1354
1355 // No user preference - use the best match
1356 if (select_best_channel_mode(ch_mode, &result_config_cie, &codec_config_)) {
1357 break;
1358 }
1359 } while (false);
1360 if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) {
1361 LOG_ERROR(LOG_TAG,
1362 "%s: cannot match channel mode: local caps = 0x%x "
1363 "peer info = 0x%x",
1364 __func__, p_a2dp_sbc_caps->ch_mode, peer_info_cie.ch_mode);
1365 goto fail;
1366 }
1367
1368 //
1369 // Select the block length
1370 //
1371 block_len = p_a2dp_sbc_caps->block_len & peer_info_cie.block_len;
1372 if (block_len & A2DP_SBC_IE_BLOCKS_16) {
1373 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_16;
1374 } else if (block_len & A2DP_SBC_IE_BLOCKS_12) {
1375 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_12;
1376 } else if (block_len & A2DP_SBC_IE_BLOCKS_8) {
1377 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_8;
1378 } else if (block_len & A2DP_SBC_IE_BLOCKS_4) {
1379 result_config_cie.block_len = A2DP_SBC_IE_BLOCKS_4;
1380 } else {
1381 LOG_ERROR(LOG_TAG,
1382 "%s: cannot match block length: local caps = 0x%x "
1383 "peer info = 0x%x",
1384 __func__, p_a2dp_sbc_caps->block_len, peer_info_cie.block_len);
1385 goto fail;
1386 }
1387
1388 //
1389 // Select the number of sub-bands
1390 //
1391 num_subbands = p_a2dp_sbc_caps->num_subbands & peer_info_cie.num_subbands;
1392 if (num_subbands & A2DP_SBC_IE_SUBBAND_8) {
1393 result_config_cie.num_subbands = A2DP_SBC_IE_SUBBAND_8;
1394 } else if (num_subbands & A2DP_SBC_IE_SUBBAND_4) {
1395 result_config_cie.num_subbands = A2DP_SBC_IE_SUBBAND_4;
1396 } else {
1397 LOG_ERROR(LOG_TAG,
1398 "%s: cannot match number of sub-bands: local caps = 0x%x "
1399 "peer info = 0x%x",
1400 __func__, p_a2dp_sbc_caps->num_subbands,
1401 peer_info_cie.num_subbands);
1402 goto fail;
1403 }
1404
1405 //
1406 // Select the allocation method
1407 //
1408 alloc_method = p_a2dp_sbc_caps->alloc_method & peer_info_cie.alloc_method;
1409 if (alloc_method & A2DP_SBC_IE_ALLOC_MD_L) {
1410 result_config_cie.alloc_method = A2DP_SBC_IE_ALLOC_MD_L;
1411 } else if (alloc_method & A2DP_SBC_IE_ALLOC_MD_S) {
1412 result_config_cie.alloc_method = A2DP_SBC_IE_ALLOC_MD_S;
1413 } else {
1414 LOG_ERROR(LOG_TAG,
1415 "%s: cannot match allocation method: local caps = 0x%x "
1416 "peer info = 0x%x",
1417 __func__, p_a2dp_sbc_caps->alloc_method,
1418 peer_info_cie.alloc_method);
1419 goto fail;
1420 }
1421
1422 //
1423 // Select the min/max bitpool
1424 //
1425 result_config_cie.min_bitpool = p_a2dp_sbc_caps->min_bitpool;
1426 if (result_config_cie.min_bitpool < peer_info_cie.min_bitpool)
1427 result_config_cie.min_bitpool = peer_info_cie.min_bitpool;
1428 result_config_cie.max_bitpool = p_a2dp_sbc_caps->max_bitpool;
1429 if (result_config_cie.max_bitpool > peer_info_cie.max_bitpool)
1430 result_config_cie.max_bitpool = peer_info_cie.max_bitpool;
1431 if (result_config_cie.min_bitpool > result_config_cie.max_bitpool) {
1432 LOG_ERROR(LOG_TAG,
1433 "%s: cannot match min/max bitpool: "
1434 "local caps min/max = 0x%x/0x%x peer info min/max = 0x%x/0x%x",
1435 __func__, p_a2dp_sbc_caps->min_bitpool,
1436 p_a2dp_sbc_caps->max_bitpool, peer_info_cie.min_bitpool,
1437 peer_info_cie.max_bitpool);
1438 goto fail;
1439 }
1440
1441 if (A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1442 p_result_codec_config) != A2DP_SUCCESS) {
1443 goto fail;
1444 }
1445
1446 //
1447 // Copy the codec-specific fields if they are not zero
1448 //
1449 if (codec_user_config_.codec_specific_1 != 0)
1450 codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
1451 if (codec_user_config_.codec_specific_2 != 0)
1452 codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2;
1453 if (codec_user_config_.codec_specific_3 != 0)
1454 codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3;
1455 if (codec_user_config_.codec_specific_4 != 0)
1456 codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4;
1457
1458 // Create a local copy of the peer codec capability/config, and the
1459 // result codec config.
1460 if (is_capability) {
1461 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1462 ota_codec_peer_capability_);
1463 } else {
1464 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1465 ota_codec_peer_config_);
1466 }
1467 CHECK(status == A2DP_SUCCESS);
1468 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1469 ota_codec_config_);
1470 CHECK(status == A2DP_SUCCESS);
1471 return true;
1472
1473 fail:
1474 // Restore the internal state
1475 codec_config_ = saved_codec_config;
1476 codec_capability_ = saved_codec_capability;
1477 codec_selectable_capability_ = saved_codec_selectable_capability;
1478 codec_user_config_ = saved_codec_user_config;
1479 codec_audio_config_ = saved_codec_audio_config;
1480 memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_));
1481 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1482 sizeof(ota_codec_peer_capability_));
1483 memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config,
1484 sizeof(ota_codec_peer_config_));
1485 return false;
1486 }
1487
setPeerCodecCapabilities(const uint8_t * p_peer_codec_capabilities)1488 bool A2dpCodecConfigSbcBase::setPeerCodecCapabilities(
1489 const uint8_t* p_peer_codec_capabilities) {
1490 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
1491 tA2DP_SBC_CIE peer_info_cie;
1492 uint8_t samp_freq;
1493 uint8_t ch_mode;
1494 const tA2DP_SBC_CIE* p_a2dp_sbc_caps =
1495 (is_source_) ? &a2dp_sbc_source_caps : &a2dp_sbc_sink_caps;
1496
1497 // Save the internal state
1498 btav_a2dp_codec_config_t saved_codec_selectable_capability =
1499 codec_selectable_capability_;
1500 uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
1501 memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
1502 sizeof(ota_codec_peer_capability_));
1503
1504 tA2DP_STATUS status =
1505 A2DP_ParseInfoSbc(&peer_info_cie, p_peer_codec_capabilities, true);
1506 if (status != A2DP_SUCCESS) {
1507 LOG_ERROR(LOG_TAG, "%s: can't parse peer's capabilities: error = %d",
1508 __func__, status);
1509 goto fail;
1510 }
1511
1512 // Compute the selectable capability - sample rate
1513 samp_freq = p_a2dp_sbc_caps->samp_freq & peer_info_cie.samp_freq;
1514 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_44) {
1515 codec_selectable_capability_.sample_rate |=
1516 BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
1517 }
1518 if (samp_freq & A2DP_SBC_IE_SAMP_FREQ_48) {
1519 codec_selectable_capability_.sample_rate |=
1520 BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1521 }
1522
1523 // Compute the selectable capability - bits per sample
1524 codec_selectable_capability_.bits_per_sample =
1525 p_a2dp_sbc_caps->bits_per_sample;
1526
1527 // Compute the selectable capability - channel mode
1528 ch_mode = p_a2dp_sbc_caps->ch_mode & peer_info_cie.ch_mode;
1529 if (ch_mode & A2DP_SBC_IE_CH_MD_MONO) {
1530 codec_selectable_capability_.channel_mode |=
1531 BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1532 }
1533 if (ch_mode & A2DP_SBC_IE_CH_MD_JOINT) {
1534 codec_selectable_capability_.channel_mode |=
1535 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1536 }
1537 if (ch_mode & A2DP_SBC_IE_CH_MD_STEREO) {
1538 codec_selectable_capability_.channel_mode |=
1539 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1540 }
1541 if (ch_mode & A2DP_SBC_IE_CH_MD_DUAL) {
1542 codec_selectable_capability_.channel_mode |=
1543 BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1544 }
1545
1546 status = A2DP_BuildInfoSbc(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1547 ota_codec_peer_capability_);
1548 CHECK(status == A2DP_SUCCESS);
1549 return true;
1550
1551 fail:
1552 // Restore the internal state
1553 codec_selectable_capability_ = saved_codec_selectable_capability;
1554 memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1555 sizeof(ota_codec_peer_capability_));
1556 return false;
1557 }
1558
A2dpCodecConfigSbcSink(btav_a2dp_codec_priority_t codec_priority)1559 A2dpCodecConfigSbcSink::A2dpCodecConfigSbcSink(
1560 btav_a2dp_codec_priority_t codec_priority)
1561 : A2dpCodecConfigSbcBase(BTAV_A2DP_CODEC_INDEX_SINK_SBC,
1562 A2DP_CodecIndexStrSbcSink(), codec_priority,
1563 false) {}
1564
~A2dpCodecConfigSbcSink()1565 A2dpCodecConfigSbcSink::~A2dpCodecConfigSbcSink() {}
1566
init()1567 bool A2dpCodecConfigSbcSink::init() {
1568 if (!isValid()) return false;
1569
1570 // Load the decoder
1571 if (!A2DP_LoadDecoderSbc()) {
1572 LOG_ERROR(LOG_TAG, "%s: cannot load the decoder", __func__);
1573 return false;
1574 }
1575
1576 return true;
1577 }
1578
useRtpHeaderMarkerBit() const1579 bool A2dpCodecConfigSbcSink::useRtpHeaderMarkerBit() const {
1580 // TODO: This method applies only to Source codecs
1581 return false;
1582 }
1583
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)1584 bool A2dpCodecConfigSbcSink::updateEncoderUserConfig(
1585 UNUSED_ATTR const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
1586 UNUSED_ATTR bool* p_restart_input, UNUSED_ATTR bool* p_restart_output,
1587 UNUSED_ATTR bool* p_config_updated) {
1588 // TODO: This method applies only to Source codecs
1589 return false;
1590 }
1591
encoderIntervalMs() const1592 uint64_t A2dpCodecConfigSbcSink::encoderIntervalMs() const {
1593 // TODO: This method applies only to Source codecs
1594 return 0;
1595 }
1596
getEffectiveMtu() const1597 int A2dpCodecConfigSbcSink::getEffectiveMtu() const {
1598 // TODO: This method applies only to Source codecs
1599 return 0;
1600 }
1601