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