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