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" |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame^] | 77 | #define ENCODER_LATENCY_SBC 10 |
| 78 | #define ENCODER_LATENCY_APTX 40 |
| 79 | #define ENCODER_LATENCY_APTX_HD 20 |
| 80 | #define ENCODER_LATENCY_AAC 70 |
| 81 | #define DEFAULT_SINK_LATENCY_SBC 140 |
| 82 | #define DEFAULT_SINK_LATENCY_APTX 160 |
| 83 | #define DEFAULT_SINK_LATENCY_APTX_HD 180 |
| 84 | #define DEFAULT_SINK_LATENCY_AAC 180 |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 85 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 86 | typedef int (*audio_stream_open_t)(void); |
| 87 | typedef int (*audio_stream_close_t)(void); |
| 88 | typedef int (*audio_start_stream_t)(void); |
| 89 | typedef int (*audio_stop_stream_t)(void); |
| 90 | typedef int (*audio_suspend_stream_t)(void); |
| 91 | typedef void (*audio_handoff_triggered_t)(void); |
| 92 | typedef void (*clear_a2dpsuspend_flag_t)(void); |
| 93 | typedef void * (*audio_get_codec_config_t)(uint8_t *multicast_status,uint8_t *num_dev, |
| 94 | audio_format_t *codec_type); |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 95 | typedef int (*audio_check_a2dp_ready_t)(void); |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame^] | 96 | typedef uint16_t (*audio_get_a2dp_sink_latency_t)(void); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 97 | |
| 98 | enum A2DP_STATE { |
| 99 | A2DP_STATE_CONNECTED, |
| 100 | A2DP_STATE_STARTED, |
| 101 | A2DP_STATE_STOPPED, |
| 102 | A2DP_STATE_DISCONNECTED, |
| 103 | }; |
| 104 | |
| 105 | /* structure used to update a2dp state machine |
| 106 | * to communicate IPC library |
| 107 | * to store DSP encoder configuration information |
| 108 | */ |
| 109 | struct a2dp_data { |
| 110 | struct audio_device *adev; |
| 111 | void *bt_lib_handle; |
| 112 | audio_stream_open_t audio_stream_open; |
| 113 | audio_stream_close_t audio_stream_close; |
| 114 | audio_start_stream_t audio_start_stream; |
| 115 | audio_stop_stream_t audio_stop_stream; |
| 116 | audio_suspend_stream_t audio_suspend_stream; |
| 117 | audio_handoff_triggered_t audio_handoff_triggered; |
| 118 | clear_a2dpsuspend_flag_t clear_a2dpsuspend_flag; |
| 119 | audio_get_codec_config_t audio_get_codec_config; |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 120 | audio_check_a2dp_ready_t audio_check_a2dp_ready; |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame^] | 121 | audio_get_a2dp_sink_latency_t audio_get_a2dp_sink_latency; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 122 | enum A2DP_STATE bt_state; |
| 123 | audio_format_t bt_encoder_format; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 124 | uint32_t enc_sampling_rate; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 125 | bool a2dp_started; |
| 126 | bool a2dp_suspended; |
| 127 | int a2dp_total_active_session_request; |
| 128 | bool is_a2dp_offload_supported; |
| 129 | bool is_handoff_in_progress; |
| 130 | }; |
| 131 | |
| 132 | struct a2dp_data a2dp; |
| 133 | |
| 134 | /* START of DSP configurable structures |
| 135 | * These values should match with DSP interface defintion |
| 136 | */ |
| 137 | |
| 138 | /* AAC encoder configuration structure. */ |
| 139 | typedef struct aac_enc_cfg_t aac_enc_cfg_t; |
| 140 | |
| 141 | /* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS |
| 142 | * supported aac_fmt_flag are ADTS/RAW |
| 143 | * supported channel_cfg are Native mode, Mono , Stereo |
| 144 | */ |
| 145 | struct aac_enc_cfg_t { |
| 146 | uint32_t enc_format; |
| 147 | uint32_t bit_rate; |
| 148 | uint32_t enc_mode; |
| 149 | uint16_t aac_fmt_flag; |
Naresh Tanniru | a42d0bd | 2016-09-21 15:30:46 +0530 | [diff] [blame] | 150 | uint16_t channel_cfg; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 151 | uint32_t sample_rate; |
| 152 | } ; |
| 153 | |
| 154 | /* SBC encoder configuration structure. */ |
| 155 | typedef struct sbc_enc_cfg_t sbc_enc_cfg_t; |
| 156 | |
| 157 | /* supported num_subbands are 4/8 |
| 158 | * supported blk_len are 4, 8, 12, 16 |
| 159 | * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO |
| 160 | * supported alloc_method are LOUNDNESS/SNR |
| 161 | * supported bit_rate for mono channel is max 320kbps |
| 162 | * supported bit rate for stereo channel is max 512 kbps |
| 163 | */ |
| 164 | struct sbc_enc_cfg_t{ |
| 165 | uint32_t enc_format; |
| 166 | uint32_t num_subbands; |
| 167 | uint32_t blk_len; |
| 168 | uint32_t channel_mode; |
| 169 | uint32_t alloc_method; |
| 170 | uint32_t bit_rate; |
| 171 | uint32_t sample_rate; |
| 172 | }; |
| 173 | |
| 174 | |
| 175 | /* supported num_channels are Mono/Stereo |
| 176 | * supported channel_mapping for mono is CHANNEL_C |
| 177 | * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R |
| 178 | * custom size and reserved are not used(for future enhancement) |
| 179 | */ |
| 180 | struct custom_enc_cfg_aptx_t |
| 181 | { |
| 182 | uint32_t enc_format; |
| 183 | uint32_t sample_rate; |
| 184 | uint16_t num_channels; |
| 185 | uint16_t reserved; |
| 186 | uint8_t channel_mapping[8]; |
| 187 | uint32_t custom_size; |
| 188 | }; |
| 189 | |
Satya Krishna Pindiproli | f7d6571 | 2017-04-26 14:24:53 +0530 | [diff] [blame] | 190 | /* TODO: Define the following structures only for O using PLATFORM_VERSION */ |
| 191 | /* Information about BT SBC encoder configuration |
| 192 | * This data is used between audio HAL module and |
| 193 | * BT IPC library to configure DSP encoder |
| 194 | */ |
| 195 | typedef struct { |
| 196 | uint32_t subband; /* 4, 8 */ |
| 197 | uint32_t blk_len; /* 4, 8, 12, 16 */ |
| 198 | uint16_t sampling_rate; /*44.1khz,48khz*/ |
| 199 | uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/ |
| 200 | uint8_t alloc; /*0(Loudness),1(SNR)*/ |
| 201 | uint8_t min_bitpool; /* 2 */ |
| 202 | uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */ |
| 203 | uint32_t bitrate; /* 320kbps to 512kbps */ |
| 204 | } audio_sbc_encoder_config; |
| 205 | |
| 206 | |
| 207 | /* Information about BT APTX encoder configuration |
| 208 | * This data is used between audio HAL module and |
| 209 | * BT IPC library to configure DSP encoder |
| 210 | */ |
| 211 | typedef struct { |
| 212 | uint16_t sampling_rate; |
| 213 | uint8_t channels; |
| 214 | uint32_t bitrate; |
| 215 | } audio_aptx_encoder_config; |
| 216 | |
| 217 | |
| 218 | /* Information about BT AAC encoder configuration |
| 219 | * This data is used between audio HAL module and |
| 220 | * BT IPC library to configure DSP encoder |
| 221 | */ |
| 222 | typedef struct { |
| 223 | uint32_t enc_mode; /* LC, SBR, PS */ |
| 224 | uint16_t format_flag; /* RAW, ADTS */ |
| 225 | uint16_t channels; /* 1-Mono, 2-Stereo */ |
| 226 | uint32_t sampling_rate; |
| 227 | uint32_t bitrate; |
| 228 | } audio_aac_encoder_config; |
| 229 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 230 | /*********** END of DSP configurable structures ********************/ |
| 231 | |
| 232 | /* API to identify DSP encoder captabilities */ |
| 233 | static void a2dp_offload_codec_cap_parser(char *value) |
| 234 | { |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 235 | char *tok = NULL,*saveptr; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 236 | |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 237 | tok = strtok_r(value, "-", &saveptr); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 238 | while (tok != NULL) { |
| 239 | if (strcmp(tok, "sbc") == 0) { |
| 240 | ALOGD("%s: SBC offload supported\n",__func__); |
| 241 | a2dp.is_a2dp_offload_supported = true; |
| 242 | break; |
| 243 | } else if (strcmp(tok, "aptx") == 0) { |
| 244 | ALOGD("%s: aptx offload supported\n",__func__); |
| 245 | a2dp.is_a2dp_offload_supported = true; |
| 246 | break; |
Naresh Tanniru | ed694c8 | 2017-02-07 17:01:28 +0530 | [diff] [blame] | 247 | } else if (strcmp(tok, "aptxhd") == 0) { |
| 248 | ALOGD("%s: aptx HD offload supported\n",__func__); |
| 249 | a2dp.is_a2dp_offload_supported = true; |
| 250 | break; |
| 251 | } else if (strcmp(tok, "aac") == 0) { |
| 252 | ALOGD("%s: aac offload supported\n",__func__); |
| 253 | a2dp.is_a2dp_offload_supported = true; |
| 254 | break; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 255 | } |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 256 | tok = strtok_r(NULL, "-", &saveptr); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 257 | }; |
| 258 | } |
| 259 | |
| 260 | static void update_offload_codec_capabilities() |
| 261 | { |
| 262 | char value[PROPERTY_VALUE_MAX] = {'\0'}; |
| 263 | |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 264 | property_get("persist.vendor.bt.a2dp_offload_cap", value, "false"); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 265 | ALOGD("get_offload_codec_capabilities = %s",value); |
| 266 | a2dp.is_a2dp_offload_supported = |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 267 | property_get_bool("persist.vendor.bt.a2dp_offload_cap", false); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 268 | if (strcmp(value, "false") != 0) |
| 269 | a2dp_offload_codec_cap_parser(value); |
| 270 | ALOGD("%s: codec cap = %s",__func__,value); |
| 271 | } |
| 272 | |
| 273 | /* API to open BT IPC library to start IPC communication */ |
| 274 | static void open_a2dp_output() |
| 275 | { |
| 276 | int ret = 0; |
| 277 | |
| 278 | ALOGD(" Open A2DP output start "); |
| 279 | if (a2dp.bt_lib_handle == NULL){ |
| 280 | ALOGD(" Requesting for BT lib handle"); |
| 281 | a2dp.bt_lib_handle = dlopen(BT_IPC_LIB_NAME, RTLD_NOW); |
| 282 | |
| 283 | if (a2dp.bt_lib_handle == NULL) { |
| 284 | ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_LIB_NAME); |
| 285 | ret = -ENOSYS; |
| 286 | goto init_fail; |
| 287 | } else { |
| 288 | a2dp.audio_stream_open = (audio_stream_open_t) |
| 289 | dlsym(a2dp.bt_lib_handle, "audio_stream_open"); |
| 290 | a2dp.audio_start_stream = (audio_start_stream_t) |
| 291 | dlsym(a2dp.bt_lib_handle, "audio_start_stream"); |
| 292 | a2dp.audio_get_codec_config = (audio_get_codec_config_t) |
| 293 | dlsym(a2dp.bt_lib_handle, "audio_get_codec_config"); |
| 294 | a2dp.audio_suspend_stream = (audio_suspend_stream_t) |
| 295 | dlsym(a2dp.bt_lib_handle, "audio_suspend_stream"); |
| 296 | a2dp.audio_handoff_triggered = (audio_handoff_triggered_t) |
| 297 | dlsym(a2dp.bt_lib_handle, "audio_handoff_triggered"); |
| 298 | a2dp.clear_a2dpsuspend_flag = (clear_a2dpsuspend_flag_t) |
| 299 | dlsym(a2dp.bt_lib_handle, "clear_a2dpsuspend_flag"); |
| 300 | a2dp.audio_stop_stream = (audio_stop_stream_t) |
| 301 | dlsym(a2dp.bt_lib_handle, "audio_stop_stream"); |
| 302 | a2dp.audio_stream_close = (audio_stream_close_t) |
| 303 | dlsym(a2dp.bt_lib_handle, "audio_stream_close"); |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 304 | a2dp.audio_check_a2dp_ready = (audio_check_a2dp_ready_t) |
| 305 | dlsym(a2dp.bt_lib_handle,"audio_check_a2dp_ready"); |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame^] | 306 | a2dp.audio_get_a2dp_sink_latency = (audio_get_a2dp_sink_latency_t) |
| 307 | dlsym(a2dp.bt_lib_handle,"audio_get_a2dp_sink_latency"); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | if (a2dp.bt_lib_handle && a2dp.audio_stream_open) { |
| 312 | if (a2dp.bt_state == A2DP_STATE_DISCONNECTED) { |
| 313 | ALOGD("calling BT stream open"); |
| 314 | ret = a2dp.audio_stream_open(); |
| 315 | if(ret != 0) { |
| 316 | ALOGE("Failed to open output stream for a2dp: status %d", ret); |
| 317 | goto init_fail; |
| 318 | } |
| 319 | a2dp.bt_state = A2DP_STATE_CONNECTED; |
| 320 | } else { |
| 321 | ALOGD("Called a2dp open with improper state, Ignoring request state %d", a2dp.bt_state); |
| 322 | } |
| 323 | } else { |
| 324 | ALOGE("a2dp handle is not identified, Ignoring open request"); |
| 325 | a2dp.bt_state = A2DP_STATE_DISCONNECTED; |
| 326 | goto init_fail; |
| 327 | } |
| 328 | |
| 329 | init_fail: |
| 330 | if(ret != 0 && (a2dp.bt_lib_handle != NULL)) { |
| 331 | dlclose(a2dp.bt_lib_handle); |
| 332 | a2dp.bt_lib_handle = NULL; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | static int close_a2dp_output() |
| 337 | { |
| 338 | ALOGV("%s\n",__func__); |
| 339 | if (!(a2dp.bt_lib_handle && a2dp.audio_stream_close)) { |
| 340 | ALOGE("a2dp handle is not identified, Ignoring close request"); |
| 341 | return -ENOSYS; |
| 342 | } |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 343 | if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) { |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 344 | ALOGD("calling BT stream close"); |
| 345 | if(a2dp.audio_stream_close() == false) |
| 346 | ALOGE("failed close a2dp control path from BT library"); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 347 | } |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 348 | a2dp.a2dp_started = false; |
| 349 | a2dp.a2dp_total_active_session_request = 0; |
| 350 | a2dp.a2dp_suspended = false; |
| 351 | a2dp.bt_encoder_format = AUDIO_FORMAT_INVALID; |
| 352 | a2dp.enc_sampling_rate = 48000; |
| 353 | a2dp.bt_state = A2DP_STATE_DISCONNECTED; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | /* API to configure SBC DSP encoder */ |
| 359 | bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg) |
| 360 | { |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 361 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 362 | struct sbc_enc_cfg_t sbc_dsp_cfg; |
| 363 | bool is_configured = false; |
| 364 | int ret = 0; |
| 365 | |
| 366 | if(sbc_bt_cfg == NULL) |
| 367 | return false; |
| 368 | |
| 369 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 370 | if (!ctl_enc_data) { |
| 371 | ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed"); |
| 372 | is_configured = false; |
| 373 | goto fail; |
| 374 | } |
| 375 | a2dp.bt_encoder_format = AUDIO_FORMAT_SBC; |
| 376 | memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t)); |
| 377 | sbc_dsp_cfg.enc_format = ENC_MEDIA_FMT_SBC; |
| 378 | sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband; |
| 379 | sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len; |
| 380 | switch(sbc_bt_cfg->channels) { |
| 381 | case 0: |
| 382 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO; |
| 383 | break; |
| 384 | case 1: |
| 385 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO; |
| 386 | break; |
| 387 | case 3: |
| 388 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO; |
| 389 | break; |
| 390 | case 2: |
| 391 | default: |
| 392 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO; |
| 393 | break; |
| 394 | } |
| 395 | if (sbc_bt_cfg->alloc) |
| 396 | sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS; |
| 397 | else |
| 398 | sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR; |
| 399 | sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate; |
| 400 | sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate; |
| 401 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg, |
| 402 | sizeof(struct sbc_enc_cfg_t)); |
| 403 | if (ret != 0) { |
| 404 | ALOGE("%s: failed to set SBC encoder config", __func__); |
| 405 | is_configured = false; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 406 | goto fail; |
| 407 | } |
| 408 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 409 | MIXER_ENC_BIT_FORMAT); |
| 410 | if (!ctrl_bit_format) { |
| 411 | ALOGE(" ERROR bit format CONFIG data mixer control not identifed"); |
| 412 | is_configured = false; |
| 413 | goto fail; |
| 414 | } |
| 415 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE"); |
| 416 | if (ret != 0) { |
| 417 | ALOGE("%s: Failed to set bit format to encoder", __func__); |
| 418 | is_configured = false; |
| 419 | goto fail; |
| 420 | } |
| 421 | is_configured = true; |
| 422 | a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate; |
| 423 | ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d", |
| 424 | sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 425 | fail: |
| 426 | return is_configured; |
| 427 | } |
| 428 | |
| 429 | /* API to configure APTX DSP encoder */ |
| 430 | bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg) |
| 431 | { |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 432 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 433 | struct custom_enc_cfg_aptx_t aptx_dsp_cfg; |
| 434 | bool is_configured = false; |
| 435 | int ret = 0; |
| 436 | |
| 437 | if(aptx_bt_cfg == NULL) |
| 438 | return false; |
| 439 | |
| 440 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 441 | if (!ctl_enc_data) { |
| 442 | ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed"); |
| 443 | is_configured = false; |
| 444 | goto fail; |
| 445 | } |
| 446 | a2dp.bt_encoder_format = AUDIO_FORMAT_APTX; |
| 447 | memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_aptx_t)); |
| 448 | aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX; |
| 449 | aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate; |
| 450 | aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels; |
| 451 | switch(aptx_dsp_cfg.num_channels) { |
| 452 | case 1: |
| 453 | aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C; |
| 454 | break; |
| 455 | case 2: |
| 456 | default: |
| 457 | aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L; |
| 458 | aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R; |
| 459 | break; |
| 460 | } |
| 461 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg, |
| 462 | sizeof(struct custom_enc_cfg_aptx_t)); |
| 463 | if (ret != 0) { |
| 464 | ALOGE("%s: Failed to set APTX encoder config", __func__); |
| 465 | is_configured = false; |
| 466 | goto fail; |
| 467 | } |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 468 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 469 | MIXER_ENC_BIT_FORMAT); |
| 470 | if (!ctrl_bit_format) { |
| 471 | ALOGE("ERROR bit format CONFIG data mixer control not identifed"); |
| 472 | is_configured = false; |
| 473 | goto fail; |
| 474 | } else { |
| 475 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE"); |
| 476 | if (ret != 0) { |
| 477 | ALOGE("%s: Failed to set bit format to encoder", __func__); |
| 478 | is_configured = false; |
| 479 | goto fail; |
| 480 | } |
| 481 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 482 | is_configured = true; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 483 | a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate; |
| 484 | ALOGV("Successfully updated APTX enc format with samplingrate: %d channels:%d", |
| 485 | aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 486 | fail: |
| 487 | return is_configured; |
| 488 | } |
| 489 | |
| 490 | /* API to configure APTX HD DSP encoder |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 491 | */ |
| 492 | bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg) |
| 493 | { |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 494 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 495 | struct custom_enc_cfg_aptx_t aptx_dsp_cfg; |
| 496 | bool is_configured = false; |
| 497 | int ret = 0; |
| 498 | |
| 499 | if(aptx_bt_cfg == NULL) |
| 500 | return false; |
| 501 | |
| 502 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 503 | if (!ctl_enc_data) { |
| 504 | ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed"); |
| 505 | is_configured = false; |
| 506 | goto fail; |
| 507 | } |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 508 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 509 | a2dp.bt_encoder_format = AUDIO_FORMAT_APTX_HD; |
| 510 | memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_aptx_t)); |
| 511 | aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX_HD; |
| 512 | aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate; |
| 513 | aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels; |
| 514 | switch(aptx_dsp_cfg.num_channels) { |
| 515 | case 1: |
| 516 | aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C; |
| 517 | break; |
| 518 | case 2: |
| 519 | default: |
| 520 | aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L; |
| 521 | aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R; |
| 522 | break; |
| 523 | } |
| 524 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg, |
| 525 | sizeof(struct custom_enc_cfg_aptx_t)); |
| 526 | if (ret != 0) { |
| 527 | ALOGE("%s: Failed to set APTX HD encoder config", __func__); |
| 528 | is_configured = false; |
| 529 | goto fail; |
| 530 | } |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 531 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_BIT_FORMAT); |
| 532 | if (!ctrl_bit_format) { |
| 533 | ALOGE(" ERROR bit format CONFIG data mixer control not identifed"); |
| 534 | is_configured = false; |
| 535 | goto fail; |
| 536 | } |
| 537 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S24_LE"); |
| 538 | if (ret != 0) { |
| 539 | ALOGE("%s: Failed to set APTX HD encoder config", __func__); |
| 540 | is_configured = false; |
| 541 | goto fail; |
| 542 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 543 | is_configured = true; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 544 | a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate; |
| 545 | ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d", |
| 546 | aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 547 | fail: |
| 548 | return is_configured; |
| 549 | } |
| 550 | |
| 551 | /* API to configure AAC DSP encoder */ |
| 552 | bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg) |
| 553 | { |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 554 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 555 | struct aac_enc_cfg_t aac_dsp_cfg; |
| 556 | bool is_configured = false; |
| 557 | int ret = 0; |
| 558 | |
| 559 | if(aac_bt_cfg == NULL) |
| 560 | return false; |
| 561 | |
| 562 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 563 | if (!ctl_enc_data) { |
| 564 | ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed"); |
| 565 | is_configured = false; |
| 566 | goto fail; |
| 567 | } |
| 568 | a2dp.bt_encoder_format = AUDIO_FORMAT_AAC; |
| 569 | memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t)); |
| 570 | aac_dsp_cfg.enc_format = ENC_MEDIA_FMT_AAC; |
| 571 | aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate; |
Naresh Tanniru | a42d0bd | 2016-09-21 15:30:46 +0530 | [diff] [blame] | 572 | aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 573 | switch(aac_bt_cfg->enc_mode) { |
| 574 | case 0: |
| 575 | aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC; |
| 576 | break; |
| 577 | case 2: |
| 578 | aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS; |
| 579 | break; |
| 580 | case 1: |
| 581 | default: |
| 582 | aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR; |
| 583 | break; |
| 584 | } |
Naresh Tanniru | a42d0bd | 2016-09-21 15:30:46 +0530 | [diff] [blame] | 585 | aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 586 | aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels; |
| 587 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg, |
| 588 | sizeof(struct aac_enc_cfg_t)); |
| 589 | if (ret != 0) { |
| 590 | ALOGE("%s: failed to set SBC encoder config", __func__); |
| 591 | is_configured = false; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 592 | goto fail; |
| 593 | } |
| 594 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 595 | MIXER_ENC_BIT_FORMAT); |
| 596 | if (!ctrl_bit_format) { |
| 597 | is_configured = false; |
| 598 | ALOGE(" ERROR bit format CONFIG data mixer control not identifed"); |
| 599 | goto fail; |
| 600 | } |
| 601 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE"); |
| 602 | if (ret != 0) { |
| 603 | ALOGE("%s: Failed to set bit format to encoder", __func__); |
| 604 | is_configured = false; |
| 605 | goto fail; |
| 606 | } |
| 607 | is_configured = true; |
| 608 | a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate; |
| 609 | ALOGV("Successfully updated AAC enc format with samplingrate: %d channels:%d", |
| 610 | aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 611 | fail: |
| 612 | return is_configured; |
| 613 | } |
| 614 | |
| 615 | bool configure_a2dp_encoder_format() |
| 616 | { |
| 617 | void *codec_info = NULL; |
| 618 | uint8_t multi_cast = 0, num_dev = 1; |
| 619 | audio_format_t codec_type = AUDIO_FORMAT_INVALID; |
| 620 | bool is_configured = false; |
| 621 | |
| 622 | if (!a2dp.audio_get_codec_config) { |
| 623 | ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config"); |
| 624 | return false; |
| 625 | } |
| 626 | ALOGD("configure_a2dp_encoder_format start"); |
| 627 | codec_info = a2dp.audio_get_codec_config(&multi_cast, &num_dev, |
| 628 | &codec_type); |
| 629 | |
| 630 | switch(codec_type) { |
| 631 | case AUDIO_FORMAT_SBC: |
| 632 | ALOGD(" Received SBC encoder supported BT device"); |
| 633 | is_configured = |
| 634 | configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info); |
| 635 | break; |
| 636 | case AUDIO_FORMAT_APTX: |
| 637 | ALOGD(" Received APTX encoder supported BT device"); |
| 638 | is_configured = |
| 639 | configure_aptx_enc_format((audio_aptx_encoder_config *)codec_info); |
| 640 | break; |
| 641 | case AUDIO_FORMAT_APTX_HD: |
| 642 | ALOGD(" Received APTX HD encoder supported BT device"); |
| 643 | is_configured = |
| 644 | configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info); |
| 645 | break; |
| 646 | case AUDIO_FORMAT_AAC: |
| 647 | ALOGD(" Received AAC encoder supported BT device"); |
| 648 | is_configured = |
| 649 | configure_aac_enc_format((audio_aac_encoder_config *)codec_info); |
| 650 | break; |
| 651 | default: |
| 652 | ALOGD(" Received Unsupported encoder formar"); |
| 653 | is_configured = false; |
| 654 | break; |
| 655 | } |
| 656 | return is_configured; |
| 657 | } |
| 658 | |
| 659 | int audio_extn_a2dp_start_playback() |
| 660 | { |
| 661 | int ret = 0; |
| 662 | |
| 663 | ALOGD("audio_extn_a2dp_start_playback start"); |
| 664 | |
| 665 | if(!(a2dp.bt_lib_handle && a2dp.audio_start_stream |
| 666 | && a2dp.audio_get_codec_config)) { |
| 667 | ALOGE("a2dp handle is not identified, Ignoring start request"); |
| 668 | return -ENOSYS; |
| 669 | } |
| 670 | |
| 671 | if(a2dp.a2dp_suspended == true) { |
| 672 | //session will be restarted after suspend completion |
| 673 | ALOGD("a2dp start requested during suspend state"); |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 674 | return -ENOSYS; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | if (!a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) { |
| 678 | ALOGD("calling BT module stream start"); |
| 679 | /* This call indicates BT IPC lib to start playback */ |
| 680 | ret = a2dp.audio_start_stream(); |
| 681 | ALOGE("BT controller start return = %d",ret); |
| 682 | if (ret != 0 ) { |
| 683 | ALOGE("BT controller start failed"); |
| 684 | a2dp.a2dp_started = false; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 685 | } else { |
| 686 | if(configure_a2dp_encoder_format() == true) { |
| 687 | a2dp.a2dp_started = true; |
| 688 | ret = 0; |
| 689 | ALOGD("Start playback successful to BT library"); |
| 690 | } else { |
| 691 | ALOGD(" unable to configure DSP encoder"); |
| 692 | a2dp.a2dp_started = false; |
| 693 | ret = -ETIMEDOUT; |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | if (a2dp.a2dp_started) |
| 699 | a2dp.a2dp_total_active_session_request++; |
| 700 | |
| 701 | ALOGD("start A2DP playback total active sessions :%d", |
| 702 | a2dp.a2dp_total_active_session_request); |
| 703 | return ret; |
| 704 | } |
| 705 | |
Naresh Tanniru | 03f9dd5 | 2016-10-19 18:46:22 +0530 | [diff] [blame] | 706 | static void reset_a2dp_enc_config_params() |
| 707 | { |
| 708 | int ret =0; |
| 709 | |
| 710 | struct mixer_ctl *ctl_enc_config, *ctrl_bit_format; |
| 711 | struct sbc_enc_cfg_t dummy_reset_config; |
| 712 | |
| 713 | memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t)); |
| 714 | ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 715 | MIXER_ENC_CONFIG_BLOCK); |
| 716 | if (!ctl_enc_config) { |
| 717 | ALOGE(" ERROR a2dp encoder format mixer control not identifed"); |
| 718 | } else { |
| 719 | ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config, |
| 720 | sizeof(struct sbc_enc_cfg_t)); |
| 721 | a2dp.bt_encoder_format = ENC_MEDIA_FMT_NONE; |
| 722 | } |
| 723 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 724 | MIXER_ENC_BIT_FORMAT); |
| 725 | if (!ctrl_bit_format) { |
| 726 | ALOGE(" ERROR bit format CONFIG data mixer control not identifed"); |
| 727 | } else { |
| 728 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE"); |
| 729 | if (ret != 0) { |
| 730 | ALOGE("%s: Failed to set bit format to encoder", __func__); |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 735 | int audio_extn_a2dp_stop_playback() |
| 736 | { |
| 737 | int ret =0; |
| 738 | |
| 739 | ALOGV("audio_extn_a2dp_stop_playback start"); |
| 740 | if(!(a2dp.bt_lib_handle && a2dp.audio_stop_stream)) { |
| 741 | ALOGE("a2dp handle is not identified, Ignoring start request"); |
| 742 | return -ENOSYS; |
| 743 | } |
| 744 | |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 745 | if (a2dp.a2dp_total_active_session_request > 0) |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 746 | a2dp.a2dp_total_active_session_request--; |
| 747 | |
| 748 | if ( a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) { |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 749 | ALOGV("calling BT module stream stop"); |
| 750 | ret = a2dp.audio_stop_stream(); |
| 751 | if (ret < 0) |
| 752 | ALOGE("stop stream to BT IPC lib failed"); |
| 753 | else |
| 754 | ALOGV("stop steam to BT IPC lib successful"); |
Naresh Tanniru | 03f9dd5 | 2016-10-19 18:46:22 +0530 | [diff] [blame] | 755 | reset_a2dp_enc_config_params(); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 756 | } |
| 757 | if(!a2dp.a2dp_total_active_session_request) |
| 758 | a2dp.a2dp_started = false; |
| 759 | ALOGD("Stop A2DP playback total active sessions :%d", |
| 760 | a2dp.a2dp_total_active_session_request); |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | void audio_extn_a2dp_set_parameters(struct str_parms *parms) |
| 765 | { |
| 766 | int ret, val; |
| 767 | char value[32]={0}; |
| 768 | |
| 769 | if(a2dp.is_a2dp_offload_supported == false) { |
| 770 | ALOGV("no supported encoders identified,ignoring a2dp setparam"); |
| 771 | return; |
| 772 | } |
| 773 | |
| 774 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, |
| 775 | sizeof(value)); |
| 776 | if( ret >= 0) { |
| 777 | val = atoi(value); |
| 778 | if (val & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 779 | ALOGV("Received device connect request for A2DP"); |
| 780 | open_a2dp_output(); |
| 781 | } |
| 782 | goto param_handled; |
| 783 | } |
| 784 | |
| 785 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, |
| 786 | sizeof(value)); |
| 787 | |
| 788 | if( ret >= 0) { |
| 789 | val = atoi(value); |
| 790 | if (val & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 791 | ALOGV("Received device dis- connect request"); |
Naresh Tanniru | 03f9dd5 | 2016-10-19 18:46:22 +0530 | [diff] [blame] | 792 | reset_a2dp_enc_config_params(); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 793 | close_a2dp_output(); |
| 794 | } |
| 795 | goto param_handled; |
| 796 | } |
| 797 | |
| 798 | ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value)); |
| 799 | if (ret >= 0) { |
| 800 | if (a2dp.bt_lib_handle && (a2dp.bt_state != A2DP_STATE_DISCONNECTED) ) { |
| 801 | if ((!strncmp(value,"true",sizeof(value)))) { |
| 802 | ALOGD("Setting a2dp to suspend state"); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 803 | a2dp.a2dp_suspended = true; |
Naresh Tanniru | 03f9dd5 | 2016-10-19 18:46:22 +0530 | [diff] [blame] | 804 | reset_a2dp_enc_config_params(); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 805 | if(a2dp.audio_suspend_stream) |
| 806 | a2dp.audio_suspend_stream(); |
| 807 | } else if (a2dp.a2dp_suspended == true) { |
| 808 | ALOGD("Resetting a2dp suspend state"); |
Zhou Song | 10617ed | 2017-05-26 13:28:48 +0800 | [diff] [blame] | 809 | struct audio_usecase *uc_info; |
| 810 | struct listnode *node; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 811 | if(a2dp.clear_a2dpsuspend_flag) |
| 812 | a2dp.clear_a2dpsuspend_flag(); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 813 | a2dp.a2dp_suspended = false; |
Naresh Tanniru | 649871a | 2016-11-04 18:08:32 +0530 | [diff] [blame] | 814 | /* |
| 815 | * It is possible that before suspend,a2dp sessions can be active |
| 816 | * for example during music + voice activation concurrency |
| 817 | * a2dp suspend will be called & BT will change to sco mode |
| 818 | * though music is paused as a part of voice activation |
| 819 | * compress session close happens only after pause timeout(10secs) |
| 820 | * so if resume request comes before pause timeout as a2dp session |
| 821 | * is already active IPC start will not be called from APM/audio_hw |
| 822 | * Fix is to call a2dp start for IPC library post suspend |
| 823 | * based on number of active session count |
| 824 | */ |
| 825 | if (a2dp.a2dp_total_active_session_request > 0) { |
| 826 | ALOGD(" Calling IPC lib start post suspend state"); |
| 827 | if(a2dp.audio_start_stream) { |
| 828 | ret = a2dp.audio_start_stream(); |
| 829 | if (ret != 0) { |
| 830 | ALOGE("BT controller start failed"); |
| 831 | a2dp.a2dp_started = false; |
| 832 | } |
| 833 | } |
| 834 | } |
Zhou Song | 10617ed | 2017-05-26 13:28:48 +0800 | [diff] [blame] | 835 | // restore A2DP device for active usecases |
| 836 | list_for_each(node, &a2dp.adev->usecase_list) { |
| 837 | uc_info = node_to_item(node, struct audio_usecase, list); |
| 838 | if ((uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && |
| 839 | (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP)) |
| 840 | select_devices(a2dp.adev, uc_info->id); |
| 841 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | goto param_handled; |
| 845 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 846 | param_handled: |
| 847 | ALOGV("end of a2dp setparam"); |
| 848 | } |
| 849 | |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 850 | void audio_extn_a2dp_set_handoff_mode(bool is_on) |
| 851 | { |
| 852 | a2dp.is_handoff_in_progress = is_on; |
| 853 | } |
| 854 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 855 | bool audio_extn_a2dp_is_force_device_switch() |
| 856 | { |
| 857 | //During encoder reconfiguration mode, force a2dp device switch |
Ashish Jain | c597d10 | 2016-12-12 10:31:34 +0530 | [diff] [blame] | 858 | // Or if a2dp device is selected but earlier start failed ( as a2dp |
| 859 | // was suspended, force retry. |
| 860 | return a2dp.is_handoff_in_progress || !a2dp.a2dp_started; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 861 | } |
| 862 | |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 863 | void audio_extn_a2dp_get_apptype_params(uint32_t *sample_rate, |
| 864 | uint32_t *bit_width) |
| 865 | { |
| 866 | if(a2dp.bt_encoder_format == AUDIO_FORMAT_APTX_HD) |
| 867 | *bit_width = 24; |
| 868 | else |
| 869 | *bit_width = 16; |
| 870 | *sample_rate = a2dp.enc_sampling_rate; |
| 871 | } |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 872 | |
| 873 | bool audio_extn_a2dp_is_ready() |
| 874 | { |
| 875 | bool ret = false; |
| 876 | |
Aniket Kumar Lata | 901bcb8 | 2017-03-10 15:42:46 -0800 | [diff] [blame] | 877 | if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) && |
| 878 | (a2dp.is_a2dp_offload_supported) && |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 879 | (a2dp.audio_check_a2dp_ready)) |
| 880 | ret = a2dp.audio_check_a2dp_ready(); |
| 881 | return ret; |
| 882 | } |
| 883 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 884 | void audio_extn_a2dp_init (void *adev) |
| 885 | { |
| 886 | a2dp.adev = (struct audio_device*)adev; |
| 887 | a2dp.bt_lib_handle = NULL; |
| 888 | a2dp.a2dp_started = false; |
| 889 | a2dp.bt_state = A2DP_STATE_DISCONNECTED; |
| 890 | a2dp.a2dp_total_active_session_request = 0; |
| 891 | a2dp.a2dp_suspended = false; |
| 892 | a2dp.bt_encoder_format = AUDIO_FORMAT_INVALID; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 893 | a2dp.enc_sampling_rate = 48000; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 894 | a2dp.is_a2dp_offload_supported = false; |
| 895 | a2dp.is_handoff_in_progress = false; |
| 896 | update_offload_codec_capabilities(); |
| 897 | } |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 898 | |
| 899 | uint32_t audio_extn_a2dp_get_encoder_latency() |
| 900 | { |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 901 | uint32_t latency = 0; |
| 902 | int avsync_runtime_prop = 0; |
| 903 | int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0, aac_offset = 0; |
| 904 | char value[PROPERTY_VALUE_MAX]; |
| 905 | |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 906 | memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX); |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 907 | avsync_runtime_prop = property_get("vendor.audio.a2dp.codec.latency", value, NULL); |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 908 | if (avsync_runtime_prop > 0) { |
| 909 | if (sscanf(value, "%d/%d/%d/%d", |
| 910 | &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset) != 4) { |
| 911 | ALOGI("Failed to parse avsync offset params from '%s'.", value); |
| 912 | avsync_runtime_prop = 0; |
| 913 | } |
| 914 | } |
| 915 | |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame^] | 916 | uint32_t slatency = 0; |
| 917 | if (a2dp.audio_get_a2dp_sink_latency && a2dp.bt_state != A2DP_STATE_DISCONNECTED) { |
| 918 | slatency = a2dp.audio_get_a2dp_sink_latency(); |
| 919 | } |
| 920 | |
Aniket Kumar Lata | faaffde | 2017-03-22 19:18:15 -0700 | [diff] [blame] | 921 | switch(a2dp.bt_encoder_format) { |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 922 | case AUDIO_FORMAT_SBC: |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame^] | 923 | latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC; |
| 924 | latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency; |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 925 | break; |
| 926 | case AUDIO_FORMAT_APTX: |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame^] | 927 | latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX; |
| 928 | latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency; |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 929 | break; |
| 930 | case AUDIO_FORMAT_APTX_HD: |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame^] | 931 | latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD; |
| 932 | latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency; |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 933 | break; |
| 934 | case AUDIO_FORMAT_AAC: |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame^] | 935 | latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC; |
| 936 | latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency; |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 937 | break; |
| 938 | default: |
| 939 | latency = 200; |
| 940 | break; |
| 941 | } |
| 942 | return latency; |
| 943 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 944 | #endif // SPLIT_A2DP_ENABLED |