Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1 | /* |
Naresh Tanniru | ed694c8 | 2017-02-07 17:01:28 +0530 | [diff] [blame] | 2 | * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved. |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | #define LOG_TAG "split_a2dp" |
| 30 | /*#define LOG_NDEBUG 0*/ |
| 31 | #define LOG_NDDEBUG 0 |
| 32 | #include <errno.h> |
| 33 | #include <cutils/log.h> |
| 34 | #include <dlfcn.h> |
| 35 | #include "audio_hw.h" |
| 36 | #include "platform.h" |
| 37 | #include "platform_api.h" |
| 38 | #include <stdlib.h> |
| 39 | #include <cutils/str_parms.h> |
| 40 | #include <hardware/audio.h> |
| 41 | #include <hardware/hardware.h> |
| 42 | #include <cutils/properties.h> |
| 43 | |
Revathi Uddaraju | 1eac8b0 | 2017-05-18 17:13:33 +0530 | [diff] [blame^] | 44 | #ifdef DYNAMIC_LOG_ENABLED |
| 45 | #include <log_xml_parser.h> |
| 46 | #define LOG_MASK HAL_MOD_FILE_A2DP |
| 47 | #include <log_utils.h> |
| 48 | #endif |
| 49 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 50 | #ifdef SPLIT_A2DP_ENABLED |
| 51 | #define AUDIO_PARAMETER_A2DP_STARTED "A2dpStarted" |
| 52 | #define BT_IPC_LIB_NAME "libbthost_if.so" |
| 53 | #define ENC_MEDIA_FMT_NONE 0 |
| 54 | #define ENC_MEDIA_FMT_AAC 0x00010DA6 |
| 55 | #define ENC_MEDIA_FMT_APTX 0x000131ff |
| 56 | #define ENC_MEDIA_FMT_APTX_HD 0x00013200 |
| 57 | #define ENC_MEDIA_FMT_SBC 0x00010BF2 |
| 58 | #define MEDIA_FMT_AAC_AOT_LC 2 |
| 59 | #define MEDIA_FMT_AAC_AOT_SBR 5 |
| 60 | #define MEDIA_FMT_AAC_AOT_PS 29 |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 61 | #define PCM_CHANNEL_L 1 |
| 62 | #define PCM_CHANNEL_R 2 |
| 63 | #define PCM_CHANNEL_C 3 |
| 64 | #define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1 |
| 65 | #define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2 |
| 66 | #define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8 |
| 67 | #define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9 |
| 68 | #define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0 |
| 69 | #define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1 |
| 70 | #define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config" |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 71 | #define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format" |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 72 | #define MIXER_ENC_FMT_SBC "SBC" |
| 73 | #define MIXER_ENC_FMT_AAC "AAC" |
| 74 | #define MIXER_ENC_FMT_APTX "APTX" |
| 75 | #define MIXER_ENC_FMT_APTXHD "APTXHD" |
| 76 | #define MIXER_ENC_FMT_NONE "NONE" |
| 77 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 78 | typedef int (*audio_stream_open_t)(void); |
| 79 | typedef int (*audio_stream_close_t)(void); |
| 80 | typedef int (*audio_start_stream_t)(void); |
| 81 | typedef int (*audio_stop_stream_t)(void); |
| 82 | typedef int (*audio_suspend_stream_t)(void); |
| 83 | typedef void (*audio_handoff_triggered_t)(void); |
| 84 | typedef void (*clear_a2dpsuspend_flag_t)(void); |
| 85 | typedef void * (*audio_get_codec_config_t)(uint8_t *multicast_status,uint8_t *num_dev, |
| 86 | audio_format_t *codec_type); |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 87 | typedef int (*audio_check_a2dp_ready_t)(void); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 88 | |
| 89 | enum A2DP_STATE { |
| 90 | A2DP_STATE_CONNECTED, |
| 91 | A2DP_STATE_STARTED, |
| 92 | A2DP_STATE_STOPPED, |
| 93 | A2DP_STATE_DISCONNECTED, |
| 94 | }; |
| 95 | |
| 96 | /* structure used to update a2dp state machine |
| 97 | * to communicate IPC library |
| 98 | * to store DSP encoder configuration information |
| 99 | */ |
| 100 | struct a2dp_data { |
| 101 | struct audio_device *adev; |
| 102 | void *bt_lib_handle; |
| 103 | audio_stream_open_t audio_stream_open; |
| 104 | audio_stream_close_t audio_stream_close; |
| 105 | audio_start_stream_t audio_start_stream; |
| 106 | audio_stop_stream_t audio_stop_stream; |
| 107 | audio_suspend_stream_t audio_suspend_stream; |
| 108 | audio_handoff_triggered_t audio_handoff_triggered; |
| 109 | clear_a2dpsuspend_flag_t clear_a2dpsuspend_flag; |
| 110 | audio_get_codec_config_t audio_get_codec_config; |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 111 | audio_check_a2dp_ready_t audio_check_a2dp_ready; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 112 | enum A2DP_STATE bt_state; |
| 113 | audio_format_t bt_encoder_format; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 114 | uint32_t enc_sampling_rate; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 115 | bool a2dp_started; |
| 116 | bool a2dp_suspended; |
| 117 | int a2dp_total_active_session_request; |
| 118 | bool is_a2dp_offload_supported; |
| 119 | bool is_handoff_in_progress; |
| 120 | }; |
| 121 | |
| 122 | struct a2dp_data a2dp; |
| 123 | |
| 124 | /* START of DSP configurable structures |
| 125 | * These values should match with DSP interface defintion |
| 126 | */ |
| 127 | |
| 128 | /* AAC encoder configuration structure. */ |
| 129 | typedef struct aac_enc_cfg_t aac_enc_cfg_t; |
| 130 | |
| 131 | /* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS |
| 132 | * supported aac_fmt_flag are ADTS/RAW |
| 133 | * supported channel_cfg are Native mode, Mono , Stereo |
| 134 | */ |
| 135 | struct aac_enc_cfg_t { |
| 136 | uint32_t enc_format; |
| 137 | uint32_t bit_rate; |
| 138 | uint32_t enc_mode; |
| 139 | uint16_t aac_fmt_flag; |
Naresh Tanniru | a42d0bd | 2016-09-21 15:30:46 +0530 | [diff] [blame] | 140 | uint16_t channel_cfg; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 141 | uint32_t sample_rate; |
| 142 | } ; |
| 143 | |
| 144 | /* SBC encoder configuration structure. */ |
| 145 | typedef struct sbc_enc_cfg_t sbc_enc_cfg_t; |
| 146 | |
| 147 | /* supported num_subbands are 4/8 |
| 148 | * supported blk_len are 4, 8, 12, 16 |
| 149 | * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO |
| 150 | * supported alloc_method are LOUNDNESS/SNR |
| 151 | * supported bit_rate for mono channel is max 320kbps |
| 152 | * supported bit rate for stereo channel is max 512 kbps |
| 153 | */ |
| 154 | struct sbc_enc_cfg_t{ |
| 155 | uint32_t enc_format; |
| 156 | uint32_t num_subbands; |
| 157 | uint32_t blk_len; |
| 158 | uint32_t channel_mode; |
| 159 | uint32_t alloc_method; |
| 160 | uint32_t bit_rate; |
| 161 | uint32_t sample_rate; |
| 162 | }; |
| 163 | |
| 164 | |
| 165 | /* supported num_channels are Mono/Stereo |
| 166 | * supported channel_mapping for mono is CHANNEL_C |
| 167 | * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R |
| 168 | * custom size and reserved are not used(for future enhancement) |
| 169 | */ |
| 170 | struct custom_enc_cfg_aptx_t |
| 171 | { |
| 172 | uint32_t enc_format; |
| 173 | uint32_t sample_rate; |
| 174 | uint16_t num_channels; |
| 175 | uint16_t reserved; |
| 176 | uint8_t channel_mapping[8]; |
| 177 | uint32_t custom_size; |
| 178 | }; |
| 179 | |
| 180 | /*********** END of DSP configurable structures ********************/ |
| 181 | |
| 182 | /* API to identify DSP encoder captabilities */ |
| 183 | static void a2dp_offload_codec_cap_parser(char *value) |
| 184 | { |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 185 | char *tok = NULL,*saveptr; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 186 | |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 187 | tok = strtok_r(value, "-", &saveptr); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 188 | while (tok != NULL) { |
| 189 | if (strcmp(tok, "sbc") == 0) { |
| 190 | ALOGD("%s: SBC offload supported\n",__func__); |
| 191 | a2dp.is_a2dp_offload_supported = true; |
| 192 | break; |
| 193 | } else if (strcmp(tok, "aptx") == 0) { |
| 194 | ALOGD("%s: aptx offload supported\n",__func__); |
| 195 | a2dp.is_a2dp_offload_supported = true; |
| 196 | break; |
Naresh Tanniru | ed694c8 | 2017-02-07 17:01:28 +0530 | [diff] [blame] | 197 | } else if (strcmp(tok, "aptxhd") == 0) { |
| 198 | ALOGD("%s: aptx HD offload supported\n",__func__); |
| 199 | a2dp.is_a2dp_offload_supported = true; |
| 200 | break; |
| 201 | } else if (strcmp(tok, "aac") == 0) { |
| 202 | ALOGD("%s: aac offload supported\n",__func__); |
| 203 | a2dp.is_a2dp_offload_supported = true; |
| 204 | break; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 205 | } |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 206 | tok = strtok_r(NULL, "-", &saveptr); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 207 | }; |
| 208 | } |
| 209 | |
| 210 | static void update_offload_codec_capabilities() |
| 211 | { |
| 212 | char value[PROPERTY_VALUE_MAX] = {'\0'}; |
| 213 | |
| 214 | property_get("persist.bt.a2dp_offload_cap", value, "false"); |
| 215 | ALOGD("get_offload_codec_capabilities = %s",value); |
| 216 | a2dp.is_a2dp_offload_supported = |
| 217 | property_get_bool("persist.bt.a2dp_offload_cap", false); |
| 218 | if (strcmp(value, "false") != 0) |
| 219 | a2dp_offload_codec_cap_parser(value); |
| 220 | ALOGD("%s: codec cap = %s",__func__,value); |
| 221 | } |
| 222 | |
| 223 | /* API to open BT IPC library to start IPC communication */ |
| 224 | static void open_a2dp_output() |
| 225 | { |
| 226 | int ret = 0; |
| 227 | |
| 228 | ALOGD(" Open A2DP output start "); |
| 229 | if (a2dp.bt_lib_handle == NULL){ |
| 230 | ALOGD(" Requesting for BT lib handle"); |
| 231 | a2dp.bt_lib_handle = dlopen(BT_IPC_LIB_NAME, RTLD_NOW); |
| 232 | |
| 233 | if (a2dp.bt_lib_handle == NULL) { |
| 234 | ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_LIB_NAME); |
| 235 | ret = -ENOSYS; |
| 236 | goto init_fail; |
| 237 | } else { |
| 238 | a2dp.audio_stream_open = (audio_stream_open_t) |
| 239 | dlsym(a2dp.bt_lib_handle, "audio_stream_open"); |
| 240 | a2dp.audio_start_stream = (audio_start_stream_t) |
| 241 | dlsym(a2dp.bt_lib_handle, "audio_start_stream"); |
| 242 | a2dp.audio_get_codec_config = (audio_get_codec_config_t) |
| 243 | dlsym(a2dp.bt_lib_handle, "audio_get_codec_config"); |
| 244 | a2dp.audio_suspend_stream = (audio_suspend_stream_t) |
| 245 | dlsym(a2dp.bt_lib_handle, "audio_suspend_stream"); |
| 246 | a2dp.audio_handoff_triggered = (audio_handoff_triggered_t) |
| 247 | dlsym(a2dp.bt_lib_handle, "audio_handoff_triggered"); |
| 248 | a2dp.clear_a2dpsuspend_flag = (clear_a2dpsuspend_flag_t) |
| 249 | dlsym(a2dp.bt_lib_handle, "clear_a2dpsuspend_flag"); |
| 250 | a2dp.audio_stop_stream = (audio_stop_stream_t) |
| 251 | dlsym(a2dp.bt_lib_handle, "audio_stop_stream"); |
| 252 | a2dp.audio_stream_close = (audio_stream_close_t) |
| 253 | dlsym(a2dp.bt_lib_handle, "audio_stream_close"); |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 254 | a2dp.audio_check_a2dp_ready = (audio_check_a2dp_ready_t) |
| 255 | dlsym(a2dp.bt_lib_handle,"audio_check_a2dp_ready"); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | if (a2dp.bt_lib_handle && a2dp.audio_stream_open) { |
| 260 | if (a2dp.bt_state == A2DP_STATE_DISCONNECTED) { |
| 261 | ALOGD("calling BT stream open"); |
| 262 | ret = a2dp.audio_stream_open(); |
| 263 | if(ret != 0) { |
| 264 | ALOGE("Failed to open output stream for a2dp: status %d", ret); |
| 265 | goto init_fail; |
| 266 | } |
| 267 | a2dp.bt_state = A2DP_STATE_CONNECTED; |
| 268 | } else { |
| 269 | ALOGD("Called a2dp open with improper state, Ignoring request state %d", a2dp.bt_state); |
| 270 | } |
| 271 | } else { |
| 272 | ALOGE("a2dp handle is not identified, Ignoring open request"); |
| 273 | a2dp.bt_state = A2DP_STATE_DISCONNECTED; |
| 274 | goto init_fail; |
| 275 | } |
| 276 | |
| 277 | init_fail: |
| 278 | if(ret != 0 && (a2dp.bt_lib_handle != NULL)) { |
| 279 | dlclose(a2dp.bt_lib_handle); |
| 280 | a2dp.bt_lib_handle = NULL; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | static int close_a2dp_output() |
| 285 | { |
| 286 | ALOGV("%s\n",__func__); |
| 287 | if (!(a2dp.bt_lib_handle && a2dp.audio_stream_close)) { |
| 288 | ALOGE("a2dp handle is not identified, Ignoring close request"); |
| 289 | return -ENOSYS; |
| 290 | } |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 291 | if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) { |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 292 | ALOGD("calling BT stream close"); |
| 293 | if(a2dp.audio_stream_close() == false) |
| 294 | ALOGE("failed close a2dp control path from BT library"); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 295 | } |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 296 | a2dp.a2dp_started = false; |
| 297 | a2dp.a2dp_total_active_session_request = 0; |
| 298 | a2dp.a2dp_suspended = false; |
| 299 | a2dp.bt_encoder_format = AUDIO_FORMAT_INVALID; |
| 300 | a2dp.enc_sampling_rate = 48000; |
| 301 | a2dp.bt_state = A2DP_STATE_DISCONNECTED; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | /* API to configure SBC DSP encoder */ |
| 307 | bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg) |
| 308 | { |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 309 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 310 | struct sbc_enc_cfg_t sbc_dsp_cfg; |
| 311 | bool is_configured = false; |
| 312 | int ret = 0; |
| 313 | |
| 314 | if(sbc_bt_cfg == NULL) |
| 315 | return false; |
| 316 | |
| 317 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 318 | if (!ctl_enc_data) { |
| 319 | ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed"); |
| 320 | is_configured = false; |
| 321 | goto fail; |
| 322 | } |
| 323 | a2dp.bt_encoder_format = AUDIO_FORMAT_SBC; |
| 324 | memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t)); |
| 325 | sbc_dsp_cfg.enc_format = ENC_MEDIA_FMT_SBC; |
| 326 | sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband; |
| 327 | sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len; |
| 328 | switch(sbc_bt_cfg->channels) { |
| 329 | case 0: |
| 330 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO; |
| 331 | break; |
| 332 | case 1: |
| 333 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO; |
| 334 | break; |
| 335 | case 3: |
| 336 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO; |
| 337 | break; |
| 338 | case 2: |
| 339 | default: |
| 340 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO; |
| 341 | break; |
| 342 | } |
| 343 | if (sbc_bt_cfg->alloc) |
| 344 | sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS; |
| 345 | else |
| 346 | sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR; |
| 347 | sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate; |
| 348 | sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate; |
| 349 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg, |
| 350 | sizeof(struct sbc_enc_cfg_t)); |
| 351 | if (ret != 0) { |
| 352 | ALOGE("%s: failed to set SBC encoder config", __func__); |
| 353 | is_configured = false; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 354 | goto fail; |
| 355 | } |
| 356 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 357 | MIXER_ENC_BIT_FORMAT); |
| 358 | if (!ctrl_bit_format) { |
| 359 | ALOGE(" ERROR bit format CONFIG data mixer control not identifed"); |
| 360 | is_configured = false; |
| 361 | goto fail; |
| 362 | } |
| 363 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE"); |
| 364 | if (ret != 0) { |
| 365 | ALOGE("%s: Failed to set bit format to encoder", __func__); |
| 366 | is_configured = false; |
| 367 | goto fail; |
| 368 | } |
| 369 | is_configured = true; |
| 370 | a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate; |
| 371 | ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d", |
| 372 | sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 373 | fail: |
| 374 | return is_configured; |
| 375 | } |
| 376 | |
| 377 | /* API to configure APTX DSP encoder */ |
| 378 | bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg) |
| 379 | { |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 380 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 381 | struct custom_enc_cfg_aptx_t aptx_dsp_cfg; |
| 382 | bool is_configured = false; |
| 383 | int ret = 0; |
| 384 | |
| 385 | if(aptx_bt_cfg == NULL) |
| 386 | return false; |
| 387 | |
| 388 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 389 | if (!ctl_enc_data) { |
| 390 | ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed"); |
| 391 | is_configured = false; |
| 392 | goto fail; |
| 393 | } |
| 394 | a2dp.bt_encoder_format = AUDIO_FORMAT_APTX; |
| 395 | memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_aptx_t)); |
| 396 | aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX; |
| 397 | aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate; |
| 398 | aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels; |
| 399 | switch(aptx_dsp_cfg.num_channels) { |
| 400 | case 1: |
| 401 | aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C; |
| 402 | break; |
| 403 | case 2: |
| 404 | default: |
| 405 | aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L; |
| 406 | aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R; |
| 407 | break; |
| 408 | } |
| 409 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg, |
| 410 | sizeof(struct custom_enc_cfg_aptx_t)); |
| 411 | if (ret != 0) { |
| 412 | ALOGE("%s: Failed to set APTX encoder config", __func__); |
| 413 | is_configured = false; |
| 414 | goto fail; |
| 415 | } |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 416 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 417 | MIXER_ENC_BIT_FORMAT); |
| 418 | if (!ctrl_bit_format) { |
| 419 | ALOGE("ERROR bit format CONFIG data mixer control not identifed"); |
| 420 | is_configured = false; |
| 421 | goto fail; |
| 422 | } else { |
| 423 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE"); |
| 424 | if (ret != 0) { |
| 425 | ALOGE("%s: Failed to set bit format to encoder", __func__); |
| 426 | is_configured = false; |
| 427 | goto fail; |
| 428 | } |
| 429 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 430 | is_configured = true; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 431 | a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate; |
| 432 | ALOGV("Successfully updated APTX enc format with samplingrate: %d channels:%d", |
| 433 | aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 434 | fail: |
| 435 | return is_configured; |
| 436 | } |
| 437 | |
| 438 | /* API to configure APTX HD DSP encoder |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 439 | */ |
| 440 | bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg) |
| 441 | { |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 442 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 443 | struct custom_enc_cfg_aptx_t aptx_dsp_cfg; |
| 444 | bool is_configured = false; |
| 445 | int ret = 0; |
| 446 | |
| 447 | if(aptx_bt_cfg == NULL) |
| 448 | return false; |
| 449 | |
| 450 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 451 | if (!ctl_enc_data) { |
| 452 | ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed"); |
| 453 | is_configured = false; |
| 454 | goto fail; |
| 455 | } |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 456 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 457 | a2dp.bt_encoder_format = AUDIO_FORMAT_APTX_HD; |
| 458 | memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_aptx_t)); |
| 459 | aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX_HD; |
| 460 | aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate; |
| 461 | aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels; |
| 462 | switch(aptx_dsp_cfg.num_channels) { |
| 463 | case 1: |
| 464 | aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C; |
| 465 | break; |
| 466 | case 2: |
| 467 | default: |
| 468 | aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L; |
| 469 | aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R; |
| 470 | break; |
| 471 | } |
| 472 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg, |
| 473 | sizeof(struct custom_enc_cfg_aptx_t)); |
| 474 | if (ret != 0) { |
| 475 | ALOGE("%s: Failed to set APTX HD encoder config", __func__); |
| 476 | is_configured = false; |
| 477 | goto fail; |
| 478 | } |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 479 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_BIT_FORMAT); |
| 480 | if (!ctrl_bit_format) { |
| 481 | ALOGE(" ERROR bit format CONFIG data mixer control not identifed"); |
| 482 | is_configured = false; |
| 483 | goto fail; |
| 484 | } |
| 485 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S24_LE"); |
| 486 | if (ret != 0) { |
| 487 | ALOGE("%s: Failed to set APTX HD encoder config", __func__); |
| 488 | is_configured = false; |
| 489 | goto fail; |
| 490 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 491 | is_configured = true; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 492 | a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate; |
| 493 | ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d", |
| 494 | aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 495 | fail: |
| 496 | return is_configured; |
| 497 | } |
| 498 | |
| 499 | /* API to configure AAC DSP encoder */ |
| 500 | bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg) |
| 501 | { |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 502 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 503 | struct aac_enc_cfg_t aac_dsp_cfg; |
| 504 | bool is_configured = false; |
| 505 | int ret = 0; |
| 506 | |
| 507 | if(aac_bt_cfg == NULL) |
| 508 | return false; |
| 509 | |
| 510 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 511 | if (!ctl_enc_data) { |
| 512 | ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed"); |
| 513 | is_configured = false; |
| 514 | goto fail; |
| 515 | } |
| 516 | a2dp.bt_encoder_format = AUDIO_FORMAT_AAC; |
| 517 | memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t)); |
| 518 | aac_dsp_cfg.enc_format = ENC_MEDIA_FMT_AAC; |
| 519 | aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate; |
Naresh Tanniru | a42d0bd | 2016-09-21 15:30:46 +0530 | [diff] [blame] | 520 | aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 521 | switch(aac_bt_cfg->enc_mode) { |
| 522 | case 0: |
| 523 | aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC; |
| 524 | break; |
| 525 | case 2: |
| 526 | aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS; |
| 527 | break; |
| 528 | case 1: |
| 529 | default: |
| 530 | aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR; |
| 531 | break; |
| 532 | } |
Naresh Tanniru | a42d0bd | 2016-09-21 15:30:46 +0530 | [diff] [blame] | 533 | aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 534 | aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels; |
| 535 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg, |
| 536 | sizeof(struct aac_enc_cfg_t)); |
| 537 | if (ret != 0) { |
| 538 | ALOGE("%s: failed to set SBC encoder config", __func__); |
| 539 | is_configured = false; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 540 | goto fail; |
| 541 | } |
| 542 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 543 | MIXER_ENC_BIT_FORMAT); |
| 544 | if (!ctrl_bit_format) { |
| 545 | is_configured = false; |
| 546 | ALOGE(" ERROR bit format CONFIG data mixer control not identifed"); |
| 547 | goto fail; |
| 548 | } |
| 549 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE"); |
| 550 | if (ret != 0) { |
| 551 | ALOGE("%s: Failed to set bit format to encoder", __func__); |
| 552 | is_configured = false; |
| 553 | goto fail; |
| 554 | } |
| 555 | is_configured = true; |
| 556 | a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate; |
| 557 | ALOGV("Successfully updated AAC enc format with samplingrate: %d channels:%d", |
| 558 | aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 559 | fail: |
| 560 | return is_configured; |
| 561 | } |
| 562 | |
| 563 | bool configure_a2dp_encoder_format() |
| 564 | { |
| 565 | void *codec_info = NULL; |
| 566 | uint8_t multi_cast = 0, num_dev = 1; |
| 567 | audio_format_t codec_type = AUDIO_FORMAT_INVALID; |
| 568 | bool is_configured = false; |
| 569 | |
| 570 | if (!a2dp.audio_get_codec_config) { |
| 571 | ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config"); |
| 572 | return false; |
| 573 | } |
| 574 | ALOGD("configure_a2dp_encoder_format start"); |
| 575 | codec_info = a2dp.audio_get_codec_config(&multi_cast, &num_dev, |
| 576 | &codec_type); |
| 577 | |
| 578 | switch(codec_type) { |
| 579 | case AUDIO_FORMAT_SBC: |
| 580 | ALOGD(" Received SBC encoder supported BT device"); |
| 581 | is_configured = |
| 582 | configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info); |
| 583 | break; |
| 584 | case AUDIO_FORMAT_APTX: |
| 585 | ALOGD(" Received APTX encoder supported BT device"); |
| 586 | is_configured = |
| 587 | configure_aptx_enc_format((audio_aptx_encoder_config *)codec_info); |
| 588 | break; |
| 589 | case AUDIO_FORMAT_APTX_HD: |
| 590 | ALOGD(" Received APTX HD encoder supported BT device"); |
| 591 | is_configured = |
| 592 | configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info); |
| 593 | break; |
| 594 | case AUDIO_FORMAT_AAC: |
| 595 | ALOGD(" Received AAC encoder supported BT device"); |
| 596 | is_configured = |
| 597 | configure_aac_enc_format((audio_aac_encoder_config *)codec_info); |
| 598 | break; |
| 599 | default: |
| 600 | ALOGD(" Received Unsupported encoder formar"); |
| 601 | is_configured = false; |
| 602 | break; |
| 603 | } |
| 604 | return is_configured; |
| 605 | } |
| 606 | |
| 607 | int audio_extn_a2dp_start_playback() |
| 608 | { |
| 609 | int ret = 0; |
| 610 | |
| 611 | ALOGD("audio_extn_a2dp_start_playback start"); |
| 612 | |
| 613 | if(!(a2dp.bt_lib_handle && a2dp.audio_start_stream |
| 614 | && a2dp.audio_get_codec_config)) { |
| 615 | ALOGE("a2dp handle is not identified, Ignoring start request"); |
| 616 | return -ENOSYS; |
| 617 | } |
| 618 | |
| 619 | if(a2dp.a2dp_suspended == true) { |
| 620 | //session will be restarted after suspend completion |
| 621 | ALOGD("a2dp start requested during suspend state"); |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 622 | return -ENOSYS; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | if (!a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) { |
| 626 | ALOGD("calling BT module stream start"); |
| 627 | /* This call indicates BT IPC lib to start playback */ |
| 628 | ret = a2dp.audio_start_stream(); |
| 629 | ALOGE("BT controller start return = %d",ret); |
| 630 | if (ret != 0 ) { |
| 631 | ALOGE("BT controller start failed"); |
| 632 | a2dp.a2dp_started = false; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 633 | } else { |
| 634 | if(configure_a2dp_encoder_format() == true) { |
| 635 | a2dp.a2dp_started = true; |
| 636 | ret = 0; |
| 637 | ALOGD("Start playback successful to BT library"); |
| 638 | } else { |
| 639 | ALOGD(" unable to configure DSP encoder"); |
| 640 | a2dp.a2dp_started = false; |
| 641 | ret = -ETIMEDOUT; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | if (a2dp.a2dp_started) |
| 647 | a2dp.a2dp_total_active_session_request++; |
| 648 | |
| 649 | ALOGD("start A2DP playback total active sessions :%d", |
| 650 | a2dp.a2dp_total_active_session_request); |
| 651 | return ret; |
| 652 | } |
| 653 | |
Naresh Tanniru | 03f9dd5 | 2016-10-19 18:46:22 +0530 | [diff] [blame] | 654 | static void reset_a2dp_enc_config_params() |
| 655 | { |
| 656 | int ret =0; |
| 657 | |
| 658 | struct mixer_ctl *ctl_enc_config, *ctrl_bit_format; |
| 659 | struct sbc_enc_cfg_t dummy_reset_config; |
| 660 | |
| 661 | memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t)); |
| 662 | ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 663 | MIXER_ENC_CONFIG_BLOCK); |
| 664 | if (!ctl_enc_config) { |
| 665 | ALOGE(" ERROR a2dp encoder format mixer control not identifed"); |
| 666 | } else { |
| 667 | ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config, |
| 668 | sizeof(struct sbc_enc_cfg_t)); |
| 669 | a2dp.bt_encoder_format = ENC_MEDIA_FMT_NONE; |
| 670 | } |
| 671 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 672 | MIXER_ENC_BIT_FORMAT); |
| 673 | if (!ctrl_bit_format) { |
| 674 | ALOGE(" ERROR bit format CONFIG data mixer control not identifed"); |
| 675 | } else { |
| 676 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE"); |
| 677 | if (ret != 0) { |
| 678 | ALOGE("%s: Failed to set bit format to encoder", __func__); |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 683 | int audio_extn_a2dp_stop_playback() |
| 684 | { |
| 685 | int ret =0; |
| 686 | |
| 687 | ALOGV("audio_extn_a2dp_stop_playback start"); |
| 688 | if(!(a2dp.bt_lib_handle && a2dp.audio_stop_stream)) { |
| 689 | ALOGE("a2dp handle is not identified, Ignoring start request"); |
| 690 | return -ENOSYS; |
| 691 | } |
| 692 | |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 693 | if (a2dp.a2dp_total_active_session_request > 0) |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 694 | a2dp.a2dp_total_active_session_request--; |
| 695 | |
| 696 | if ( a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) { |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 697 | ALOGV("calling BT module stream stop"); |
| 698 | ret = a2dp.audio_stop_stream(); |
| 699 | if (ret < 0) |
| 700 | ALOGE("stop stream to BT IPC lib failed"); |
| 701 | else |
| 702 | ALOGV("stop steam to BT IPC lib successful"); |
Naresh Tanniru | 03f9dd5 | 2016-10-19 18:46:22 +0530 | [diff] [blame] | 703 | reset_a2dp_enc_config_params(); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 704 | } |
| 705 | if(!a2dp.a2dp_total_active_session_request) |
| 706 | a2dp.a2dp_started = false; |
| 707 | ALOGD("Stop A2DP playback total active sessions :%d", |
| 708 | a2dp.a2dp_total_active_session_request); |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | void audio_extn_a2dp_set_parameters(struct str_parms *parms) |
| 713 | { |
| 714 | int ret, val; |
| 715 | char value[32]={0}; |
| 716 | |
| 717 | if(a2dp.is_a2dp_offload_supported == false) { |
| 718 | ALOGV("no supported encoders identified,ignoring a2dp setparam"); |
| 719 | return; |
| 720 | } |
| 721 | |
| 722 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, |
| 723 | sizeof(value)); |
| 724 | if( ret >= 0) { |
| 725 | val = atoi(value); |
| 726 | if (val & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 727 | ALOGV("Received device connect request for A2DP"); |
| 728 | open_a2dp_output(); |
| 729 | } |
| 730 | goto param_handled; |
| 731 | } |
| 732 | |
| 733 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, |
| 734 | sizeof(value)); |
| 735 | |
| 736 | if( ret >= 0) { |
| 737 | val = atoi(value); |
| 738 | if (val & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 739 | ALOGV("Received device dis- connect request"); |
Naresh Tanniru | 03f9dd5 | 2016-10-19 18:46:22 +0530 | [diff] [blame] | 740 | reset_a2dp_enc_config_params(); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 741 | close_a2dp_output(); |
| 742 | } |
| 743 | goto param_handled; |
| 744 | } |
| 745 | |
| 746 | ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value)); |
| 747 | if (ret >= 0) { |
| 748 | if (a2dp.bt_lib_handle && (a2dp.bt_state != A2DP_STATE_DISCONNECTED) ) { |
| 749 | if ((!strncmp(value,"true",sizeof(value)))) { |
| 750 | ALOGD("Setting a2dp to suspend state"); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 751 | a2dp.a2dp_suspended = true; |
Naresh Tanniru | 03f9dd5 | 2016-10-19 18:46:22 +0530 | [diff] [blame] | 752 | reset_a2dp_enc_config_params(); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 753 | if(a2dp.audio_suspend_stream) |
| 754 | a2dp.audio_suspend_stream(); |
| 755 | } else if (a2dp.a2dp_suspended == true) { |
| 756 | ALOGD("Resetting a2dp suspend state"); |
| 757 | if(a2dp.clear_a2dpsuspend_flag) |
| 758 | a2dp.clear_a2dpsuspend_flag(); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 759 | a2dp.a2dp_suspended = false; |
Naresh Tanniru | 649871a | 2016-11-04 18:08:32 +0530 | [diff] [blame] | 760 | /* |
| 761 | * It is possible that before suspend,a2dp sessions can be active |
| 762 | * for example during music + voice activation concurrency |
| 763 | * a2dp suspend will be called & BT will change to sco mode |
| 764 | * though music is paused as a part of voice activation |
| 765 | * compress session close happens only after pause timeout(10secs) |
| 766 | * so if resume request comes before pause timeout as a2dp session |
| 767 | * is already active IPC start will not be called from APM/audio_hw |
| 768 | * Fix is to call a2dp start for IPC library post suspend |
| 769 | * based on number of active session count |
| 770 | */ |
| 771 | if (a2dp.a2dp_total_active_session_request > 0) { |
| 772 | ALOGD(" Calling IPC lib start post suspend state"); |
| 773 | if(a2dp.audio_start_stream) { |
| 774 | ret = a2dp.audio_start_stream(); |
| 775 | if (ret != 0) { |
| 776 | ALOGE("BT controller start failed"); |
| 777 | a2dp.a2dp_started = false; |
| 778 | } |
| 779 | } |
| 780 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | goto param_handled; |
| 784 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 785 | param_handled: |
| 786 | ALOGV("end of a2dp setparam"); |
| 787 | } |
| 788 | |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 789 | void audio_extn_a2dp_set_handoff_mode(bool is_on) |
| 790 | { |
| 791 | a2dp.is_handoff_in_progress = is_on; |
| 792 | } |
| 793 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 794 | bool audio_extn_a2dp_is_force_device_switch() |
| 795 | { |
| 796 | //During encoder reconfiguration mode, force a2dp device switch |
Ashish Jain | c597d10 | 2016-12-12 10:31:34 +0530 | [diff] [blame] | 797 | // Or if a2dp device is selected but earlier start failed ( as a2dp |
| 798 | // was suspended, force retry. |
| 799 | return a2dp.is_handoff_in_progress || !a2dp.a2dp_started; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 800 | } |
| 801 | |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 802 | void audio_extn_a2dp_get_apptype_params(uint32_t *sample_rate, |
| 803 | uint32_t *bit_width) |
| 804 | { |
| 805 | if(a2dp.bt_encoder_format == AUDIO_FORMAT_APTX_HD) |
| 806 | *bit_width = 24; |
| 807 | else |
| 808 | *bit_width = 16; |
| 809 | *sample_rate = a2dp.enc_sampling_rate; |
| 810 | } |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 811 | |
| 812 | bool audio_extn_a2dp_is_ready() |
| 813 | { |
| 814 | bool ret = false; |
| 815 | |
Aniket Kumar Lata | 901bcb8 | 2017-03-10 15:42:46 -0800 | [diff] [blame] | 816 | if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) && |
| 817 | (a2dp.is_a2dp_offload_supported) && |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 818 | (a2dp.audio_check_a2dp_ready)) |
| 819 | ret = a2dp.audio_check_a2dp_ready(); |
| 820 | return ret; |
| 821 | } |
| 822 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 823 | void audio_extn_a2dp_init (void *adev) |
| 824 | { |
| 825 | a2dp.adev = (struct audio_device*)adev; |
| 826 | a2dp.bt_lib_handle = NULL; |
| 827 | a2dp.a2dp_started = false; |
| 828 | a2dp.bt_state = A2DP_STATE_DISCONNECTED; |
| 829 | a2dp.a2dp_total_active_session_request = 0; |
| 830 | a2dp.a2dp_suspended = false; |
| 831 | a2dp.bt_encoder_format = AUDIO_FORMAT_INVALID; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 832 | a2dp.enc_sampling_rate = 48000; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 833 | a2dp.is_a2dp_offload_supported = false; |
| 834 | a2dp.is_handoff_in_progress = false; |
| 835 | update_offload_codec_capabilities(); |
| 836 | } |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 837 | |
| 838 | uint32_t audio_extn_a2dp_get_encoder_latency() |
| 839 | { |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 840 | uint32_t latency = 0; |
| 841 | int avsync_runtime_prop = 0; |
| 842 | int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0, aac_offset = 0; |
| 843 | char value[PROPERTY_VALUE_MAX]; |
| 844 | |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 845 | memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX); |
| 846 | avsync_runtime_prop = property_get("audio.a2dp.codec.latency", value, NULL); |
| 847 | if (avsync_runtime_prop > 0) { |
| 848 | if (sscanf(value, "%d/%d/%d/%d", |
| 849 | &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset) != 4) { |
| 850 | ALOGI("Failed to parse avsync offset params from '%s'.", value); |
| 851 | avsync_runtime_prop = 0; |
| 852 | } |
| 853 | } |
| 854 | |
Aniket Kumar Lata | faaffde | 2017-03-22 19:18:15 -0700 | [diff] [blame] | 855 | switch(a2dp.bt_encoder_format) { |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 856 | case AUDIO_FORMAT_SBC: |
| 857 | latency = (avsync_runtime_prop > 0) ? sbc_offset : 150; |
| 858 | break; |
| 859 | case AUDIO_FORMAT_APTX: |
| 860 | latency = (avsync_runtime_prop > 0) ? aptx_offset : 200; |
| 861 | break; |
| 862 | case AUDIO_FORMAT_APTX_HD: |
| 863 | latency = (avsync_runtime_prop > 0) ? aptxhd_offset : 200; |
| 864 | break; |
| 865 | case AUDIO_FORMAT_AAC: |
| 866 | latency = (avsync_runtime_prop > 0) ? aac_offset : 250; |
| 867 | break; |
| 868 | default: |
| 869 | latency = 200; |
| 870 | break; |
| 871 | } |
| 872 | return latency; |
| 873 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 874 | #endif // SPLIT_A2DP_ENABLED |