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