1 /*
2 * Copyright 2016 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 * A2DP Codecs Configuration
19 */
20
21 #define LOG_TAG "a2dp_codec"
22
23 #include "a2dp_codec_api.h"
24
25 #include <base/logging.h>
26 #include <inttypes.h>
27
28 #include "a2dp_aac.h"
29 #include "a2dp_sbc.h"
30 #include "a2dp_vendor.h"
31 #include "a2dp_vendor_aptx.h"
32 #include "a2dp_vendor_aptx_hd.h"
33 #include "a2dp_vendor_ldac.h"
34 #include "bta/av/bta_av_int.h"
35 #include "osi/include/log.h"
36 #include "osi/include/properties.h"
37
38 /* The Media Type offset within the codec info byte array */
39 #define A2DP_MEDIA_TYPE_OFFSET 1
40
41 /* A2DP Offload enabled in stack */
42 static bool a2dp_offload_status;
43
44 // Initializes the codec config.
45 // |codec_config| is the codec config to initialize.
46 // |codec_index| and |codec_priority| are the codec type and priority to use
47 // for the initialization.
48
init_btav_a2dp_codec_config(btav_a2dp_codec_config_t * codec_config,btav_a2dp_codec_index_t codec_index,btav_a2dp_codec_priority_t codec_priority)49 static void init_btav_a2dp_codec_config(
50 btav_a2dp_codec_config_t* codec_config, btav_a2dp_codec_index_t codec_index,
51 btav_a2dp_codec_priority_t codec_priority) {
52 memset(codec_config, 0, sizeof(btav_a2dp_codec_config_t));
53 codec_config->codec_type = codec_index;
54 codec_config->codec_priority = codec_priority;
55 }
56
A2dpCodecConfig(btav_a2dp_codec_index_t codec_index,const std::string & name,btav_a2dp_codec_priority_t codec_priority)57 A2dpCodecConfig::A2dpCodecConfig(btav_a2dp_codec_index_t codec_index,
58 const std::string& name,
59 btav_a2dp_codec_priority_t codec_priority)
60 : codec_index_(codec_index),
61 name_(name),
62 default_codec_priority_(codec_priority) {
63 setCodecPriority(codec_priority);
64
65 init_btav_a2dp_codec_config(&codec_config_, codec_index_, codecPriority());
66 init_btav_a2dp_codec_config(&codec_capability_, codec_index_,
67 codecPriority());
68 init_btav_a2dp_codec_config(&codec_local_capability_, codec_index_,
69 codecPriority());
70 init_btav_a2dp_codec_config(&codec_selectable_capability_, codec_index_,
71 codecPriority());
72 init_btav_a2dp_codec_config(&codec_user_config_, codec_index_,
73 BTAV_A2DP_CODEC_PRIORITY_DEFAULT);
74 init_btav_a2dp_codec_config(&codec_audio_config_, codec_index_,
75 BTAV_A2DP_CODEC_PRIORITY_DEFAULT);
76
77 memset(ota_codec_config_, 0, sizeof(ota_codec_config_));
78 memset(ota_codec_peer_capability_, 0, sizeof(ota_codec_peer_capability_));
79 memset(ota_codec_peer_config_, 0, sizeof(ota_codec_peer_config_));
80 }
81
~A2dpCodecConfig()82 A2dpCodecConfig::~A2dpCodecConfig() {}
83
setCodecPriority(btav_a2dp_codec_priority_t codec_priority)84 void A2dpCodecConfig::setCodecPriority(
85 btav_a2dp_codec_priority_t codec_priority) {
86 if (codec_priority == BTAV_A2DP_CODEC_PRIORITY_DEFAULT) {
87 // Compute the default codec priority
88 setDefaultCodecPriority();
89 } else {
90 codec_priority_ = codec_priority;
91 }
92 codec_config_.codec_priority = codec_priority_;
93 }
94
setDefaultCodecPriority()95 void A2dpCodecConfig::setDefaultCodecPriority() {
96 if (default_codec_priority_ != BTAV_A2DP_CODEC_PRIORITY_DEFAULT) {
97 codec_priority_ = default_codec_priority_;
98 } else {
99 // Compute the default codec priority
100 uint32_t priority = 1000 * (codec_index_ + 1) + 1;
101 codec_priority_ = static_cast<btav_a2dp_codec_priority_t>(priority);
102 }
103 codec_config_.codec_priority = codec_priority_;
104 }
105
createCodec(btav_a2dp_codec_index_t codec_index,btav_a2dp_codec_priority_t codec_priority)106 A2dpCodecConfig* A2dpCodecConfig::createCodec(
107 btav_a2dp_codec_index_t codec_index,
108 btav_a2dp_codec_priority_t codec_priority) {
109 LOG_DEBUG("%s: codec %s", __func__, A2DP_CodecIndexStr(codec_index));
110
111 A2dpCodecConfig* codec_config = nullptr;
112 switch (codec_index) {
113 case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
114 codec_config = new A2dpCodecConfigSbcSource(codec_priority);
115 break;
116 case BTAV_A2DP_CODEC_INDEX_SINK_SBC:
117 codec_config = new A2dpCodecConfigSbcSink(codec_priority);
118 break;
119 case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
120 codec_config = new A2dpCodecConfigAacSource(codec_priority);
121 break;
122 case BTAV_A2DP_CODEC_INDEX_SINK_AAC:
123 codec_config = new A2dpCodecConfigAacSink(codec_priority);
124 break;
125 case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX:
126 codec_config = new A2dpCodecConfigAptx(codec_priority);
127 break;
128 case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD:
129 codec_config = new A2dpCodecConfigAptxHd(codec_priority);
130 break;
131 case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC:
132 codec_config = new A2dpCodecConfigLdacSource(codec_priority);
133 break;
134 case BTAV_A2DP_CODEC_INDEX_SINK_LDAC:
135 codec_config = new A2dpCodecConfigLdacSink(codec_priority);
136 break;
137 case BTAV_A2DP_CODEC_INDEX_MAX:
138 break;
139 }
140
141 if (codec_config != nullptr) {
142 if (!codec_config->init()) {
143 delete codec_config;
144 codec_config = nullptr;
145 }
146 }
147
148 return codec_config;
149 }
150
getTrackBitRate() const151 int A2dpCodecConfig::getTrackBitRate() const {
152 uint8_t p_codec_info[AVDT_CODEC_SIZE];
153 memcpy(p_codec_info, ota_codec_config_, sizeof(ota_codec_config_));
154 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
155
156 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
157
158 switch (codec_type) {
159 case A2DP_MEDIA_CT_SBC:
160 return A2DP_GetBitrateSbc();
161 case A2DP_MEDIA_CT_AAC:
162 return A2DP_GetBitRateAac(p_codec_info);
163 case A2DP_MEDIA_CT_NON_A2DP:
164 return A2DP_VendorGetBitRate(p_codec_info);
165 default:
166 break;
167 }
168
169 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
170 return -1;
171 }
172
getCodecSpecificConfig(tBT_A2DP_OFFLOAD * p_a2dp_offload)173 bool A2dpCodecConfig::getCodecSpecificConfig(tBT_A2DP_OFFLOAD* p_a2dp_offload) {
174 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
175
176 uint8_t codec_config[AVDT_CODEC_SIZE];
177 uint32_t vendor_id;
178 uint16_t codec_id;
179
180 memset(p_a2dp_offload->codec_info, 0, sizeof(p_a2dp_offload->codec_info));
181
182 if (!A2DP_IsSourceCodecValid(ota_codec_config_)) {
183 return false;
184 }
185
186 memcpy(codec_config, ota_codec_config_, sizeof(ota_codec_config_));
187 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(codec_config);
188 switch (codec_type) {
189 case A2DP_MEDIA_CT_SBC:
190 p_a2dp_offload->codec_info[0] =
191 codec_config[4]; // blk_len | subbands | Alloc Method
192 p_a2dp_offload->codec_info[1] = codec_config[5]; // Min bit pool
193 p_a2dp_offload->codec_info[2] = codec_config[6]; // Max bit pool
194 p_a2dp_offload->codec_info[3] =
195 codec_config[3]; // Sample freq | channel mode
196 break;
197 case A2DP_MEDIA_CT_AAC:
198 p_a2dp_offload->codec_info[0] = codec_config[3]; // object type
199 p_a2dp_offload->codec_info[1] = codec_config[6]; // VBR | BR
200 break;
201 case A2DP_MEDIA_CT_NON_A2DP:
202 vendor_id = A2DP_VendorCodecGetVendorId(codec_config);
203 codec_id = A2DP_VendorCodecGetCodecId(codec_config);
204 p_a2dp_offload->codec_info[0] = (vendor_id & 0x000000FF);
205 p_a2dp_offload->codec_info[1] = (vendor_id & 0x0000FF00) >> 8;
206 p_a2dp_offload->codec_info[2] = (vendor_id & 0x00FF0000) >> 16;
207 p_a2dp_offload->codec_info[3] = (vendor_id & 0xFF000000) >> 24;
208 p_a2dp_offload->codec_info[4] = (codec_id & 0x000000FF);
209 p_a2dp_offload->codec_info[5] = (codec_id & 0x0000FF00) >> 8;
210 if (vendor_id == A2DP_LDAC_VENDOR_ID && codec_id == A2DP_LDAC_CODEC_ID) {
211 if (codec_config_.codec_specific_1 == 0) { // default is 0, ABR
212 p_a2dp_offload->codec_info[6] =
213 A2DP_LDAC_QUALITY_ABR_OFFLOAD; // ABR in offload
214 } else {
215 switch (codec_config_.codec_specific_1 % 10) {
216 case 0:
217 p_a2dp_offload->codec_info[6] =
218 A2DP_LDAC_QUALITY_HIGH; // High bitrate
219 break;
220 case 1:
221 p_a2dp_offload->codec_info[6] =
222 A2DP_LDAC_QUALITY_MID; // Mid birate
223 break;
224 case 2:
225 p_a2dp_offload->codec_info[6] =
226 A2DP_LDAC_QUALITY_LOW; // Low birate
227 break;
228 case 3:
229 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
230 default:
231 p_a2dp_offload->codec_info[6] =
232 A2DP_LDAC_QUALITY_ABR_OFFLOAD; // ABR in offload
233 break;
234 }
235 }
236 p_a2dp_offload->codec_info[7] =
237 codec_config[10]; // LDAC specific channel mode
238 LOG_VERBOSE("%s: Ldac specific channelmode =%d", __func__,
239 p_a2dp_offload->codec_info[7]);
240 }
241 break;
242 default:
243 break;
244 }
245 return true;
246 }
247
isValid() const248 bool A2dpCodecConfig::isValid() const { return true; }
249
copyOutOtaCodecConfig(uint8_t * p_codec_info)250 bool A2dpCodecConfig::copyOutOtaCodecConfig(uint8_t* p_codec_info) {
251 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
252
253 // TODO: We should use a mechanism to verify codec config,
254 // not codec capability.
255 if (!A2DP_IsSourceCodecValid(ota_codec_config_)) {
256 return false;
257 }
258 memcpy(p_codec_info, ota_codec_config_, sizeof(ota_codec_config_));
259 return true;
260 }
261
getCodecConfig()262 btav_a2dp_codec_config_t A2dpCodecConfig::getCodecConfig() {
263 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
264
265 // TODO: We should check whether the codec config is valid
266 return codec_config_;
267 }
268
getCodecCapability()269 btav_a2dp_codec_config_t A2dpCodecConfig::getCodecCapability() {
270 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
271
272 // TODO: We should check whether the codec capability is valid
273 return codec_capability_;
274 }
275
getCodecLocalCapability()276 btav_a2dp_codec_config_t A2dpCodecConfig::getCodecLocalCapability() {
277 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
278
279 // TODO: We should check whether the codec capability is valid
280 return codec_local_capability_;
281 }
282
getCodecSelectableCapability()283 btav_a2dp_codec_config_t A2dpCodecConfig::getCodecSelectableCapability() {
284 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
285
286 // TODO: We should check whether the codec capability is valid
287 return codec_selectable_capability_;
288 }
289
getCodecUserConfig()290 btav_a2dp_codec_config_t A2dpCodecConfig::getCodecUserConfig() {
291 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
292
293 return codec_user_config_;
294 }
295
getCodecAudioConfig()296 btav_a2dp_codec_config_t A2dpCodecConfig::getCodecAudioConfig() {
297 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
298
299 return codec_audio_config_;
300 }
301
getAudioBitsPerSample()302 uint8_t A2dpCodecConfig::getAudioBitsPerSample() {
303 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
304
305 switch (codec_config_.bits_per_sample) {
306 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
307 return 16;
308 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
309 return 24;
310 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
311 return 32;
312 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
313 break;
314 }
315 return 0;
316 }
317
isCodecConfigEmpty(const btav_a2dp_codec_config_t & codec_config)318 bool A2dpCodecConfig::isCodecConfigEmpty(
319 const btav_a2dp_codec_config_t& codec_config) {
320 return (
321 (codec_config.codec_priority == BTAV_A2DP_CODEC_PRIORITY_DEFAULT) &&
322 (codec_config.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) &&
323 (codec_config.bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) &&
324 (codec_config.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE) &&
325 (codec_config.codec_specific_1 == 0) &&
326 (codec_config.codec_specific_2 == 0) &&
327 (codec_config.codec_specific_3 == 0) &&
328 (codec_config.codec_specific_4 == 0));
329 }
330
setCodecUserConfig(const btav_a2dp_codec_config_t & codec_user_config,const btav_a2dp_codec_config_t & codec_audio_config,const tA2DP_ENCODER_INIT_PEER_PARAMS * p_peer_params,const uint8_t * p_peer_codec_info,bool is_capability,uint8_t * p_result_codec_config,bool * p_restart_input,bool * p_restart_output,bool * p_config_updated)331 bool A2dpCodecConfig::setCodecUserConfig(
332 const btav_a2dp_codec_config_t& codec_user_config,
333 const btav_a2dp_codec_config_t& codec_audio_config,
334 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
335 const uint8_t* p_peer_codec_info, bool is_capability,
336 uint8_t* p_result_codec_config, bool* p_restart_input,
337 bool* p_restart_output, bool* p_config_updated) {
338 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
339 *p_restart_input = false;
340 *p_restart_output = false;
341 *p_config_updated = false;
342
343 // Save copies of the current codec config, and the OTA codec config, so they
344 // can be compared for changes.
345 btav_a2dp_codec_config_t saved_codec_config = getCodecConfig();
346 uint8_t saved_ota_codec_config[AVDT_CODEC_SIZE];
347 memcpy(saved_ota_codec_config, ota_codec_config_, sizeof(ota_codec_config_));
348
349 btav_a2dp_codec_config_t saved_codec_user_config = codec_user_config_;
350 codec_user_config_ = codec_user_config;
351 btav_a2dp_codec_config_t saved_codec_audio_config = codec_audio_config_;
352 codec_audio_config_ = codec_audio_config;
353 bool success =
354 setCodecConfig(p_peer_codec_info, is_capability, p_result_codec_config);
355 if (!success) {
356 // Restore the local copy of the user and audio config
357 codec_user_config_ = saved_codec_user_config;
358 codec_audio_config_ = saved_codec_audio_config;
359 return false;
360 }
361
362 //
363 // The input (audio data) should be restarted if the audio format has changed
364 //
365 btav_a2dp_codec_config_t new_codec_config = getCodecConfig();
366 if ((saved_codec_config.sample_rate != new_codec_config.sample_rate) ||
367 (saved_codec_config.bits_per_sample !=
368 new_codec_config.bits_per_sample) ||
369 (saved_codec_config.channel_mode != new_codec_config.channel_mode)) {
370 *p_restart_input = true;
371 }
372
373 //
374 // The output (the connection) should be restarted if OTA codec config
375 // has changed.
376 //
377 if (!A2DP_CodecEquals(saved_ota_codec_config, p_result_codec_config)) {
378 *p_restart_output = true;
379 }
380
381 bool encoder_restart_input = *p_restart_input;
382 bool encoder_restart_output = *p_restart_output;
383 bool encoder_config_updated = *p_config_updated;
384
385 if (!a2dp_offload_status) {
386 if (updateEncoderUserConfig(p_peer_params, &encoder_restart_input,
387 &encoder_restart_output,
388 &encoder_config_updated)) {
389 if (encoder_restart_input) *p_restart_input = true;
390 if (encoder_restart_output) *p_restart_output = true;
391 if (encoder_config_updated) *p_config_updated = true;
392 }
393 }
394 if (*p_restart_input || *p_restart_output) *p_config_updated = true;
395
396 return true;
397 }
398
codecConfigIsValid(const btav_a2dp_codec_config_t & codec_config)399 bool A2dpCodecConfig::codecConfigIsValid(
400 const btav_a2dp_codec_config_t& codec_config) {
401 return (codec_config.codec_type < BTAV_A2DP_CODEC_INDEX_MAX) &&
402 (codec_config.sample_rate != BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) &&
403 (codec_config.bits_per_sample !=
404 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) &&
405 (codec_config.channel_mode != BTAV_A2DP_CODEC_CHANNEL_MODE_NONE);
406 }
407
codecConfig2Str(const btav_a2dp_codec_config_t & codec_config)408 std::string A2dpCodecConfig::codecConfig2Str(
409 const btav_a2dp_codec_config_t& codec_config) {
410 std::string result;
411
412 if (!codecConfigIsValid(codec_config)) return "Invalid";
413
414 result.append("Rate=");
415 result.append(codecSampleRate2Str(codec_config.sample_rate));
416 result.append(" Bits=");
417 result.append(codecBitsPerSample2Str(codec_config.bits_per_sample));
418 result.append(" Mode=");
419 result.append(codecChannelMode2Str(codec_config.channel_mode));
420
421 return result;
422 }
423
codecSampleRate2Str(btav_a2dp_codec_sample_rate_t codec_sample_rate)424 std::string A2dpCodecConfig::codecSampleRate2Str(
425 btav_a2dp_codec_sample_rate_t codec_sample_rate) {
426 std::string result;
427
428 if (codec_sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_44100) {
429 if (!result.empty()) result += "|";
430 result += "44100";
431 }
432 if (codec_sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_48000) {
433 if (!result.empty()) result += "|";
434 result += "48000";
435 }
436 if (codec_sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_88200) {
437 if (!result.empty()) result += "|";
438 result += "88200";
439 }
440 if (codec_sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_96000) {
441 if (!result.empty()) result += "|";
442 result += "96000";
443 }
444 if (codec_sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_176400) {
445 if (!result.empty()) result += "|";
446 result += "176400";
447 }
448 if (codec_sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_192000) {
449 if (!result.empty()) result += "|";
450 result += "192000";
451 }
452 if (result.empty()) {
453 std::stringstream ss;
454 ss << "UnknownSampleRate(0x" << std::hex << codec_sample_rate << ")";
455 ss >> result;
456 }
457
458 return result;
459 }
460
codecBitsPerSample2Str(btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample)461 std::string A2dpCodecConfig::codecBitsPerSample2Str(
462 btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample) {
463 std::string result;
464
465 if (codec_bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
466 if (!result.empty()) result += "|";
467 result += "16";
468 }
469 if (codec_bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
470 if (!result.empty()) result += "|";
471 result += "24";
472 }
473 if (codec_bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
474 if (!result.empty()) result += "|";
475 result += "32";
476 }
477 if (result.empty()) {
478 std::stringstream ss;
479 ss << "UnknownBitsPerSample(0x" << std::hex << codec_bits_per_sample << ")";
480 ss >> result;
481 }
482
483 return result;
484 }
485
codecChannelMode2Str(btav_a2dp_codec_channel_mode_t codec_channel_mode)486 std::string A2dpCodecConfig::codecChannelMode2Str(
487 btav_a2dp_codec_channel_mode_t codec_channel_mode) {
488 std::string result;
489
490 if (codec_channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_MONO) {
491 if (!result.empty()) result += "|";
492 result += "MONO";
493 }
494 if (codec_channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO) {
495 if (!result.empty()) result += "|";
496 result += "STEREO";
497 }
498 if (result.empty()) {
499 std::stringstream ss;
500 ss << "UnknownChannelMode(0x" << std::hex << codec_channel_mode << ")";
501 ss >> result;
502 }
503
504 return result;
505 }
506
debug_codec_dump(int fd)507 void A2dpCodecConfig::debug_codec_dump(int fd) {
508 std::string result;
509 dprintf(fd, "\nA2DP %s State:\n", name().c_str());
510 dprintf(fd, " Priority: %d\n", codecPriority());
511 dprintf(fd, " Encoder interval (ms): %" PRIu64 "\n", encoderIntervalMs());
512 dprintf(fd, " Effective MTU: %d\n", getEffectiveMtu());
513
514 result = codecConfig2Str(getCodecConfig());
515 dprintf(fd, " Config: %s\n", result.c_str());
516
517 result = codecConfig2Str(getCodecSelectableCapability());
518 dprintf(fd, " Selectable: %s\n", result.c_str());
519
520 result = codecConfig2Str(getCodecLocalCapability());
521 dprintf(fd, " Local capability: %s\n", result.c_str());
522 }
523
524 //
525 // Compares two codecs |lhs| and |rhs| based on their priority.
526 // Returns true if |lhs| has higher priority (larger priority value).
527 // If |lhs| and |rhs| have same priority, the unique codec index is used
528 // as a tie-breaker: larger codec index value means higher priority.
529 //
compare_codec_priority(const A2dpCodecConfig * lhs,const A2dpCodecConfig * rhs)530 static bool compare_codec_priority(const A2dpCodecConfig* lhs,
531 const A2dpCodecConfig* rhs) {
532 if (lhs->codecPriority() > rhs->codecPriority()) return true;
533 if (lhs->codecPriority() < rhs->codecPriority()) return false;
534 return (lhs->codecIndex() > rhs->codecIndex());
535 }
536
A2dpCodecs(const std::vector<btav_a2dp_codec_config_t> & codec_priorities)537 A2dpCodecs::A2dpCodecs(
538 const std::vector<btav_a2dp_codec_config_t>& codec_priorities)
539 : current_codec_config_(nullptr) {
540 for (auto config : codec_priorities) {
541 codec_priorities_.insert(
542 std::make_pair(config.codec_type, config.codec_priority));
543 }
544 }
545
~A2dpCodecs()546 A2dpCodecs::~A2dpCodecs() {
547 std::unique_lock<std::recursive_mutex> lock(codec_mutex_);
548 for (const auto& iter : indexed_codecs_) {
549 delete iter.second;
550 }
551 for (const auto& iter : disabled_codecs_) {
552 delete iter.second;
553 }
554 lock.unlock();
555 }
556
init()557 bool A2dpCodecs::init() {
558 LOG_DEBUG("%s", __func__);
559 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
560 char* tok = NULL;
561 char* tmp_token = NULL;
562 bool offload_codec_support[BTAV_A2DP_CODEC_INDEX_MAX] = {false};
563 char value_sup[PROPERTY_VALUE_MAX], value_dis[PROPERTY_VALUE_MAX];
564
565 osi_property_get("ro.bluetooth.a2dp_offload.supported", value_sup, "false");
566 osi_property_get("persist.bluetooth.a2dp_offload.disabled", value_dis,
567 "false");
568 a2dp_offload_status =
569 (strcmp(value_sup, "true") == 0) && (strcmp(value_dis, "false") == 0);
570
571 if (a2dp_offload_status) {
572 char value_cap[PROPERTY_VALUE_MAX];
573 osi_property_get("persist.bluetooth.a2dp_offload.cap", value_cap, "");
574 tok = strtok_r((char*)value_cap, "-", &tmp_token);
575 while (tok != NULL) {
576 if (strcmp(tok, "sbc") == 0) {
577 LOG_INFO("%s: SBC offload supported", __func__);
578 offload_codec_support[BTAV_A2DP_CODEC_INDEX_SOURCE_SBC] = true;
579 } else if (strcmp(tok, "aac") == 0) {
580 LOG_INFO("%s: AAC offload supported", __func__);
581 offload_codec_support[BTAV_A2DP_CODEC_INDEX_SOURCE_AAC] = true;
582 } else if (strcmp(tok, "aptx") == 0) {
583 LOG_INFO("%s: APTX offload supported", __func__);
584 offload_codec_support[BTAV_A2DP_CODEC_INDEX_SOURCE_APTX] = true;
585 } else if (strcmp(tok, "aptxhd") == 0) {
586 LOG_INFO("%s: APTXHD offload supported", __func__);
587 offload_codec_support[BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD] = true;
588 } else if (strcmp(tok, "ldac") == 0) {
589 LOG_INFO("%s: LDAC offload supported", __func__);
590 offload_codec_support[BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC] = true;
591 }
592 tok = strtok_r(NULL, "-", &tmp_token);
593 };
594 }
595
596 for (int i = BTAV_A2DP_CODEC_INDEX_MIN; i < BTAV_A2DP_CODEC_INDEX_MAX; i++) {
597 btav_a2dp_codec_index_t codec_index =
598 static_cast<btav_a2dp_codec_index_t>(i);
599
600 // Select the codec priority if explicitly configured
601 btav_a2dp_codec_priority_t codec_priority =
602 BTAV_A2DP_CODEC_PRIORITY_DEFAULT;
603 auto cp_iter = codec_priorities_.find(codec_index);
604 if (cp_iter != codec_priorities_.end()) {
605 codec_priority = cp_iter->second;
606 }
607
608 // In offload mode, disable the codecs based on the property
609 if ((codec_index < BTAV_A2DP_CODEC_INDEX_SOURCE_MAX) &&
610 a2dp_offload_status && (offload_codec_support[i] != true)) {
611 codec_priority = BTAV_A2DP_CODEC_PRIORITY_DISABLED;
612 }
613
614 A2dpCodecConfig* codec_config =
615 A2dpCodecConfig::createCodec(codec_index, codec_priority);
616 if (codec_config == nullptr) continue;
617
618 if (codec_priority != BTAV_A2DP_CODEC_PRIORITY_DEFAULT) {
619 LOG_INFO("%s: updated %s codec priority to %d", __func__,
620 codec_config->name().c_str(), codec_priority);
621 }
622
623 // Test if the codec is disabled
624 if (codec_config->codecPriority() == BTAV_A2DP_CODEC_PRIORITY_DISABLED) {
625 disabled_codecs_.insert(std::make_pair(codec_index, codec_config));
626 continue;
627 }
628
629 indexed_codecs_.insert(std::make_pair(codec_index, codec_config));
630
631 if (codec_index < BTAV_A2DP_CODEC_INDEX_SOURCE_MAX) {
632 ordered_source_codecs_.push_back(codec_config);
633 ordered_source_codecs_.sort(compare_codec_priority);
634 } else {
635 ordered_sink_codecs_.push_back(codec_config);
636 ordered_sink_codecs_.sort(compare_codec_priority);
637 }
638 }
639
640 if (ordered_source_codecs_.empty()) {
641 LOG_ERROR("%s: no Source codecs were initialized", __func__);
642 } else {
643 for (auto iter : ordered_source_codecs_) {
644 LOG_INFO("%s: initialized Source codec %s", __func__,
645 iter->name().c_str());
646 }
647 }
648 if (ordered_sink_codecs_.empty()) {
649 LOG_ERROR("%s: no Sink codecs were initialized", __func__);
650 } else {
651 for (auto iter : ordered_sink_codecs_) {
652 LOG_INFO("%s: initialized Sink codec %s", __func__, iter->name().c_str());
653 }
654 }
655
656 return (!ordered_source_codecs_.empty() && !ordered_sink_codecs_.empty());
657 }
658
findSourceCodecConfig(const uint8_t * p_codec_info)659 A2dpCodecConfig* A2dpCodecs::findSourceCodecConfig(
660 const uint8_t* p_codec_info) {
661 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
662 btav_a2dp_codec_index_t codec_index = A2DP_SourceCodecIndex(p_codec_info);
663 if (codec_index == BTAV_A2DP_CODEC_INDEX_MAX) return nullptr;
664
665 auto iter = indexed_codecs_.find(codec_index);
666 if (iter == indexed_codecs_.end()) return nullptr;
667 return iter->second;
668 }
669
findSinkCodecConfig(const uint8_t * p_codec_info)670 A2dpCodecConfig* A2dpCodecs::findSinkCodecConfig(const uint8_t* p_codec_info) {
671 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
672 btav_a2dp_codec_index_t codec_index = A2DP_SinkCodecIndex(p_codec_info);
673 if (codec_index == BTAV_A2DP_CODEC_INDEX_MAX) return nullptr;
674
675 auto iter = indexed_codecs_.find(codec_index);
676 if (iter == indexed_codecs_.end()) return nullptr;
677 return iter->second;
678 }
679
isSupportedCodec(btav_a2dp_codec_index_t codec_index)680 bool A2dpCodecs::isSupportedCodec(btav_a2dp_codec_index_t codec_index) {
681 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
682 return indexed_codecs_.find(codec_index) != indexed_codecs_.end();
683 }
684
setCodecConfig(const uint8_t * p_peer_codec_info,bool is_capability,uint8_t * p_result_codec_config,bool select_current_codec)685 bool A2dpCodecs::setCodecConfig(const uint8_t* p_peer_codec_info,
686 bool is_capability,
687 uint8_t* p_result_codec_config,
688 bool select_current_codec) {
689 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
690 A2dpCodecConfig* a2dp_codec_config = findSourceCodecConfig(p_peer_codec_info);
691 if (a2dp_codec_config == nullptr) return false;
692 if (!a2dp_codec_config->setCodecConfig(p_peer_codec_info, is_capability,
693 p_result_codec_config)) {
694 return false;
695 }
696 if (select_current_codec) {
697 current_codec_config_ = a2dp_codec_config;
698 }
699 return true;
700 }
701
setSinkCodecConfig(const uint8_t * p_peer_codec_info,bool is_capability,uint8_t * p_result_codec_config,bool select_current_codec)702 bool A2dpCodecs::setSinkCodecConfig(const uint8_t* p_peer_codec_info,
703 bool is_capability,
704 uint8_t* p_result_codec_config,
705 bool select_current_codec) {
706 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
707 A2dpCodecConfig* a2dp_codec_config = findSinkCodecConfig(p_peer_codec_info);
708 if (a2dp_codec_config == nullptr) return false;
709 if (!a2dp_codec_config->setCodecConfig(p_peer_codec_info, is_capability,
710 p_result_codec_config)) {
711 return false;
712 }
713 if (select_current_codec) {
714 current_codec_config_ = a2dp_codec_config;
715 }
716 return true;
717 }
718
setCodecUserConfig(const btav_a2dp_codec_config_t & codec_user_config,const tA2DP_ENCODER_INIT_PEER_PARAMS * p_peer_params,const uint8_t * p_peer_sink_capabilities,uint8_t * p_result_codec_config,bool * p_restart_input,bool * p_restart_output,bool * p_config_updated)719 bool A2dpCodecs::setCodecUserConfig(
720 const btav_a2dp_codec_config_t& codec_user_config,
721 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
722 const uint8_t* p_peer_sink_capabilities, uint8_t* p_result_codec_config,
723 bool* p_restart_input, bool* p_restart_output, bool* p_config_updated) {
724 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
725 btav_a2dp_codec_config_t codec_audio_config;
726 A2dpCodecConfig* a2dp_codec_config = nullptr;
727 A2dpCodecConfig* last_codec_config = current_codec_config_;
728 *p_restart_input = false;
729 *p_restart_output = false;
730 *p_config_updated = false;
731
732 LOG_DEBUG("%s: Configuring: %s", __func__,
733 codec_user_config.ToString().c_str());
734
735 if (codec_user_config.codec_type < BTAV_A2DP_CODEC_INDEX_MAX) {
736 auto iter = indexed_codecs_.find(codec_user_config.codec_type);
737 if (iter == indexed_codecs_.end()) goto fail;
738 a2dp_codec_config = iter->second;
739 } else {
740 // Update the default codec
741 a2dp_codec_config = current_codec_config_;
742 }
743 if (a2dp_codec_config == nullptr) goto fail;
744
745 // Reuse the existing codec audio config
746 codec_audio_config = a2dp_codec_config->getCodecAudioConfig();
747 if (!a2dp_codec_config->setCodecUserConfig(
748 codec_user_config, codec_audio_config, p_peer_params,
749 p_peer_sink_capabilities, true, p_result_codec_config,
750 p_restart_input, p_restart_output, p_config_updated)) {
751 goto fail;
752 }
753
754 // Update the codec priorities, and eventually restart the connection
755 // if a new codec needs to be selected.
756 do {
757 // Update the codec priority
758 btav_a2dp_codec_priority_t old_priority =
759 a2dp_codec_config->codecPriority();
760 btav_a2dp_codec_priority_t new_priority = codec_user_config.codec_priority;
761 a2dp_codec_config->setCodecPriority(new_priority);
762 // Get the actual (recomputed) priority
763 new_priority = a2dp_codec_config->codecPriority();
764
765 // Check if there was no previous codec
766 if (last_codec_config == nullptr) {
767 current_codec_config_ = a2dp_codec_config;
768 *p_restart_input = true;
769 *p_restart_output = true;
770 break;
771 }
772
773 // Check if the priority of the current codec was updated
774 if (a2dp_codec_config == last_codec_config) {
775 if (old_priority == new_priority) break; // No change in priority
776
777 *p_config_updated = true;
778 if (new_priority < old_priority) {
779 // The priority has become lower - restart the connection to
780 // select a new codec.
781 *p_restart_output = true;
782 }
783 break;
784 }
785
786 if (new_priority <= old_priority) {
787 // No change in priority, or the priority has become lower.
788 // This wasn't the current codec, so we shouldn't select a new codec.
789 if (*p_restart_input || *p_restart_output ||
790 (old_priority != new_priority)) {
791 *p_config_updated = true;
792 }
793 *p_restart_input = false;
794 *p_restart_output = false;
795 break;
796 }
797
798 *p_config_updated = true;
799 if (new_priority >= last_codec_config->codecPriority()) {
800 // The new priority is higher than the current codec. Restart the
801 // connection to select a new codec.
802 current_codec_config_ = a2dp_codec_config;
803 last_codec_config->setDefaultCodecPriority();
804 *p_restart_input = true;
805 *p_restart_output = true;
806 }
807 } while (false);
808 ordered_source_codecs_.sort(compare_codec_priority);
809
810 if (*p_restart_input || *p_restart_output) *p_config_updated = true;
811
812 LOG_DEBUG(
813 "%s: Configured: restart_input = %d restart_output = %d "
814 "config_updated = %d",
815 __func__, *p_restart_input, *p_restart_output, *p_config_updated);
816
817 return true;
818
819 fail:
820 current_codec_config_ = last_codec_config;
821 return false;
822 }
823
setCodecAudioConfig(const btav_a2dp_codec_config_t & codec_audio_config,const tA2DP_ENCODER_INIT_PEER_PARAMS * p_peer_params,const uint8_t * p_peer_sink_capabilities,uint8_t * p_result_codec_config,bool * p_restart_output,bool * p_config_updated)824 bool A2dpCodecs::setCodecAudioConfig(
825 const btav_a2dp_codec_config_t& codec_audio_config,
826 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
827 const uint8_t* p_peer_sink_capabilities, uint8_t* p_result_codec_config,
828 bool* p_restart_output, bool* p_config_updated) {
829 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
830 btav_a2dp_codec_config_t codec_user_config;
831 A2dpCodecConfig* a2dp_codec_config = current_codec_config_;
832 *p_restart_output = false;
833 *p_config_updated = false;
834
835 if (a2dp_codec_config == nullptr) return false;
836
837 // Reuse the existing codec user config
838 codec_user_config = a2dp_codec_config->getCodecUserConfig();
839 bool restart_input = false; // Flag ignored - input was just restarted
840 if (!a2dp_codec_config->setCodecUserConfig(
841 codec_user_config, codec_audio_config, p_peer_params,
842 p_peer_sink_capabilities, true, p_result_codec_config, &restart_input,
843 p_restart_output, p_config_updated)) {
844 return false;
845 }
846
847 return true;
848 }
849
setCodecOtaConfig(const uint8_t * p_ota_codec_config,const tA2DP_ENCODER_INIT_PEER_PARAMS * p_peer_params,uint8_t * p_result_codec_config,bool * p_restart_input,bool * p_restart_output,bool * p_config_updated)850 bool A2dpCodecs::setCodecOtaConfig(
851 const uint8_t* p_ota_codec_config,
852 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
853 uint8_t* p_result_codec_config, bool* p_restart_input,
854 bool* p_restart_output, bool* p_config_updated) {
855 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
856 btav_a2dp_codec_index_t codec_type;
857 btav_a2dp_codec_config_t codec_user_config;
858 btav_a2dp_codec_config_t codec_audio_config;
859 A2dpCodecConfig* a2dp_codec_config = nullptr;
860 A2dpCodecConfig* last_codec_config = current_codec_config_;
861 *p_restart_input = false;
862 *p_restart_output = false;
863 *p_config_updated = false;
864
865 // Check whether the current codec config is explicitly configured by
866 // user configuration. If yes, then the OTA codec configuration is ignored.
867 if (current_codec_config_ != nullptr) {
868 codec_user_config = current_codec_config_->getCodecUserConfig();
869 if (!A2dpCodecConfig::isCodecConfigEmpty(codec_user_config)) {
870 LOG_WARN(
871 "%s: ignoring peer OTA configuration for codec %s: "
872 "existing user configuration for current codec %s",
873 __func__, A2DP_CodecName(p_ota_codec_config),
874 current_codec_config_->name().c_str());
875 goto fail;
876 }
877 }
878
879 // Check whether the codec config for the same codec is explicitly configured
880 // by user configuration. If yes, then the OTA codec configuration is
881 // ignored.
882 codec_type = A2DP_SourceCodecIndex(p_ota_codec_config);
883 if (codec_type == BTAV_A2DP_CODEC_INDEX_MAX) {
884 LOG_WARN(
885 "%s: ignoring peer OTA codec configuration: "
886 "invalid codec",
887 __func__);
888 goto fail; // Invalid codec
889 } else {
890 auto iter = indexed_codecs_.find(codec_type);
891 if (iter == indexed_codecs_.end()) {
892 LOG_WARN("%s: cannot find codec configuration for peer OTA codec %s",
893 __func__, A2DP_CodecName(p_ota_codec_config));
894 goto fail;
895 }
896 a2dp_codec_config = iter->second;
897 }
898 if (a2dp_codec_config == nullptr) goto fail;
899 codec_user_config = a2dp_codec_config->getCodecUserConfig();
900 if (!A2dpCodecConfig::isCodecConfigEmpty(codec_user_config)) {
901 LOG_WARN(
902 "%s: ignoring peer OTA configuration for codec %s: "
903 "existing user configuration for same codec",
904 __func__, A2DP_CodecName(p_ota_codec_config));
905 goto fail;
906 }
907 current_codec_config_ = a2dp_codec_config;
908
909 // Reuse the existing codec user config and codec audio config
910 codec_audio_config = a2dp_codec_config->getCodecAudioConfig();
911 if (!a2dp_codec_config->setCodecUserConfig(
912 codec_user_config, codec_audio_config, p_peer_params,
913 p_ota_codec_config, false, p_result_codec_config, p_restart_input,
914 p_restart_output, p_config_updated)) {
915 LOG_WARN("%s: cannot set codec configuration for peer OTA codec %s",
916 __func__, A2DP_CodecName(p_ota_codec_config));
917 goto fail;
918 }
919 CHECK(current_codec_config_ != nullptr);
920
921 if (*p_restart_input || *p_restart_output) *p_config_updated = true;
922
923 return true;
924
925 fail:
926 current_codec_config_ = last_codec_config;
927 return false;
928 }
929
setPeerSinkCodecCapabilities(const uint8_t * p_peer_codec_capabilities)930 bool A2dpCodecs::setPeerSinkCodecCapabilities(
931 const uint8_t* p_peer_codec_capabilities) {
932 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
933
934 if (!A2DP_IsPeerSinkCodecValid(p_peer_codec_capabilities)) return false;
935 A2dpCodecConfig* a2dp_codec_config =
936 findSourceCodecConfig(p_peer_codec_capabilities);
937 if (a2dp_codec_config == nullptr) return false;
938 return a2dp_codec_config->setPeerCodecCapabilities(p_peer_codec_capabilities);
939 }
940
setPeerSourceCodecCapabilities(const uint8_t * p_peer_codec_capabilities)941 bool A2dpCodecs::setPeerSourceCodecCapabilities(
942 const uint8_t* p_peer_codec_capabilities) {
943 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
944
945 if (!A2DP_IsPeerSourceCodecValid(p_peer_codec_capabilities)) return false;
946 A2dpCodecConfig* a2dp_codec_config =
947 findSinkCodecConfig(p_peer_codec_capabilities);
948 if (a2dp_codec_config == nullptr) return false;
949 return a2dp_codec_config->setPeerCodecCapabilities(p_peer_codec_capabilities);
950 }
951
getCodecConfigAndCapabilities(btav_a2dp_codec_config_t * p_codec_config,std::vector<btav_a2dp_codec_config_t> * p_codecs_local_capabilities,std::vector<btav_a2dp_codec_config_t> * p_codecs_selectable_capabilities)952 bool A2dpCodecs::getCodecConfigAndCapabilities(
953 btav_a2dp_codec_config_t* p_codec_config,
954 std::vector<btav_a2dp_codec_config_t>* p_codecs_local_capabilities,
955 std::vector<btav_a2dp_codec_config_t>* p_codecs_selectable_capabilities) {
956 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
957
958 if (current_codec_config_ != nullptr) {
959 *p_codec_config = current_codec_config_->getCodecConfig();
960 } else {
961 btav_a2dp_codec_config_t codec_config;
962 memset(&codec_config, 0, sizeof(codec_config));
963 *p_codec_config = codec_config;
964 }
965
966 std::vector<btav_a2dp_codec_config_t> codecs_capabilities;
967 for (auto codec : orderedSourceCodecs()) {
968 codecs_capabilities.push_back(codec->getCodecLocalCapability());
969 }
970 *p_codecs_local_capabilities = codecs_capabilities;
971
972 codecs_capabilities.clear();
973 for (auto codec : orderedSourceCodecs()) {
974 btav_a2dp_codec_config_t codec_capability =
975 codec->getCodecSelectableCapability();
976 // Don't add entries that cannot be used
977 if ((codec_capability.sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE) ||
978 (codec_capability.bits_per_sample ==
979 BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE) ||
980 (codec_capability.channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE)) {
981 continue;
982 }
983 codecs_capabilities.push_back(codec_capability);
984 }
985 *p_codecs_selectable_capabilities = codecs_capabilities;
986
987 return true;
988 }
989
debug_codec_dump(int fd)990 void A2dpCodecs::debug_codec_dump(int fd) {
991 std::lock_guard<std::recursive_mutex> lock(codec_mutex_);
992 dprintf(fd, "\nA2DP Codecs State:\n");
993
994 // Print the current codec name
995 if (current_codec_config_ != nullptr) {
996 dprintf(fd, " Current Codec: %s\n", current_codec_config_->name().c_str());
997 } else {
998 dprintf(fd, " Current Codec: None\n");
999 }
1000
1001 // Print the codec-specific state
1002 for (auto codec_config : ordered_source_codecs_) {
1003 codec_config->debug_codec_dump(fd);
1004 }
1005 }
1006
A2DP_GetCodecType(const uint8_t * p_codec_info)1007 tA2DP_CODEC_TYPE A2DP_GetCodecType(const uint8_t* p_codec_info) {
1008 return (tA2DP_CODEC_TYPE)(p_codec_info[AVDT_CODEC_TYPE_INDEX]);
1009 }
1010
A2DP_IsSourceCodecValid(const uint8_t * p_codec_info)1011 bool A2DP_IsSourceCodecValid(const uint8_t* p_codec_info) {
1012 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1013
1014 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1015
1016 switch (codec_type) {
1017 case A2DP_MEDIA_CT_SBC:
1018 return A2DP_IsSourceCodecValidSbc(p_codec_info);
1019 case A2DP_MEDIA_CT_AAC:
1020 return A2DP_IsSourceCodecValidAac(p_codec_info);
1021 case A2DP_MEDIA_CT_NON_A2DP:
1022 return A2DP_IsVendorSourceCodecValid(p_codec_info);
1023 default:
1024 break;
1025 }
1026
1027 return false;
1028 }
1029
A2DP_IsSinkCodecValid(const uint8_t * p_codec_info)1030 bool A2DP_IsSinkCodecValid(const uint8_t* p_codec_info) {
1031 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1032
1033 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1034
1035 switch (codec_type) {
1036 case A2DP_MEDIA_CT_SBC:
1037 return A2DP_IsSinkCodecValidSbc(p_codec_info);
1038 case A2DP_MEDIA_CT_AAC:
1039 return A2DP_IsSinkCodecValidAac(p_codec_info);
1040 case A2DP_MEDIA_CT_NON_A2DP:
1041 return A2DP_IsVendorSinkCodecValid(p_codec_info);
1042 default:
1043 break;
1044 }
1045
1046 return false;
1047 }
1048
A2DP_IsPeerSourceCodecValid(const uint8_t * p_codec_info)1049 bool A2DP_IsPeerSourceCodecValid(const uint8_t* p_codec_info) {
1050 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1051
1052 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1053
1054 switch (codec_type) {
1055 case A2DP_MEDIA_CT_SBC:
1056 return A2DP_IsPeerSourceCodecValidSbc(p_codec_info);
1057 case A2DP_MEDIA_CT_AAC:
1058 return A2DP_IsPeerSourceCodecValidAac(p_codec_info);
1059 case A2DP_MEDIA_CT_NON_A2DP:
1060 return A2DP_IsVendorPeerSourceCodecValid(p_codec_info);
1061 default:
1062 break;
1063 }
1064
1065 return false;
1066 }
1067
A2DP_IsPeerSinkCodecValid(const uint8_t * p_codec_info)1068 bool A2DP_IsPeerSinkCodecValid(const uint8_t* p_codec_info) {
1069 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1070
1071 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1072
1073 switch (codec_type) {
1074 case A2DP_MEDIA_CT_SBC:
1075 return A2DP_IsPeerSinkCodecValidSbc(p_codec_info);
1076 case A2DP_MEDIA_CT_AAC:
1077 return A2DP_IsPeerSinkCodecValidAac(p_codec_info);
1078 case A2DP_MEDIA_CT_NON_A2DP:
1079 return A2DP_IsVendorPeerSinkCodecValid(p_codec_info);
1080 default:
1081 break;
1082 }
1083
1084 return false;
1085 }
1086
A2DP_IsSinkCodecSupported(const uint8_t * p_codec_info)1087 bool A2DP_IsSinkCodecSupported(const uint8_t* p_codec_info) {
1088 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1089
1090 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1091
1092 switch (codec_type) {
1093 case A2DP_MEDIA_CT_SBC:
1094 return A2DP_IsSinkCodecSupportedSbc(p_codec_info);
1095 case A2DP_MEDIA_CT_AAC:
1096 return A2DP_IsSinkCodecSupportedAac(p_codec_info);
1097 case A2DP_MEDIA_CT_NON_A2DP:
1098 return A2DP_IsVendorSinkCodecSupported(p_codec_info);
1099 default:
1100 break;
1101 }
1102
1103 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
1104 return false;
1105 }
1106
A2DP_IsPeerSourceCodecSupported(const uint8_t * p_codec_info)1107 bool A2DP_IsPeerSourceCodecSupported(const uint8_t* p_codec_info) {
1108 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1109
1110 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1111
1112 switch (codec_type) {
1113 case A2DP_MEDIA_CT_SBC:
1114 return A2DP_IsPeerSourceCodecSupportedSbc(p_codec_info);
1115 case A2DP_MEDIA_CT_AAC:
1116 return A2DP_IsPeerSourceCodecSupportedAac(p_codec_info);
1117 case A2DP_MEDIA_CT_NON_A2DP:
1118 return A2DP_IsVendorPeerSourceCodecSupported(p_codec_info);
1119 default:
1120 break;
1121 }
1122
1123 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
1124 return false;
1125 }
1126
A2DP_InitDefaultCodec(uint8_t * p_codec_info)1127 void A2DP_InitDefaultCodec(uint8_t* p_codec_info) {
1128 A2DP_InitDefaultCodecSbc(p_codec_info);
1129 }
1130
A2DP_UsesRtpHeader(bool content_protection_enabled,const uint8_t * p_codec_info)1131 bool A2DP_UsesRtpHeader(bool content_protection_enabled,
1132 const uint8_t* p_codec_info) {
1133 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1134
1135 if (codec_type != A2DP_MEDIA_CT_NON_A2DP) return true;
1136
1137 return A2DP_VendorUsesRtpHeader(content_protection_enabled, p_codec_info);
1138 }
1139
A2DP_GetMediaType(const uint8_t * p_codec_info)1140 uint8_t A2DP_GetMediaType(const uint8_t* p_codec_info) {
1141 uint8_t media_type = (p_codec_info[A2DP_MEDIA_TYPE_OFFSET] >> 4) & 0x0f;
1142 return media_type;
1143 }
1144
A2DP_CodecName(const uint8_t * p_codec_info)1145 const char* A2DP_CodecName(const uint8_t* p_codec_info) {
1146 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1147
1148 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1149
1150 switch (codec_type) {
1151 case A2DP_MEDIA_CT_SBC:
1152 return A2DP_CodecNameSbc(p_codec_info);
1153 case A2DP_MEDIA_CT_AAC:
1154 return A2DP_CodecNameAac(p_codec_info);
1155 case A2DP_MEDIA_CT_NON_A2DP:
1156 return A2DP_VendorCodecName(p_codec_info);
1157 default:
1158 break;
1159 }
1160
1161 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
1162 return "UNKNOWN CODEC";
1163 }
1164
A2DP_CodecTypeEquals(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)1165 bool A2DP_CodecTypeEquals(const uint8_t* p_codec_info_a,
1166 const uint8_t* p_codec_info_b) {
1167 tA2DP_CODEC_TYPE codec_type_a = A2DP_GetCodecType(p_codec_info_a);
1168 tA2DP_CODEC_TYPE codec_type_b = A2DP_GetCodecType(p_codec_info_b);
1169
1170 if (codec_type_a != codec_type_b) return false;
1171
1172 switch (codec_type_a) {
1173 case A2DP_MEDIA_CT_SBC:
1174 return A2DP_CodecTypeEqualsSbc(p_codec_info_a, p_codec_info_b);
1175 case A2DP_MEDIA_CT_AAC:
1176 return A2DP_CodecTypeEqualsAac(p_codec_info_a, p_codec_info_b);
1177 case A2DP_MEDIA_CT_NON_A2DP:
1178 return A2DP_VendorCodecTypeEquals(p_codec_info_a, p_codec_info_b);
1179 default:
1180 break;
1181 }
1182
1183 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type_a);
1184 return false;
1185 }
1186
A2DP_CodecEquals(const uint8_t * p_codec_info_a,const uint8_t * p_codec_info_b)1187 bool A2DP_CodecEquals(const uint8_t* p_codec_info_a,
1188 const uint8_t* p_codec_info_b) {
1189 tA2DP_CODEC_TYPE codec_type_a = A2DP_GetCodecType(p_codec_info_a);
1190 tA2DP_CODEC_TYPE codec_type_b = A2DP_GetCodecType(p_codec_info_b);
1191
1192 if (codec_type_a != codec_type_b) return false;
1193
1194 switch (codec_type_a) {
1195 case A2DP_MEDIA_CT_SBC:
1196 return A2DP_CodecEqualsSbc(p_codec_info_a, p_codec_info_b);
1197 case A2DP_MEDIA_CT_AAC:
1198 return A2DP_CodecEqualsAac(p_codec_info_a, p_codec_info_b);
1199 case A2DP_MEDIA_CT_NON_A2DP:
1200 return A2DP_VendorCodecEquals(p_codec_info_a, p_codec_info_b);
1201 default:
1202 break;
1203 }
1204
1205 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type_a);
1206 return false;
1207 }
1208
A2DP_GetTrackSampleRate(const uint8_t * p_codec_info)1209 int A2DP_GetTrackSampleRate(const uint8_t* p_codec_info) {
1210 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1211
1212 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1213
1214 switch (codec_type) {
1215 case A2DP_MEDIA_CT_SBC:
1216 return A2DP_GetTrackSampleRateSbc(p_codec_info);
1217 case A2DP_MEDIA_CT_AAC:
1218 return A2DP_GetTrackSampleRateAac(p_codec_info);
1219 case A2DP_MEDIA_CT_NON_A2DP:
1220 return A2DP_VendorGetTrackSampleRate(p_codec_info);
1221 default:
1222 break;
1223 }
1224
1225 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
1226 return -1;
1227 }
1228
A2DP_GetTrackBitsPerSample(const uint8_t * p_codec_info)1229 int A2DP_GetTrackBitsPerSample(const uint8_t* p_codec_info) {
1230 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1231
1232 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1233
1234 switch (codec_type) {
1235 case A2DP_MEDIA_CT_SBC:
1236 return A2DP_GetTrackBitsPerSampleSbc(p_codec_info);
1237 case A2DP_MEDIA_CT_AAC:
1238 return A2DP_GetTrackBitsPerSampleAac(p_codec_info);
1239 case A2DP_MEDIA_CT_NON_A2DP:
1240 return A2DP_VendorGetTrackBitsPerSample(p_codec_info);
1241 default:
1242 break;
1243 }
1244
1245 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
1246 return -1;
1247 }
1248
A2DP_GetTrackChannelCount(const uint8_t * p_codec_info)1249 int A2DP_GetTrackChannelCount(const uint8_t* p_codec_info) {
1250 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1251
1252 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1253
1254 switch (codec_type) {
1255 case A2DP_MEDIA_CT_SBC:
1256 return A2DP_GetTrackChannelCountSbc(p_codec_info);
1257 case A2DP_MEDIA_CT_AAC:
1258 return A2DP_GetTrackChannelCountAac(p_codec_info);
1259 case A2DP_MEDIA_CT_NON_A2DP:
1260 return A2DP_VendorGetTrackChannelCount(p_codec_info);
1261 default:
1262 break;
1263 }
1264
1265 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
1266 return -1;
1267 }
1268
A2DP_GetSinkTrackChannelType(const uint8_t * p_codec_info)1269 int A2DP_GetSinkTrackChannelType(const uint8_t* p_codec_info) {
1270 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1271
1272 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1273
1274 switch (codec_type) {
1275 case A2DP_MEDIA_CT_SBC:
1276 return A2DP_GetSinkTrackChannelTypeSbc(p_codec_info);
1277 case A2DP_MEDIA_CT_AAC:
1278 return A2DP_GetSinkTrackChannelTypeAac(p_codec_info);
1279 case A2DP_MEDIA_CT_NON_A2DP:
1280 return A2DP_VendorGetSinkTrackChannelType(p_codec_info);
1281 default:
1282 break;
1283 }
1284
1285 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
1286 return -1;
1287 }
1288
A2DP_GetPacketTimestamp(const uint8_t * p_codec_info,const uint8_t * p_data,uint32_t * p_timestamp)1289 bool A2DP_GetPacketTimestamp(const uint8_t* p_codec_info, const uint8_t* p_data,
1290 uint32_t* p_timestamp) {
1291 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1292
1293 switch (codec_type) {
1294 case A2DP_MEDIA_CT_SBC:
1295 return A2DP_GetPacketTimestampSbc(p_codec_info, p_data, p_timestamp);
1296 case A2DP_MEDIA_CT_AAC:
1297 return A2DP_GetPacketTimestampAac(p_codec_info, p_data, p_timestamp);
1298 case A2DP_MEDIA_CT_NON_A2DP:
1299 return A2DP_VendorGetPacketTimestamp(p_codec_info, p_data, p_timestamp);
1300 default:
1301 break;
1302 }
1303
1304 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
1305 return false;
1306 }
1307
A2DP_BuildCodecHeader(const uint8_t * p_codec_info,BT_HDR * p_buf,uint16_t frames_per_packet)1308 bool A2DP_BuildCodecHeader(const uint8_t* p_codec_info, BT_HDR* p_buf,
1309 uint16_t frames_per_packet) {
1310 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1311
1312 switch (codec_type) {
1313 case A2DP_MEDIA_CT_SBC:
1314 return A2DP_BuildCodecHeaderSbc(p_codec_info, p_buf, frames_per_packet);
1315 case A2DP_MEDIA_CT_AAC:
1316 return A2DP_BuildCodecHeaderAac(p_codec_info, p_buf, frames_per_packet);
1317 case A2DP_MEDIA_CT_NON_A2DP:
1318 return A2DP_VendorBuildCodecHeader(p_codec_info, p_buf,
1319 frames_per_packet);
1320 default:
1321 break;
1322 }
1323
1324 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
1325 return false;
1326 }
1327
A2DP_GetEncoderInterface(const uint8_t * p_codec_info)1328 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterface(
1329 const uint8_t* p_codec_info) {
1330 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1331
1332 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1333
1334 switch (codec_type) {
1335 case A2DP_MEDIA_CT_SBC:
1336 return A2DP_GetEncoderInterfaceSbc(p_codec_info);
1337 case A2DP_MEDIA_CT_AAC:
1338 return A2DP_GetEncoderInterfaceAac(p_codec_info);
1339 case A2DP_MEDIA_CT_NON_A2DP:
1340 return A2DP_VendorGetEncoderInterface(p_codec_info);
1341 default:
1342 break;
1343 }
1344
1345 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
1346 return NULL;
1347 }
1348
A2DP_GetDecoderInterface(const uint8_t * p_codec_info)1349 const tA2DP_DECODER_INTERFACE* A2DP_GetDecoderInterface(
1350 const uint8_t* p_codec_info) {
1351 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1352
1353 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1354
1355 switch (codec_type) {
1356 case A2DP_MEDIA_CT_SBC:
1357 return A2DP_GetDecoderInterfaceSbc(p_codec_info);
1358 case A2DP_MEDIA_CT_AAC:
1359 return A2DP_GetDecoderInterfaceAac(p_codec_info);
1360 case A2DP_MEDIA_CT_NON_A2DP:
1361 return A2DP_VendorGetDecoderInterface(p_codec_info);
1362 default:
1363 break;
1364 }
1365
1366 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
1367 return NULL;
1368 }
1369
A2DP_AdjustCodec(uint8_t * p_codec_info)1370 bool A2DP_AdjustCodec(uint8_t* p_codec_info) {
1371 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1372
1373 switch (codec_type) {
1374 case A2DP_MEDIA_CT_SBC:
1375 return A2DP_AdjustCodecSbc(p_codec_info);
1376 case A2DP_MEDIA_CT_AAC:
1377 return A2DP_AdjustCodecAac(p_codec_info);
1378 case A2DP_MEDIA_CT_NON_A2DP:
1379 return A2DP_VendorAdjustCodec(p_codec_info);
1380 default:
1381 break;
1382 }
1383
1384 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
1385 return false;
1386 }
1387
A2DP_SourceCodecIndex(const uint8_t * p_codec_info)1388 btav_a2dp_codec_index_t A2DP_SourceCodecIndex(const uint8_t* p_codec_info) {
1389 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1390
1391 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1392
1393 switch (codec_type) {
1394 case A2DP_MEDIA_CT_SBC:
1395 return A2DP_SourceCodecIndexSbc(p_codec_info);
1396 case A2DP_MEDIA_CT_AAC:
1397 return A2DP_SourceCodecIndexAac(p_codec_info);
1398 case A2DP_MEDIA_CT_NON_A2DP:
1399 return A2DP_VendorSourceCodecIndex(p_codec_info);
1400 default:
1401 break;
1402 }
1403
1404 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
1405 return BTAV_A2DP_CODEC_INDEX_MAX;
1406 }
1407
A2DP_SinkCodecIndex(const uint8_t * p_codec_info)1408 btav_a2dp_codec_index_t A2DP_SinkCodecIndex(const uint8_t* p_codec_info) {
1409 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1410
1411 LOG_VERBOSE("%s: codec_type = 0x%x", __func__, codec_type);
1412
1413 switch (codec_type) {
1414 case A2DP_MEDIA_CT_SBC:
1415 return A2DP_SinkCodecIndexSbc(p_codec_info);
1416 case A2DP_MEDIA_CT_AAC:
1417 return A2DP_SinkCodecIndexAac(p_codec_info);
1418 case A2DP_MEDIA_CT_NON_A2DP:
1419 return A2DP_VendorSinkCodecIndex(p_codec_info);
1420 default:
1421 break;
1422 }
1423
1424 LOG_ERROR("%s: unsupported codec type 0x%x", __func__, codec_type);
1425 return BTAV_A2DP_CODEC_INDEX_MAX;
1426 }
1427
A2DP_CodecIndexStr(btav_a2dp_codec_index_t codec_index)1428 const char* A2DP_CodecIndexStr(btav_a2dp_codec_index_t codec_index) {
1429 switch (codec_index) {
1430 case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
1431 return A2DP_CodecIndexStrSbc();
1432 case BTAV_A2DP_CODEC_INDEX_SINK_SBC:
1433 return A2DP_CodecIndexStrSbcSink();
1434 case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
1435 return A2DP_CodecIndexStrAac();
1436 case BTAV_A2DP_CODEC_INDEX_SINK_AAC:
1437 return A2DP_CodecIndexStrAacSink();
1438 default:
1439 break;
1440 }
1441
1442 if (codec_index < BTAV_A2DP_CODEC_INDEX_MAX)
1443 return A2DP_VendorCodecIndexStr(codec_index);
1444
1445 return "UNKNOWN CODEC INDEX";
1446 }
1447
A2DP_InitCodecConfig(btav_a2dp_codec_index_t codec_index,AvdtpSepConfig * p_cfg)1448 bool A2DP_InitCodecConfig(btav_a2dp_codec_index_t codec_index,
1449 AvdtpSepConfig* p_cfg) {
1450 LOG_VERBOSE("%s: codec %s", __func__, A2DP_CodecIndexStr(codec_index));
1451
1452 /* Default: no content protection info */
1453 p_cfg->num_protect = 0;
1454 p_cfg->protect_info[0] = 0;
1455
1456 switch (codec_index) {
1457 case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
1458 return A2DP_InitCodecConfigSbc(p_cfg);
1459 case BTAV_A2DP_CODEC_INDEX_SINK_SBC:
1460 return A2DP_InitCodecConfigSbcSink(p_cfg);
1461 case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
1462 return A2DP_InitCodecConfigAac(p_cfg);
1463 case BTAV_A2DP_CODEC_INDEX_SINK_AAC:
1464 return A2DP_InitCodecConfigAacSink(p_cfg);
1465 default:
1466 break;
1467 }
1468
1469 if (codec_index < BTAV_A2DP_CODEC_INDEX_MAX)
1470 return A2DP_VendorInitCodecConfig(codec_index, p_cfg);
1471
1472 return false;
1473 }
1474
A2DP_CodecInfoString(const uint8_t * p_codec_info)1475 std::string A2DP_CodecInfoString(const uint8_t* p_codec_info) {
1476 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(p_codec_info);
1477
1478 switch (codec_type) {
1479 case A2DP_MEDIA_CT_SBC:
1480 return A2DP_CodecInfoStringSbc(p_codec_info);
1481 case A2DP_MEDIA_CT_AAC:
1482 return A2DP_CodecInfoStringAac(p_codec_info);
1483 case A2DP_MEDIA_CT_NON_A2DP:
1484 return A2DP_VendorCodecInfoString(p_codec_info);
1485 default:
1486 break;
1487 }
1488
1489 return "Unsupported codec type: " + loghex(codec_type);
1490 }
1491