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