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