1 /* 2 * Copyright (C) 2013-2018 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 #define LOG_TAG "a2dp_offload" 18 /*#define LOG_NDEBUG 0*/ 19 #define LOG_NDDEBUG 0 20 21 #include <dlfcn.h> 22 #include <errno.h> 23 #include <pthread.h> 24 #include <stdlib.h> 25 26 #include <cutils/log.h> 27 #include <cutils/str_parms.h> 28 #include <cutils/properties.h> 29 #include <hardware/audio.h> 30 31 #include "audio_hw.h" 32 #include "audio_extn.h" 33 #include "platform_api.h" 34 35 #ifdef A2DP_OFFLOAD_ENABLED 36 #define BT_IPC_LIB_NAME "libbthost_if.so" 37 #define BTAUDIO_OFFLOAD_LIB_NAME "btaudio_offload_if.so" 38 39 // Media format definitions 40 #define ENC_MEDIA_FMT_AAC 0x00010DA6 41 #define ENC_MEDIA_FMT_APTX 0x000131ff 42 #define ENC_MEDIA_FMT_APTX_HD 0x00013200 43 #define ENC_MEDIA_FMT_LDAC 0x00013224 44 #define ENC_MEDIA_FMT_SBC 0x00010BF2 45 #define ENC_MEDIA_FMT_NONE 0 46 #define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0 47 #define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1 48 #define MEDIA_FMT_AAC_AOT_LC 2 49 #define MEDIA_FMT_AAC_AOT_SBR 5 50 #define MEDIA_FMT_AAC_AOT_PS 29 51 #define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1 52 #define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2 53 #define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8 54 #define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9 55 56 // PCM channels 57 #define PCM_CHANNEL_L 1 58 #define PCM_CHANNEL_R 2 59 #define PCM_CHANNEL_C 3 60 61 // Mixer controls sent to ALSA 62 #define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config" 63 #define MIXER_DEC_CONFIG_BLOCK "SLIM_7_TX Decoder Config" 64 #define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format" 65 #define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode" 66 #define MIXER_SAMPLE_RATE_RX "BT SampleRate RX" 67 #define MIXER_SAMPLE_RATE_TX "BT SampleRate TX" 68 #define MIXER_AFE_IN_CHANNELS "AFE Input Channels" 69 #define MIXER_ABR_TX_FEEDBACK_PATH "A2DP_SLIM7_UL_HL Switch" 70 #define MIXER_SET_FEEDBACK_CHANNEL "BT set feedback channel" 71 72 // Encoder format strings 73 #define ENC_FMT_AAC "aac" 74 #define ENC_FMT_APTX "aptx" 75 #define ENC_FMT_APTXHD "aptxhd" 76 #define ENC_FMT_LDAC "ldac" 77 #define ENC_FMT_SBC "sbc" 78 79 // System properties used for A2DP Offload 80 #define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported" 81 #define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled" 82 #define SYSPROP_BLUETOOTH_AUDIO_HAL_DISABLED "persist.bluetooth.bluetooth_audio_hal.disabled" 83 #define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency" 84 85 // Default encoder bit width 86 #define DEFAULT_ENCODER_BIT_FORMAT 16 87 88 // Default encoder latency 89 #define DEFAULT_ENCODER_LATENCY 200 90 91 // Encoder latency offset for codecs supported 92 #define ENCODER_LATENCY_AAC 70 93 #define ENCODER_LATENCY_APTX 40 94 #define ENCODER_LATENCY_APTX_HD 20 95 #define ENCODER_LATENCY_LDAC 40 96 #define ENCODER_LATENCY_SBC 10 97 #define ENCODER_LATENCY_PCM 50 98 99 // Default A2DP sink latency offset 100 #define DEFAULT_SINK_LATENCY_AAC 180 101 #define DEFAULT_SINK_LATENCY_APTX 160 102 #define DEFAULT_SINK_LATENCY_APTX_HD 180 103 #define DEFAULT_SINK_LATENCY_LDAC 180 104 #define DEFAULT_SINK_LATENCY_SBC 140 105 #define DEFAULT_SINK_LATENCY_PCM 140 106 107 // Slimbus Tx sample rate for ABR feedback channel 108 #define ABR_TX_SAMPLE_RATE "KHZ_8" 109 110 // Purpose ID for Inter Module Communication (IMC) in AFE 111 #define IMC_PURPOSE_ID_BT_INFO 0x000132E2 112 113 // Maximum quality levels for ABR 114 #define MAX_ABR_QUALITY_LEVELS 5 115 116 // Instance identifier for A2DP 117 #define MAX_INSTANCE_ID (UINT32_MAX / 2) 118 119 /* 120 * Below enum values are extended from audio-base.h to 121 * keep encoder codec type local to bthost_ipc 122 * and audio_hal as these are intended only for handshake 123 * between IPC lib and Audio HAL. 124 */ 125 typedef enum { 126 ENC_CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL 127 ENC_CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL 128 ENC_CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL 129 ENC_CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL 130 ENC_CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL 131 ENC_CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL 132 ENC_CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u 133 } enc_codec_t; 134 135 typedef int (*audio_stream_open_t)(void); 136 typedef int (*audio_stream_close_t)(void); 137 typedef int (*audio_stream_start_t)(void); 138 typedef int (*audio_stream_stop_t)(void); 139 typedef int (*audio_stream_suspend_t)(void); 140 typedef void (*audio_handoff_triggered_t)(void); 141 typedef void (*clear_a2dp_suspend_flag_t)(void); 142 typedef void * (*audio_get_codec_config_t)(uint8_t *multicast_status, uint8_t *num_dev, 143 enc_codec_t *codec_type); 144 typedef int (*audio_check_a2dp_ready_t)(void); 145 typedef int (*audio_is_scrambling_enabled_t)(void); 146 typedef uint16_t (*audio_get_a2dp_sink_latency_t)(void); 147 148 enum A2DP_STATE { 149 A2DP_STATE_CONNECTED, 150 A2DP_STATE_STARTED, 151 A2DP_STATE_STOPPED, 152 A2DP_STATE_DISCONNECTED, 153 }; 154 155 typedef enum { 156 IMC_TRANSMIT, 157 IMC_RECEIVE, 158 } imc_direction_t; 159 160 typedef enum { 161 IMC_DISABLE, 162 IMC_ENABLE, 163 } imc_status_t; 164 165 typedef enum { 166 MTU_SIZE, 167 PEAK_BIT_RATE, 168 } frame_control_type_t; 169 170 /* PCM config for ABR Feedback hostless front end */ 171 static struct pcm_config pcm_config_abr = { 172 .channels = 1, 173 .rate = 8000, 174 .period_size = 240, 175 .period_count = 2, 176 .format = PCM_FORMAT_S16_LE, 177 .start_threshold = 0, 178 .stop_threshold = INT_MAX, 179 .avail_min = 0, 180 }; 181 182 /* Adaptive bitrate config for A2DP codecs */ 183 struct a2dp_abr_config { 184 /* Flag to denote whether Adaptive bitrate is enabled for codec */ 185 bool is_abr_enabled; 186 /* Flag to denote whether front end has been opened for ABR */ 187 bool abr_started; 188 /* ABR Tx path pcm handle */ 189 struct pcm *abr_tx_handle; 190 /* ABR Inter Module Communication (IMC) instance ID */ 191 uint32_t imc_instance; 192 }; 193 194 static uint32_t instance_id = MAX_INSTANCE_ID; 195 196 /* Data structure used to: 197 * - Update the A2DP state machine 198 * - Communicate with the libbthost_if.so IPC library 199 * - Store DSP encoder configuration information 200 */ 201 struct a2dp_data { 202 /* Audio device handle */ 203 struct audio_device *adev; 204 /* Bluetooth IPC library handle */ 205 void *bt_lib_handle; 206 /* Open A2DP audio stream. Initialize audio datapath */ 207 audio_stream_open_t audio_stream_open; 208 /* Close A2DP audio stream */ 209 audio_stream_close_t audio_stream_close; 210 /* Start A2DP audio stream. Start audio datapath */ 211 audio_stream_start_t audio_stream_start; 212 /* Stop A2DP audio stream */ 213 audio_stream_stop_t audio_stream_stop; 214 /* Suspend A2DP audio stream */ 215 audio_stream_suspend_t audio_stream_suspend; 216 /* Notify Bluetooth IPC library of handoff being triggered */ 217 audio_handoff_triggered_t audio_handoff_triggered; 218 /* Clear A2DP suspend flag in Bluetooth IPC library */ 219 clear_a2dp_suspend_flag_t clear_a2dp_suspend_flag; 220 /* Get codec configuration from Bluetooth stack via 221 * Bluetooth IPC library */ 222 audio_get_codec_config_t audio_get_codec_config; 223 /* Check if A2DP is ready */ 224 audio_check_a2dp_ready_t audio_check_a2dp_ready; 225 /* Check if scrambling is enabled on BTSoC */ 226 audio_is_scrambling_enabled_t audio_is_scrambling_enabled; 227 /* Get sink latency from Bluetooth stack */ 228 audio_get_a2dp_sink_latency_t audio_get_a2dp_sink_latency; 229 /* Internal A2DP state identifier */ 230 enum A2DP_STATE bt_state; 231 /* A2DP codec type configured */ 232 enc_codec_t bt_encoder_format; 233 /* Sampling rate configured with A2DP encoder on DSP */ 234 uint32_t enc_sampling_rate; 235 /* Channel configuration of A2DP on DSP */ 236 uint32_t enc_channels; 237 /* Flag to denote whether A2DP audio datapath has started */ 238 bool a2dp_started; 239 /* Flag to denote whether A2DP audio datapath is suspended */ 240 bool a2dp_suspended; 241 /* Number of active sessions on A2DP output */ 242 int a2dp_total_active_session_request; 243 /* Flag to denote whether A2DP offload is enabled */ 244 bool is_a2dp_offload_enabled; 245 /* Flag to denote whether codec reconfiguration/soft handoff is in progress */ 246 bool is_handoff_in_progress; 247 /* Flag to denote whether APTX Dual Mono encoder is supported */ 248 bool is_aptx_dual_mono_supported; 249 /* Adaptive bitrate config for A2DP codecs */ 250 struct a2dp_abr_config abr_config; 251 }; 252 253 struct a2dp_data a2dp; 254 255 /* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs. 256 * Structures sent to configure DSP for ABR are defined below. 257 * This data helps DSP configure feedback path (BTSoC to LPASS) 258 * for link quality levels and mapping quality levels to codec 259 * specific bitrate. 260 */ 261 262 /* Key value pair for link quality level to bitrate mapping. */ 263 struct bit_rate_level_map_t { 264 uint32_t link_quality_level; 265 uint32_t bitrate; 266 }; 267 268 /* Link quality level to bitrate mapping info sent to DSP. */ 269 struct quality_level_to_bitrate_info { 270 /* Number of quality levels being mapped. 271 * This will be equal to the size of mapping table. 272 */ 273 uint32_t num_levels; 274 /* Quality level to bitrate mapping table */ 275 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS]; 276 }; 277 278 /* Structure to set up Inter Module Communication (IMC) between 279 * AFE Decoder and Encoder. 280 */ 281 struct imc_dec_enc_info { 282 /* Decoder to encoder communication direction. 283 * Transmit = 0 / Receive = 1 284 */ 285 uint32_t direction; 286 /* Enable / disable IMC between decoder and encoder */ 287 uint32_t enable; 288 /* Purpose of IMC being set up between decoder and encoder. 289 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback 290 * is the default value to be sent as purpose. 291 */ 292 uint32_t purpose; 293 /* Unique communication instance ID. 294 * purpose and comm_instance together form the actual key 295 * used in IMC registration, which must be the same for 296 * encoder and decoder for which IMC is being set up. 297 */ 298 uint32_t comm_instance; 299 }; 300 301 /* Structure to control frame size of AAC encoded frames. */ 302 struct aac_frame_size_control_t { 303 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/ 304 uint32_t ctl_type; 305 /* Control value 306 * MTU_SIZE: MTU size in bytes 307 * PEAK_BIT_RATE: Peak bitrate in bits per second. 308 */ 309 uint32_t ctl_value; 310 }; 311 312 /* Structure used for ABR config of AFE encoder and decoder. */ 313 struct abr_enc_cfg_t { 314 /* Link quality level to bitrate mapping info sent to DSP. */ 315 struct quality_level_to_bitrate_info mapping_info; 316 /* Information to set up IMC between decoder and encoder */ 317 struct imc_dec_enc_info imc_info; 318 /* Flag to indicate whether ABR is enabled */ 319 bool is_abr_enabled; 320 } __attribute__ ((packed)); 321 322 /* Structure to send configuration for decoder introduced 323 * on AFE Tx path for ABR link quality feedback to BT encoder. 324 */ 325 struct abr_dec_cfg_t { 326 /* Decoder media format */ 327 uint32_t dec_format; 328 /* Information to set up IMC between decoder and encoder */ 329 struct imc_dec_enc_info imc_info; 330 } __attribute__ ((packed)); 331 332 /* START of DSP configurable structures 333 * These values should match with DSP interface defintion 334 */ 335 336 struct aac_cfg_blk_t { 337 /* Encoder media format for AAC */ 338 uint32_t enc_format; 339 340 /* Encoding rate in bits per second */ 341 uint32_t bit_rate; 342 343 /* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS */ 344 uint32_t enc_mode; 345 346 /* supported aac_fmt_flag are ADTS/RAW */ 347 uint16_t aac_fmt_flag; 348 349 /* supported channel_cfg are Native mode, Mono , Stereo */ 350 uint16_t channel_cfg; 351 352 /* Number of samples per second */ 353 uint32_t sample_rate; 354 } __attribute__ ((packed)); 355 356 /* AAC encoder configuration structure. */ 357 typedef struct aac_enc_cfg_t aac_enc_cfg_t; 358 359 struct aac_enc_cfg_t { 360 struct aac_cfg_blk_t aac_cfg; 361 struct aac_frame_size_control_t frame_ctl; 362 } __attribute__ ((packed)); 363 364 /* SBC encoder configuration structure. */ 365 typedef struct sbc_enc_cfg_t sbc_enc_cfg_t; 366 367 struct sbc_enc_cfg_t { 368 /* Encoder media format for SBC */ 369 uint32_t enc_format; 370 371 /* supported num_subbands are 4/8 */ 372 uint32_t num_subbands; 373 374 /* supported blk_len are 4, 8, 12, 16 */ 375 uint32_t blk_len; 376 377 /* supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO */ 378 uint32_t channel_mode; 379 380 /* supported alloc_method are LOUNDNESS/SNR */ 381 uint32_t alloc_method; 382 383 /* supported bit_rate for mono channel is max 320kbps 384 * supported bit rate for stereo channel is max 512 kbps */ 385 uint32_t bit_rate; 386 387 /* Number of samples per second */ 388 uint32_t sample_rate; 389 } __attribute__ ((packed)); 390 391 struct custom_enc_cfg_t { 392 /* Custom encoder media format */ 393 uint32_t enc_format; 394 395 /* Number of samples per second */ 396 uint32_t sample_rate; 397 398 /* supported num_channels are Mono/Stereo */ 399 uint16_t num_channels; 400 401 /* Reserved for future enhancement */ 402 uint16_t reserved; 403 404 /* supported channel_mapping for mono is CHANNEL_C 405 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R */ 406 uint8_t channel_mapping[8]; 407 408 /* Reserved for future enhancement */ 409 uint32_t custom_size; 410 } __attribute__ ((packed)); 411 412 struct aptx_v2_enc_cfg_ext_t { 413 /* sync_mode introduced with APTX V2 libraries 414 * sync mode: 0x0 = stereo sync mode 415 * 0x01 = dual mono sync mode 416 * 0x02 = dual mono with no sync on either L or R codewords 417 */ 418 uint32_t sync_mode; 419 } __attribute__ ((packed)); 420 421 /* APTX struct for combining custom enc and V2 members */ 422 struct aptx_enc_cfg_t { 423 struct custom_enc_cfg_t custom_cfg; 424 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg; 425 } __attribute__ ((packed)); 426 427 struct ldac_specific_enc_cfg_t { 428 /* 429 * This is used to calculate the encoder output 430 * bytes per frame (i.e. bytes per packet). 431 * Bit rate also configures the EQMID. 432 * The min bit rate 303000 bps is calculated for 433 * 44.1 kHz and 88.2 KHz sampling frequencies with 434 * Mobile use Quality. 435 * The max bit rate of 990000 bps is calculated for 436 * 96kHz and 48 KHz with High Quality 437 * @Range(in bits per second) 438 * 303000 for Mobile use Quality 439 * 606000 for standard Quality 440 * 909000 for High Quality 441 */ 442 uint32_t bit_rate; 443 444 /* 445 * The channel setting information for LDAC specification 446 * of Bluetooth A2DP which is determined by SRC and SNK 447 * devices in Bluetooth transmission. 448 * @Range: 449 * 0 for native mode 450 * 4 for mono 451 * 2 for dual channel 452 * 1 for stereo 453 */ 454 uint16_t channel_mode; 455 456 /* 457 * Maximum Transmission Unit (MTU). 458 * The minimum MTU that a L2CAP implementation for LDAC shall 459 * support is 679 bytes, because LDAC is optimized with 2-DH5 460 * packet as its target. 461 * @Range : 679 462 * @Default: 679 for LDACBT_MTU_2DH5 463 */ 464 uint16_t mtu; 465 } __attribute__ ((packed)); 466 467 /* LDAC struct for combining custom enc and standard members */ 468 struct ldac_enc_cfg_t { 469 struct custom_enc_cfg_t custom_cfg; 470 struct ldac_specific_enc_cfg_t ldac_cfg; 471 struct abr_enc_cfg_t abr_cfg; 472 } __attribute__ ((packed)); 473 474 /* Information about Bluetooth SBC encoder configuration 475 * This data is used between audio HAL module and 476 * Bluetooth IPC library to configure DSP encoder 477 */ 478 typedef struct { 479 uint32_t subband; /* 4, 8 */ 480 uint32_t blk_len; /* 4, 8, 12, 16 */ 481 uint16_t sampling_rate; /* 44.1khz, 48khz */ 482 uint8_t channels; /* 0(Mono), 1(Dual_mono), 2(Stereo), 3(JS) */ 483 uint8_t alloc; /* 0(Loudness), 1(SNR) */ 484 uint8_t min_bitpool; /* 2 */ 485 uint8_t max_bitpool; /* 53(44.1khz), 51 (48khz) */ 486 uint32_t bitrate; /* 320kbps to 512kbps */ 487 uint32_t bits_per_sample; /* 16 bit */ 488 } audio_sbc_encoder_config; 489 490 /* Information about Bluetooth APTX encoder configuration 491 * This data is used between audio HAL module and 492 * Bluetooth IPC library to configure DSP encoder 493 */ 494 typedef struct { 495 uint16_t sampling_rate; 496 uint8_t channels; 497 uint32_t bitrate; 498 uint32_t bits_per_sample; 499 } audio_aptx_default_config; 500 501 typedef struct { 502 uint16_t sampling_rate; 503 uint8_t channels; 504 uint32_t bitrate; 505 uint32_t sync_mode; 506 } audio_aptx_dual_mono_config; 507 508 typedef union { 509 audio_aptx_default_config *default_cfg; 510 audio_aptx_dual_mono_config *dual_mono_cfg; 511 } audio_aptx_encoder_config; 512 513 /* Information about Bluetooth AAC encoder configuration 514 * This data is used between audio HAL module and 515 * Bluetooth IPC library to configure DSP encoder 516 */ 517 typedef struct { 518 uint32_t enc_mode; /* LC, SBR, PS */ 519 uint16_t format_flag; /* RAW, ADTS */ 520 uint16_t channels; /* 1-Mono, 2-Stereo */ 521 uint32_t sampling_rate; 522 uint32_t bitrate; 523 uint32_t bits_per_sample; 524 struct aac_frame_size_control_t frame_ctl; 525 } audio_aac_encoder_config; 526 527 /* Information about Bluetooth LDAC encoder configuration 528 * This data is used between audio HAL module and 529 * Bluetooth IPC library to configure DSP encoder 530 */ 531 typedef struct { 532 uint32_t sampling_rate; /* 44100, 48000, 88200, 96000 */ 533 uint32_t bit_rate; /* 303000, 606000, 909000 (in bits per second) */ 534 uint16_t channel_mode; /* 0, 4, 2, 1 */ 535 uint16_t mtu; 536 uint32_t bits_per_sample; /* 16, 24, 32 (bits) */ 537 bool is_abr_enabled; 538 struct quality_level_to_bitrate_info level_to_bitrate_map; 539 } audio_ldac_encoder_config; 540 541 /*********** END of DSP configurable structures ********************/ 542 543 static void a2dp_common_init() 544 { 545 a2dp.a2dp_started = false; 546 a2dp.a2dp_total_active_session_request = 0; 547 a2dp.a2dp_suspended = false; 548 a2dp.bt_encoder_format = ENC_CODEC_TYPE_INVALID; 549 a2dp.bt_state = A2DP_STATE_DISCONNECTED; 550 a2dp.abr_config.is_abr_enabled = false; 551 a2dp.abr_config.abr_started = false; 552 a2dp.abr_config.imc_instance = 0; 553 a2dp.abr_config.abr_tx_handle = NULL; 554 } 555 556 static void update_offload_codec_support() 557 { 558 a2dp.is_a2dp_offload_enabled = 559 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) && 560 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false); 561 562 ALOGD("%s: A2DP offload enabled = %d", __func__, 563 a2dp.is_a2dp_offload_enabled); 564 } 565 566 static int stop_abr() 567 { 568 struct mixer_ctl *ctl_abr_tx_path = NULL; 569 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL; 570 571 /* This function can be used if !abr_started for clean up */ 572 ALOGV("%s: enter", __func__); 573 574 // Close hostless front end 575 if (a2dp.abr_config.abr_tx_handle != NULL) { 576 pcm_close(a2dp.abr_config.abr_tx_handle); 577 a2dp.abr_config.abr_tx_handle = NULL; 578 } 579 a2dp.abr_config.abr_started = false; 580 a2dp.abr_config.imc_instance = 0; 581 582 // Reset BT driver mixer control for ABR usecase 583 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer, 584 MIXER_SET_FEEDBACK_CHANNEL); 585 if (!ctl_set_bt_feedback_channel) { 586 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__); 587 return -ENOSYS; 588 } 589 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) { 590 ALOGE("%s: Failed to set BT usecase", __func__); 591 return -ENOSYS; 592 } 593 594 // Reset ABR Tx feedback path 595 ALOGV("%s: Disable ABR Tx feedback path", __func__); 596 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer, 597 MIXER_ABR_TX_FEEDBACK_PATH); 598 if (!ctl_abr_tx_path) { 599 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__); 600 return -ENOSYS; 601 } 602 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) { 603 ALOGE("%s: Failed to set ABR Tx feedback path", __func__); 604 return -ENOSYS; 605 } 606 607 return 0; 608 } 609 610 static int start_abr() 611 { 612 struct mixer_ctl *ctl_abr_tx_path = NULL; 613 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL; 614 int abr_device_id; 615 int ret = 0; 616 617 if (!a2dp.abr_config.is_abr_enabled) { 618 ALOGE("%s: Cannot start if ABR is not enabled", __func__); 619 return -ENOSYS; 620 } 621 622 if (a2dp.abr_config.abr_started) { 623 ALOGI("%s: ABR has already started", __func__); 624 return ret; 625 } 626 627 // Enable Slimbus 7 Tx feedback path 628 ALOGV("%s: Enable ABR Tx feedback path", __func__); 629 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer, 630 MIXER_ABR_TX_FEEDBACK_PATH); 631 if (!ctl_abr_tx_path) { 632 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__); 633 return -ENOSYS; 634 } 635 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) { 636 ALOGE("%s: Failed to set ABR Tx feedback path", __func__); 637 return -ENOSYS; 638 } 639 640 // Notify ABR usecase information to BT driver to distinguish 641 // between SCO and feedback usecase 642 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer, 643 MIXER_SET_FEEDBACK_CHANNEL); 644 if (!ctl_set_bt_feedback_channel) { 645 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__); 646 return -ENOSYS; 647 } 648 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) { 649 ALOGE("%s: Failed to set BT usecase", __func__); 650 return -ENOSYS; 651 } 652 653 // Open hostless front end and prepare ABR Tx path 654 abr_device_id = platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK, 655 PCM_CAPTURE); 656 if (!a2dp.abr_config.abr_tx_handle) { 657 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card, 658 abr_device_id, PCM_IN, 659 &pcm_config_abr); 660 if (a2dp.abr_config.abr_tx_handle == NULL || 661 !pcm_is_ready(a2dp.abr_config.abr_tx_handle)) 662 goto fail; 663 } 664 ret = pcm_start(a2dp.abr_config.abr_tx_handle); 665 if (ret < 0) 666 goto fail; 667 a2dp.abr_config.abr_started = true; 668 669 return ret; 670 671 fail: 672 ALOGE("%s: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle)); 673 stop_abr(); 674 return -ENOSYS; 675 } 676 677 /* API to open Bluetooth IPC library to start IPC communication */ 678 static int open_a2dp_output() 679 { 680 int ret = 0; 681 ALOGD("%s: Open A2DP output start", __func__); 682 bool hal_v2_enabled = 683 !property_get_bool(SYSPROP_BLUETOOTH_AUDIO_HAL_DISABLED, false); 684 685 if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) { 686 ALOGD("%s: Called A2DP open with improper state, Ignoring request state %d", 687 __func__, a2dp.bt_state); 688 return -ENOSYS; 689 } 690 691 if (a2dp.bt_lib_handle == NULL) { 692 ALOGD("%s: Requesting for Bluetooth IPC lib handle [%s]", __func__, 693 hal_v2_enabled ? BTAUDIO_OFFLOAD_LIB_NAME : BT_IPC_LIB_NAME); 694 if (hal_v2_enabled) { 695 a2dp.bt_lib_handle = dlopen(BTAUDIO_OFFLOAD_LIB_NAME, RTLD_NOW); 696 } else { 697 a2dp.bt_lib_handle = dlopen(BT_IPC_LIB_NAME, RTLD_NOW); 698 } 699 if (a2dp.bt_lib_handle == NULL) { 700 ret = -errno; 701 ALOGE("%s: DLOPEN failed for %s errno %d strerror %s", __func__, 702 BT_IPC_LIB_NAME, errno, strerror(errno)); 703 a2dp.bt_state = A2DP_STATE_DISCONNECTED; 704 return ret; 705 } else { 706 a2dp.audio_stream_open = (audio_stream_open_t) 707 dlsym(a2dp.bt_lib_handle, "audio_stream_open"); 708 a2dp.audio_stream_start = (audio_stream_start_t) 709 dlsym(a2dp.bt_lib_handle, "audio_stream_start"); 710 a2dp.audio_get_codec_config = (audio_get_codec_config_t) 711 dlsym(a2dp.bt_lib_handle, "audio_get_codec_config"); 712 a2dp.audio_stream_suspend = (audio_stream_suspend_t) 713 dlsym(a2dp.bt_lib_handle, "audio_stream_suspend"); 714 a2dp.audio_handoff_triggered = (audio_handoff_triggered_t) 715 dlsym(a2dp.bt_lib_handle, "audio_handoff_triggered"); 716 a2dp.clear_a2dp_suspend_flag = (clear_a2dp_suspend_flag_t) 717 dlsym(a2dp.bt_lib_handle, "clear_a2dp_suspend_flag"); 718 a2dp.audio_stream_stop = (audio_stream_stop_t) 719 dlsym(a2dp.bt_lib_handle, "audio_stream_stop"); 720 a2dp.audio_stream_close = (audio_stream_close_t) 721 dlsym(a2dp.bt_lib_handle, "audio_stream_close"); 722 a2dp.audio_check_a2dp_ready = (audio_check_a2dp_ready_t) 723 dlsym(a2dp.bt_lib_handle,"audio_check_a2dp_ready"); 724 a2dp.audio_is_scrambling_enabled = (audio_is_scrambling_enabled_t) 725 dlsym(a2dp.bt_lib_handle,"audio_is_scrambling_enabled"); 726 a2dp.audio_get_a2dp_sink_latency = (audio_get_a2dp_sink_latency_t) 727 dlsym(a2dp.bt_lib_handle,"audio_get_a2dp_sink_latency"); 728 } 729 } 730 731 if (a2dp.bt_lib_handle && a2dp.audio_stream_open) { 732 ALOGD("%s: calling Bluetooth stream open", __func__); 733 ret = a2dp.audio_stream_open(); 734 if (ret != 0) { 735 ALOGE("%s: Failed to open output stream for A2DP: status %d", __func__, ret); 736 dlclose(a2dp.bt_lib_handle); 737 a2dp.bt_lib_handle = NULL; 738 a2dp.bt_state = A2DP_STATE_DISCONNECTED; 739 return ret; 740 } 741 a2dp.bt_state = A2DP_STATE_CONNECTED; 742 } else { 743 ALOGE("%s: A2DP handle is not identified, Ignoring open request", __func__); 744 a2dp.bt_state = A2DP_STATE_DISCONNECTED; 745 return -ENOSYS; 746 } 747 748 return ret; 749 } 750 751 static int close_a2dp_output() 752 { 753 ALOGV("%s\n",__func__); 754 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_close)) { 755 ALOGE("%s: A2DP handle is not identified, Ignoring close request", __func__); 756 return -ENOSYS; 757 } 758 if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) { 759 ALOGD("%s: calling Bluetooth stream close", __func__); 760 if (a2dp.audio_stream_close() == false) 761 ALOGE("%s: failed close A2DP control path from Bluetooth IPC library", __func__); 762 } 763 a2dp_common_init(); 764 a2dp.enc_sampling_rate = 0; 765 a2dp.enc_channels = 0; 766 767 return 0; 768 } 769 770 static int a2dp_check_and_set_scrambler() 771 { 772 bool scrambler_mode = false; 773 struct mixer_ctl *ctrl_scrambler_mode = NULL; 774 int ret = 0; 775 if (a2dp.audio_is_scrambling_enabled && (a2dp.bt_state != A2DP_STATE_DISCONNECTED)) 776 scrambler_mode = a2dp.audio_is_scrambling_enabled(); 777 778 // Scrambling needs to be enabled in DSP if scrambler mode is set 779 // disable scrambling not required 780 if (scrambler_mode) { 781 // enable scrambler in dsp 782 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer, 783 MIXER_SCRAMBLER_MODE); 784 if (!ctrl_scrambler_mode) { 785 ALOGE("%s: ERROR scrambler mode mixer control not identifed", __func__); 786 return -ENOSYS; 787 } else { 788 ret = mixer_ctl_set_value(ctrl_scrambler_mode, 0, true); 789 if (ret != 0) { 790 ALOGE("%s: Could not set scrambler mode", __func__); 791 return ret; 792 } 793 } 794 } 795 return 0; 796 } 797 798 static int a2dp_set_backend_cfg() 799 { 800 const char *rate_str = NULL, *in_channels = NULL; 801 uint32_t sampling_rate_rx = a2dp.enc_sampling_rate; 802 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL; 803 804 // For LDAC encoder open slimbus port at 96Khz for 48Khz input 805 // and 88.2Khz for 44.1Khz input. 806 if ((a2dp.bt_encoder_format == ENC_CODEC_TYPE_LDAC) && 807 (sampling_rate_rx == 48000 || sampling_rate_rx == 44100 )) { 808 sampling_rate_rx *= 2; 809 } 810 // No need to configure backend for PCM format. 811 if (a2dp.bt_encoder_format == ENC_CODEC_TYPE_PCM) { 812 return 0; 813 } 814 // Set Rx backend sample rate 815 switch (sampling_rate_rx) { 816 case 44100: 817 rate_str = "KHZ_44P1"; 818 break; 819 case 88200: 820 rate_str = "KHZ_88P2"; 821 break; 822 case 96000: 823 rate_str = "KHZ_96"; 824 break; 825 case 48000: 826 default: 827 rate_str = "KHZ_48"; 828 break; 829 } 830 831 ALOGV("%s: set backend rx sample rate = %s", __func__, rate_str); 832 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer, 833 MIXER_SAMPLE_RATE_RX); 834 if (!ctl_sample_rate) { 835 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__); 836 return -ENOSYS; 837 } 838 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) { 839 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str); 840 return -ENOSYS; 841 } 842 843 // Set Tx backend sample rate 844 if (a2dp.abr_config.is_abr_enabled) { 845 rate_str = ABR_TX_SAMPLE_RATE; 846 847 ALOGV("%s: set backend tx sample rate = %s", __func__, rate_str); 848 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer, 849 MIXER_SAMPLE_RATE_TX); 850 if (!ctl_sample_rate) { 851 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__); 852 return -ENOSYS; 853 } 854 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) { 855 ALOGE("%s: Failed to set backend sample rate = %s", 856 __func__, rate_str); 857 return -ENOSYS; 858 } 859 } 860 861 // Configure AFE input channels 862 switch (a2dp.enc_channels) { 863 case 1: 864 in_channels = "One"; 865 break; 866 case 2: 867 default: 868 in_channels = "Two"; 869 break; 870 } 871 872 ALOGV("%s: set AFE input channels = %d", __func__, a2dp.enc_channels); 873 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer, 874 MIXER_AFE_IN_CHANNELS); 875 if (!ctrl_in_channels) { 876 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__); 877 return -ENOSYS; 878 } 879 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) { 880 ALOGE("%s: Failed to set AFE in channels = %d", __func__, a2dp.enc_channels); 881 return -ENOSYS; 882 } 883 884 return 0; 885 } 886 887 static int a2dp_set_bit_format(uint32_t enc_bit_format) 888 { 889 const char *bit_format = NULL; 890 struct mixer_ctl *ctrl_bit_format = NULL; 891 892 // Configure AFE Input Bit Format 893 switch (enc_bit_format) { 894 case 32: 895 bit_format = "S32_LE"; 896 break; 897 case 24: 898 bit_format = "S24_LE"; 899 break; 900 case 16: 901 default: 902 bit_format = "S16_LE"; 903 break; 904 } 905 906 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format); 907 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, 908 MIXER_ENC_BIT_FORMAT); 909 if (!ctrl_bit_format) { 910 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__); 911 return -ENOSYS; 912 } 913 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) { 914 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format); 915 return -ENOSYS; 916 } 917 return 0; 918 } 919 920 static int a2dp_reset_backend_cfg() 921 { 922 const char *rate_str = "KHZ_8", *in_channels = "Zero"; 923 struct mixer_ctl *ctl_sample_rate_rx = NULL, *ctl_sample_rate_tx = NULL; 924 struct mixer_ctl *ctrl_in_channels = NULL; 925 926 // Reset backend sampling rate 927 ALOGV("%s: reset backend sample rate = %s", __func__, rate_str); 928 ctl_sample_rate_rx = mixer_get_ctl_by_name(a2dp.adev->mixer, 929 MIXER_SAMPLE_RATE_RX); 930 if (!ctl_sample_rate_rx) { 931 ALOGE("%s: ERROR Rx backend sample rate mixer control not identifed", __func__); 932 return -ENOSYS; 933 } 934 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_rx, rate_str) != 0) { 935 ALOGE("%s: Failed to reset Rx backend sample rate = %s", __func__, rate_str); 936 return -ENOSYS; 937 } 938 939 if (a2dp.abr_config.is_abr_enabled) { 940 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer, 941 MIXER_SAMPLE_RATE_TX); 942 if (!ctl_sample_rate_tx) { 943 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__); 944 return -ENOSYS; 945 } 946 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) { 947 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str); 948 return -ENOSYS; 949 } 950 } 951 952 // Reset AFE input channels 953 ALOGV("%s: reset AFE input channels = %s", __func__, in_channels); 954 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer, 955 MIXER_AFE_IN_CHANNELS); 956 if (!ctrl_in_channels) { 957 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__); 958 return -ENOSYS; 959 } 960 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) { 961 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels); 962 return -ENOSYS; 963 } 964 965 return 0; 966 } 967 968 /* API to configure AFE decoder in DSP */ 969 static bool configure_a2dp_decoder_format(int dec_format) 970 { 971 struct mixer_ctl *ctl_dec_data = NULL; 972 struct abr_dec_cfg_t dec_cfg; 973 int ret = 0; 974 975 if (a2dp.abr_config.is_abr_enabled) { 976 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_DEC_CONFIG_BLOCK); 977 if (!ctl_dec_data) { 978 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__); 979 return false; 980 } 981 memset(&dec_cfg, 0x0, sizeof(dec_cfg)); 982 dec_cfg.dec_format = dec_format; 983 dec_cfg.imc_info.direction = IMC_TRANSMIT; 984 dec_cfg.imc_info.enable = IMC_ENABLE; 985 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO; 986 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance; 987 988 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dec_cfg, 989 sizeof(dec_cfg)); 990 if (ret != 0) { 991 ALOGE("%s: Failed to set decoder config", __func__); 992 return false; 993 } 994 } 995 996 return true; 997 } 998 999 /* API to configure SBC DSP encoder */ 1000 static bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg) 1001 { 1002 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; 1003 struct sbc_enc_cfg_t sbc_dsp_cfg; 1004 bool is_configured = false; 1005 int ret = 0; 1006 1007 if (sbc_bt_cfg == NULL) { 1008 ALOGE("%s: Failed to get SBC encoder config from BT", __func__); 1009 return false; 1010 } 1011 1012 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); 1013 if (!ctl_enc_data) { 1014 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__); 1015 is_configured = false; 1016 goto exit; 1017 } 1018 memset(&sbc_dsp_cfg, 0x0, sizeof(sbc_dsp_cfg)); 1019 sbc_dsp_cfg.enc_format = ENC_MEDIA_FMT_SBC; 1020 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband; 1021 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len; 1022 switch (sbc_bt_cfg->channels) { 1023 case 0: 1024 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO; 1025 break; 1026 case 1: 1027 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO; 1028 break; 1029 case 3: 1030 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO; 1031 break; 1032 case 2: 1033 default: 1034 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO; 1035 break; 1036 } 1037 if (sbc_bt_cfg->alloc) 1038 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS; 1039 else 1040 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR; 1041 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate; 1042 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate; 1043 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg, 1044 sizeof(sbc_dsp_cfg)); 1045 if (ret != 0) { 1046 ALOGE("%s: failed to set SBC encoder config", __func__); 1047 is_configured = false; 1048 goto exit; 1049 } 1050 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample); 1051 if (ret != 0) { 1052 is_configured = false; 1053 goto exit; 1054 } 1055 is_configured = true; 1056 a2dp.bt_encoder_format = ENC_CODEC_TYPE_SBC; 1057 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate; 1058 1059 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO) 1060 a2dp.enc_channels = 1; 1061 else 1062 a2dp.enc_channels = 2; 1063 1064 ALOGV("%s: Successfully updated SBC enc format with sampling rate: %d channel mode:%d", 1065 __func__, sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode); 1066 exit: 1067 return is_configured; 1068 } 1069 1070 /* API to configure APTX DSP encoder */ 1071 static bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg) 1072 { 1073 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; 1074 int mixer_size; 1075 bool is_configured = false; 1076 int ret = 0; 1077 struct aptx_enc_cfg_t aptx_dsp_cfg; 1078 mixer_size = sizeof(aptx_dsp_cfg); 1079 1080 if (aptx_bt_cfg == NULL) { 1081 ALOGE("%s: Failed to get APTX encoder config from BT", __func__); 1082 return false; 1083 } 1084 1085 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); 1086 if (!ctl_enc_data) { 1087 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__); 1088 is_configured = false; 1089 goto exit; 1090 } 1091 1092 memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg)); 1093 aptx_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_APTX; 1094 1095 if (!a2dp.is_aptx_dual_mono_supported) { 1096 aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate; 1097 aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels; 1098 } else { 1099 aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate; 1100 aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels; 1101 aptx_dsp_cfg.aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode; 1102 } 1103 1104 switch (aptx_dsp_cfg.custom_cfg.num_channels) { 1105 case 1: 1106 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C; 1107 break; 1108 case 2: 1109 default: 1110 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L; 1111 aptx_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R; 1112 break; 1113 } 1114 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg, 1115 mixer_size); 1116 if (ret != 0) { 1117 ALOGE("%s: Failed to set APTX encoder config", __func__); 1118 is_configured = false; 1119 goto exit; 1120 } 1121 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample); 1122 if (ret != 0) { 1123 is_configured = false; 1124 goto exit; 1125 } 1126 is_configured = true; 1127 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX; 1128 a2dp.enc_channels = aptx_dsp_cfg.custom_cfg.num_channels; 1129 if (!a2dp.is_aptx_dual_mono_supported) { 1130 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate; 1131 ALOGV("%s: Successfully updated APTX enc format with sampling rate: %d \ 1132 channels:%d", __func__, aptx_dsp_cfg.custom_cfg.sample_rate, 1133 aptx_dsp_cfg.custom_cfg.num_channels); 1134 } else { 1135 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate; 1136 ALOGV("%s: Successfully updated APTX dual mono enc format with \ 1137 sampling rate: %d channels:%d sync mode %d", __func__, 1138 aptx_dsp_cfg.custom_cfg.sample_rate, 1139 aptx_dsp_cfg.custom_cfg.num_channels, 1140 aptx_dsp_cfg.aptx_v2_cfg.sync_mode); 1141 } 1142 1143 exit: 1144 return is_configured; 1145 } 1146 1147 /* API to configure APTX HD DSP encoder 1148 */ 1149 static bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg) 1150 { 1151 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; 1152 struct custom_enc_cfg_t aptx_dsp_cfg; 1153 bool is_configured = false; 1154 int ret = 0; 1155 1156 if (aptx_bt_cfg == NULL) { 1157 ALOGE("%s: Failed to get APTX HD encoder config from BT", __func__); 1158 return false; 1159 } 1160 1161 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); 1162 if (!ctl_enc_data) { 1163 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__); 1164 is_configured = false; 1165 goto exit; 1166 } 1167 1168 memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg)); 1169 aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX_HD; 1170 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate; 1171 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels; 1172 switch (aptx_dsp_cfg.num_channels) { 1173 case 1: 1174 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C; 1175 break; 1176 case 2: 1177 default: 1178 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L; 1179 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R; 1180 break; 1181 } 1182 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg, 1183 sizeof(aptx_dsp_cfg)); 1184 if (ret != 0) { 1185 ALOGE("%s: Failed to set APTX HD encoder config", __func__); 1186 is_configured = false; 1187 goto exit; 1188 } 1189 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample); 1190 if (ret != 0) { 1191 is_configured = false; 1192 goto exit; 1193 } 1194 is_configured = true; 1195 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX_HD; 1196 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate; 1197 a2dp.enc_channels = aptx_bt_cfg->channels; 1198 ALOGV("%s: Successfully updated APTX HD encformat with sampling rate: %d channels:%d", 1199 __func__, aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels); 1200 exit: 1201 return is_configured; 1202 } 1203 1204 /* API to configure AAC DSP encoder */ 1205 static bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg) 1206 { 1207 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; 1208 struct aac_enc_cfg_t aac_dsp_cfg; 1209 bool is_configured = false; 1210 int ret = 0; 1211 1212 if (aac_bt_cfg == NULL) { 1213 ALOGE("%s: Failed to get AAC encoder config from BT", __func__); 1214 return false; 1215 } 1216 1217 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); 1218 if (!ctl_enc_data) { 1219 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__); 1220 is_configured = false; 1221 goto exit; 1222 } 1223 memset(&aac_dsp_cfg, 0x0, sizeof(aac_dsp_cfg)); 1224 aac_dsp_cfg.aac_cfg.enc_format = ENC_MEDIA_FMT_AAC; 1225 aac_dsp_cfg.aac_cfg.bit_rate = aac_bt_cfg->bitrate; 1226 aac_dsp_cfg.aac_cfg.sample_rate = aac_bt_cfg->sampling_rate; 1227 switch (aac_bt_cfg->enc_mode) { 1228 case 0: 1229 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC; 1230 break; 1231 case 2: 1232 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS; 1233 break; 1234 case 1: 1235 default: 1236 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR; 1237 break; 1238 } 1239 aac_dsp_cfg.aac_cfg.aac_fmt_flag = aac_bt_cfg->format_flag; 1240 aac_dsp_cfg.aac_cfg.channel_cfg = aac_bt_cfg->channels; 1241 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type; 1242 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value; 1243 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg, 1244 sizeof(aac_dsp_cfg)); 1245 if (ret != 0) { 1246 ALOGE("%s: failed to set AAC encoder config", __func__); 1247 is_configured = false; 1248 goto exit; 1249 } 1250 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample); 1251 if (ret != 0) { 1252 is_configured = false; 1253 goto exit; 1254 } 1255 is_configured = true; 1256 a2dp.bt_encoder_format = ENC_CODEC_TYPE_AAC; 1257 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate; 1258 a2dp.enc_channels = aac_bt_cfg->channels; 1259 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d", 1260 __func__, aac_dsp_cfg.aac_cfg.sample_rate, aac_dsp_cfg.aac_cfg.channel_cfg); 1261 exit: 1262 return is_configured; 1263 } 1264 1265 static bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg) 1266 { 1267 struct mixer_ctl *ldac_enc_data = NULL, *ctrl_bit_format = NULL; 1268 struct ldac_enc_cfg_t ldac_dsp_cfg; 1269 bool is_configured = false; 1270 int ret = 0; 1271 1272 if (ldac_bt_cfg == NULL) { 1273 ALOGE("%s: Failed to get LDAC encoder config from BT", __func__); 1274 return false; 1275 } 1276 1277 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); 1278 if (!ldac_enc_data) { 1279 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__); 1280 is_configured = false; 1281 goto exit; 1282 } 1283 memset(&ldac_dsp_cfg, 0x0, sizeof(ldac_dsp_cfg)); 1284 1285 ldac_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_LDAC; 1286 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate; 1287 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode; 1288 switch (ldac_dsp_cfg.ldac_cfg.channel_mode) { 1289 case 4: 1290 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C; 1291 ldac_dsp_cfg.custom_cfg.num_channels = 1; 1292 break; 1293 case 2: 1294 case 1: 1295 default: 1296 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L; 1297 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R; 1298 ldac_dsp_cfg.custom_cfg.num_channels = 2; 1299 break; 1300 } 1301 1302 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(ldac_dsp_cfg); 1303 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu; 1304 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate; 1305 if (ldac_bt_cfg->is_abr_enabled) { 1306 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map; 1307 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE; 1308 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE; 1309 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO; 1310 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance; 1311 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled; 1312 } 1313 1314 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg, 1315 sizeof(ldac_dsp_cfg)); 1316 if (ret != 0) { 1317 ALOGE("%s: Failed to set LDAC encoder config", __func__); 1318 is_configured = false; 1319 goto exit; 1320 } 1321 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample); 1322 if (ret != 0) { 1323 is_configured = false; 1324 goto exit; 1325 } 1326 is_configured = true; 1327 a2dp.bt_encoder_format = ENC_CODEC_TYPE_LDAC; 1328 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate; 1329 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels; 1330 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled; 1331 ALOGV("%s: Successfully updated LDAC encformat with sampling rate: %d channels:%d", 1332 __func__, ldac_dsp_cfg.custom_cfg.sample_rate, 1333 ldac_dsp_cfg.custom_cfg.num_channels); 1334 exit: 1335 return is_configured; 1336 } 1337 1338 bool configure_a2dp_encoder_format() 1339 { 1340 void *codec_info = NULL; 1341 uint8_t multi_cast = 0, num_dev = 1; 1342 enc_codec_t codec_type = ENC_CODEC_TYPE_INVALID; 1343 bool is_configured = false; 1344 audio_aptx_encoder_config aptx_encoder_cfg; 1345 1346 if (!a2dp.audio_get_codec_config) { 1347 ALOGE("%s: A2DP handle is not identified, ignoring A2DP encoder config", __func__); 1348 return false; 1349 } 1350 ALOGD("%s: start", __func__); 1351 codec_info = a2dp.audio_get_codec_config(&multi_cast, &num_dev, 1352 &codec_type); 1353 1354 // ABR disabled by default for all codecs 1355 a2dp.abr_config.is_abr_enabled = false; 1356 1357 switch (codec_type) { 1358 case ENC_CODEC_TYPE_SBC: 1359 ALOGD("%s: Received SBC encoder supported Bluetooth device", __func__); 1360 is_configured = 1361 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info); 1362 break; 1363 case ENC_CODEC_TYPE_APTX: 1364 ALOGD("%s: Received APTX encoder supported Bluetooth device", __func__); 1365 a2dp.is_aptx_dual_mono_supported = false; 1366 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info; 1367 is_configured = 1368 configure_aptx_enc_format(&aptx_encoder_cfg); 1369 break; 1370 case ENC_CODEC_TYPE_APTX_HD: 1371 ALOGD("%s: Received APTX HD encoder supported Bluetooth device", __func__); 1372 is_configured = 1373 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info); 1374 break; 1375 case ENC_CODEC_TYPE_AAC: 1376 ALOGD("%s: Received AAC encoder supported Bluetooth device", __func__); 1377 is_configured = 1378 configure_aac_enc_format((audio_aac_encoder_config *)codec_info); 1379 break; 1380 case ENC_CODEC_TYPE_LDAC: 1381 ALOGD("%s: Received LDAC encoder supported Bluetooth device", __func__); 1382 if (!instance_id || instance_id > MAX_INSTANCE_ID) 1383 instance_id = MAX_INSTANCE_ID; 1384 a2dp.abr_config.imc_instance = instance_id--; 1385 is_configured = 1386 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) && 1387 configure_a2dp_decoder_format(ENC_CODEC_TYPE_LDAC)); 1388 break; 1389 case ENC_CODEC_TYPE_PCM: 1390 ALOGD("Received PCM format for BT device"); 1391 a2dp.bt_encoder_format = ENC_CODEC_TYPE_PCM; 1392 is_configured = true; 1393 break; 1394 default: 1395 ALOGD("%s: Received unsupported encoder format", __func__); 1396 is_configured = false; 1397 break; 1398 } 1399 return is_configured; 1400 } 1401 1402 int audio_extn_a2dp_start_playback() 1403 { 1404 int ret = 0; 1405 1406 ALOGD("%s: start", __func__); 1407 1408 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_start 1409 && a2dp.audio_get_codec_config)) { 1410 ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__); 1411 return -ENOSYS; 1412 } 1413 1414 if (a2dp.a2dp_suspended) { 1415 // session will be restarted after suspend completion 1416 ALOGD("%s: A2DP start requested during suspend state", __func__); 1417 return -ENOSYS; 1418 } 1419 1420 if (!a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) { 1421 ALOGD("%s: calling Bluetooth module stream start", __func__); 1422 /* This call indicates Bluetooth IPC lib to start playback */ 1423 ret = a2dp.audio_stream_start(); 1424 if (ret != 0 ) { 1425 ALOGE("%s: Bluetooth controller start failed", __func__); 1426 a2dp.a2dp_started = false; 1427 } else { 1428 if (configure_a2dp_encoder_format() == true) { 1429 a2dp.a2dp_started = true; 1430 ret = 0; 1431 ALOGD("%s: Start playback successful to Bluetooth IPC library", __func__); 1432 } else { 1433 ALOGD("%s: unable to configure DSP encoder", __func__); 1434 a2dp.a2dp_started = false; 1435 ret = -ETIMEDOUT; 1436 } 1437 } 1438 } 1439 1440 if (a2dp.a2dp_started) { 1441 a2dp.a2dp_total_active_session_request++; 1442 a2dp_check_and_set_scrambler(); 1443 a2dp_set_backend_cfg(); 1444 if (a2dp.abr_config.is_abr_enabled) 1445 start_abr(); 1446 } 1447 1448 ALOGD("%s: start A2DP playback total active sessions :%d", __func__, 1449 a2dp.a2dp_total_active_session_request); 1450 return ret; 1451 } 1452 1453 static int reset_a2dp_enc_config_params() 1454 { 1455 int ret = 0; 1456 1457 struct mixer_ctl *ctl_enc_config, *ctrl_bit_format; 1458 struct sbc_enc_cfg_t dummy_reset_config; 1459 1460 memset(&dummy_reset_config, 0x0, sizeof(dummy_reset_config)); 1461 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer, 1462 MIXER_ENC_CONFIG_BLOCK); 1463 if (!ctl_enc_config) { 1464 ALOGE("%s: ERROR A2DP encoder format mixer control not identifed", __func__); 1465 } else { 1466 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config, 1467 sizeof(dummy_reset_config)); 1468 a2dp.bt_encoder_format = ENC_MEDIA_FMT_NONE; 1469 } 1470 1471 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT); 1472 1473 return ret; 1474 } 1475 1476 static int reset_a2dp_dec_config_params() 1477 { 1478 struct mixer_ctl *ctl_dec_data = NULL; 1479 struct abr_dec_cfg_t dummy_reset_cfg; 1480 int ret = 0; 1481 1482 if (a2dp.abr_config.is_abr_enabled) { 1483 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_DEC_CONFIG_BLOCK); 1484 if (!ctl_dec_data) { 1485 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__); 1486 return -EINVAL; 1487 } 1488 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg)); 1489 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg, 1490 sizeof(dummy_reset_cfg)); 1491 if (ret != 0) { 1492 ALOGE("%s: Failed to set dummy decoder config", __func__); 1493 return ret; 1494 } 1495 } 1496 1497 return ret; 1498 } 1499 1500 static void reset_a2dp_config() { 1501 reset_a2dp_enc_config_params(); 1502 reset_a2dp_dec_config_params(); 1503 a2dp_reset_backend_cfg(); 1504 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started) 1505 stop_abr(); 1506 a2dp.abr_config.is_abr_enabled = false; 1507 } 1508 1509 int audio_extn_a2dp_stop_playback() 1510 { 1511 int ret = 0; 1512 1513 ALOGV("%s: stop", __func__); 1514 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_stop)) { 1515 ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__); 1516 return -ENOSYS; 1517 } 1518 1519 if (a2dp.a2dp_total_active_session_request > 0) 1520 a2dp.a2dp_total_active_session_request--; 1521 else 1522 ALOGE("%s: No active playback session requests on A2DP", __func__); 1523 1524 if (a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) { 1525 ALOGV("%s: calling Bluetooth module stream stop", __func__); 1526 ret = a2dp.audio_stream_stop(); 1527 if (ret < 0) 1528 ALOGE("%s: stop stream to Bluetooth IPC lib failed", __func__); 1529 else 1530 ALOGV("%s: stop steam to Bluetooth IPC lib successful", __func__); 1531 if (!a2dp.a2dp_suspended) 1532 reset_a2dp_config(); 1533 a2dp.a2dp_started = false; 1534 } 1535 ALOGD("%s: Stop A2DP playback total active sessions :%d", __func__, 1536 a2dp.a2dp_total_active_session_request); 1537 return 0; 1538 } 1539 1540 int audio_extn_a2dp_set_parameters(struct str_parms *parms, bool *reconfig) 1541 { 1542 int ret = 0, val; 1543 int status = 0; 1544 char value[32] = {0}; 1545 struct audio_usecase *uc_info; 1546 struct listnode *node; 1547 1548 if (a2dp.is_a2dp_offload_enabled == false) { 1549 ALOGV("%s: No supported encoders identified,ignoring A2DP setparam", __func__); 1550 status = -EINVAL; 1551 goto param_handled; 1552 } 1553 1554 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, 1555 sizeof(value)); 1556 if (ret >= 0) { 1557 val = atoi(value); 1558 if (audio_is_a2dp_out_device(val)) { 1559 ALOGV("%s: Received device connect request for A2DP", __func__); 1560 open_a2dp_output(); 1561 } 1562 goto param_handled; 1563 } 1564 1565 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, 1566 sizeof(value)); 1567 1568 if (ret >= 0) { 1569 val = atoi(value); 1570 if (audio_is_a2dp_out_device(val)) { 1571 ALOGV("%s: Received device disconnect request", __func__); 1572 reset_a2dp_config(); 1573 close_a2dp_output(); 1574 } 1575 goto param_handled; 1576 } 1577 1578 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value)); 1579 if (ret >= 0) { 1580 if (a2dp.bt_lib_handle && (a2dp.bt_state != A2DP_STATE_DISCONNECTED)) { 1581 if (strncmp(value, "true", sizeof(value)) == 0) { 1582 if (a2dp.a2dp_suspended) { 1583 ALOGD("%s: A2DP is already suspended", __func__); 1584 goto param_handled; 1585 } 1586 ALOGD("%s: Setting A2DP to suspend state", __func__); 1587 a2dp.a2dp_suspended = true; 1588 list_for_each(node, &a2dp.adev->usecase_list) { 1589 uc_info = node_to_item(node, struct audio_usecase, list); 1590 if (uc_info->type == PCM_PLAYBACK && 1591 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) { 1592 pthread_mutex_unlock(&a2dp.adev->lock); 1593 check_a2dp_restore(a2dp.adev, uc_info->stream.out, false); 1594 pthread_mutex_lock(&a2dp.adev->lock); 1595 } 1596 } 1597 reset_a2dp_config(); 1598 if (a2dp.audio_stream_suspend) { 1599 a2dp.audio_stream_suspend(); 1600 } 1601 } else { 1602 if (!a2dp.a2dp_suspended) { 1603 ALOGD("%s: A2DP is already unsuspended", __func__); 1604 goto param_handled; 1605 } 1606 ALOGD("%s: Resetting A2DP suspend state", __func__); 1607 struct audio_usecase *uc_info; 1608 struct listnode *node; 1609 if (a2dp.clear_a2dp_suspend_flag) { 1610 a2dp.clear_a2dp_suspend_flag(); 1611 } 1612 a2dp.a2dp_suspended = false; 1613 /* 1614 * It is possible that before suspend, A2DP sessions can be active. 1615 * For example, during music + voice activation concurrency, 1616 * A2DP suspend will be called & Bluetooth will change to SCO mode. 1617 * Though music is paused as a part of voice activation, 1618 * compress session close happens only after pause timeout(10 secs). 1619 * So, if resume request comes before pause timeout, as A2DP session 1620 * is already active, IPC start will not be called from APM/audio_hw. 1621 * Fix this by calling A2DP start for IPC library post suspend 1622 * based on number of active session count. 1623 */ 1624 if (a2dp.a2dp_total_active_session_request > 0) { 1625 ALOGD("%s: Calling Bluetooth IPC lib start post suspend state", __func__); 1626 if (a2dp.audio_stream_start) { 1627 status = a2dp.audio_stream_start(); 1628 if (status != 0) { 1629 ALOGE("%s: Bluetooth controller start failed", __func__); 1630 a2dp.a2dp_started = false; 1631 } else { 1632 if (!configure_a2dp_encoder_format()) { 1633 ALOGE("%s: Encoder params configuration failed post suspend", __func__); 1634 a2dp.a2dp_started = false; 1635 status = -ETIMEDOUT; 1636 } 1637 } 1638 } 1639 if (a2dp.a2dp_started) { 1640 a2dp_set_backend_cfg(); 1641 if (a2dp.abr_config.is_abr_enabled) 1642 start_abr(); 1643 } 1644 } 1645 list_for_each(node, &a2dp.adev->usecase_list) { 1646 uc_info = node_to_item(node, struct audio_usecase, list); 1647 if (uc_info->type == PCM_PLAYBACK && 1648 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) { 1649 pthread_mutex_unlock(&a2dp.adev->lock); 1650 check_a2dp_restore(a2dp.adev, uc_info->stream.out, true); 1651 pthread_mutex_lock(&a2dp.adev->lock); 1652 } 1653 } 1654 } 1655 } 1656 goto param_handled; 1657 } 1658 1659 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value, 1660 sizeof(value)); 1661 if (ret >= 0) { 1662 if (a2dp.is_a2dp_offload_enabled && 1663 a2dp.bt_state != A2DP_STATE_DISCONNECTED) { 1664 *reconfig = true; 1665 } 1666 goto param_handled; 1667 } 1668 1669 param_handled: 1670 ALOGV("%s: end of A2DP setparam", __func__); 1671 return status; 1672 } 1673 1674 void audio_extn_a2dp_set_handoff_mode(bool is_on) 1675 { 1676 a2dp.is_handoff_in_progress = is_on; 1677 } 1678 1679 bool audio_extn_a2dp_is_force_device_switch() 1680 { 1681 // During encoder reconfiguration mode, force A2DP device switch 1682 // Or if A2DP device is selected but earlier start failed as A2DP 1683 // was suspended, force retry. 1684 return a2dp.is_handoff_in_progress || !a2dp.a2dp_started; 1685 } 1686 1687 void audio_extn_a2dp_get_sample_rate(int *sample_rate) 1688 { 1689 *sample_rate = a2dp.enc_sampling_rate; 1690 } 1691 1692 bool audio_extn_a2dp_is_ready() 1693 { 1694 bool ret = false; 1695 1696 if (a2dp.a2dp_suspended) 1697 goto exit; 1698 1699 if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) && 1700 (a2dp.is_a2dp_offload_enabled) && 1701 (a2dp.audio_check_a2dp_ready)) 1702 ret = a2dp.audio_check_a2dp_ready(); 1703 1704 exit: 1705 return ret; 1706 } 1707 1708 bool audio_extn_a2dp_is_suspended() 1709 { 1710 return a2dp.a2dp_suspended; 1711 } 1712 1713 void audio_extn_a2dp_init(void *adev) 1714 { 1715 a2dp.adev = (struct audio_device*)adev; 1716 a2dp.bt_lib_handle = NULL; 1717 a2dp_common_init(); 1718 a2dp.enc_sampling_rate = 48000; 1719 a2dp.is_a2dp_offload_enabled = false; 1720 a2dp.is_handoff_in_progress = false; 1721 a2dp.is_aptx_dual_mono_supported = false; 1722 reset_a2dp_enc_config_params(); 1723 reset_a2dp_dec_config_params(); 1724 update_offload_codec_support(); 1725 } 1726 1727 uint32_t audio_extn_a2dp_get_encoder_latency() 1728 { 1729 uint32_t latency_ms = 0; 1730 int avsync_runtime_prop = 0; 1731 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0, 1732 aac_offset = 0, ldac_offset = 0; 1733 char value[PROPERTY_VALUE_MAX]; 1734 1735 memset(value, '\0', sizeof(char) * PROPERTY_VALUE_MAX); 1736 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL); 1737 if (avsync_runtime_prop > 0) { 1738 if (sscanf(value, "%d/%d/%d/%d/%d", 1739 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, 1740 &ldac_offset) != 5) { 1741 ALOGI("%s: Failed to parse avsync offset params from '%s'.", __func__, value); 1742 avsync_runtime_prop = 0; 1743 } 1744 } 1745 1746 uint32_t slatency_ms = 0; 1747 if (a2dp.audio_get_a2dp_sink_latency && a2dp.bt_state != A2DP_STATE_DISCONNECTED) { 1748 slatency_ms = a2dp.audio_get_a2dp_sink_latency(); 1749 } 1750 1751 switch (a2dp.bt_encoder_format) { 1752 case ENC_CODEC_TYPE_SBC: 1753 latency_ms = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC; 1754 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_SBC : slatency_ms; 1755 break; 1756 case ENC_CODEC_TYPE_APTX: 1757 latency_ms = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX; 1758 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_APTX : slatency_ms; 1759 break; 1760 case ENC_CODEC_TYPE_APTX_HD: 1761 latency_ms = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD; 1762 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency_ms; 1763 break; 1764 case ENC_CODEC_TYPE_AAC: 1765 latency_ms = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC; 1766 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_AAC : slatency_ms; 1767 break; 1768 case ENC_CODEC_TYPE_LDAC: 1769 latency_ms = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC; 1770 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency_ms; 1771 break; 1772 case ENC_CODEC_TYPE_PCM: 1773 latency_ms = ENCODER_LATENCY_PCM; 1774 latency_ms += DEFAULT_SINK_LATENCY_PCM; 1775 break; 1776 default: 1777 latency_ms = DEFAULT_ENCODER_LATENCY; 1778 break; 1779 } 1780 return latency_ms; 1781 } 1782 1783 int audio_extn_a2dp_get_parameters(struct str_parms *query, 1784 struct str_parms *reply) 1785 { 1786 int ret, val = 0; 1787 char value[32]={0}; 1788 1789 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, 1790 value, sizeof(value)); 1791 if (ret >= 0) { 1792 val = a2dp.is_a2dp_offload_enabled; 1793 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val); 1794 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val); 1795 } 1796 1797 return 0; 1798 } 1799 #endif // A2DP_OFFLOAD_ENABLED 1800