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