• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /******************************************************************************
18  *
19  *  Utility functions to help build and parse the Opus Codec Information
20  *  Element and Media Payload.
21  *
22  ******************************************************************************/
23 
24 #define LOG_TAG "a2dp_vendor_opus"
25 
26 #include "a2dp_vendor_opus.h"
27 
28 #include <base/logging.h>
29 #include <string.h>
30 
31 #include "a2dp_vendor.h"
32 #include "a2dp_vendor_opus_decoder.h"
33 #include "a2dp_vendor_opus_encoder.h"
34 #include "bt_target.h"
35 #include "bt_utils.h"
36 #include "btif_av_co.h"
37 #include "osi/include/log.h"
38 #include "osi/include/osi.h"
39 
40 // data type for the Opus Codec Information Element */
41 // NOTE: bits_per_sample and frameSize for Opus encoder initialization.
42 typedef struct {
43   uint32_t vendorId;
44   uint16_t codecId;    /* Codec ID for Opus */
45   uint8_t sampleRate;  /* Sampling Frequency */
46   uint8_t channelMode; /* STEREO/DUAL/MONO */
47   btav_a2dp_codec_bits_per_sample_t bits_per_sample;
48   uint8_t future1; /* codec_specific_1 framesize */
49   uint8_t future2; /* codec_specific_2 */
50   uint8_t future3; /* codec_specific_3 */
51   uint8_t future4; /* codec_specific_4 */
52 } tA2DP_OPUS_CIE;
53 
54 /* Opus Source codec capabilities */
55 static const tA2DP_OPUS_CIE a2dp_opus_source_caps = {
56     A2DP_OPUS_VENDOR_ID,  // vendorId
57     A2DP_OPUS_CODEC_ID,   // codecId
58     // sampleRate
59     (A2DP_OPUS_SAMPLING_FREQ_48000),
60     // channelMode
61     (A2DP_OPUS_CHANNEL_MODE_STEREO),
62     // bits_per_sample
63     (BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16),
64     // future 1 frameSize
65     (A2DP_OPUS_20MS_FRAMESIZE),
66     // future 2
67     0x00,
68     // future 3
69     0x00,
70     // future 4
71     0x00};
72 
73 /* Opus Sink codec capabilities */
74 static const tA2DP_OPUS_CIE a2dp_opus_sink_caps = {
75     A2DP_OPUS_VENDOR_ID,  // vendorId
76     A2DP_OPUS_CODEC_ID,   // codecId
77     // sampleRate
78     (A2DP_OPUS_SAMPLING_FREQ_48000),
79     // channelMode
80     (A2DP_OPUS_CHANNEL_MODE_STEREO),
81     // bits_per_sample
82     (BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16),
83     // future 1 frameSize
84     (A2DP_OPUS_20MS_FRAMESIZE),
85     // future 2
86     0x00,
87     // future 3
88     0x00,
89     // future 4
90     0x00};
91 
92 /* Default Opus codec configuration */
93 static const tA2DP_OPUS_CIE a2dp_opus_default_config = {
94     A2DP_OPUS_VENDOR_ID,                 // vendorId
95     A2DP_OPUS_CODEC_ID,                  // codecId
96     A2DP_OPUS_SAMPLING_FREQ_48000,       // sampleRate
97     A2DP_OPUS_CHANNEL_MODE_STEREO,       // channelMode
98     BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16,  // bits_per_sample
99     A2DP_OPUS_20MS_FRAMESIZE,            // frameSize
100     0x00,                                // future 2
101     0x00,                                // future 3
102     0x00                                 // future 4
103 };
104 
105 static const tA2DP_ENCODER_INTERFACE a2dp_encoder_interface_opus = {
106     a2dp_vendor_opus_encoder_init,
107     a2dp_vendor_opus_encoder_cleanup,
108     a2dp_vendor_opus_feeding_reset,
109     a2dp_vendor_opus_feeding_flush,
110     a2dp_vendor_opus_get_encoder_interval_ms,
111     a2dp_vendor_opus_get_effective_frame_size,
112     a2dp_vendor_opus_send_frames,
113     a2dp_vendor_opus_set_transmit_queue_length};
114 
115 static const tA2DP_DECODER_INTERFACE a2dp_decoder_interface_opus = {
116     a2dp_vendor_opus_decoder_init,          a2dp_vendor_opus_decoder_cleanup,
117     a2dp_vendor_opus_decoder_decode_packet, a2dp_vendor_opus_decoder_start,
118     a2dp_vendor_opus_decoder_suspend,       a2dp_vendor_opus_decoder_configure,
119 };
120 
121 UNUSED_ATTR static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityOpus(
122     const tA2DP_OPUS_CIE* p_cap, const uint8_t* p_codec_info,
123     bool is_peer_codec_info);
124 
125 // Builds the Opus Media Codec Capabilities byte sequence beginning from the
126 // LOSC octet. |media_type| is the media type |AVDT_MEDIA_TYPE_*|.
127 // |p_ie| is a pointer to the Opus Codec Information Element information.
128 // The result is stored in |p_result|. Returns A2DP_SUCCESS on success,
129 // otherwise the corresponding A2DP error status code.
A2DP_BuildInfoOpus(uint8_t media_type,const tA2DP_OPUS_CIE * p_ie,uint8_t * p_result)130 static tA2DP_STATUS A2DP_BuildInfoOpus(uint8_t media_type,
131                                        const tA2DP_OPUS_CIE* p_ie,
132                                        uint8_t* p_result) {
133   if (p_ie == NULL || p_result == NULL) {
134     LOG_ERROR("invalid information element");
135     return A2DP_INVALID_PARAMS;
136   }
137 
138   *p_result++ = A2DP_OPUS_CODEC_LEN;
139   *p_result++ = (media_type << 4);
140   *p_result++ = A2DP_MEDIA_CT_NON_A2DP;
141 
142   // Vendor ID and Codec ID
143   *p_result++ = (uint8_t)(p_ie->vendorId & 0x000000FF);
144   *p_result++ = (uint8_t)((p_ie->vendorId & 0x0000FF00) >> 8);
145   *p_result++ = (uint8_t)((p_ie->vendorId & 0x00FF0000) >> 16);
146   *p_result++ = (uint8_t)((p_ie->vendorId & 0xFF000000) >> 24);
147   *p_result++ = (uint8_t)(p_ie->codecId & 0x00FF);
148   *p_result++ = (uint8_t)((p_ie->codecId & 0xFF00) >> 8);
149 
150   *p_result = 0;
151   *p_result |= (uint8_t)(p_ie->channelMode) & A2DP_OPUS_CHANNEL_MODE_MASK;
152   if ((*p_result & A2DP_OPUS_CHANNEL_MODE_MASK) == 0) {
153     LOG_ERROR("channelmode 0x%X setting failed", (p_ie->channelMode));
154     return A2DP_INVALID_PARAMS;
155   }
156 
157   *p_result |= ((uint8_t)(p_ie->future1) & A2DP_OPUS_FRAMESIZE_MASK);
158   if ((*p_result & A2DP_OPUS_FRAMESIZE_MASK) == 0) {
159     LOG_ERROR("frameSize 0x%X setting failed", (p_ie->future1));
160     return A2DP_INVALID_PARAMS;
161   }
162 
163   *p_result |= ((uint8_t)(p_ie->sampleRate) & A2DP_OPUS_SAMPLING_FREQ_MASK);
164   if ((*p_result & A2DP_OPUS_SAMPLING_FREQ_MASK) == 0) {
165     LOG_ERROR("samplerate 0x%X setting failed", (p_ie->sampleRate));
166     return A2DP_INVALID_PARAMS;
167   }
168 
169   p_result++;
170 
171   return A2DP_SUCCESS;
172 }
173 
174 // Parses the Opus Media Codec Capabilities byte sequence beginning from the
175 // LOSC octet. The result is stored in |p_ie|. The byte sequence to parse is
176 // |p_codec_info|. If |is_capability| is true, the byte sequence is
177 // codec capabilities, otherwise is codec configuration.
178 // Returns A2DP_SUCCESS on success, otherwise the corresponding A2DP error
179 // status code.
A2DP_ParseInfoOpus(tA2DP_OPUS_CIE * p_ie,const uint8_t * p_codec_info,bool is_capability)180 static tA2DP_STATUS A2DP_ParseInfoOpus(tA2DP_OPUS_CIE* p_ie,
181                                        const uint8_t* p_codec_info,
182                                        bool is_capability) {
183   uint8_t losc;
184   uint8_t media_type;
185   tA2DP_CODEC_TYPE codec_type;
186 
187   if (p_ie == NULL || p_codec_info == NULL) {
188     LOG_ERROR("unable to parse information element");
189     return A2DP_INVALID_PARAMS;
190   }
191 
192   // Check the codec capability length
193   losc = *p_codec_info++;
194   if (losc != A2DP_OPUS_CODEC_LEN) {
195     LOG_ERROR("invalid codec ie length %d", losc);
196     return A2DP_WRONG_CODEC;
197   }
198 
199   media_type = (*p_codec_info++) >> 4;
200   codec_type = *p_codec_info++;
201   /* Check the Media Type and Media Codec Type */
202   if (media_type != AVDT_MEDIA_TYPE_AUDIO ||
203       codec_type != A2DP_MEDIA_CT_NON_A2DP) {
204     LOG_ERROR("invalid codec");
205     return A2DP_WRONG_CODEC;
206   }
207 
208   // Check the Vendor ID and Codec ID */
209   p_ie->vendorId = (*p_codec_info & 0x000000FF) |
210                    (*(p_codec_info + 1) << 8 & 0x0000FF00) |
211                    (*(p_codec_info + 2) << 16 & 0x00FF0000) |
212                    (*(p_codec_info + 3) << 24 & 0xFF000000);
213   p_codec_info += 4;
214   p_ie->codecId =
215       (*p_codec_info & 0x00FF) | (*(p_codec_info + 1) << 8 & 0xFF00);
216   p_codec_info += 2;
217   if (p_ie->vendorId != A2DP_OPUS_VENDOR_ID ||
218       p_ie->codecId != A2DP_OPUS_CODEC_ID) {
219     LOG_ERROR("wrong vendor or codec id");
220     return A2DP_WRONG_CODEC;
221   }
222 
223   p_ie->channelMode = *p_codec_info & A2DP_OPUS_CHANNEL_MODE_MASK;
224   p_ie->future1 = *p_codec_info & A2DP_OPUS_FRAMESIZE_MASK;
225   p_ie->sampleRate = *p_codec_info & A2DP_OPUS_SAMPLING_FREQ_MASK;
226   p_ie->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
227 
228   if (is_capability) {
229     // NOTE: The checks here are very liberal. We should be using more
230     // pedantic checks specific to the SRC or SNK as specified in the spec.
231     if (A2DP_BitsSet(p_ie->sampleRate) == A2DP_SET_ZERO_BIT) {
232       LOG_ERROR("invalid sample rate 0x%X", p_ie->sampleRate);
233       return A2DP_BAD_SAMP_FREQ;
234     }
235     if (A2DP_BitsSet(p_ie->channelMode) == A2DP_SET_ZERO_BIT) {
236       LOG_ERROR("invalid channel mode");
237       return A2DP_BAD_CH_MODE;
238     }
239 
240     return A2DP_SUCCESS;
241   }
242 
243   if (A2DP_BitsSet(p_ie->sampleRate) != A2DP_SET_ONE_BIT) {
244     LOG_ERROR("invalid sampling frequency 0x%X", p_ie->sampleRate);
245     return A2DP_BAD_SAMP_FREQ;
246   }
247   if (A2DP_BitsSet(p_ie->channelMode) != A2DP_SET_ONE_BIT) {
248     LOG_ERROR("invalid channel mode.");
249     return A2DP_BAD_CH_MODE;
250   }
251 
252   return A2DP_SUCCESS;
253 }
254 
255 // Build the Opus Media Payload Header.
256 // |p_dst| points to the location where the header should be written to.
257 // If |frag| is true, the media payload frame is fragmented.
258 // |start| is true for the first packet of a fragmented frame.
259 // |last| is true for the last packet of a fragmented frame.
260 // If |frag| is false, |num| is the number of number of frames in the packet,
261 // otherwise is the number of remaining fragments (including this one).
A2DP_BuildMediaPayloadHeaderOpus(uint8_t * p_dst,bool frag,bool start,bool last,uint8_t num)262 static void A2DP_BuildMediaPayloadHeaderOpus(uint8_t* p_dst, bool frag,
263                                              bool start, bool last,
264                                              uint8_t num) {
265   if (p_dst == NULL) return;
266 
267   *p_dst = 0;
268   if (frag) *p_dst |= A2DP_OPUS_HDR_F_MSK;
269   if (start) *p_dst |= A2DP_OPUS_HDR_S_MSK;
270   if (last) *p_dst |= A2DP_OPUS_HDR_L_MSK;
271   *p_dst |= (A2DP_OPUS_HDR_NUM_MSK & num);
272 }
273 
A2DP_IsVendorSourceCodecValidOpus(const uint8_t * p_codec_info)274 bool A2DP_IsVendorSourceCodecValidOpus(const uint8_t* p_codec_info) {
275   tA2DP_OPUS_CIE cfg_cie;
276 
277   /* Use a liberal check when parsing the codec info */
278   return (A2DP_ParseInfoOpus(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
279          (A2DP_ParseInfoOpus(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
280 }
281 
A2DP_IsVendorSinkCodecValidOpus(const uint8_t * p_codec_info)282 bool A2DP_IsVendorSinkCodecValidOpus(const uint8_t* p_codec_info) {
283   tA2DP_OPUS_CIE cfg_cie;
284 
285   /* Use a liberal check when parsing the codec info */
286   return (A2DP_ParseInfoOpus(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
287          (A2DP_ParseInfoOpus(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
288 }
289 
A2DP_IsVendorPeerSourceCodecValidOpus(const uint8_t * p_codec_info)290 bool A2DP_IsVendorPeerSourceCodecValidOpus(const uint8_t* p_codec_info) {
291   tA2DP_OPUS_CIE cfg_cie;
292 
293   /* Use a liberal check when parsing the codec info */
294   return (A2DP_ParseInfoOpus(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
295          (A2DP_ParseInfoOpus(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
296 }
297 
A2DP_IsVendorPeerSinkCodecValidOpus(const uint8_t * p_codec_info)298 bool A2DP_IsVendorPeerSinkCodecValidOpus(const uint8_t* p_codec_info) {
299   tA2DP_OPUS_CIE cfg_cie;
300 
301   /* Use a liberal check when parsing the codec info */
302   return (A2DP_ParseInfoOpus(&cfg_cie, p_codec_info, false) == A2DP_SUCCESS) ||
303          (A2DP_ParseInfoOpus(&cfg_cie, p_codec_info, true) == A2DP_SUCCESS);
304 }
305 
A2DP_IsVendorSinkCodecSupportedOpus(const uint8_t * p_codec_info)306 bool A2DP_IsVendorSinkCodecSupportedOpus(const uint8_t* p_codec_info) {
307   return A2DP_CodecInfoMatchesCapabilityOpus(&a2dp_opus_sink_caps, p_codec_info,
308                                              false) == A2DP_SUCCESS;
309 }
A2DP_IsPeerSourceCodecSupportedOpus(const uint8_t * p_codec_info)310 bool A2DP_IsPeerSourceCodecSupportedOpus(const uint8_t* p_codec_info) {
311   return A2DP_CodecInfoMatchesCapabilityOpus(&a2dp_opus_sink_caps, p_codec_info,
312                                              true) == A2DP_SUCCESS;
313 }
314 
315 // Checks whether A2DP Opus codec configuration matches with a device's codec
316 // capabilities. |p_cap| is the Opus codec configuration. |p_codec_info| is
317 // the device's codec capabilities.
318 // If |is_capability| is true, the byte sequence is codec capabilities,
319 // otherwise is codec configuration.
320 // |p_codec_info| contains the codec capabilities for a peer device that
321 // is acting as an A2DP source.
322 // Returns A2DP_SUCCESS if the codec configuration matches with capabilities,
323 // otherwise the corresponding A2DP error status code.
A2DP_CodecInfoMatchesCapabilityOpus(const tA2DP_OPUS_CIE * p_cap,const uint8_t * p_codec_info,bool is_capability)324 static tA2DP_STATUS A2DP_CodecInfoMatchesCapabilityOpus(
325     const tA2DP_OPUS_CIE* p_cap, const uint8_t* p_codec_info,
326     bool is_capability) {
327   tA2DP_STATUS status;
328   tA2DP_OPUS_CIE cfg_cie;
329 
330   /* parse configuration */
331   status = A2DP_ParseInfoOpus(&cfg_cie, p_codec_info, is_capability);
332   if (status != A2DP_SUCCESS) {
333     LOG_ERROR("parsing failed %d", status);
334     return status;
335   }
336 
337   /* verify that each parameter is in range */
338 
339   LOG_VERBOSE("SAMPLING FREQ peer: 0x%x, capability 0x%x", cfg_cie.sampleRate,
340               p_cap->sampleRate);
341   LOG_VERBOSE("CH_MODE peer: 0x%x, capability 0x%x", cfg_cie.channelMode,
342               p_cap->channelMode);
343   LOG_VERBOSE("FRAMESIZE peer: 0x%x, capability 0x%x", cfg_cie.future1,
344               p_cap->future1);
345 
346   /* sampling frequency */
347   if ((cfg_cie.sampleRate & p_cap->sampleRate) == 0) return A2DP_NS_SAMP_FREQ;
348 
349   /* channel mode */
350   if ((cfg_cie.channelMode & p_cap->channelMode) == 0) return A2DP_NS_CH_MODE;
351 
352   /* frameSize */
353   if ((cfg_cie.future1 & p_cap->future1) == 0) return A2DP_NS_FRAMESIZE;
354 
355   return A2DP_SUCCESS;
356 }
357 
A2DP_VendorUsesRtpHeaderOpus(UNUSED_ATTR bool content_protection_enabled,UNUSED_ATTR const uint8_t * p_codec_info)358 bool A2DP_VendorUsesRtpHeaderOpus(UNUSED_ATTR bool content_protection_enabled,
359                                   UNUSED_ATTR const uint8_t* p_codec_info) {
360   return true;
361 }
362 
A2DP_VendorCodecNameOpus(UNUSED_ATTR const uint8_t * p_codec_info)363 const char* A2DP_VendorCodecNameOpus(UNUSED_ATTR const uint8_t* p_codec_info) {
364   return "Opus";
365 }
366 
A2DP_VendorCodecTypeEqualsOpus(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)367 bool A2DP_VendorCodecTypeEqualsOpus(const uint8_t* p_codec_info_a,
368                                     const uint8_t* p_codec_info_b) {
369   tA2DP_OPUS_CIE Opus_cie_a;
370   tA2DP_OPUS_CIE Opus_cie_b;
371 
372   // Check whether the codec info contains valid data
373   tA2DP_STATUS a2dp_status =
374       A2DP_ParseInfoOpus(&Opus_cie_a, p_codec_info_a, true);
375   if (a2dp_status != A2DP_SUCCESS) {
376     LOG_ERROR("cannot decode codec information: %d", a2dp_status);
377     return false;
378   }
379   a2dp_status = A2DP_ParseInfoOpus(&Opus_cie_b, p_codec_info_b, true);
380   if (a2dp_status != A2DP_SUCCESS) {
381     LOG_ERROR("cannot decode codec information: %d", a2dp_status);
382     return false;
383   }
384 
385   return true;
386 }
387 
A2DP_VendorCodecEqualsOpus(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)388 bool A2DP_VendorCodecEqualsOpus(const uint8_t* p_codec_info_a,
389                                 const uint8_t* p_codec_info_b) {
390   tA2DP_OPUS_CIE Opus_cie_a;
391   tA2DP_OPUS_CIE Opus_cie_b;
392 
393   // Check whether the codec info contains valid data
394   tA2DP_STATUS a2dp_status =
395       A2DP_ParseInfoOpus(&Opus_cie_a, p_codec_info_a, true);
396   if (a2dp_status != A2DP_SUCCESS) {
397     LOG_ERROR("cannot decode codec information: %d", a2dp_status);
398     return false;
399   }
400   a2dp_status = A2DP_ParseInfoOpus(&Opus_cie_b, p_codec_info_b, true);
401   if (a2dp_status != A2DP_SUCCESS) {
402     LOG_ERROR("cannot decode codec information: %d", a2dp_status);
403     return false;
404   }
405 
406   return (Opus_cie_a.sampleRate == Opus_cie_b.sampleRate) &&
407          (Opus_cie_a.channelMode == Opus_cie_b.channelMode) &&
408          (Opus_cie_a.future1 == Opus_cie_b.future1);
409 }
410 
A2DP_VendorGetBitRateOpus(const uint8_t * p_codec_info)411 int A2DP_VendorGetBitRateOpus(const uint8_t* p_codec_info) {
412   int channel_count = A2DP_VendorGetTrackChannelCountOpus(p_codec_info);
413   int framesize = A2DP_VendorGetFrameSizeOpus(p_codec_info);
414   int samplerate = A2DP_VendorGetTrackSampleRateOpus(p_codec_info);
415 
416   // in milliseconds
417   switch ((framesize * 1000) / samplerate) {
418     case 20:
419       if (channel_count == 2) {
420         return 256000;
421       } else if (channel_count == 1) {
422         return 128000;
423       } else
424         return -1;
425     default:
426       return -1;
427   }
428 }
429 
A2DP_VendorGetTrackSampleRateOpus(const uint8_t * p_codec_info)430 int A2DP_VendorGetTrackSampleRateOpus(const uint8_t* p_codec_info) {
431   tA2DP_OPUS_CIE Opus_cie;
432 
433   // Check whether the codec info contains valid data
434   tA2DP_STATUS a2dp_status = A2DP_ParseInfoOpus(&Opus_cie, p_codec_info, false);
435   if (a2dp_status != A2DP_SUCCESS) {
436     LOG_ERROR("cannot decode codec information: %d", a2dp_status);
437     return -1;
438   }
439 
440   switch (Opus_cie.sampleRate) {
441     case A2DP_OPUS_SAMPLING_FREQ_48000:
442       return 48000;
443   }
444 
445   return -1;
446 }
447 
A2DP_VendorGetTrackBitsPerSampleOpus(const uint8_t * p_codec_info)448 int A2DP_VendorGetTrackBitsPerSampleOpus(const uint8_t* p_codec_info) {
449   tA2DP_OPUS_CIE Opus_cie;
450 
451   // Check whether the codec info contains valid data
452   tA2DP_STATUS a2dp_status = A2DP_ParseInfoOpus(&Opus_cie, p_codec_info, false);
453   if (a2dp_status != A2DP_SUCCESS) {
454     LOG_ERROR("cannot decode codec information: %d", a2dp_status);
455     return -1;
456   }
457 
458   switch (Opus_cie.bits_per_sample) {
459     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
460       return 16;
461     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
462       return 24;
463     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
464       return 32;
465     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
466     default:
467       LOG_ERROR("Invalid bit depth setting");
468       return -1;
469   }
470 }
471 
A2DP_VendorGetTrackChannelCountOpus(const uint8_t * p_codec_info)472 int A2DP_VendorGetTrackChannelCountOpus(const uint8_t* p_codec_info) {
473   tA2DP_OPUS_CIE Opus_cie;
474 
475   // Check whether the codec info contains valid data
476   tA2DP_STATUS a2dp_status = A2DP_ParseInfoOpus(&Opus_cie, p_codec_info, false);
477   if (a2dp_status != A2DP_SUCCESS) {
478     LOG_ERROR("cannot decode codec information: %d", a2dp_status);
479     return -1;
480   }
481 
482   switch (Opus_cie.channelMode) {
483     case A2DP_OPUS_CHANNEL_MODE_MONO:
484       return 1;
485     case A2DP_OPUS_CHANNEL_MODE_STEREO:
486     case A2DP_OPUS_CHANNEL_MODE_DUAL_MONO:
487       return 2;
488     default:
489       LOG_ERROR("Invalid channel setting");
490   }
491 
492   return -1;
493 }
494 
A2DP_VendorGetSinkTrackChannelTypeOpus(const uint8_t * p_codec_info)495 int A2DP_VendorGetSinkTrackChannelTypeOpus(const uint8_t* p_codec_info) {
496   tA2DP_OPUS_CIE Opus_cie;
497 
498   // Check whether the codec info contains valid data
499   tA2DP_STATUS a2dp_status = A2DP_ParseInfoOpus(&Opus_cie, p_codec_info, false);
500   if (a2dp_status != A2DP_SUCCESS) {
501     LOG_ERROR("cannot decode codec information: %d", a2dp_status);
502     return -1;
503   }
504 
505   switch (Opus_cie.channelMode) {
506     case A2DP_OPUS_CHANNEL_MODE_MONO:
507       return 1;
508     case A2DP_OPUS_CHANNEL_MODE_STEREO:
509       return 2;
510   }
511 
512   return -1;
513 }
514 
A2DP_VendorGetChannelModeCodeOpus(const uint8_t * p_codec_info)515 int A2DP_VendorGetChannelModeCodeOpus(const uint8_t* p_codec_info) {
516   tA2DP_OPUS_CIE Opus_cie;
517 
518   // Check whether the codec info contains valid data
519   tA2DP_STATUS a2dp_status = A2DP_ParseInfoOpus(&Opus_cie, p_codec_info, false);
520   if (a2dp_status != A2DP_SUCCESS) {
521     LOG_ERROR("cannot decode codec information: %d", a2dp_status);
522     return -1;
523   }
524 
525   switch (Opus_cie.channelMode) {
526     case A2DP_OPUS_CHANNEL_MODE_MONO:
527     case A2DP_OPUS_CHANNEL_MODE_STEREO:
528       return Opus_cie.channelMode;
529     default:
530       break;
531   }
532 
533   return -1;
534 }
535 
A2DP_VendorGetFrameSizeOpus(const uint8_t * p_codec_info)536 int A2DP_VendorGetFrameSizeOpus(const uint8_t* p_codec_info) {
537   tA2DP_OPUS_CIE Opus_cie;
538 
539   // Check whether the codec info contains valid data
540   tA2DP_STATUS a2dp_status = A2DP_ParseInfoOpus(&Opus_cie, p_codec_info, false);
541   if (a2dp_status != A2DP_SUCCESS) {
542     LOG_ERROR("cannot decode codec information: %d", a2dp_status);
543     return -1;
544   }
545   int samplerate = A2DP_VendorGetTrackSampleRateOpus(p_codec_info);
546 
547   switch (Opus_cie.future1) {
548     case A2DP_OPUS_20MS_FRAMESIZE:
549       if (samplerate == 48000) {
550         return 960;
551       }
552   }
553 
554   return -1;
555 }
556 
A2DP_VendorGetPacketTimestampOpus(UNUSED_ATTR const uint8_t * p_codec_info,const uint8_t * p_data,uint32_t * p_timestamp)557 bool A2DP_VendorGetPacketTimestampOpus(UNUSED_ATTR const uint8_t* p_codec_info,
558                                        const uint8_t* p_data,
559                                        uint32_t* p_timestamp) {
560   *p_timestamp = *(const uint32_t*)p_data;
561   return true;
562 }
563 
A2DP_VendorBuildCodecHeaderOpus(UNUSED_ATTR const uint8_t * p_codec_info,BT_HDR * p_buf,uint16_t frames_per_packet)564 bool A2DP_VendorBuildCodecHeaderOpus(UNUSED_ATTR const uint8_t* p_codec_info,
565                                      BT_HDR* p_buf,
566                                      uint16_t frames_per_packet) {
567   uint8_t* p;
568 
569   p_buf->offset -= A2DP_OPUS_MPL_HDR_LEN;
570   p = (uint8_t*)(p_buf + 1) + p_buf->offset;
571   p_buf->len += A2DP_OPUS_MPL_HDR_LEN;
572 
573   A2DP_BuildMediaPayloadHeaderOpus(p, false, false, false,
574                                    (uint8_t)frames_per_packet);
575 
576   return true;
577 }
578 
A2DP_VendorCodecInfoStringOpus(const uint8_t * p_codec_info)579 std::string A2DP_VendorCodecInfoStringOpus(const uint8_t* p_codec_info) {
580   std::stringstream res;
581   std::string field;
582   tA2DP_STATUS a2dp_status;
583   tA2DP_OPUS_CIE Opus_cie;
584 
585   a2dp_status = A2DP_ParseInfoOpus(&Opus_cie, p_codec_info, true);
586   if (a2dp_status != A2DP_SUCCESS) {
587     res << "A2DP_ParseInfoOpus fail: " << loghex(a2dp_status);
588     return res.str();
589   }
590 
591   res << "\tname: Opus\n";
592 
593   // Sample frequency
594   field.clear();
595   AppendField(&field, (Opus_cie.sampleRate == 0), "NONE");
596   AppendField(&field, (Opus_cie.sampleRate & A2DP_OPUS_SAMPLING_FREQ_48000),
597               "48000");
598   res << "\tsamp_freq: " << field << " (" << loghex(Opus_cie.sampleRate)
599       << ")\n";
600 
601   // Channel mode
602   field.clear();
603   AppendField(&field, (Opus_cie.channelMode == 0), "NONE");
604   AppendField(&field, (Opus_cie.channelMode & A2DP_OPUS_CHANNEL_MODE_MONO),
605               "Mono");
606   AppendField(&field, (Opus_cie.channelMode & A2DP_OPUS_CHANNEL_MODE_STEREO),
607               "Stereo");
608   res << "\tch_mode: " << field << " (" << loghex(Opus_cie.channelMode)
609       << ")\n";
610 
611   // Framesize
612   field.clear();
613   AppendField(&field, (Opus_cie.future1 == 0), "NONE");
614   AppendField(&field, (Opus_cie.future1 & A2DP_OPUS_20MS_FRAMESIZE), "20ms");
615   AppendField(&field, (Opus_cie.future1 & A2DP_OPUS_10MS_FRAMESIZE), "10ms");
616   res << "\tframesize: " << field << " (" << loghex(Opus_cie.future1) << ")\n";
617 
618   return res.str();
619 }
620 
A2DP_VendorGetEncoderInterfaceOpus(const uint8_t * p_codec_info)621 const tA2DP_ENCODER_INTERFACE* A2DP_VendorGetEncoderInterfaceOpus(
622     const uint8_t* p_codec_info) {
623   if (!A2DP_IsVendorSourceCodecValidOpus(p_codec_info)) return NULL;
624 
625   return &a2dp_encoder_interface_opus;
626 }
627 
A2DP_VendorGetDecoderInterfaceOpus(const uint8_t * p_codec_info)628 const tA2DP_DECODER_INTERFACE* A2DP_VendorGetDecoderInterfaceOpus(
629     const uint8_t* p_codec_info) {
630   if (!A2DP_IsVendorSinkCodecValidOpus(p_codec_info)) return NULL;
631 
632   return &a2dp_decoder_interface_opus;
633 }
634 
A2DP_VendorAdjustCodecOpus(uint8_t * p_codec_info)635 bool A2DP_VendorAdjustCodecOpus(uint8_t* p_codec_info) {
636   tA2DP_OPUS_CIE cfg_cie;
637 
638   // Nothing to do: just verify the codec info is valid
639   if (A2DP_ParseInfoOpus(&cfg_cie, p_codec_info, true) != A2DP_SUCCESS)
640     return false;
641 
642   return true;
643 }
644 
A2DP_VendorSourceCodecIndexOpus(UNUSED_ATTR const uint8_t * p_codec_info)645 btav_a2dp_codec_index_t A2DP_VendorSourceCodecIndexOpus(
646     UNUSED_ATTR const uint8_t* p_codec_info) {
647   return BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS;
648 }
649 
A2DP_VendorSinkCodecIndexOpus(UNUSED_ATTR const uint8_t * p_codec_info)650 btav_a2dp_codec_index_t A2DP_VendorSinkCodecIndexOpus(
651     UNUSED_ATTR const uint8_t* p_codec_info) {
652   return BTAV_A2DP_CODEC_INDEX_SINK_OPUS;
653 }
654 
A2DP_VendorCodecIndexStrOpus(void)655 const char* A2DP_VendorCodecIndexStrOpus(void) { return "Opus"; }
656 
A2DP_VendorCodecIndexStrOpusSink(void)657 const char* A2DP_VendorCodecIndexStrOpusSink(void) { return "Opus SINK"; }
658 
A2DP_VendorInitCodecConfigOpus(AvdtpSepConfig * p_cfg)659 bool A2DP_VendorInitCodecConfigOpus(AvdtpSepConfig* p_cfg) {
660   if (A2DP_BuildInfoOpus(AVDT_MEDIA_TYPE_AUDIO, &a2dp_opus_source_caps,
661                          p_cfg->codec_info) != A2DP_SUCCESS) {
662     return false;
663   }
664 
665 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
666   /* Content protection info - support SCMS-T */
667   uint8_t* p = p_cfg->protect_info;
668   *p++ = AVDT_CP_LOSC;
669   UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID);
670   p_cfg->num_protect = 1;
671 #endif
672 
673   return true;
674 }
675 
A2DP_VendorInitCodecConfigOpusSink(AvdtpSepConfig * p_cfg)676 bool A2DP_VendorInitCodecConfigOpusSink(AvdtpSepConfig* p_cfg) {
677   return A2DP_BuildInfoOpus(AVDT_MEDIA_TYPE_AUDIO, &a2dp_opus_sink_caps,
678                             p_cfg->codec_info) == A2DP_SUCCESS;
679 }
680 
build_codec_config(const tA2DP_OPUS_CIE & config_cie,btav_a2dp_codec_config_t * result)681 UNUSED_ATTR static void build_codec_config(const tA2DP_OPUS_CIE& config_cie,
682                                            btav_a2dp_codec_config_t* result) {
683   if (config_cie.sampleRate & A2DP_OPUS_SAMPLING_FREQ_48000)
684     result->sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
685 
686   result->bits_per_sample = config_cie.bits_per_sample;
687 
688   if (config_cie.channelMode & A2DP_OPUS_CHANNEL_MODE_MONO)
689     result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
690   if (config_cie.channelMode & A2DP_OPUS_CHANNEL_MODE_STEREO) {
691     result->channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
692   }
693 
694   if (config_cie.future1 & A2DP_OPUS_20MS_FRAMESIZE)
695     result->codec_specific_1 |= BTAV_A2DP_CODEC_FRAME_SIZE_20MS;
696   if (config_cie.future1 & A2DP_OPUS_10MS_FRAMESIZE)
697     result->codec_specific_1 |= BTAV_A2DP_CODEC_FRAME_SIZE_10MS;
698 }
699 
A2dpCodecConfigOpusSource(btav_a2dp_codec_priority_t codec_priority)700 A2dpCodecConfigOpusSource::A2dpCodecConfigOpusSource(
701     btav_a2dp_codec_priority_t codec_priority)
702     : A2dpCodecConfigOpusBase(BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS,
703                               A2DP_VendorCodecIndexStrOpus(), codec_priority,
704                               true) {
705   // Compute the local capability
706   if (a2dp_opus_source_caps.sampleRate & A2DP_OPUS_SAMPLING_FREQ_48000) {
707     codec_local_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
708   }
709   codec_local_capability_.bits_per_sample =
710       a2dp_opus_source_caps.bits_per_sample;
711   if (a2dp_opus_source_caps.channelMode & A2DP_OPUS_CHANNEL_MODE_MONO) {
712     codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
713   }
714   if (a2dp_opus_source_caps.channelMode & A2DP_OPUS_CHANNEL_MODE_STEREO) {
715     codec_local_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
716   }
717 }
718 
~A2dpCodecConfigOpusSource()719 A2dpCodecConfigOpusSource::~A2dpCodecConfigOpusSource() {}
720 
init()721 bool A2dpCodecConfigOpusSource::init() {
722   if (!isValid()) return false;
723 
724   return true;
725 }
726 
useRtpHeaderMarkerBit() const727 bool A2dpCodecConfigOpusSource::useRtpHeaderMarkerBit() const { return false; }
728 
729 //
730 // Selects the best sample rate from |sampleRate|.
731 // The result is stored in |p_result| and |p_codec_config|.
732 // Returns true if a selection was made, otherwise false.
733 //
select_best_sample_rate(uint8_t sampleRate,tA2DP_OPUS_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)734 static bool select_best_sample_rate(uint8_t sampleRate,
735                                     tA2DP_OPUS_CIE* p_result,
736                                     btav_a2dp_codec_config_t* p_codec_config) {
737   if (sampleRate & A2DP_OPUS_SAMPLING_FREQ_48000) {
738     p_result->sampleRate = A2DP_OPUS_SAMPLING_FREQ_48000;
739     p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
740     return true;
741   }
742   return false;
743 }
744 
745 //
746 // Selects the audio sample rate from |p_codec_audio_config|.
747 // |sampleRate| contains the capability.
748 // The result is stored in |p_result| and |p_codec_config|.
749 // Returns true if a selection was made, otherwise false.
750 //
select_audio_sample_rate(const btav_a2dp_codec_config_t * p_codec_audio_config,uint8_t sampleRate,tA2DP_OPUS_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)751 static bool select_audio_sample_rate(
752     const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t sampleRate,
753     tA2DP_OPUS_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
754   switch (p_codec_audio_config->sample_rate) {
755     case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
756       if (sampleRate & A2DP_OPUS_SAMPLING_FREQ_48000) {
757         p_result->sampleRate = A2DP_OPUS_SAMPLING_FREQ_48000;
758         p_codec_config->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
759         return true;
760       }
761       break;
762     case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
763     case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
764     case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
765     case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
766     case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
767     case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
768     case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
769     case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
770       break;
771   }
772 
773   return false;
774 }
775 
776 //
777 // Selects the best bits per sample from |bits_per_sample|.
778 // |bits_per_sample| contains the capability.
779 // The result is stored in |p_result| and |p_codec_config|.
780 // Returns true if a selection was made, otherwise false.
781 //
select_best_bits_per_sample(btav_a2dp_codec_bits_per_sample_t bits_per_sample,tA2DP_OPUS_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)782 static bool select_best_bits_per_sample(
783     btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_OPUS_CIE* p_result,
784     btav_a2dp_codec_config_t* p_codec_config) {
785   if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
786     p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
787     p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
788     return true;
789   }
790   if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
791     p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
792     p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
793     return true;
794   }
795   if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
796     p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
797     p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
798     return true;
799   }
800   return false;
801 }
802 
803 //
804 // Selects the audio bits per sample from |p_codec_audio_config|.
805 // |bits_per_sample| contains the capability.
806 // The result is stored in |p_result| and |p_codec_config|.
807 // Returns true if a selection was made, otherwise false.
808 //
select_audio_bits_per_sample(const btav_a2dp_codec_config_t * p_codec_audio_config,btav_a2dp_codec_bits_per_sample_t bits_per_sample,tA2DP_OPUS_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)809 static bool select_audio_bits_per_sample(
810     const btav_a2dp_codec_config_t* p_codec_audio_config,
811     btav_a2dp_codec_bits_per_sample_t bits_per_sample, tA2DP_OPUS_CIE* p_result,
812     btav_a2dp_codec_config_t* p_codec_config) {
813   switch (p_codec_audio_config->bits_per_sample) {
814     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
815       if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
816         p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
817         p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
818         return true;
819       }
820       break;
821     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
822       if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
823         p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
824         p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
825         return true;
826       }
827       break;
828     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
829       if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
830         p_codec_config->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
831         p_result->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
832         return true;
833       }
834       break;
835     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
836       break;
837   }
838   return false;
839 }
840 
841 //
842 // Selects the best channel mode from |channelMode|.
843 // The result is stored in |p_result| and |p_codec_config|.
844 // Returns true if a selection was made, otherwise false.
845 //
select_best_channel_mode(uint8_t channelMode,tA2DP_OPUS_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)846 static bool select_best_channel_mode(uint8_t channelMode,
847                                      tA2DP_OPUS_CIE* p_result,
848                                      btav_a2dp_codec_config_t* p_codec_config) {
849   if (channelMode & A2DP_OPUS_CHANNEL_MODE_STEREO) {
850     p_result->channelMode = A2DP_OPUS_CHANNEL_MODE_STEREO;
851     p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
852     return true;
853   }
854   if (channelMode & A2DP_OPUS_CHANNEL_MODE_MONO) {
855     p_result->channelMode = A2DP_OPUS_CHANNEL_MODE_MONO;
856     p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
857     return true;
858   }
859   return false;
860 }
861 
862 //
863 // Selects the audio channel mode from |p_codec_audio_config|.
864 // |channelMode| contains the capability.
865 // The result is stored in |p_result| and |p_codec_config|.
866 // Returns true if a selection was made, otherwise false.
867 //
select_audio_channel_mode(const btav_a2dp_codec_config_t * p_codec_audio_config,uint8_t channelMode,tA2DP_OPUS_CIE * p_result,btav_a2dp_codec_config_t * p_codec_config)868 static bool select_audio_channel_mode(
869     const btav_a2dp_codec_config_t* p_codec_audio_config, uint8_t channelMode,
870     tA2DP_OPUS_CIE* p_result, btav_a2dp_codec_config_t* p_codec_config) {
871   switch (p_codec_audio_config->channel_mode) {
872     case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
873       if (channelMode & A2DP_OPUS_CHANNEL_MODE_MONO) {
874         p_result->channelMode = A2DP_OPUS_CHANNEL_MODE_MONO;
875         p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
876         return true;
877       }
878       break;
879     case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
880       if (channelMode & A2DP_OPUS_CHANNEL_MODE_STEREO) {
881         p_result->channelMode = A2DP_OPUS_CHANNEL_MODE_STEREO;
882         p_codec_config->channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
883         return true;
884       }
885       break;
886     case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
887       break;
888   }
889 
890   return false;
891 }
892 
setCodecConfig(const uint8_t * p_peer_codec_info,bool is_capability,uint8_t * p_result_codec_config)893 bool A2dpCodecConfigOpusBase::setCodecConfig(const uint8_t* p_peer_codec_info,
894                                              bool is_capability,
895                                              uint8_t* p_result_codec_config) {
896   std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
897   tA2DP_OPUS_CIE peer_info_cie;
898   tA2DP_OPUS_CIE result_config_cie;
899   uint8_t channelMode;
900   uint8_t sampleRate;
901   uint8_t frameSize;
902   btav_a2dp_codec_bits_per_sample_t bits_per_sample;
903   const tA2DP_OPUS_CIE* p_a2dp_opus_caps =
904       (is_source_) ? &a2dp_opus_source_caps : &a2dp_opus_sink_caps;
905 
906   btav_a2dp_codec_config_t device_codec_config_ = getCodecConfig();
907 
908   LOG_INFO(
909       "AudioManager stream config %d sample rate %d bit depth %d channel "
910       "mode",
911       device_codec_config_.sample_rate, device_codec_config_.bits_per_sample,
912       device_codec_config_.channel_mode);
913 
914   // Save the internal state
915   btav_a2dp_codec_config_t saved_codec_config = codec_config_;
916   btav_a2dp_codec_config_t saved_codec_capability = codec_capability_;
917   btav_a2dp_codec_config_t saved_codec_selectable_capability =
918       codec_selectable_capability_;
919   btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_;
920   btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_;
921   uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE];
922   uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
923   uint8_t saved_ota_codec_peer_config[AVDT_CODEC_SIZE];
924   memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_));
925   memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
926          sizeof(ota_codec_peer_capability_));
927   memcpy(saved_ota_codec_peer_config, ota_codec_peer_config_,
928          sizeof(ota_codec_peer_config_));
929 
930   tA2DP_STATUS status =
931       A2DP_ParseInfoOpus(&peer_info_cie, p_peer_codec_info, is_capability);
932   if (status != A2DP_SUCCESS) {
933     LOG_ERROR("can't parse peer's capabilities: error = %d", status);
934     goto fail;
935   }
936 
937   //
938   // Build the preferred configuration
939   //
940   memset(&result_config_cie, 0, sizeof(result_config_cie));
941   result_config_cie.vendorId = p_a2dp_opus_caps->vendorId;
942   result_config_cie.codecId = p_a2dp_opus_caps->codecId;
943 
944   //
945   // Select the sample frequency
946   //
947   sampleRate = p_a2dp_opus_caps->sampleRate & peer_info_cie.sampleRate;
948   codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
949 
950   switch (codec_user_config_.sample_rate) {
951     case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
952       if (sampleRate & A2DP_OPUS_SAMPLING_FREQ_48000) {
953         result_config_cie.sampleRate = A2DP_OPUS_SAMPLING_FREQ_48000;
954         codec_capability_.sample_rate = codec_user_config_.sample_rate;
955         codec_config_.sample_rate = codec_user_config_.sample_rate;
956       }
957       break;
958     case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
959     case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
960     case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
961     case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
962     case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
963     case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
964     case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
965     case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
966       codec_capability_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
967       codec_config_.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
968       break;
969   }
970 
971   // Select the sample frequency if there is no user preference
972   do {
973     // Compute the selectable capability
974     if (sampleRate & A2DP_OPUS_SAMPLING_FREQ_48000) {
975       codec_selectable_capability_.sample_rate |=
976           BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
977     }
978 
979     if (codec_config_.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) break;
980 
981     // Compute the common capability
982     if (sampleRate & A2DP_OPUS_SAMPLING_FREQ_48000)
983       codec_capability_.sample_rate |= BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
984 
985     // No user preference - try the codec audio config
986     if (select_audio_sample_rate(&codec_audio_config_, sampleRate,
987                                  &result_config_cie, &codec_config_)) {
988       break;
989     }
990 
991     // No user preference - try the default config
992     if (select_best_sample_rate(
993             a2dp_opus_default_config.sampleRate & peer_info_cie.sampleRate,
994             &result_config_cie, &codec_config_)) {
995       break;
996     }
997 
998     // No user preference - use the best match
999     if (select_best_sample_rate(sampleRate, &result_config_cie,
1000                                 &codec_config_)) {
1001       break;
1002     }
1003   } while (false);
1004   if (codec_config_.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) {
1005     LOG_ERROR(
1006         "cannot match sample frequency: local caps = 0x%x "
1007         "peer info = 0x%x",
1008         p_a2dp_opus_caps->sampleRate, peer_info_cie.sampleRate);
1009     goto fail;
1010   }
1011 
1012   //
1013   // Select the bits per sample
1014   //
1015   // NOTE: this information is NOT included in the Opus A2DP codec description
1016   // that is sent OTA.
1017   bits_per_sample = p_a2dp_opus_caps->bits_per_sample;
1018   codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1019   switch (codec_user_config_.bits_per_sample) {
1020     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
1021       if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
1022         result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1023         codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1024         codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1025       }
1026       break;
1027     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
1028       if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
1029         result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1030         codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1031         codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1032       }
1033       break;
1034     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
1035       if (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
1036         result_config_cie.bits_per_sample = codec_user_config_.bits_per_sample;
1037         codec_capability_.bits_per_sample = codec_user_config_.bits_per_sample;
1038         codec_config_.bits_per_sample = codec_user_config_.bits_per_sample;
1039       }
1040       break;
1041     case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
1042       result_config_cie.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1043       codec_capability_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1044       codec_config_.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
1045       break;
1046   }
1047 
1048   // Select the bits per sample if there is no user preference
1049   do {
1050     // Compute the selectable capability
1051     codec_selectable_capability_.bits_per_sample =
1052         p_a2dp_opus_caps->bits_per_sample;
1053 
1054     if (codec_config_.bits_per_sample != BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE)
1055       break;
1056 
1057     // Compute the common capability
1058     codec_capability_.bits_per_sample = bits_per_sample;
1059 
1060     // No user preference - try yhe codec audio config
1061     if (select_audio_bits_per_sample(&codec_audio_config_,
1062                                      p_a2dp_opus_caps->bits_per_sample,
1063                                      &result_config_cie, &codec_config_)) {
1064       break;
1065     }
1066 
1067     // No user preference - try the default config
1068     if (select_best_bits_per_sample(a2dp_opus_default_config.bits_per_sample,
1069                                     &result_config_cie, &codec_config_)) {
1070       break;
1071     }
1072 
1073     // No user preference - use the best match
1074     if (select_best_bits_per_sample(p_a2dp_opus_caps->bits_per_sample,
1075                                     &result_config_cie, &codec_config_)) {
1076       break;
1077     }
1078   } while (false);
1079   if (codec_config_.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) {
1080     LOG_ERROR(
1081         "cannot match bits per sample: default = 0x%x "
1082         "user preference = 0x%x",
1083         a2dp_opus_default_config.bits_per_sample,
1084         codec_user_config_.bits_per_sample);
1085     goto fail;
1086   }
1087 
1088   //
1089   // Select the channel mode
1090   //
1091   channelMode = p_a2dp_opus_caps->channelMode & peer_info_cie.channelMode;
1092   codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1093   switch (codec_user_config_.channel_mode) {
1094     case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
1095       if (channelMode & A2DP_OPUS_CHANNEL_MODE_MONO) {
1096         result_config_cie.channelMode = A2DP_OPUS_CHANNEL_MODE_MONO;
1097         codec_capability_.channel_mode = codec_user_config_.channel_mode;
1098         codec_config_.channel_mode = codec_user_config_.channel_mode;
1099       }
1100       break;
1101     case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
1102       if (channelMode & A2DP_OPUS_CHANNEL_MODE_STEREO) {
1103         result_config_cie.channelMode = A2DP_OPUS_CHANNEL_MODE_STEREO;
1104         codec_capability_.channel_mode = codec_user_config_.channel_mode;
1105         codec_config_.channel_mode = codec_user_config_.channel_mode;
1106       }
1107       break;
1108     case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1109       codec_capability_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1110       codec_config_.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
1111       break;
1112   }
1113 
1114   // Select the channel mode if there is no user preference
1115   do {
1116     // Compute the selectable capability
1117     if (channelMode & A2DP_OPUS_CHANNEL_MODE_MONO) {
1118       codec_selectable_capability_.channel_mode |=
1119           BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1120     }
1121     if (channelMode & A2DP_OPUS_CHANNEL_MODE_STEREO) {
1122       codec_selectable_capability_.channel_mode |=
1123           BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1124     }
1125 
1126     if (codec_config_.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) break;
1127 
1128     // Compute the common capability
1129     if (channelMode & A2DP_OPUS_CHANNEL_MODE_MONO)
1130       codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1131     if (channelMode & A2DP_OPUS_CHANNEL_MODE_STEREO) {
1132       codec_capability_.channel_mode |= BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1133     }
1134 
1135     // No user preference - try the codec audio config
1136     if (select_audio_channel_mode(&codec_audio_config_, channelMode,
1137                                   &result_config_cie, &codec_config_)) {
1138       break;
1139     }
1140 
1141     // No user preference - try the default config
1142     if (select_best_channel_mode(
1143             a2dp_opus_default_config.channelMode & peer_info_cie.channelMode,
1144             &result_config_cie, &codec_config_)) {
1145       break;
1146     }
1147 
1148     // No user preference - use the best match
1149     if (select_best_channel_mode(channelMode, &result_config_cie,
1150                                  &codec_config_)) {
1151       break;
1152     }
1153   } while (false);
1154   if (codec_config_.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) {
1155     LOG_ERROR(
1156         "cannot match channel mode: local caps = 0x%x "
1157         "peer info = 0x%x",
1158         p_a2dp_opus_caps->channelMode, peer_info_cie.channelMode);
1159     goto fail;
1160   }
1161 
1162   //
1163   // Select the frame size
1164   //
1165   frameSize = p_a2dp_opus_caps->future1 & peer_info_cie.future1;
1166   codec_config_.codec_specific_1 = BTAV_A2DP_CODEC_FRAME_SIZE_NONE;
1167   switch (codec_user_config_.codec_specific_1) {
1168     case BTAV_A2DP_CODEC_FRAME_SIZE_20MS:
1169       if (frameSize & A2DP_OPUS_20MS_FRAMESIZE) {
1170         result_config_cie.future1 = A2DP_OPUS_20MS_FRAMESIZE;
1171         codec_capability_.codec_specific_1 =
1172             codec_user_config_.codec_specific_1;
1173         codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
1174       }
1175       break;
1176     case BTAV_A2DP_CODEC_FRAME_SIZE_10MS:
1177       if (frameSize & A2DP_OPUS_10MS_FRAMESIZE) {
1178         result_config_cie.future1 = A2DP_OPUS_10MS_FRAMESIZE;
1179         codec_capability_.codec_specific_1 =
1180             codec_user_config_.codec_specific_1;
1181         codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
1182       }
1183       break;
1184     case BTAV_A2DP_CODEC_FRAME_SIZE_NONE:
1185       codec_capability_.codec_specific_1 = BTAV_A2DP_CODEC_FRAME_SIZE_NONE;
1186       codec_config_.codec_specific_1 = BTAV_A2DP_CODEC_FRAME_SIZE_NONE;
1187       break;
1188   }
1189 
1190   // No user preference - set default value
1191   codec_config_.codec_specific_1 = BTAV_A2DP_CODEC_FRAME_SIZE_20MS;
1192   result_config_cie.future1 = A2DP_OPUS_20MS_FRAMESIZE;
1193   result_config_cie.future3 = 0x00;
1194 
1195   if (codec_config_.codec_specific_1 == BTAV_A2DP_CODEC_FRAME_SIZE_NONE) {
1196     LOG_ERROR(
1197         "cannot match frame size: local caps = 0x%x "
1198         "peer info = 0x%x",
1199         p_a2dp_opus_caps->future1, peer_info_cie.future1);
1200     goto fail;
1201   }
1202 
1203   if (A2DP_BuildInfoOpus(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1204                          p_result_codec_config) != A2DP_SUCCESS) {
1205     LOG_ERROR("failed to BuildInfoOpus for result_config_cie");
1206     goto fail;
1207   }
1208 
1209   //
1210   // Copy the codec-specific fields if they are not zero
1211   //
1212   if (codec_user_config_.codec_specific_1 != 0)
1213     codec_config_.codec_specific_1 = codec_user_config_.codec_specific_1;
1214   if (codec_user_config_.codec_specific_2 != 0)
1215     codec_config_.codec_specific_2 = codec_user_config_.codec_specific_2;
1216   if (codec_user_config_.codec_specific_3 != 0)
1217     codec_config_.codec_specific_3 = codec_user_config_.codec_specific_3;
1218   if (codec_user_config_.codec_specific_4 != 0)
1219     codec_config_.codec_specific_4 = codec_user_config_.codec_specific_4;
1220 
1221   // Create a local copy of the peer codec capability, and the
1222   // result codec config.
1223   if (is_capability) {
1224     status = A2DP_BuildInfoOpus(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1225                                 ota_codec_peer_capability_);
1226   } else {
1227     status = A2DP_BuildInfoOpus(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1228                                 ota_codec_peer_config_);
1229   }
1230   CHECK(status == A2DP_SUCCESS);
1231 
1232   status = A2DP_BuildInfoOpus(AVDT_MEDIA_TYPE_AUDIO, &result_config_cie,
1233                               ota_codec_config_);
1234   CHECK(status == A2DP_SUCCESS);
1235   return true;
1236 
1237 fail:
1238   // Restore the internal state
1239   codec_config_ = saved_codec_config;
1240   codec_capability_ = saved_codec_capability;
1241   codec_selectable_capability_ = saved_codec_selectable_capability;
1242   codec_user_config_ = saved_codec_user_config;
1243   codec_audio_config_ = saved_codec_audio_config;
1244   memcpy(ota_codec_config_, saved_ota_codec_config, sizeof(ota_codec_config_));
1245   memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1246          sizeof(ota_codec_peer_capability_));
1247   memcpy(ota_codec_peer_config_, saved_ota_codec_peer_config,
1248          sizeof(ota_codec_peer_config_));
1249   return false;
1250 }
1251 
setPeerCodecCapabilities(const uint8_t * p_peer_codec_capabilities)1252 bool A2dpCodecConfigOpusBase::setPeerCodecCapabilities(
1253     const uint8_t* p_peer_codec_capabilities) {
1254   std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
1255   tA2DP_OPUS_CIE peer_info_cie;
1256   uint8_t channelMode;
1257   uint8_t sampleRate;
1258   const tA2DP_OPUS_CIE* p_a2dp_opus_caps =
1259       (is_source_) ? &a2dp_opus_source_caps : &a2dp_opus_sink_caps;
1260 
1261   // Save the internal state
1262   btav_a2dp_codec_config_t saved_codec_selectable_capability =
1263       codec_selectable_capability_;
1264   uint8_t saved_ota_codec_peer_capability[AVDT_CODEC_SIZE];
1265   memcpy(saved_ota_codec_peer_capability, ota_codec_peer_capability_,
1266          sizeof(ota_codec_peer_capability_));
1267 
1268   tA2DP_STATUS status =
1269       A2DP_ParseInfoOpus(&peer_info_cie, p_peer_codec_capabilities, true);
1270   if (status != A2DP_SUCCESS) {
1271     LOG_ERROR("can't parse peer's capabilities: error = %d", status);
1272     goto fail;
1273   }
1274 
1275   // Compute the selectable capability - sample rate
1276   sampleRate = p_a2dp_opus_caps->sampleRate & peer_info_cie.sampleRate;
1277   if (sampleRate & A2DP_OPUS_SAMPLING_FREQ_48000) {
1278     codec_selectable_capability_.sample_rate |=
1279         BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
1280   }
1281 
1282   // Compute the selectable capability - bits per sample
1283   codec_selectable_capability_.bits_per_sample =
1284       p_a2dp_opus_caps->bits_per_sample;
1285 
1286   // Compute the selectable capability - channel mode
1287   channelMode = p_a2dp_opus_caps->channelMode & peer_info_cie.channelMode;
1288   if (channelMode & A2DP_OPUS_CHANNEL_MODE_MONO) {
1289     codec_selectable_capability_.channel_mode |=
1290         BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
1291   }
1292   if (channelMode & A2DP_OPUS_CHANNEL_MODE_STEREO) {
1293     codec_selectable_capability_.channel_mode |=
1294         BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
1295   }
1296 
1297   LOG_INFO("BuildInfoOpus for peer info cie for ota caps");
1298   status = A2DP_BuildInfoOpus(AVDT_MEDIA_TYPE_AUDIO, &peer_info_cie,
1299                               ota_codec_peer_capability_);
1300   CHECK(status == A2DP_SUCCESS);
1301   return true;
1302 
1303 fail:
1304   // Restore the internal state
1305   codec_selectable_capability_ = saved_codec_selectable_capability;
1306   memcpy(ota_codec_peer_capability_, saved_ota_codec_peer_capability,
1307          sizeof(ota_codec_peer_capability_));
1308   return false;
1309 }
1310 
A2dpCodecConfigOpusSink(btav_a2dp_codec_priority_t codec_priority)1311 A2dpCodecConfigOpusSink::A2dpCodecConfigOpusSink(
1312     btav_a2dp_codec_priority_t codec_priority)
1313     : A2dpCodecConfigOpusBase(BTAV_A2DP_CODEC_INDEX_SINK_OPUS,
1314                               A2DP_VendorCodecIndexStrOpusSink(),
1315                               codec_priority, false) {}
1316 
~A2dpCodecConfigOpusSink()1317 A2dpCodecConfigOpusSink::~A2dpCodecConfigOpusSink() {}
1318 
init()1319 bool A2dpCodecConfigOpusSink::init() {
1320   if (!isValid()) return false;
1321 
1322   return true;
1323 }
1324 
useRtpHeaderMarkerBit() const1325 bool A2dpCodecConfigOpusSink::useRtpHeaderMarkerBit() const { return false; }
1326 
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)1327 bool A2dpCodecConfigOpusSink::updateEncoderUserConfig(
1328     UNUSED_ATTR const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
1329     UNUSED_ATTR bool* p_restart_input, UNUSED_ATTR bool* p_restart_output,
1330     UNUSED_ATTR bool* p_config_updated) {
1331   return false;
1332 }
1333