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