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