Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame^] | 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 <stdlib.h> |
| 24 | |
| 25 | #include <cutils/log.h> |
| 26 | #include <cutils/str_parms.h> |
| 27 | #include <cutils/properties.h> |
| 28 | #include <hardware/audio.h> |
| 29 | |
| 30 | #include "audio_hw.h" |
| 31 | #include "audio_extn.h" |
| 32 | #include "platform_api.h" |
| 33 | |
| 34 | #ifdef A2DP_OFFLOAD_ENABLED |
| 35 | #define AUDIO_PARAMETER_A2DP_STARTED "A2dpStarted" |
| 36 | #define BT_IPC_LIB_NAME "libbthost_if.so" |
| 37 | |
| 38 | // Media format definitions |
| 39 | #define ENC_MEDIA_FMT_AAC 0x00010DA6 |
| 40 | #define ENC_MEDIA_FMT_APTX 0x000131ff |
| 41 | #define ENC_MEDIA_FMT_APTX_HD 0x00013200 |
| 42 | #define ENC_MEDIA_FMT_LDAC 0x00013224 |
| 43 | #define ENC_MEDIA_FMT_SBC 0x00010BF2 |
| 44 | #define ENC_MEDIA_FMT_NONE 0 |
| 45 | #define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0 |
| 46 | #define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1 |
| 47 | #define MEDIA_FMT_AAC_AOT_LC 2 |
| 48 | #define MEDIA_FMT_AAC_AOT_SBR 5 |
| 49 | #define MEDIA_FMT_AAC_AOT_PS 29 |
| 50 | #define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1 |
| 51 | #define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2 |
| 52 | #define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8 |
| 53 | #define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9 |
| 54 | |
| 55 | // PCM channels |
| 56 | #define PCM_CHANNEL_L 1 |
| 57 | #define PCM_CHANNEL_R 2 |
| 58 | #define PCM_CHANNEL_C 3 |
| 59 | |
| 60 | // Mixer controls sent to ALSA |
| 61 | #define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config" |
| 62 | #define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format" |
| 63 | #define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode" |
| 64 | #define MIXER_SAMPLE_RATE "BT SampleRate" |
| 65 | #define MIXER_AFE_IN_CHANNELS "AFE Input Channels" |
| 66 | |
| 67 | // Encoder format strings |
| 68 | #define ENC_FMT_AAC "aac" |
| 69 | #define ENC_FMT_APTX "aptx" |
| 70 | #define ENC_FMT_APTXHD "aptxhd" |
| 71 | #define ENC_FMT_LDAC "ldac" |
| 72 | #define ENC_FMT_SBC "sbc" |
| 73 | |
| 74 | // System properties used for A2DP Offload |
| 75 | #define SYSPROP_A2DP_OFFLOAD_ENABLED "persist.vendor.bluetooth.a2dp_offload.enable" |
| 76 | #define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency" |
| 77 | |
| 78 | // Default encoder bit width |
| 79 | #define DEFAULT_ENCODER_BIT_FORMAT 16 |
| 80 | |
| 81 | // Default encoder latency |
| 82 | #define DEFAULT_ENCODER_LATENCY 200 |
| 83 | |
| 84 | // Encoder latency offset for codecs supported |
| 85 | #define ENCODER_LATENCY_AAC 70 |
| 86 | #define ENCODER_LATENCY_APTX 40 |
| 87 | #define ENCODER_LATENCY_APTX_HD 20 |
| 88 | #define ENCODER_LATENCY_LDAC 40 |
| 89 | #define ENCODER_LATENCY_SBC 10 |
| 90 | |
| 91 | // Default A2DP sink latency offset |
| 92 | #define DEFAULT_SINK_LATENCY_AAC 180 |
| 93 | #define DEFAULT_SINK_LATENCY_APTX 160 |
| 94 | #define DEFAULT_SINK_LATENCY_APTX_HD 180 |
| 95 | #define DEFAULT_SINK_LATENCY_LDAC 180 |
| 96 | #define DEFAULT_SINK_LATENCY_SBC 140 |
| 97 | |
| 98 | /* |
| 99 | * Below enum values are extended from audio-base.h to |
| 100 | * keep encoder codec type local to bthost_ipc |
| 101 | * and audio_hal as these are intended only for handshake |
| 102 | * between IPC lib and Audio HAL. |
| 103 | */ |
| 104 | typedef enum { |
| 105 | ENC_CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL |
| 106 | ENC_CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL |
| 107 | ENC_CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL |
| 108 | ENC_CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL |
| 109 | ENC_CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL |
| 110 | ENC_CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL |
| 111 | } enc_codec_t; |
| 112 | |
| 113 | typedef int (*audio_stream_open_t)(void); |
| 114 | typedef int (*audio_stream_close_t)(void); |
| 115 | typedef int (*audio_stream_start_t)(void); |
| 116 | typedef int (*audio_stream_stop_t)(void); |
| 117 | typedef int (*audio_stream_suspend_t)(void); |
| 118 | typedef void (*audio_handoff_triggered_t)(void); |
| 119 | typedef void (*clear_a2dp_suspend_flag_t)(void); |
| 120 | typedef void * (*audio_get_codec_config_t)(uint8_t *multicast_status, uint8_t *num_dev, |
| 121 | enc_codec_t *codec_type); |
| 122 | typedef int (*audio_check_a2dp_ready_t)(void); |
| 123 | typedef int (*audio_is_scrambling_enabled_t)(void); |
| 124 | |
| 125 | enum A2DP_STATE { |
| 126 | A2DP_STATE_CONNECTED, |
| 127 | A2DP_STATE_STARTED, |
| 128 | A2DP_STATE_STOPPED, |
| 129 | A2DP_STATE_DISCONNECTED, |
| 130 | }; |
| 131 | |
| 132 | /* Data structure used to: |
| 133 | * - Update the A2DP state machine |
| 134 | * - Communicate with the libbthost_if.so IPC library |
| 135 | * - Store DSP encoder configuration information |
| 136 | */ |
| 137 | struct a2dp_data { |
| 138 | /* Audio device handle */ |
| 139 | struct audio_device *adev; |
| 140 | /* Bluetooth IPC library handle */ |
| 141 | void *bt_lib_handle; |
| 142 | /* Open A2DP audio stream. Initialize audio datapath */ |
| 143 | audio_stream_open_t audio_stream_open; |
| 144 | /* Close A2DP audio stream */ |
| 145 | audio_stream_close_t audio_stream_close; |
| 146 | /* Start A2DP audio stream. Start audio datapath */ |
| 147 | audio_stream_start_t audio_stream_start; |
| 148 | /* Stop A2DP audio stream */ |
| 149 | audio_stream_stop_t audio_stream_stop; |
| 150 | /* Suspend A2DP audio stream */ |
| 151 | audio_stream_suspend_t audio_stream_suspend; |
| 152 | /* Notify Bluetooth IPC library of handoff being triggered */ |
| 153 | audio_handoff_triggered_t audio_handoff_triggered; |
| 154 | /* Clear A2DP suspend flag in Bluetooth IPC library */ |
| 155 | clear_a2dp_suspend_flag_t clear_a2dp_suspend_flag; |
| 156 | /* Get codec configuration from Bluetooth stack via |
| 157 | * Bluetooth IPC library */ |
| 158 | audio_get_codec_config_t audio_get_codec_config; |
| 159 | /* Check if A2DP is ready */ |
| 160 | audio_check_a2dp_ready_t audio_check_a2dp_ready; |
| 161 | /* Check if scrambling is enabled on BTSoC */ |
| 162 | audio_is_scrambling_enabled_t audio_is_scrambling_enabled; |
| 163 | /* Internal A2DP state identifier */ |
| 164 | enum A2DP_STATE bt_state; |
| 165 | /* A2DP codec type configured */ |
| 166 | enc_codec_t bt_encoder_format; |
| 167 | /* Sampling rate configured with A2DP encoder on DSP */ |
| 168 | uint32_t enc_sampling_rate; |
| 169 | /* Channel configuration of A2DP on DSP */ |
| 170 | uint32_t enc_channels; |
| 171 | /* Flag to denote whether A2DP audio datapath has started */ |
| 172 | bool a2dp_started; |
| 173 | /* Flag to denote whether A2DP audio datapath is suspended */ |
| 174 | bool a2dp_suspended; |
| 175 | /* Number of active sessions on A2DP output */ |
| 176 | int a2dp_total_active_session_request; |
| 177 | /* Flag to denote whether A2DP offload is supported */ |
| 178 | bool is_a2dp_offload_supported; |
| 179 | /* Flag to denote whether codec reconfiguration/soft handoff is in progress */ |
| 180 | bool is_handoff_in_progress; |
| 181 | /* Flag to denote whether APTX Dual Mono encoder is supported */ |
| 182 | bool is_aptx_dual_mono_supported; |
| 183 | }; |
| 184 | |
| 185 | struct a2dp_data a2dp; |
| 186 | |
| 187 | /* START of DSP configurable structures |
| 188 | * These values should match with DSP interface defintion |
| 189 | */ |
| 190 | |
| 191 | /* AAC encoder configuration structure. */ |
| 192 | typedef struct aac_enc_cfg_t aac_enc_cfg_t; |
| 193 | |
| 194 | struct aac_enc_cfg_t { |
| 195 | /* Encoder media format for AAC */ |
| 196 | uint32_t enc_format; |
| 197 | |
| 198 | /* Encoding rate in bits per second */ |
| 199 | uint32_t bit_rate; |
| 200 | |
| 201 | /* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS */ |
| 202 | uint32_t enc_mode; |
| 203 | |
| 204 | /* supported aac_fmt_flag are ADTS/RAW */ |
| 205 | uint16_t aac_fmt_flag; |
| 206 | |
| 207 | /* supported channel_cfg are Native mode, Mono , Stereo */ |
| 208 | uint16_t channel_cfg; |
| 209 | |
| 210 | /* Number of samples per second */ |
| 211 | uint32_t sample_rate; |
| 212 | } __attribute__ ((packed)); |
| 213 | |
| 214 | /* SBC encoder configuration structure. */ |
| 215 | typedef struct sbc_enc_cfg_t sbc_enc_cfg_t; |
| 216 | |
| 217 | struct sbc_enc_cfg_t { |
| 218 | /* Encoder media format for SBC */ |
| 219 | uint32_t enc_format; |
| 220 | |
| 221 | /* supported num_subbands are 4/8 */ |
| 222 | uint32_t num_subbands; |
| 223 | |
| 224 | /* supported blk_len are 4, 8, 12, 16 */ |
| 225 | uint32_t blk_len; |
| 226 | |
| 227 | /* supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO */ |
| 228 | uint32_t channel_mode; |
| 229 | |
| 230 | /* supported alloc_method are LOUNDNESS/SNR */ |
| 231 | uint32_t alloc_method; |
| 232 | |
| 233 | /* supported bit_rate for mono channel is max 320kbps |
| 234 | * supported bit rate for stereo channel is max 512 kbps */ |
| 235 | uint32_t bit_rate; |
| 236 | |
| 237 | /* Number of samples per second */ |
| 238 | uint32_t sample_rate; |
| 239 | } __attribute__ ((packed)); |
| 240 | |
| 241 | struct custom_enc_cfg_t { |
| 242 | /* Custom encoder media format */ |
| 243 | uint32_t enc_format; |
| 244 | |
| 245 | /* Number of samples per second */ |
| 246 | uint32_t sample_rate; |
| 247 | |
| 248 | /* supported num_channels are Mono/Stereo */ |
| 249 | uint16_t num_channels; |
| 250 | |
| 251 | /* Reserved for future enhancement */ |
| 252 | uint16_t reserved; |
| 253 | |
| 254 | /* supported channel_mapping for mono is CHANNEL_C |
| 255 | * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R */ |
| 256 | uint8_t channel_mapping[8]; |
| 257 | |
| 258 | /* Reserved for future enhancement */ |
| 259 | uint32_t custom_size; |
| 260 | } __attribute__ ((packed)); |
| 261 | |
| 262 | struct aptx_v2_enc_cfg_ext_t { |
| 263 | /* sync_mode introduced with APTX V2 libraries |
| 264 | * sync mode: 0x0 = stereo sync mode |
| 265 | * 0x01 = dual mono sync mode |
| 266 | * 0x02 = dual mono with no sync on either L or R codewords |
| 267 | */ |
| 268 | uint32_t sync_mode; |
| 269 | } __attribute__ ((packed)); |
| 270 | |
| 271 | /* APTX struct for combining custom enc and V2 members */ |
| 272 | struct aptx_enc_cfg_t { |
| 273 | struct custom_enc_cfg_t custom_cfg; |
| 274 | struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg; |
| 275 | } __attribute__ ((packed)); |
| 276 | |
| 277 | struct ldac_specific_enc_cfg_t { |
| 278 | /* |
| 279 | * This is used to calculate the encoder output |
| 280 | * bytes per frame (i.e. bytes per packet). |
| 281 | * Bit rate also configures the EQMID. |
| 282 | * The min bit rate 303000 bps is calculated for |
| 283 | * 44.1 kHz and 88.2 KHz sampling frequencies with |
| 284 | * Mobile use Quality. |
| 285 | * The max bit rate of 990000 bps is calculated for |
| 286 | * 96kHz and 48 KHz with High Quality |
| 287 | * @Range(in bits per second) |
| 288 | * 303000 for Mobile use Quality |
| 289 | * 606000 for standard Quality |
| 290 | * 909000 for High Quality |
| 291 | */ |
| 292 | uint32_t bit_rate; |
| 293 | |
| 294 | /* |
| 295 | * The channel setting information for LDAC specification |
| 296 | * of Bluetooth A2DP which is determined by SRC and SNK |
| 297 | * devices in Bluetooth transmission. |
| 298 | * @Range: |
| 299 | * 0 for native mode |
| 300 | * 4 for mono |
| 301 | * 2 for dual channel |
| 302 | * 1 for stereo |
| 303 | */ |
| 304 | uint16_t channel_mode; |
| 305 | |
| 306 | /* |
| 307 | * Maximum Transmission Unit (MTU). |
| 308 | * The minimum MTU that a L2CAP implementation for LDAC shall |
| 309 | * support is 679 bytes, because LDAC is optimized with 2-DH5 |
| 310 | * packet as its target. |
| 311 | * @Range : 679 |
| 312 | * @Default: 679 for LDACBT_MTU_2DH5 |
| 313 | */ |
| 314 | uint16_t mtu; |
| 315 | } __attribute__ ((packed)); |
| 316 | |
| 317 | /* LDAC struct for combining custom enc and standard members */ |
| 318 | struct ldac_enc_cfg_t { |
| 319 | struct custom_enc_cfg_t custom_cfg; |
| 320 | struct ldac_specific_enc_cfg_t ldac_cfg; |
| 321 | } __attribute__ ((packed)); |
| 322 | |
| 323 | /* Information about Bluetooth SBC encoder configuration |
| 324 | * This data is used between audio HAL module and |
| 325 | * Bluetooth IPC library to configure DSP encoder |
| 326 | */ |
| 327 | typedef struct { |
| 328 | uint32_t subband; /* 4, 8 */ |
| 329 | uint32_t blk_len; /* 4, 8, 12, 16 */ |
| 330 | uint16_t sampling_rate; /* 44.1khz, 48khz */ |
| 331 | uint8_t channels; /* 0(Mono), 1(Dual_mono), 2(Stereo), 3(JS) */ |
| 332 | uint8_t alloc; /* 0(Loudness), 1(SNR) */ |
| 333 | uint8_t min_bitpool; /* 2 */ |
| 334 | uint8_t max_bitpool; /* 53(44.1khz), 51 (48khz) */ |
| 335 | uint32_t bitrate; /* 320kbps to 512kbps */ |
| 336 | uint32_t bits_per_sample; /* 16 bit */ |
| 337 | } audio_sbc_encoder_config; |
| 338 | |
| 339 | /* Information about Bluetooth APTX encoder configuration |
| 340 | * This data is used between audio HAL module and |
| 341 | * Bluetooth IPC library to configure DSP encoder |
| 342 | */ |
| 343 | typedef struct { |
| 344 | uint16_t sampling_rate; |
| 345 | uint8_t channels; |
| 346 | uint32_t bitrate; |
| 347 | uint32_t bits_per_sample; |
| 348 | } audio_aptx_default_config; |
| 349 | |
| 350 | typedef struct { |
| 351 | uint16_t sampling_rate; |
| 352 | uint8_t channels; |
| 353 | uint32_t bitrate; |
| 354 | uint32_t sync_mode; |
| 355 | } audio_aptx_dual_mono_config; |
| 356 | |
| 357 | typedef union { |
| 358 | audio_aptx_default_config *default_cfg; |
| 359 | audio_aptx_dual_mono_config *dual_mono_cfg; |
| 360 | } audio_aptx_encoder_config; |
| 361 | |
| 362 | /* Information about Bluetooth AAC encoder configuration |
| 363 | * This data is used between audio HAL module and |
| 364 | * Bluetooth IPC library to configure DSP encoder |
| 365 | */ |
| 366 | typedef struct { |
| 367 | uint32_t enc_mode; /* LC, SBR, PS */ |
| 368 | uint16_t format_flag; /* RAW, ADTS */ |
| 369 | uint16_t channels; /* 1-Mono, 2-Stereo */ |
| 370 | uint32_t sampling_rate; |
| 371 | uint32_t bitrate; |
| 372 | uint32_t bits_per_sample; |
| 373 | } audio_aac_encoder_config; |
| 374 | |
| 375 | /* Information about Bluetooth LDAC encoder configuration |
| 376 | * This data is used between audio HAL module and |
| 377 | * Bluetooth IPC library to configure DSP encoder |
| 378 | */ |
| 379 | typedef struct { |
| 380 | uint32_t sampling_rate; /* 44100, 48000, 88200, 96000 */ |
| 381 | uint32_t bit_rate; /* 303000, 606000, 909000 (in bits per second) */ |
| 382 | uint16_t channel_mode; /* 0, 4, 2, 1 */ |
| 383 | uint16_t mtu; |
| 384 | uint32_t bits_per_sample; /* 16, 24, 32 (bits) */ |
| 385 | } audio_ldac_encoder_config; |
| 386 | |
| 387 | /*********** END of DSP configurable structures ********************/ |
| 388 | |
| 389 | static void a2dp_common_init() |
| 390 | { |
| 391 | a2dp.a2dp_started = false; |
| 392 | a2dp.a2dp_total_active_session_request = 0; |
| 393 | a2dp.a2dp_suspended = false; |
| 394 | a2dp.bt_encoder_format = ENC_CODEC_TYPE_INVALID; |
| 395 | a2dp.bt_state = A2DP_STATE_DISCONNECTED; |
| 396 | } |
| 397 | |
| 398 | static void update_offload_codec_support() |
| 399 | { |
| 400 | char value[PROPERTY_VALUE_MAX] = {'\0'}; |
| 401 | |
| 402 | a2dp.is_a2dp_offload_supported = |
| 403 | property_get_bool(SYSPROP_A2DP_OFFLOAD_ENABLED, false); |
| 404 | ALOGD("%s: A2DP offload supported = %s", __func__, value); |
| 405 | } |
| 406 | |
| 407 | /* API to open Bluetooth IPC library to start IPC communication */ |
| 408 | static int open_a2dp_output() |
| 409 | { |
| 410 | int ret = 0; |
| 411 | |
| 412 | ALOGD("%s: Open A2DP output start", __func__); |
| 413 | |
| 414 | if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) { |
| 415 | ALOGD("%s: Called A2DP open with improper state, Ignoring request state %d", |
| 416 | __func__, a2dp.bt_state); |
| 417 | return -ENOSYS; |
| 418 | } |
| 419 | |
| 420 | if (a2dp.bt_lib_handle == NULL) { |
| 421 | ALOGD("%s: Requesting for Bluetooth IPC lib handle", __func__); |
| 422 | a2dp.bt_lib_handle = dlopen(BT_IPC_LIB_NAME, RTLD_NOW); |
| 423 | |
| 424 | if (a2dp.bt_lib_handle == NULL) { |
| 425 | ret = -errno; |
| 426 | ALOGE("%s: DLOPEN failed for %s errno %d strerror %s", __func__, |
| 427 | BT_IPC_LIB_NAME, errno, strerror(errno)); |
| 428 | a2dp.bt_state = A2DP_STATE_DISCONNECTED; |
| 429 | return ret; |
| 430 | } else { |
| 431 | a2dp.audio_stream_open = (audio_stream_open_t) |
| 432 | dlsym(a2dp.bt_lib_handle, "audio_stream_open"); |
| 433 | a2dp.audio_stream_start = (audio_stream_start_t) |
| 434 | dlsym(a2dp.bt_lib_handle, "audio_stream_start"); |
| 435 | a2dp.audio_get_codec_config = (audio_get_codec_config_t) |
| 436 | dlsym(a2dp.bt_lib_handle, "audio_get_codec_config"); |
| 437 | a2dp.audio_stream_suspend = (audio_stream_suspend_t) |
| 438 | dlsym(a2dp.bt_lib_handle, "audio_stream_suspend"); |
| 439 | a2dp.audio_handoff_triggered = (audio_handoff_triggered_t) |
| 440 | dlsym(a2dp.bt_lib_handle, "audio_handoff_triggered"); |
| 441 | a2dp.clear_a2dp_suspend_flag = (clear_a2dp_suspend_flag_t) |
| 442 | dlsym(a2dp.bt_lib_handle, "clear_a2dp_suspend_flag"); |
| 443 | a2dp.audio_stream_stop = (audio_stream_stop_t) |
| 444 | dlsym(a2dp.bt_lib_handle, "audio_stream_stop"); |
| 445 | a2dp.audio_stream_close = (audio_stream_close_t) |
| 446 | dlsym(a2dp.bt_lib_handle, "audio_stream_close"); |
| 447 | a2dp.audio_check_a2dp_ready = (audio_check_a2dp_ready_t) |
| 448 | dlsym(a2dp.bt_lib_handle,"audio_check_a2dp_ready"); |
| 449 | a2dp.audio_is_scrambling_enabled = (audio_is_scrambling_enabled_t) |
| 450 | dlsym(a2dp.bt_lib_handle,"audio_is_scrambling_enabled"); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | if (a2dp.bt_lib_handle && a2dp.audio_stream_open) { |
| 455 | ALOGD("%s: calling Bluetooth stream open", __func__); |
| 456 | ret = a2dp.audio_stream_open(); |
| 457 | if (ret != 0) { |
| 458 | ALOGE("%s: Failed to open output stream for A2DP: status %d", __func__, ret); |
| 459 | dlclose(a2dp.bt_lib_handle); |
| 460 | a2dp.bt_lib_handle = NULL; |
| 461 | a2dp.bt_state = A2DP_STATE_DISCONNECTED; |
| 462 | return ret; |
| 463 | } |
| 464 | a2dp.bt_state = A2DP_STATE_CONNECTED; |
| 465 | } else { |
| 466 | ALOGE("%s: A2DP handle is not identified, Ignoring open request", __func__); |
| 467 | a2dp.bt_state = A2DP_STATE_DISCONNECTED; |
| 468 | return -ENOSYS; |
| 469 | } |
| 470 | |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | static int close_a2dp_output() |
| 475 | { |
| 476 | ALOGV("%s\n",__func__); |
| 477 | if (!(a2dp.bt_lib_handle && a2dp.audio_stream_close)) { |
| 478 | ALOGE("%s: A2DP handle is not identified, Ignoring close request", __func__); |
| 479 | return -ENOSYS; |
| 480 | } |
| 481 | if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) { |
| 482 | ALOGD("%s: calling Bluetooth stream close", __func__); |
| 483 | if (a2dp.audio_stream_close() == false) |
| 484 | ALOGE("%s: failed close A2DP control path from Bluetooth IPC library", __func__); |
| 485 | } |
| 486 | a2dp_common_init(); |
| 487 | a2dp.enc_sampling_rate = 0; |
| 488 | a2dp.enc_channels = 0; |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | static int a2dp_check_and_set_scrambler() |
| 494 | { |
| 495 | bool scrambler_mode = false; |
| 496 | struct mixer_ctl *ctrl_scrambler_mode = NULL; |
| 497 | int ret = 0; |
| 498 | if (a2dp.audio_is_scrambling_enabled && (a2dp.bt_state != A2DP_STATE_DISCONNECTED)) |
| 499 | scrambler_mode = a2dp.audio_is_scrambling_enabled(); |
| 500 | |
| 501 | // Scrambling needs to be enabled in DSP if scrambler mode is set |
| 502 | // disable scrambling not required |
| 503 | if (scrambler_mode) { |
| 504 | // enable scrambler in dsp |
| 505 | ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 506 | MIXER_SCRAMBLER_MODE); |
| 507 | if (!ctrl_scrambler_mode) { |
| 508 | ALOGE("%s: ERROR scrambler mode mixer control not identifed", __func__); |
| 509 | return -ENOSYS; |
| 510 | } else { |
| 511 | ret = mixer_ctl_set_value(ctrl_scrambler_mode, 0, true); |
| 512 | if (ret != 0) { |
| 513 | ALOGE("%s: Could not set scrambler mode", __func__); |
| 514 | return ret; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | static void a2dp_set_backend_cfg() |
| 522 | { |
| 523 | const char *rate_str = NULL, *in_channels = NULL; |
| 524 | uint32_t sampling_rate = a2dp.enc_sampling_rate; |
| 525 | struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL; |
| 526 | |
| 527 | // For LDAC encoder open slimbus port at 96Khz for 48Khz input |
| 528 | // and 88.2Khz for 44.1Khz input. |
| 529 | if ((a2dp.bt_encoder_format == ENC_CODEC_TYPE_LDAC) && |
| 530 | (sampling_rate == 48000 || sampling_rate == 44100 )) { |
| 531 | sampling_rate = sampling_rate * 2; |
| 532 | } |
| 533 | // Configure backend sampling rate |
| 534 | switch (sampling_rate) { |
| 535 | case 44100: |
| 536 | rate_str = "KHZ_44P1"; |
| 537 | break; |
| 538 | case 48000: |
| 539 | rate_str = "KHZ_48"; |
| 540 | break; |
| 541 | case 88200: |
| 542 | rate_str = "KHZ_88P2"; |
| 543 | break; |
| 544 | case 96000: |
| 545 | rate_str = "KHZ_96"; |
| 546 | break; |
| 547 | default: |
| 548 | rate_str = "KHZ_48"; |
| 549 | break; |
| 550 | } |
| 551 | |
| 552 | ALOGD("%s: set backend sample rate =%s", __func__, rate_str); |
| 553 | ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 554 | MIXER_SAMPLE_RATE); |
| 555 | if (!ctl_sample_rate) { |
| 556 | ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__); |
| 557 | return; |
| 558 | } else { |
| 559 | if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) { |
| 560 | ALOGE("%s: Failed to set backend sample rate =%s", __func__, rate_str); |
| 561 | return; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | // Configure AFE input channels |
| 566 | switch (a2dp.enc_channels) { |
| 567 | case 1: |
| 568 | in_channels = "One"; |
| 569 | break; |
| 570 | case 2: |
| 571 | default: |
| 572 | in_channels = "Two"; |
| 573 | break; |
| 574 | } |
| 575 | |
| 576 | ALOGD("%s: set AFE input channels =%d", __func__, a2dp.enc_channels); |
| 577 | ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 578 | MIXER_AFE_IN_CHANNELS); |
| 579 | if (!ctrl_in_channels) { |
| 580 | ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__); |
| 581 | return; |
| 582 | } else { |
| 583 | if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) { |
| 584 | ALOGE("%s: Failed to set AFE in channels =%d", __func__, a2dp.enc_channels); |
| 585 | return; |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | static int a2dp_set_bit_format(uint32_t enc_bit_format) |
| 591 | { |
| 592 | const char *bit_format = NULL; |
| 593 | struct mixer_ctl *ctrl_bit_format = NULL; |
| 594 | |
| 595 | // Configure AFE Input Bit Format |
| 596 | switch (enc_bit_format) { |
| 597 | case 32: |
| 598 | bit_format = "S32_LE"; |
| 599 | break; |
| 600 | case 24: |
| 601 | bit_format = "S24_LE"; |
| 602 | break; |
| 603 | case 16: |
| 604 | default: |
| 605 | bit_format = "S16_LE"; |
| 606 | break; |
| 607 | } |
| 608 | |
| 609 | ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format); |
| 610 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 611 | MIXER_ENC_BIT_FORMAT); |
| 612 | if (!ctrl_bit_format) { |
| 613 | ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__); |
| 614 | return -ENOSYS; |
| 615 | } |
| 616 | if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) { |
| 617 | ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format); |
| 618 | return -ENOSYS; |
| 619 | } |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | static void a2dp_reset_backend_cfg() |
| 624 | { |
| 625 | const char *rate_str = "KHZ_8", *in_channels = "Zero"; |
| 626 | struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL; |
| 627 | |
| 628 | // Reset backend sampling rate |
| 629 | ALOGD("%s: reset backend sample rate = %s", __func__, rate_str); |
| 630 | ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 631 | MIXER_SAMPLE_RATE); |
| 632 | if (!ctl_sample_rate) { |
| 633 | ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__); |
| 634 | return; |
| 635 | } else { |
| 636 | if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) { |
| 637 | ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str); |
| 638 | return; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | // Reset AFE input channels |
| 643 | ALOGD("%s: reset AFE input channels = %s", __func__, in_channels); |
| 644 | ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 645 | MIXER_AFE_IN_CHANNELS); |
| 646 | if (!ctrl_in_channels) { |
| 647 | ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__); |
| 648 | return; |
| 649 | } else { |
| 650 | if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) { |
| 651 | ALOGE("%s: Failed to reset AFE in channels =%d", __func__, a2dp.enc_channels); |
| 652 | return; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /* API to configure SBC DSP encoder */ |
| 658 | static bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg) |
| 659 | { |
| 660 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
| 661 | struct sbc_enc_cfg_t sbc_dsp_cfg; |
| 662 | bool is_configured = false; |
| 663 | int ret = 0; |
| 664 | |
| 665 | if (sbc_bt_cfg == NULL) { |
| 666 | ALOGE("%s: Failed to get SBC encoder config from BT", __func__); |
| 667 | return false; |
| 668 | } |
| 669 | |
| 670 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 671 | if (!ctl_enc_data) { |
| 672 | ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__); |
| 673 | is_configured = false; |
| 674 | goto exit; |
| 675 | } |
| 676 | memset(&sbc_dsp_cfg, 0x0, sizeof(sbc_dsp_cfg)); |
| 677 | sbc_dsp_cfg.enc_format = ENC_MEDIA_FMT_SBC; |
| 678 | sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband; |
| 679 | sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len; |
| 680 | switch (sbc_bt_cfg->channels) { |
| 681 | case 0: |
| 682 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO; |
| 683 | break; |
| 684 | case 1: |
| 685 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO; |
| 686 | break; |
| 687 | case 3: |
| 688 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO; |
| 689 | break; |
| 690 | case 2: |
| 691 | default: |
| 692 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO; |
| 693 | break; |
| 694 | } |
| 695 | if (sbc_bt_cfg->alloc) |
| 696 | sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS; |
| 697 | else |
| 698 | sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR; |
| 699 | sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate; |
| 700 | sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate; |
| 701 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg, |
| 702 | sizeof(sbc_dsp_cfg)); |
| 703 | if (ret != 0) { |
| 704 | ALOGE("%s: failed to set SBC encoder config", __func__); |
| 705 | is_configured = false; |
| 706 | goto exit; |
| 707 | } |
| 708 | ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample); |
| 709 | if (ret != 0) { |
| 710 | is_configured = false; |
| 711 | goto exit; |
| 712 | } |
| 713 | is_configured = true; |
| 714 | a2dp.bt_encoder_format = ENC_CODEC_TYPE_SBC; |
| 715 | a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate; |
| 716 | |
| 717 | if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO) |
| 718 | a2dp.enc_channels = 1; |
| 719 | else |
| 720 | a2dp.enc_channels = 2; |
| 721 | |
| 722 | ALOGV("%s: Successfully updated SBC enc format with sampling rate: %d channel mode:%d", |
| 723 | __func__, sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode); |
| 724 | exit: |
| 725 | return is_configured; |
| 726 | } |
| 727 | |
| 728 | /* API to configure APTX DSP encoder */ |
| 729 | static bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg) |
| 730 | { |
| 731 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
| 732 | int mixer_size; |
| 733 | bool is_configured = false; |
| 734 | int ret = 0; |
| 735 | struct aptx_enc_cfg_t aptx_dsp_cfg; |
| 736 | mixer_size = sizeof(aptx_dsp_cfg); |
| 737 | |
| 738 | if (aptx_bt_cfg == NULL) { |
| 739 | ALOGE("%s: Failed to get APTX encoder config from BT", __func__); |
| 740 | return false; |
| 741 | } |
| 742 | |
| 743 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 744 | if (!ctl_enc_data) { |
| 745 | ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__); |
| 746 | is_configured = false; |
| 747 | goto exit; |
| 748 | } |
| 749 | |
| 750 | memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg)); |
| 751 | aptx_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_APTX; |
| 752 | |
| 753 | if (!a2dp.is_aptx_dual_mono_supported) { |
| 754 | aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate; |
| 755 | aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels; |
| 756 | } else { |
| 757 | aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate; |
| 758 | aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels; |
| 759 | aptx_dsp_cfg.aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode; |
| 760 | } |
| 761 | |
| 762 | switch (aptx_dsp_cfg.custom_cfg.num_channels) { |
| 763 | case 1: |
| 764 | aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C; |
| 765 | break; |
| 766 | case 2: |
| 767 | default: |
| 768 | aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L; |
| 769 | aptx_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R; |
| 770 | break; |
| 771 | } |
| 772 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg, |
| 773 | mixer_size); |
| 774 | if (ret != 0) { |
| 775 | ALOGE("%s: Failed to set APTX encoder config", __func__); |
| 776 | is_configured = false; |
| 777 | goto exit; |
| 778 | } |
| 779 | ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample); |
| 780 | if (ret != 0) { |
| 781 | is_configured = false; |
| 782 | goto exit; |
| 783 | } |
| 784 | is_configured = true; |
| 785 | a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX; |
| 786 | a2dp.enc_channels = aptx_dsp_cfg.custom_cfg.num_channels; |
| 787 | if (!a2dp.is_aptx_dual_mono_supported) { |
| 788 | a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate; |
| 789 | ALOGV("%s: Successfully updated APTX enc format with sampling rate: %d \ |
| 790 | channels:%d", __func__, aptx_dsp_cfg.custom_cfg.sample_rate, |
| 791 | aptx_dsp_cfg.custom_cfg.num_channels); |
| 792 | } else { |
| 793 | a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate; |
| 794 | ALOGV("%s: Successfully updated APTX dual mono enc format with \ |
| 795 | sampling rate: %d channels:%d sync mode %d", __func__, |
| 796 | aptx_dsp_cfg.custom_cfg.sample_rate, |
| 797 | aptx_dsp_cfg.custom_cfg.num_channels, |
| 798 | aptx_dsp_cfg.aptx_v2_cfg.sync_mode); |
| 799 | } |
| 800 | |
| 801 | exit: |
| 802 | return is_configured; |
| 803 | } |
| 804 | |
| 805 | /* API to configure APTX HD DSP encoder |
| 806 | */ |
| 807 | static bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg) |
| 808 | { |
| 809 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
| 810 | struct custom_enc_cfg_t aptx_dsp_cfg; |
| 811 | bool is_configured = false; |
| 812 | int ret = 0; |
| 813 | |
| 814 | if (aptx_bt_cfg == NULL) { |
| 815 | ALOGE("%s: Failed to get APTX HD encoder config from BT", __func__); |
| 816 | return false; |
| 817 | } |
| 818 | |
| 819 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 820 | if (!ctl_enc_data) { |
| 821 | ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__); |
| 822 | is_configured = false; |
| 823 | goto exit; |
| 824 | } |
| 825 | |
| 826 | memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg)); |
| 827 | aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX_HD; |
| 828 | aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate; |
| 829 | aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels; |
| 830 | switch (aptx_dsp_cfg.num_channels) { |
| 831 | case 1: |
| 832 | aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C; |
| 833 | break; |
| 834 | case 2: |
| 835 | default: |
| 836 | aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L; |
| 837 | aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R; |
| 838 | break; |
| 839 | } |
| 840 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg, |
| 841 | sizeof(aptx_dsp_cfg)); |
| 842 | if (ret != 0) { |
| 843 | ALOGE("%s: Failed to set APTX HD encoder config", __func__); |
| 844 | is_configured = false; |
| 845 | goto exit; |
| 846 | } |
| 847 | ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample); |
| 848 | if (ret != 0) { |
| 849 | is_configured = false; |
| 850 | goto exit; |
| 851 | } |
| 852 | is_configured = true; |
| 853 | a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX_HD; |
| 854 | a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate; |
| 855 | a2dp.enc_channels = aptx_bt_cfg->channels; |
| 856 | ALOGV("%s: Successfully updated APTX HD encformat with sampling rate: %d channels:%d", |
| 857 | __func__, aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels); |
| 858 | exit: |
| 859 | return is_configured; |
| 860 | } |
| 861 | |
| 862 | /* API to configure AAC DSP encoder */ |
| 863 | static bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg) |
| 864 | { |
| 865 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
| 866 | struct aac_enc_cfg_t aac_dsp_cfg; |
| 867 | bool is_configured = false; |
| 868 | int ret = 0; |
| 869 | |
| 870 | if (aac_bt_cfg == NULL) { |
| 871 | ALOGE("%s: Failed to get AAC encoder config from BT", __func__); |
| 872 | return false; |
| 873 | } |
| 874 | |
| 875 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 876 | if (!ctl_enc_data) { |
| 877 | ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__); |
| 878 | is_configured = false; |
| 879 | goto exit; |
| 880 | } |
| 881 | memset(&aac_dsp_cfg, 0x0, sizeof(aac_dsp_cfg)); |
| 882 | aac_dsp_cfg.enc_format = ENC_MEDIA_FMT_AAC; |
| 883 | aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate; |
| 884 | aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate; |
| 885 | switch (aac_bt_cfg->enc_mode) { |
| 886 | case 0: |
| 887 | aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC; |
| 888 | break; |
| 889 | case 2: |
| 890 | aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS; |
| 891 | break; |
| 892 | case 1: |
| 893 | default: |
| 894 | aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR; |
| 895 | break; |
| 896 | } |
| 897 | aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag; |
| 898 | aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels; |
| 899 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg, |
| 900 | sizeof(aac_dsp_cfg)); |
| 901 | if (ret != 0) { |
| 902 | ALOGE("%s: failed to set AAC encoder config", __func__); |
| 903 | is_configured = false; |
| 904 | goto exit; |
| 905 | } |
| 906 | ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample); |
| 907 | if (ret != 0) { |
| 908 | is_configured = false; |
| 909 | goto exit; |
| 910 | } |
| 911 | is_configured = true; |
| 912 | a2dp.bt_encoder_format = ENC_CODEC_TYPE_AAC; |
| 913 | a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate; |
| 914 | a2dp.enc_channels = aac_bt_cfg->channels;; |
| 915 | ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d", |
| 916 | __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg); |
| 917 | exit: |
| 918 | return is_configured; |
| 919 | } |
| 920 | |
| 921 | static bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg) |
| 922 | { |
| 923 | struct mixer_ctl *ldac_enc_data = NULL, *ctrl_bit_format = NULL; |
| 924 | struct ldac_enc_cfg_t ldac_dsp_cfg; |
| 925 | bool is_configured = false; |
| 926 | int ret = 0; |
| 927 | |
| 928 | if (ldac_bt_cfg == NULL) { |
| 929 | ALOGE("%s: Failed to get LDAC encoder config from BT", __func__); |
| 930 | return false; |
| 931 | } |
| 932 | |
| 933 | ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 934 | if (!ldac_enc_data) { |
| 935 | ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__); |
| 936 | is_configured = false; |
| 937 | goto exit; |
| 938 | } |
| 939 | memset(&ldac_dsp_cfg, 0x0, sizeof(ldac_dsp_cfg)); |
| 940 | |
| 941 | ldac_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_LDAC; |
| 942 | ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate; |
| 943 | ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode; |
| 944 | switch (ldac_dsp_cfg.ldac_cfg.channel_mode) { |
| 945 | case 4: |
| 946 | ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C; |
| 947 | ldac_dsp_cfg.custom_cfg.num_channels = 1; |
| 948 | break; |
| 949 | case 2: |
| 950 | case 1: |
| 951 | default: |
| 952 | ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L; |
| 953 | ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R; |
| 954 | ldac_dsp_cfg.custom_cfg.num_channels = 2; |
| 955 | break; |
| 956 | } |
| 957 | |
| 958 | ldac_dsp_cfg.custom_cfg.custom_size = sizeof(ldac_dsp_cfg); |
| 959 | ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu; |
| 960 | ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate; |
| 961 | ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg, |
| 962 | sizeof(ldac_dsp_cfg)); |
| 963 | if (ret != 0) { |
| 964 | ALOGE("%s: Failed to set LDAC encoder config", __func__); |
| 965 | is_configured = false; |
| 966 | goto exit; |
| 967 | } |
| 968 | ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample); |
| 969 | if (ret != 0) { |
| 970 | is_configured = false; |
| 971 | goto exit; |
| 972 | } |
| 973 | is_configured = true; |
| 974 | a2dp.bt_encoder_format = ENC_CODEC_TYPE_LDAC; |
| 975 | a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate; |
| 976 | a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels; |
| 977 | ALOGV("%s: Successfully updated LDAC encformat with sampling rate: %d channels:%d", |
| 978 | __func__, ldac_dsp_cfg.custom_cfg.sample_rate, |
| 979 | ldac_dsp_cfg.custom_cfg.num_channels); |
| 980 | exit: |
| 981 | return is_configured; |
| 982 | } |
| 983 | |
| 984 | bool configure_a2dp_encoder_format() |
| 985 | { |
| 986 | void *codec_info = NULL; |
| 987 | uint8_t multi_cast = 0, num_dev = 1; |
| 988 | enc_codec_t codec_type = ENC_CODEC_TYPE_INVALID; |
| 989 | bool is_configured = false; |
| 990 | audio_aptx_encoder_config aptx_encoder_cfg; |
| 991 | |
| 992 | if (!a2dp.audio_get_codec_config) { |
| 993 | ALOGE("%s: A2DP handle is not identified, ignoring A2DP encoder config", __func__); |
| 994 | return false; |
| 995 | } |
| 996 | ALOGD("%s: start", __func__); |
| 997 | codec_info = a2dp.audio_get_codec_config(&multi_cast, &num_dev, |
| 998 | &codec_type); |
| 999 | |
| 1000 | switch (codec_type) { |
| 1001 | case ENC_CODEC_TYPE_SBC: |
| 1002 | ALOGD("%s: Received SBC encoder supported Bluetooth device", __func__); |
| 1003 | is_configured = |
| 1004 | configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info); |
| 1005 | break; |
| 1006 | case ENC_CODEC_TYPE_APTX: |
| 1007 | ALOGD("%s: Received APTX encoder supported Bluetooth device", __func__); |
| 1008 | a2dp.is_aptx_dual_mono_supported = false; |
| 1009 | aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info; |
| 1010 | is_configured = |
| 1011 | configure_aptx_enc_format(&aptx_encoder_cfg); |
| 1012 | break; |
| 1013 | case ENC_CODEC_TYPE_APTX_HD: |
| 1014 | ALOGD("%s: Received APTX HD encoder supported Bluetooth device", __func__); |
| 1015 | is_configured = |
| 1016 | configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info); |
| 1017 | break; |
| 1018 | case ENC_CODEC_TYPE_AAC: |
| 1019 | ALOGD("%s: Received AAC encoder supported Bluetooth device", __func__); |
| 1020 | is_configured = |
| 1021 | configure_aac_enc_format((audio_aac_encoder_config *)codec_info); |
| 1022 | break; |
| 1023 | case ENC_CODEC_TYPE_LDAC: |
| 1024 | ALOGD("%s: Received LDAC encoder supported Bluetooth device", __func__); |
| 1025 | is_configured = |
| 1026 | configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info); |
| 1027 | break; |
| 1028 | default: |
| 1029 | ALOGD("%s: Received unsupported encoder format", __func__); |
| 1030 | is_configured = false; |
| 1031 | break; |
| 1032 | } |
| 1033 | return is_configured; |
| 1034 | } |
| 1035 | |
| 1036 | int audio_extn_a2dp_start_playback() |
| 1037 | { |
| 1038 | int ret = 0; |
| 1039 | |
| 1040 | ALOGD("%s: start", __func__); |
| 1041 | |
| 1042 | if (!(a2dp.bt_lib_handle && a2dp.audio_stream_start |
| 1043 | && a2dp.audio_get_codec_config)) { |
| 1044 | ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__); |
| 1045 | return -ENOSYS; |
| 1046 | } |
| 1047 | |
| 1048 | if (a2dp.a2dp_suspended) { |
| 1049 | // session will be restarted after suspend completion |
| 1050 | ALOGD("%s: A2DP start requested during suspend state", __func__); |
| 1051 | return -ENOSYS; |
| 1052 | } |
| 1053 | |
| 1054 | if (!a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) { |
| 1055 | ALOGD("%s: calling Bluetooth module stream start", __func__); |
| 1056 | /* This call indicates Bluetooth IPC lib to start playback */ |
| 1057 | ret = a2dp.audio_stream_start(); |
| 1058 | ALOGE("%s: Bluetooth controller start return = %d", __func__, ret); |
| 1059 | if (ret != 0 ) { |
| 1060 | ALOGE("%s: Bluetooth controller start failed", __func__); |
| 1061 | a2dp.a2dp_started = false; |
| 1062 | } else { |
| 1063 | if (configure_a2dp_encoder_format() == true) { |
| 1064 | a2dp.a2dp_started = true; |
| 1065 | ret = 0; |
| 1066 | ALOGD("%s: Start playback successful to Bluetooth IPC library", __func__); |
| 1067 | } else { |
| 1068 | ALOGD("%s: unable to configure DSP encoder", __func__); |
| 1069 | a2dp.a2dp_started = false; |
| 1070 | ret = -ETIMEDOUT; |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | if (a2dp.a2dp_started) { |
| 1076 | a2dp.a2dp_total_active_session_request++; |
| 1077 | a2dp_check_and_set_scrambler(); |
| 1078 | a2dp_set_backend_cfg(); |
| 1079 | } |
| 1080 | |
| 1081 | ALOGD("%s: start A2DP playback total active sessions :%d", __func__, |
| 1082 | a2dp.a2dp_total_active_session_request); |
| 1083 | return ret; |
| 1084 | } |
| 1085 | |
| 1086 | static int reset_a2dp_enc_config_params() |
| 1087 | { |
| 1088 | int ret = 0; |
| 1089 | |
| 1090 | struct mixer_ctl *ctl_enc_config, *ctrl_bit_format; |
| 1091 | struct sbc_enc_cfg_t dummy_reset_config; |
| 1092 | |
| 1093 | memset(&dummy_reset_config, 0x0, sizeof(dummy_reset_config)); |
| 1094 | ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 1095 | MIXER_ENC_CONFIG_BLOCK); |
| 1096 | if (!ctl_enc_config) { |
| 1097 | ALOGE("%s: ERROR A2DP encoder format mixer control not identifed", __func__); |
| 1098 | } else { |
| 1099 | ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config, |
| 1100 | sizeof(dummy_reset_config)); |
| 1101 | a2dp.bt_encoder_format = ENC_MEDIA_FMT_NONE; |
| 1102 | } |
| 1103 | |
| 1104 | ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT); |
| 1105 | |
| 1106 | return ret; |
| 1107 | } |
| 1108 | |
| 1109 | int audio_extn_a2dp_stop_playback() |
| 1110 | { |
| 1111 | int ret = 0; |
| 1112 | |
| 1113 | ALOGV("%s: stop", __func__); |
| 1114 | if (!(a2dp.bt_lib_handle && a2dp.audio_stream_stop)) { |
| 1115 | ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__); |
| 1116 | return -ENOSYS; |
| 1117 | } |
| 1118 | |
| 1119 | if (a2dp.a2dp_total_active_session_request > 0) |
| 1120 | a2dp.a2dp_total_active_session_request--; |
| 1121 | else |
| 1122 | ALOGE("%s: No active playback session requests on A2DP", __func__); |
| 1123 | |
| 1124 | if (a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) { |
| 1125 | ALOGV("%s: calling Bluetooth module stream stop", __func__); |
| 1126 | ret = a2dp.audio_stream_stop(); |
| 1127 | if (ret < 0) |
| 1128 | ALOGE("%s: stop stream to Bluetooth IPC lib failed", __func__); |
| 1129 | else |
| 1130 | ALOGV("%s: stop steam to Bluetooth IPC lib successful", __func__); |
| 1131 | reset_a2dp_enc_config_params(); |
| 1132 | a2dp_reset_backend_cfg(); |
| 1133 | a2dp.a2dp_started = false; |
| 1134 | } |
| 1135 | ALOGD("%s: Stop A2DP playback total active sessions :%d", __func__, |
| 1136 | a2dp.a2dp_total_active_session_request); |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
| 1140 | void audio_extn_a2dp_set_parameters(struct str_parms *parms) |
| 1141 | { |
| 1142 | int ret, val; |
| 1143 | char value[32] = {0}; |
| 1144 | struct audio_usecase *uc_info; |
| 1145 | struct listnode *node; |
| 1146 | |
| 1147 | if (a2dp.is_a2dp_offload_supported == false) { |
| 1148 | ALOGV("%s: No supported encoders identified,ignoring A2DP setparam", __func__); |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, |
| 1153 | sizeof(value)); |
| 1154 | if (ret >= 0) { |
| 1155 | val = atoi(value); |
| 1156 | if (audio_is_a2dp_out_device(val)) { |
| 1157 | ALOGV("%s: Received device connect request for A2DP", __func__); |
| 1158 | open_a2dp_output(); |
| 1159 | } |
| 1160 | goto param_handled; |
| 1161 | } |
| 1162 | |
| 1163 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, |
| 1164 | sizeof(value)); |
| 1165 | |
| 1166 | if (ret >= 0) { |
| 1167 | val = atoi(value); |
| 1168 | if (audio_is_a2dp_out_device(val)) { |
| 1169 | ALOGV("%s: Received device disconnect request", __func__); |
| 1170 | reset_a2dp_enc_config_params(); |
| 1171 | close_a2dp_output(); |
| 1172 | } |
| 1173 | goto param_handled; |
| 1174 | } |
| 1175 | |
| 1176 | ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value)); |
| 1177 | if (ret >= 0) { |
| 1178 | if (a2dp.bt_lib_handle && (a2dp.bt_state != A2DP_STATE_DISCONNECTED)) { |
| 1179 | if ((!strncmp(value, "true", sizeof(value))) && !a2dp.a2dp_suspended) { |
| 1180 | ALOGD("%s: Setting A2DP to suspend state", __func__); |
| 1181 | a2dp.a2dp_suspended = true; |
| 1182 | list_for_each(node, &a2dp.adev->usecase_list) { |
| 1183 | uc_info = node_to_item(node, struct audio_usecase, list); |
| 1184 | if (uc_info->type == PCM_PLAYBACK && |
| 1185 | (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) { |
| 1186 | pthread_mutex_unlock(&a2dp.adev->lock); |
| 1187 | check_a2dp_restore(a2dp.adev, uc_info->stream.out, false); |
| 1188 | pthread_mutex_lock(&a2dp.adev->lock); |
| 1189 | } |
| 1190 | } |
| 1191 | reset_a2dp_enc_config_params(); |
| 1192 | if (a2dp.audio_stream_suspend) |
| 1193 | a2dp.audio_stream_suspend(); |
| 1194 | } else if (a2dp.a2dp_suspended) { |
| 1195 | ALOGD("%s: Resetting A2DP suspend state", __func__); |
| 1196 | struct audio_usecase *uc_info; |
| 1197 | struct listnode *node; |
| 1198 | if (a2dp.clear_a2dp_suspend_flag) |
| 1199 | a2dp.clear_a2dp_suspend_flag(); |
| 1200 | a2dp.a2dp_suspended = false; |
| 1201 | /* |
| 1202 | * It is possible that before suspend, A2DP sessions can be active. |
| 1203 | * For example, during music + voice activation concurrency, |
| 1204 | * A2DP suspend will be called & Bluetooth will change to SCO mode. |
| 1205 | * Though music is paused as a part of voice activation, |
| 1206 | * compress session close happens only after pause timeout(10 secs). |
| 1207 | * So, if resume request comes before pause timeout, as A2DP session |
| 1208 | * is already active, IPC start will not be called from APM/audio_hw. |
| 1209 | * Fix this by calling A2DP start for IPC library post suspend |
| 1210 | * based on number of active session count. |
| 1211 | */ |
| 1212 | if (a2dp.a2dp_total_active_session_request > 0) { |
| 1213 | ALOGD("%s: Calling Bluetooth IPC lib start post suspend state", __func__); |
| 1214 | if (a2dp.audio_stream_start) { |
| 1215 | ret = a2dp.audio_stream_start(); |
| 1216 | if (ret != 0) { |
| 1217 | ALOGE("%s: Bluetooth controller start failed", __func__); |
| 1218 | a2dp.a2dp_started = false; |
| 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | list_for_each(node, &a2dp.adev->usecase_list) { |
| 1223 | uc_info = node_to_item(node, struct audio_usecase, list); |
| 1224 | if (uc_info->type == PCM_PLAYBACK && |
| 1225 | (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) { |
| 1226 | pthread_mutex_unlock(&a2dp.adev->lock); |
| 1227 | check_a2dp_restore(a2dp.adev, uc_info->stream.out, true); |
| 1228 | pthread_mutex_lock(&a2dp.adev->lock); |
| 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | goto param_handled; |
| 1234 | } |
| 1235 | param_handled: |
| 1236 | ALOGV("%s: end of A2DP setparam", __func__); |
| 1237 | } |
| 1238 | |
| 1239 | void audio_extn_a2dp_set_handoff_mode(bool is_on) |
| 1240 | { |
| 1241 | a2dp.is_handoff_in_progress = is_on; |
| 1242 | } |
| 1243 | |
| 1244 | bool audio_extn_a2dp_is_force_device_switch() |
| 1245 | { |
| 1246 | // During encoder reconfiguration mode, force A2DP device switch |
| 1247 | // Or if A2DP device is selected but earlier start failed as A2DP |
| 1248 | // was suspended, force retry. |
| 1249 | return a2dp.is_handoff_in_progress || !a2dp.a2dp_started; |
| 1250 | } |
| 1251 | |
| 1252 | void audio_extn_a2dp_get_sample_rate(int *sample_rate) |
| 1253 | { |
| 1254 | *sample_rate = a2dp.enc_sampling_rate; |
| 1255 | } |
| 1256 | |
| 1257 | bool audio_extn_a2dp_is_ready() |
| 1258 | { |
| 1259 | bool ret = false; |
| 1260 | |
| 1261 | if (a2dp.a2dp_suspended) |
| 1262 | goto exit; |
| 1263 | |
| 1264 | if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) && |
| 1265 | (a2dp.is_a2dp_offload_supported) && |
| 1266 | (a2dp.audio_check_a2dp_ready)) |
| 1267 | ret = a2dp.audio_check_a2dp_ready(); |
| 1268 | |
| 1269 | exit: |
| 1270 | return ret; |
| 1271 | } |
| 1272 | |
| 1273 | bool audio_extn_a2dp_is_suspended() |
| 1274 | { |
| 1275 | return a2dp.a2dp_suspended; |
| 1276 | } |
| 1277 | |
| 1278 | void audio_extn_a2dp_init(void *adev) |
| 1279 | { |
| 1280 | a2dp.adev = (struct audio_device*)adev; |
| 1281 | a2dp.bt_lib_handle = NULL; |
| 1282 | a2dp_common_init(); |
| 1283 | a2dp.enc_sampling_rate = 48000; |
| 1284 | a2dp.is_a2dp_offload_supported = false; |
| 1285 | a2dp.is_handoff_in_progress = false; |
| 1286 | a2dp.is_aptx_dual_mono_supported = false; |
| 1287 | reset_a2dp_enc_config_params(); |
| 1288 | update_offload_codec_support(); |
| 1289 | } |
| 1290 | |
| 1291 | uint32_t audio_extn_a2dp_get_encoder_latency() |
| 1292 | { |
| 1293 | uint32_t latency = 0; |
| 1294 | int avsync_runtime_prop = 0; |
| 1295 | int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0, |
| 1296 | aac_offset = 0, ldac_offset = 0; |
| 1297 | char value[PROPERTY_VALUE_MAX]; |
| 1298 | |
| 1299 | memset(value, '\0', sizeof(char) * PROPERTY_VALUE_MAX); |
| 1300 | avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL); |
| 1301 | if (avsync_runtime_prop > 0) { |
| 1302 | if (sscanf(value, "%d/%d/%d/%d/%d", |
| 1303 | &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, |
| 1304 | &ldac_offset) != 5) { |
| 1305 | ALOGI("%s: Failed to parse avsync offset params from '%s'.", __func__, value); |
| 1306 | avsync_runtime_prop = 0; |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | switch (a2dp.bt_encoder_format) { |
| 1311 | case ENC_CODEC_TYPE_SBC: |
| 1312 | latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC; |
| 1313 | latency += DEFAULT_SINK_LATENCY_SBC; |
| 1314 | break; |
| 1315 | case ENC_CODEC_TYPE_APTX: |
| 1316 | latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX; |
| 1317 | latency += DEFAULT_SINK_LATENCY_APTX; |
| 1318 | break; |
| 1319 | case ENC_CODEC_TYPE_APTX_HD: |
| 1320 | latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD; |
| 1321 | latency += DEFAULT_SINK_LATENCY_APTX_HD; |
| 1322 | break; |
| 1323 | case ENC_CODEC_TYPE_AAC: |
| 1324 | latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC; |
| 1325 | latency += DEFAULT_SINK_LATENCY_AAC; |
| 1326 | break; |
| 1327 | case ENC_CODEC_TYPE_LDAC: |
| 1328 | latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC; |
| 1329 | latency += DEFAULT_SINK_LATENCY_LDAC; |
| 1330 | break; |
| 1331 | default: |
| 1332 | latency = DEFAULT_ENCODER_LATENCY; |
| 1333 | break; |
| 1334 | } |
| 1335 | return latency; |
| 1336 | } |
| 1337 | #endif // A2DP_OFFLOAD_ENABLED |