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