Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1 | /* |
Preetam Singh Ranawat | 109bb3c | 2018-01-30 12:12:02 +0530 | [diff] [blame] | 2 | * Copyright (c) 2015-2018, 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" |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 38 | #include "audio_extn.h" |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | #include <cutils/str_parms.h> |
| 41 | #include <hardware/audio.h> |
| 42 | #include <hardware/hardware.h> |
| 43 | #include <cutils/properties.h> |
| 44 | |
Revathi Uddaraju | 1eac8b0 | 2017-05-18 17:13:33 +0530 | [diff] [blame] | 45 | #ifdef DYNAMIC_LOG_ENABLED |
| 46 | #include <log_xml_parser.h> |
| 47 | #define LOG_MASK HAL_MOD_FILE_A2DP |
| 48 | #include <log_utils.h> |
| 49 | #endif |
| 50 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 51 | #ifdef SPLIT_A2DP_ENABLED |
| 52 | #define AUDIO_PARAMETER_A2DP_STARTED "A2dpStarted" |
| 53 | #define BT_IPC_LIB_NAME "libbthost_if.so" |
| 54 | #define ENC_MEDIA_FMT_NONE 0 |
| 55 | #define ENC_MEDIA_FMT_AAC 0x00010DA6 |
| 56 | #define ENC_MEDIA_FMT_APTX 0x000131ff |
| 57 | #define ENC_MEDIA_FMT_APTX_HD 0x00013200 |
| 58 | #define ENC_MEDIA_FMT_SBC 0x00010BF2 |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 59 | #define ENC_MEDIA_FMT_CELT 0x00013221 |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 60 | #define ENC_MEDIA_FMT_LDAC 0x00013224 |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 61 | #define MEDIA_FMT_AAC_AOT_LC 2 |
| 62 | #define MEDIA_FMT_AAC_AOT_SBR 5 |
| 63 | #define MEDIA_FMT_AAC_AOT_PS 29 |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 64 | #define PCM_CHANNEL_L 1 |
| 65 | #define PCM_CHANNEL_R 2 |
| 66 | #define PCM_CHANNEL_C 3 |
| 67 | #define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1 |
| 68 | #define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2 |
| 69 | #define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8 |
| 70 | #define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9 |
| 71 | #define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0 |
| 72 | #define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1 |
| 73 | #define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config" |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 74 | #define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format" |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 75 | #define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode" |
| 76 | #define MIXER_SAMPLE_RATE "BT SampleRate" |
| 77 | #define MIXER_AFE_IN_CHANNELS "AFE Input Channels" |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 78 | #define MIXER_ENC_FMT_SBC "SBC" |
| 79 | #define MIXER_ENC_FMT_AAC "AAC" |
| 80 | #define MIXER_ENC_FMT_APTX "APTX" |
| 81 | #define MIXER_ENC_FMT_APTXHD "APTXHD" |
| 82 | #define MIXER_ENC_FMT_NONE "NONE" |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame] | 83 | #define ENCODER_LATENCY_SBC 10 |
| 84 | #define ENCODER_LATENCY_APTX 40 |
| 85 | #define ENCODER_LATENCY_APTX_HD 20 |
| 86 | #define ENCODER_LATENCY_AAC 70 |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 87 | //To Do: Fine Tune Encoder CELT/LDAC latency. |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 88 | #define ENCODER_LATENCY_CELT 40 |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 89 | #define ENCODER_LATENCY_LDAC 40 |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame] | 90 | #define DEFAULT_SINK_LATENCY_SBC 140 |
| 91 | #define DEFAULT_SINK_LATENCY_APTX 160 |
| 92 | #define DEFAULT_SINK_LATENCY_APTX_HD 180 |
| 93 | #define DEFAULT_SINK_LATENCY_AAC 180 |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 94 | //To Do: Fine Tune Default CELT/LDAC Latency. |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 95 | #define DEFAULT_SINK_LATENCY_CELT 180 |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 96 | #define DEFAULT_SINK_LATENCY_LDAC 180 |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * Below enum values are extended from audio_base.h to |
| 100 | * to keep encoder codec type local to bthost_ipc |
| 101 | * and audio_hal as these are intended only for handshake |
| 102 | * between IPC lib and Audio HAL. |
| 103 | */ |
| 104 | typedef enum { |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 105 | ENC_CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL |
| 106 | ENC_CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL |
| 107 | ENC_CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL |
| 108 | ENC_CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL |
| 109 | ENC_CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 110 | #ifndef LINUX_ENABLED |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 111 | ENC_CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 112 | #endif |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 113 | ENC_CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 114 | ENC_CODEC_TYPE_CELT = 603979776u, // 0x24000000UL |
| 115 | }enc_codec_t; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 116 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 117 | typedef int (*audio_stream_open_t)(void); |
| 118 | typedef int (*audio_stream_close_t)(void); |
| 119 | typedef int (*audio_start_stream_t)(void); |
| 120 | typedef int (*audio_stop_stream_t)(void); |
| 121 | typedef int (*audio_suspend_stream_t)(void); |
| 122 | typedef void (*audio_handoff_triggered_t)(void); |
| 123 | typedef void (*clear_a2dpsuspend_flag_t)(void); |
| 124 | typedef void * (*audio_get_codec_config_t)(uint8_t *multicast_status,uint8_t *num_dev, |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 125 | enc_codec_t *codec_type); |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 126 | typedef int (*audio_check_a2dp_ready_t)(void); |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame] | 127 | typedef uint16_t (*audio_get_a2dp_sink_latency_t)(void); |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 128 | typedef int (*audio_is_scrambling_enabled_t)(void); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 129 | |
| 130 | enum A2DP_STATE { |
| 131 | A2DP_STATE_CONNECTED, |
| 132 | A2DP_STATE_STARTED, |
| 133 | A2DP_STATE_STOPPED, |
| 134 | A2DP_STATE_DISCONNECTED, |
| 135 | }; |
| 136 | |
| 137 | /* structure used to update a2dp state machine |
| 138 | * to communicate IPC library |
| 139 | * to store DSP encoder configuration information |
| 140 | */ |
| 141 | struct a2dp_data { |
| 142 | struct audio_device *adev; |
| 143 | void *bt_lib_handle; |
| 144 | audio_stream_open_t audio_stream_open; |
| 145 | audio_stream_close_t audio_stream_close; |
| 146 | audio_start_stream_t audio_start_stream; |
| 147 | audio_stop_stream_t audio_stop_stream; |
| 148 | audio_suspend_stream_t audio_suspend_stream; |
| 149 | audio_handoff_triggered_t audio_handoff_triggered; |
| 150 | clear_a2dpsuspend_flag_t clear_a2dpsuspend_flag; |
| 151 | audio_get_codec_config_t audio_get_codec_config; |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 152 | audio_check_a2dp_ready_t audio_check_a2dp_ready; |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame] | 153 | audio_get_a2dp_sink_latency_t audio_get_a2dp_sink_latency; |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 154 | audio_is_scrambling_enabled_t audio_is_scrambling_enabled; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 155 | enum A2DP_STATE bt_state; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 156 | enc_codec_t bt_encoder_format; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 157 | uint32_t enc_sampling_rate; |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 158 | uint32_t enc_channels; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 159 | bool a2dp_started; |
| 160 | bool a2dp_suspended; |
| 161 | int a2dp_total_active_session_request; |
| 162 | bool is_a2dp_offload_supported; |
| 163 | bool is_handoff_in_progress; |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 164 | bool is_aptx_dual_mono_supported; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | struct a2dp_data a2dp; |
| 168 | |
| 169 | /* START of DSP configurable structures |
| 170 | * These values should match with DSP interface defintion |
| 171 | */ |
| 172 | |
| 173 | /* AAC encoder configuration structure. */ |
| 174 | typedef struct aac_enc_cfg_t aac_enc_cfg_t; |
| 175 | |
| 176 | /* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS |
| 177 | * supported aac_fmt_flag are ADTS/RAW |
| 178 | * supported channel_cfg are Native mode, Mono , Stereo |
| 179 | */ |
| 180 | struct aac_enc_cfg_t { |
| 181 | uint32_t enc_format; |
| 182 | uint32_t bit_rate; |
| 183 | uint32_t enc_mode; |
| 184 | uint16_t aac_fmt_flag; |
Naresh Tanniru | a42d0bd | 2016-09-21 15:30:46 +0530 | [diff] [blame] | 185 | uint16_t channel_cfg; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 186 | uint32_t sample_rate; |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 187 | } __attribute__ ((packed)); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 188 | |
| 189 | /* SBC encoder configuration structure. */ |
| 190 | typedef struct sbc_enc_cfg_t sbc_enc_cfg_t; |
| 191 | |
| 192 | /* supported num_subbands are 4/8 |
| 193 | * supported blk_len are 4, 8, 12, 16 |
| 194 | * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO |
| 195 | * supported alloc_method are LOUNDNESS/SNR |
| 196 | * supported bit_rate for mono channel is max 320kbps |
| 197 | * supported bit rate for stereo channel is max 512 kbps |
| 198 | */ |
| 199 | struct sbc_enc_cfg_t{ |
| 200 | uint32_t enc_format; |
| 201 | uint32_t num_subbands; |
| 202 | uint32_t blk_len; |
| 203 | uint32_t channel_mode; |
| 204 | uint32_t alloc_method; |
| 205 | uint32_t bit_rate; |
| 206 | uint32_t sample_rate; |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 207 | } __attribute__ ((packed)); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 208 | |
| 209 | |
| 210 | /* supported num_channels are Mono/Stereo |
| 211 | * supported channel_mapping for mono is CHANNEL_C |
| 212 | * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R |
| 213 | * custom size and reserved are not used(for future enhancement) |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 214 | */ |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 215 | struct custom_enc_cfg_t |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 216 | { |
| 217 | uint32_t enc_format; |
| 218 | uint32_t sample_rate; |
| 219 | uint16_t num_channels; |
| 220 | uint16_t reserved; |
| 221 | uint8_t channel_mapping[8]; |
| 222 | uint32_t custom_size; |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 223 | } __attribute__ ((packed)); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 224 | |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 225 | struct celt_specific_enc_cfg_t |
| 226 | { |
| 227 | uint32_t bit_rate; |
| 228 | uint16_t frame_size; |
| 229 | uint16_t complexity; |
| 230 | uint16_t prediction_mode; |
| 231 | uint16_t vbr_flag; |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 232 | } __attribute__ ((packed)); |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 233 | |
| 234 | struct celt_enc_cfg_t |
| 235 | { |
| 236 | struct custom_enc_cfg_t custom_cfg; |
| 237 | struct celt_specific_enc_cfg_t celt_cfg; |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 238 | } __attribute__ ((packed)); |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 239 | |
| 240 | /* sync_mode introduced with APTX V2 libraries |
| 241 | * sync mode: 0x0 = stereo sync mode |
| 242 | * 0x01 = dual mono sync mode |
| 243 | * 0x02 = dual mono with no sync on either L or R codewords |
| 244 | */ |
| 245 | struct aptx_v2_enc_cfg_ext_t |
| 246 | { |
| 247 | uint32_t sync_mode; |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 248 | } __attribute__ ((packed)); |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 249 | |
| 250 | /* APTX struct for combining custom enc and V2 fields */ |
| 251 | struct aptx_enc_cfg_t |
| 252 | { |
| 253 | struct custom_enc_cfg_t custom_cfg; |
| 254 | struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg; |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 255 | } __attribute__ ((packed)); |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 256 | |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 257 | struct ldac_specific_enc_cfg_t |
| 258 | { |
| 259 | uint32_t bit_rate; |
| 260 | uint16_t channel_mode; |
| 261 | uint16_t mtu; |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 262 | } __attribute__ ((packed)); |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 263 | |
| 264 | struct ldac_enc_cfg_t |
| 265 | { |
| 266 | struct custom_enc_cfg_t custom_cfg; |
| 267 | struct ldac_specific_enc_cfg_t ldac_cfg; |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 268 | } __attribute__ ((packed)); |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 269 | |
Sachin Mohan Gadag | 1657c05 | 2017-09-13 16:00:27 +0530 | [diff] [blame] | 270 | /* In LE BT source code uses system/audio.h for below |
| 271 | * structure definition. To avoid multiple definition |
| 272 | * compilation error for audiohal in LE , masking structure |
| 273 | * definition under "LINUX_ENABLED" which is defined only |
| 274 | * in LE |
| 275 | */ |
| 276 | #ifndef LINUX_ENABLED |
Satya Krishna Pindiproli | f7d6571 | 2017-04-26 14:24:53 +0530 | [diff] [blame] | 277 | /* TODO: Define the following structures only for O using PLATFORM_VERSION */ |
| 278 | /* Information about BT SBC encoder configuration |
| 279 | * This data is used between audio HAL module and |
| 280 | * BT IPC library to configure DSP encoder |
| 281 | */ |
| 282 | typedef struct { |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 283 | uint32_t subband; /* 4, 8 */ |
| 284 | uint32_t blk_len; /* 4, 8, 12, 16 */ |
| 285 | uint16_t sampling_rate; /*44.1khz,48khz*/ |
| 286 | uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/ |
| 287 | uint8_t alloc; /*0(Loudness),1(SNR)*/ |
| 288 | uint8_t min_bitpool; /* 2 */ |
| 289 | uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */ |
| 290 | uint32_t bitrate; /* 320kbps to 512kbps */ |
Satya Krishna Pindiproli | f7d6571 | 2017-04-26 14:24:53 +0530 | [diff] [blame] | 291 | } audio_sbc_encoder_config; |
| 292 | |
Satya Krishna Pindiproli | f7d6571 | 2017-04-26 14:24:53 +0530 | [diff] [blame] | 293 | /* Information about BT APTX encoder configuration |
| 294 | * This data is used between audio HAL module and |
| 295 | * BT IPC library to configure DSP encoder |
| 296 | */ |
| 297 | typedef struct { |
| 298 | uint16_t sampling_rate; |
| 299 | uint8_t channels; |
| 300 | uint32_t bitrate; |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 301 | } audio_aptx_default_config; |
Satya Krishna Pindiproli | f7d6571 | 2017-04-26 14:24:53 +0530 | [diff] [blame] | 302 | |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 303 | typedef struct { |
| 304 | uint16_t sampling_rate; |
| 305 | uint8_t channels; |
| 306 | uint32_t bitrate; |
| 307 | uint32_t sync_mode; |
| 308 | } audio_aptx_dual_mono_config; |
| 309 | |
| 310 | typedef union { |
| 311 | audio_aptx_default_config *default_cfg; |
| 312 | audio_aptx_dual_mono_config *dual_mono_cfg; |
| 313 | } audio_aptx_encoder_config; |
Satya Krishna Pindiproli | f7d6571 | 2017-04-26 14:24:53 +0530 | [diff] [blame] | 314 | |
| 315 | /* Information about BT AAC encoder configuration |
| 316 | * This data is used between audio HAL module and |
| 317 | * BT IPC library to configure DSP encoder |
| 318 | */ |
| 319 | typedef struct { |
| 320 | uint32_t enc_mode; /* LC, SBR, PS */ |
| 321 | uint16_t format_flag; /* RAW, ADTS */ |
| 322 | uint16_t channels; /* 1-Mono, 2-Stereo */ |
| 323 | uint32_t sampling_rate; |
| 324 | uint32_t bitrate; |
| 325 | } audio_aac_encoder_config; |
Sachin Mohan Gadag | 1657c05 | 2017-09-13 16:00:27 +0530 | [diff] [blame] | 326 | #endif |
Satya Krishna Pindiproli | f7d6571 | 2017-04-26 14:24:53 +0530 | [diff] [blame] | 327 | |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 328 | /* Information about BT CELT encoder configuration |
| 329 | * This data is used between audio HAL module and |
| 330 | * BT IPC library to configure DSP encoder |
| 331 | */ |
| 332 | typedef struct { |
| 333 | uint32_t sampling_rate; /* 32000 - 48000, 48000 */ |
| 334 | uint16_t channels; /* 1-Mono, 2-Stereo, 2*/ |
| 335 | uint16_t frame_size; /* 64-128-256-512, 512 */ |
| 336 | uint16_t complexity; /* 0-10, 1 */ |
| 337 | uint16_t prediction_mode; /* 0-1-2, 0 */ |
| 338 | uint16_t vbr_flag; /* 0-1, 0*/ |
| 339 | uint32_t bitrate; /*32000 - 1536000, 139500*/ |
| 340 | } audio_celt_encoder_config; |
| 341 | |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 342 | /* Information about BT LDAC encoder configuration |
| 343 | * This data is used between audio HAL module and |
| 344 | * BT IPC library to configure DSP encoder |
| 345 | */ |
| 346 | typedef struct { |
| 347 | uint32_t sampling_rate; /*44100,48000,88200,96000*/ |
| 348 | uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/ |
| 349 | uint16_t channel_mode; /* 0, 4, 2, 1*/ |
| 350 | uint16_t mtu; /*679*/ |
| 351 | } audio_ldac_encoder_config; |
| 352 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 353 | /*********** END of DSP configurable structures ********************/ |
| 354 | |
| 355 | /* API to identify DSP encoder captabilities */ |
| 356 | static void a2dp_offload_codec_cap_parser(char *value) |
| 357 | { |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 358 | char *tok = NULL,*saveptr; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 359 | |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 360 | tok = strtok_r(value, "-", &saveptr); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 361 | while (tok != NULL) { |
| 362 | if (strcmp(tok, "sbc") == 0) { |
| 363 | ALOGD("%s: SBC offload supported\n",__func__); |
| 364 | a2dp.is_a2dp_offload_supported = true; |
| 365 | break; |
| 366 | } else if (strcmp(tok, "aptx") == 0) { |
| 367 | ALOGD("%s: aptx offload supported\n",__func__); |
| 368 | a2dp.is_a2dp_offload_supported = true; |
| 369 | break; |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 370 | } else if (strcmp(tok, "aptxtws") == 0) { |
| 371 | ALOGD("%s: aptx dual mono offload supported\n",__func__); |
| 372 | a2dp.is_a2dp_offload_supported = true; |
| 373 | break; |
Naresh Tanniru | ed694c8 | 2017-02-07 17:01:28 +0530 | [diff] [blame] | 374 | } else if (strcmp(tok, "aptxhd") == 0) { |
| 375 | ALOGD("%s: aptx HD offload supported\n",__func__); |
| 376 | a2dp.is_a2dp_offload_supported = true; |
| 377 | break; |
| 378 | } else if (strcmp(tok, "aac") == 0) { |
| 379 | ALOGD("%s: aac offload supported\n",__func__); |
| 380 | a2dp.is_a2dp_offload_supported = true; |
| 381 | break; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 382 | } else if (strcmp(tok, "celt") == 0) { |
| 383 | ALOGD("%s: celt offload supported\n",__func__); |
| 384 | a2dp.is_a2dp_offload_supported = true; |
| 385 | break; |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 386 | } else if (strcmp(tok, "ldac") == 0) { |
| 387 | ALOGD("%s: ldac offload supported\n",__func__); |
| 388 | a2dp.is_a2dp_offload_supported = true; |
| 389 | break; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 390 | } |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 391 | tok = strtok_r(NULL, "-", &saveptr); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 392 | }; |
| 393 | } |
| 394 | |
| 395 | static void update_offload_codec_capabilities() |
| 396 | { |
| 397 | char value[PROPERTY_VALUE_MAX] = {'\0'}; |
| 398 | |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 399 | property_get("persist.vendor.bt.a2dp_offload_cap", value, "false"); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 400 | ALOGD("get_offload_codec_capabilities = %s",value); |
| 401 | a2dp.is_a2dp_offload_supported = |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 402 | property_get_bool("persist.vendor.bt.a2dp_offload_cap", false); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 403 | if (strcmp(value, "false") != 0) |
| 404 | a2dp_offload_codec_cap_parser(value); |
| 405 | ALOGD("%s: codec cap = %s",__func__,value); |
| 406 | } |
| 407 | |
| 408 | /* API to open BT IPC library to start IPC communication */ |
| 409 | static void open_a2dp_output() |
| 410 | { |
| 411 | int ret = 0; |
| 412 | |
| 413 | ALOGD(" Open A2DP output start "); |
| 414 | if (a2dp.bt_lib_handle == NULL){ |
| 415 | ALOGD(" Requesting for BT lib handle"); |
| 416 | a2dp.bt_lib_handle = dlopen(BT_IPC_LIB_NAME, RTLD_NOW); |
| 417 | |
| 418 | if (a2dp.bt_lib_handle == NULL) { |
| 419 | ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_LIB_NAME); |
| 420 | ret = -ENOSYS; |
| 421 | goto init_fail; |
| 422 | } else { |
| 423 | a2dp.audio_stream_open = (audio_stream_open_t) |
| 424 | dlsym(a2dp.bt_lib_handle, "audio_stream_open"); |
| 425 | a2dp.audio_start_stream = (audio_start_stream_t) |
| 426 | dlsym(a2dp.bt_lib_handle, "audio_start_stream"); |
| 427 | a2dp.audio_get_codec_config = (audio_get_codec_config_t) |
| 428 | dlsym(a2dp.bt_lib_handle, "audio_get_codec_config"); |
| 429 | a2dp.audio_suspend_stream = (audio_suspend_stream_t) |
| 430 | dlsym(a2dp.bt_lib_handle, "audio_suspend_stream"); |
| 431 | a2dp.audio_handoff_triggered = (audio_handoff_triggered_t) |
| 432 | dlsym(a2dp.bt_lib_handle, "audio_handoff_triggered"); |
| 433 | a2dp.clear_a2dpsuspend_flag = (clear_a2dpsuspend_flag_t) |
| 434 | dlsym(a2dp.bt_lib_handle, "clear_a2dpsuspend_flag"); |
| 435 | a2dp.audio_stop_stream = (audio_stop_stream_t) |
| 436 | dlsym(a2dp.bt_lib_handle, "audio_stop_stream"); |
| 437 | a2dp.audio_stream_close = (audio_stream_close_t) |
| 438 | dlsym(a2dp.bt_lib_handle, "audio_stream_close"); |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 439 | a2dp.audio_check_a2dp_ready = (audio_check_a2dp_ready_t) |
| 440 | dlsym(a2dp.bt_lib_handle,"audio_check_a2dp_ready"); |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame] | 441 | a2dp.audio_get_a2dp_sink_latency = (audio_get_a2dp_sink_latency_t) |
| 442 | dlsym(a2dp.bt_lib_handle,"audio_get_a2dp_sink_latency"); |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 443 | a2dp.audio_is_scrambling_enabled = (audio_is_scrambling_enabled_t) |
| 444 | dlsym(a2dp.bt_lib_handle,"audio_is_scrambling_enabled"); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
| 448 | if (a2dp.bt_lib_handle && a2dp.audio_stream_open) { |
| 449 | if (a2dp.bt_state == A2DP_STATE_DISCONNECTED) { |
| 450 | ALOGD("calling BT stream open"); |
| 451 | ret = a2dp.audio_stream_open(); |
| 452 | if(ret != 0) { |
| 453 | ALOGE("Failed to open output stream for a2dp: status %d", ret); |
| 454 | goto init_fail; |
| 455 | } |
| 456 | a2dp.bt_state = A2DP_STATE_CONNECTED; |
| 457 | } else { |
| 458 | ALOGD("Called a2dp open with improper state, Ignoring request state %d", a2dp.bt_state); |
| 459 | } |
| 460 | } else { |
| 461 | ALOGE("a2dp handle is not identified, Ignoring open request"); |
| 462 | a2dp.bt_state = A2DP_STATE_DISCONNECTED; |
| 463 | goto init_fail; |
| 464 | } |
| 465 | |
| 466 | init_fail: |
| 467 | if(ret != 0 && (a2dp.bt_lib_handle != NULL)) { |
| 468 | dlclose(a2dp.bt_lib_handle); |
| 469 | a2dp.bt_lib_handle = NULL; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | static int close_a2dp_output() |
| 474 | { |
| 475 | ALOGV("%s\n",__func__); |
| 476 | if (!(a2dp.bt_lib_handle && a2dp.audio_stream_close)) { |
| 477 | ALOGE("a2dp handle is not identified, Ignoring close request"); |
| 478 | return -ENOSYS; |
| 479 | } |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 480 | if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) { |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 481 | ALOGD("calling BT stream close"); |
| 482 | if(a2dp.audio_stream_close() == false) |
| 483 | ALOGE("failed close a2dp control path from BT library"); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 484 | } |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 485 | a2dp.a2dp_started = false; |
| 486 | a2dp.a2dp_total_active_session_request = 0; |
| 487 | a2dp.a2dp_suspended = false; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 488 | a2dp.bt_encoder_format = ENC_CODEC_TYPE_INVALID; |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 489 | a2dp.enc_sampling_rate = 48000; |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 490 | a2dp.enc_channels = 2; |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 491 | a2dp.bt_state = A2DP_STATE_DISCONNECTED; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 496 | static void a2dp_check_and_set_scrambler() |
| 497 | { |
| 498 | bool scrambler_mode = false; |
| 499 | struct mixer_ctl *ctrl_scrambler_mode = NULL; |
| 500 | if (a2dp.audio_is_scrambling_enabled && (a2dp.bt_state != A2DP_STATE_DISCONNECTED)) |
| 501 | scrambler_mode = a2dp.audio_is_scrambling_enabled(); |
| 502 | |
| 503 | if (scrambler_mode) { |
| 504 | //enable scrambler in dsp |
| 505 | ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 506 | MIXER_SCRAMBLER_MODE); |
| 507 | if (!ctrl_scrambler_mode) { |
| 508 | ALOGE(" ERROR scrambler mode mixer control not identifed"); |
| 509 | return; |
| 510 | } else { |
| 511 | if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) { |
| 512 | ALOGE("%s: Could not set scrambler mode", __func__); |
| 513 | return; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | static void a2dp_set_backend_cfg() |
| 520 | { |
| 521 | char *rate_str = NULL, *in_channels = NULL; |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 522 | uint32_t sampling_rate = a2dp.enc_sampling_rate; |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 523 | struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL; |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 524 | |
| 525 | //For LDAC encoder open slimbus port at 96Khz for 48Khz input |
| 526 | //and 88.2Khz for 44.1Khz input. |
| 527 | if ((a2dp.bt_encoder_format == ENC_CODEC_TYPE_LDAC) && |
| 528 | (sampling_rate == 48000 || sampling_rate == 44100 )) { |
| 529 | sampling_rate = sampling_rate *2; |
| 530 | } |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 531 | //Configure backend sampling rate |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 532 | switch (sampling_rate) { |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 533 | case 44100: |
| 534 | rate_str = "KHZ_44P1"; |
| 535 | break; |
| 536 | case 48000: |
| 537 | rate_str = "KHZ_48"; |
| 538 | break; |
| 539 | case 88200: |
| 540 | rate_str = "KHZ_88P2"; |
| 541 | break; |
| 542 | case 96000: |
| 543 | rate_str = "KHZ_96"; |
| 544 | break; |
| 545 | default: |
| 546 | rate_str = "KHZ_48"; |
| 547 | break; |
| 548 | } |
| 549 | |
| 550 | ALOGD("%s: set backend sample rate =%s", __func__, rate_str); |
| 551 | ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 552 | MIXER_SAMPLE_RATE); |
| 553 | if (!ctl_sample_rate) { |
| 554 | ALOGE(" ERROR backend sample rate mixer control not identifed"); |
| 555 | return; |
| 556 | } else { |
| 557 | if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) { |
| 558 | ALOGE("%s: Failed to set backend sample rate =%s", __func__, rate_str); |
| 559 | return; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | //Configure AFE input channels |
| 564 | switch (a2dp.enc_channels) { |
| 565 | case 1: |
| 566 | in_channels = "One"; |
| 567 | break; |
| 568 | case 2: |
| 569 | default: |
| 570 | in_channels = "Two"; |
| 571 | break; |
| 572 | } |
| 573 | |
| 574 | ALOGD("%s: set afe input channels =%d", __func__, a2dp.enc_channels); |
| 575 | ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 576 | MIXER_AFE_IN_CHANNELS); |
| 577 | if (!ctrl_in_channels) { |
| 578 | ALOGE(" ERROR AFE input channels mixer control not identifed"); |
| 579 | return; |
| 580 | } else { |
| 581 | if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) { |
| 582 | ALOGE("%s: Failed to set AFE in channels =%d", __func__, a2dp.enc_channels); |
| 583 | return; |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | static void a2dp_reset_backend_cfg() |
| 589 | { |
| 590 | char *rate_str = "KHZ_8", *in_channels = "Zero"; |
| 591 | struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL; |
| 592 | |
| 593 | //reset backend sampling rate |
| 594 | ALOGD("%s: reset backend sample rate =%s", __func__, rate_str); |
| 595 | ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 596 | MIXER_SAMPLE_RATE); |
| 597 | if (!ctl_sample_rate) { |
| 598 | ALOGE(" ERROR backend sample rate mixer control not identifed"); |
| 599 | return; |
| 600 | } else { |
| 601 | if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) { |
| 602 | ALOGE("%s: Failed to reset backend sample rate =%s", __func__, rate_str); |
| 603 | return; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | //reset AFE input channels |
| 608 | ALOGD("%s: reset afe input channels =%s", __func__, in_channels); |
| 609 | ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 610 | MIXER_AFE_IN_CHANNELS); |
| 611 | if (!ctrl_in_channels) { |
| 612 | ALOGE(" ERROR AFE input channels mixer control not identifed"); |
| 613 | return; |
| 614 | } else { |
| 615 | if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) { |
| 616 | ALOGE("%s: Failed to reset AFE in channels =%d", __func__, a2dp.enc_channels); |
| 617 | return; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 622 | /* API to configure SBC DSP encoder */ |
| 623 | bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg) |
| 624 | { |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 625 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 626 | struct sbc_enc_cfg_t sbc_dsp_cfg; |
| 627 | bool is_configured = false; |
| 628 | int ret = 0; |
| 629 | |
| 630 | if(sbc_bt_cfg == NULL) |
| 631 | return false; |
| 632 | |
| 633 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 634 | if (!ctl_enc_data) { |
| 635 | ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed"); |
| 636 | is_configured = false; |
| 637 | goto fail; |
| 638 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 639 | memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t)); |
| 640 | sbc_dsp_cfg.enc_format = ENC_MEDIA_FMT_SBC; |
| 641 | sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband; |
| 642 | sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len; |
| 643 | switch(sbc_bt_cfg->channels) { |
| 644 | case 0: |
| 645 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO; |
| 646 | break; |
| 647 | case 1: |
| 648 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO; |
| 649 | break; |
| 650 | case 3: |
| 651 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO; |
| 652 | break; |
| 653 | case 2: |
| 654 | default: |
| 655 | sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO; |
| 656 | break; |
| 657 | } |
| 658 | if (sbc_bt_cfg->alloc) |
| 659 | sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS; |
| 660 | else |
| 661 | sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR; |
| 662 | sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate; |
| 663 | sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate; |
| 664 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg, |
| 665 | sizeof(struct sbc_enc_cfg_t)); |
| 666 | if (ret != 0) { |
| 667 | ALOGE("%s: failed to set SBC encoder config", __func__); |
| 668 | is_configured = false; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 669 | goto fail; |
| 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 | is_configured = false; |
| 676 | goto fail; |
| 677 | } |
| 678 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE"); |
| 679 | if (ret != 0) { |
| 680 | ALOGE("%s: Failed to set bit format to encoder", __func__); |
| 681 | is_configured = false; |
| 682 | goto fail; |
| 683 | } |
| 684 | is_configured = true; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 685 | a2dp.bt_encoder_format = ENC_CODEC_TYPE_SBC; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 686 | a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate; |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 687 | |
| 688 | if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO) |
| 689 | a2dp.enc_channels = 1; |
| 690 | else |
| 691 | a2dp.enc_channels = 2; |
| 692 | |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 693 | ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d", |
| 694 | sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 695 | fail: |
| 696 | return is_configured; |
| 697 | } |
| 698 | |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 699 | #ifndef LINUX_ENABLED |
| 700 | static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg, |
| 701 | audio_aptx_encoder_config *aptx_bt_cfg) |
| 702 | { |
| 703 | int ret = 0; |
| 704 | |
| 705 | if(aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) { |
| 706 | ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p", |
| 707 | aptx_dsp_cfg, aptx_bt_cfg); |
| 708 | return -EINVAL; |
| 709 | } |
| 710 | |
| 711 | memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t)); |
| 712 | aptx_dsp_cfg->custom_cfg.enc_format = ENC_MEDIA_FMT_APTX; |
| 713 | |
| 714 | if (!a2dp.is_aptx_dual_mono_supported) { |
| 715 | aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate; |
| 716 | aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels; |
| 717 | } else { |
| 718 | aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate; |
| 719 | aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels; |
| 720 | aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode; |
| 721 | } |
| 722 | |
| 723 | switch(aptx_dsp_cfg->custom_cfg.num_channels) { |
| 724 | case 1: |
| 725 | aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C; |
| 726 | break; |
| 727 | case 2: |
| 728 | default: |
| 729 | aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L; |
| 730 | aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R; |
| 731 | break; |
| 732 | } |
| 733 | a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels; |
| 734 | if (!a2dp.is_aptx_dual_mono_supported) { |
| 735 | a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate; |
| 736 | ALOGV("Successfully updated APTX enc format with samplingrate: %d \ |
| 737 | channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate, |
| 738 | aptx_dsp_cfg->custom_cfg.num_channels); |
| 739 | } else { |
| 740 | a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate; |
| 741 | ALOGV("Successfully updated APTX dual mono enc format with \ |
| 742 | samplingrate: %d channels:%d syncmode %d", |
| 743 | aptx_dsp_cfg->custom_cfg.sample_rate, |
| 744 | aptx_dsp_cfg->custom_cfg.num_channels, |
| 745 | aptx_dsp_cfg->aptx_v2_cfg.sync_mode); |
| 746 | } |
| 747 | return ret; |
| 748 | } |
| 749 | #else |
| 750 | static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg, |
| 751 | audio_aptx_encoder_config *aptx_bt_cfg) |
| 752 | { |
| 753 | int ret = 0; |
| 754 | |
| 755 | if(aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) { |
| 756 | ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p", |
| 757 | aptx_dsp_cfg, aptx_bt_cfg); |
| 758 | return -EINVAL; |
| 759 | } |
| 760 | |
| 761 | memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t)); |
| 762 | aptx_dsp_cfg->enc_format = ENC_MEDIA_FMT_APTX; |
| 763 | aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate; |
| 764 | aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels; |
| 765 | switch(aptx_dsp_cfg->num_channels) { |
| 766 | case 1: |
| 767 | aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C; |
| 768 | break; |
| 769 | case 2: |
| 770 | default: |
| 771 | aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L; |
| 772 | aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R; |
| 773 | break; |
| 774 | } |
| 775 | |
| 776 | ALOGV("Updated APTX enc format with samplingrate: %d channels:%d", |
| 777 | aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels); |
| 778 | |
| 779 | return ret; |
| 780 | } |
| 781 | #endif |
| 782 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 783 | /* API to configure APTX DSP encoder */ |
| 784 | bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg) |
| 785 | { |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 786 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 787 | int mixer_size; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 788 | bool is_configured = false; |
| 789 | int ret = 0; |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 790 | int sample_rate_backup; |
Preetam Singh Ranawat | 109bb3c | 2018-01-30 12:12:02 +0530 | [diff] [blame] | 791 | |
| 792 | if(aptx_bt_cfg == NULL) |
| 793 | return false; |
| 794 | |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 795 | #ifndef LINUX_ENABLED |
| 796 | struct aptx_enc_cfg_t aptx_dsp_cfg; |
| 797 | mixer_size = sizeof(struct aptx_enc_cfg_t); |
| 798 | sample_rate_backup = aptx_bt_cfg->default_cfg->sampling_rate; |
| 799 | #else |
| 800 | struct custom_enc_cfg_t aptx_dsp_cfg; |
| 801 | mixer_size = sizeof(struct custom_enc_cfg_t); |
| 802 | sample_rate_backup = aptx_bt_cfg->sampling_rate; |
| 803 | #endif |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 804 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 805 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 806 | if (!ctl_enc_data) { |
| 807 | ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed"); |
| 808 | is_configured = false; |
| 809 | goto fail; |
| 810 | } |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 811 | |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 812 | #ifndef LINUX_ENABLED |
| 813 | ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg); |
| 814 | #else |
| 815 | ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg); |
| 816 | #endif |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 817 | |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 818 | if (ret) { |
| 819 | is_configured = false; |
| 820 | goto fail; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 821 | } |
| 822 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg, |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 823 | mixer_size); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 824 | if (ret != 0) { |
| 825 | ALOGE("%s: Failed to set APTX encoder config", __func__); |
| 826 | is_configured = false; |
| 827 | goto fail; |
| 828 | } |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 829 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 830 | MIXER_ENC_BIT_FORMAT); |
| 831 | if (!ctrl_bit_format) { |
| 832 | ALOGE("ERROR bit format CONFIG data mixer control not identifed"); |
| 833 | is_configured = false; |
| 834 | goto fail; |
| 835 | } else { |
| 836 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE"); |
| 837 | if (ret != 0) { |
| 838 | ALOGE("%s: Failed to set bit format to encoder", __func__); |
| 839 | is_configured = false; |
| 840 | goto fail; |
| 841 | } |
| 842 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 843 | is_configured = true; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 844 | a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 845 | fail: |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 846 | /*restore sample rate */ |
| 847 | if(!is_configured) |
| 848 | a2dp.enc_sampling_rate = sample_rate_backup; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 849 | return is_configured; |
| 850 | } |
| 851 | |
| 852 | /* API to configure APTX HD DSP encoder |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 853 | */ |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 854 | #ifndef LINUX_ENABLED |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 855 | bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg) |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 856 | #else |
| 857 | bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg) |
| 858 | #endif |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 859 | { |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 860 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 861 | struct custom_enc_cfg_t aptx_dsp_cfg; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 862 | bool is_configured = false; |
| 863 | int ret = 0; |
| 864 | |
| 865 | if(aptx_bt_cfg == NULL) |
| 866 | return false; |
| 867 | |
| 868 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 869 | if (!ctl_enc_data) { |
| 870 | ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed"); |
| 871 | is_configured = false; |
| 872 | goto fail; |
| 873 | } |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 874 | |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 875 | memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t)); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 876 | aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX_HD; |
| 877 | aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate; |
| 878 | aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels; |
| 879 | switch(aptx_dsp_cfg.num_channels) { |
| 880 | case 1: |
| 881 | aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C; |
| 882 | break; |
| 883 | case 2: |
| 884 | default: |
| 885 | aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L; |
| 886 | aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R; |
| 887 | break; |
| 888 | } |
| 889 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg, |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 890 | sizeof(struct custom_enc_cfg_t)); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 891 | if (ret != 0) { |
| 892 | ALOGE("%s: Failed to set APTX HD encoder config", __func__); |
| 893 | is_configured = false; |
| 894 | goto fail; |
| 895 | } |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 896 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_BIT_FORMAT); |
| 897 | if (!ctrl_bit_format) { |
| 898 | ALOGE(" ERROR bit format CONFIG data mixer control not identifed"); |
| 899 | is_configured = false; |
| 900 | goto fail; |
| 901 | } |
| 902 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S24_LE"); |
| 903 | if (ret != 0) { |
| 904 | ALOGE("%s: Failed to set APTX HD encoder config", __func__); |
| 905 | is_configured = false; |
| 906 | goto fail; |
| 907 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 908 | is_configured = true; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 909 | a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX_HD; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 910 | a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate; |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 911 | a2dp.enc_channels = aptx_bt_cfg->channels; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 912 | ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d", |
| 913 | aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 914 | fail: |
| 915 | return is_configured; |
| 916 | } |
| 917 | |
| 918 | /* API to configure AAC DSP encoder */ |
| 919 | bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg) |
| 920 | { |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 921 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 922 | struct aac_enc_cfg_t aac_dsp_cfg; |
| 923 | bool is_configured = false; |
| 924 | int ret = 0; |
| 925 | |
| 926 | if(aac_bt_cfg == NULL) |
| 927 | return false; |
| 928 | |
| 929 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 930 | if (!ctl_enc_data) { |
| 931 | ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed"); |
| 932 | is_configured = false; |
| 933 | goto fail; |
| 934 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 935 | memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t)); |
| 936 | aac_dsp_cfg.enc_format = ENC_MEDIA_FMT_AAC; |
| 937 | aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate; |
Naresh Tanniru | a42d0bd | 2016-09-21 15:30:46 +0530 | [diff] [blame] | 938 | aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 939 | switch(aac_bt_cfg->enc_mode) { |
| 940 | case 0: |
| 941 | aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC; |
| 942 | break; |
| 943 | case 2: |
| 944 | aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS; |
| 945 | break; |
| 946 | case 1: |
| 947 | default: |
| 948 | aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR; |
| 949 | break; |
| 950 | } |
Naresh Tanniru | a42d0bd | 2016-09-21 15:30:46 +0530 | [diff] [blame] | 951 | aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 952 | aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels; |
| 953 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg, |
| 954 | sizeof(struct aac_enc_cfg_t)); |
| 955 | if (ret != 0) { |
| 956 | ALOGE("%s: failed to set SBC encoder config", __func__); |
| 957 | is_configured = false; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 958 | goto fail; |
| 959 | } |
| 960 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 961 | MIXER_ENC_BIT_FORMAT); |
| 962 | if (!ctrl_bit_format) { |
| 963 | is_configured = false; |
| 964 | ALOGE(" ERROR bit format CONFIG data mixer control not identifed"); |
| 965 | goto fail; |
| 966 | } |
| 967 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE"); |
| 968 | if (ret != 0) { |
| 969 | ALOGE("%s: Failed to set bit format to encoder", __func__); |
| 970 | is_configured = false; |
| 971 | goto fail; |
| 972 | } |
| 973 | is_configured = true; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 974 | a2dp.bt_encoder_format = ENC_CODEC_TYPE_AAC; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 975 | a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate; |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 976 | a2dp.enc_channels = aac_bt_cfg->channels;; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 977 | ALOGV("Successfully updated AAC enc format with samplingrate: %d channels:%d", |
| 978 | aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 979 | fail: |
| 980 | return is_configured; |
| 981 | } |
| 982 | |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 983 | bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg) |
| 984 | { |
| 985 | struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL; |
| 986 | struct celt_enc_cfg_t celt_dsp_cfg; |
| 987 | bool is_configured = false; |
| 988 | int ret = 0; |
| 989 | if(celt_bt_cfg == NULL) |
| 990 | return false; |
| 991 | |
| 992 | ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 993 | if (!ctl_enc_data) { |
| 994 | ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed"); |
| 995 | is_configured = false; |
| 996 | goto fail; |
| 997 | } |
| 998 | memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t)); |
| 999 | |
| 1000 | celt_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_CELT; |
| 1001 | celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate; |
| 1002 | celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels; |
| 1003 | switch(celt_dsp_cfg.custom_cfg.num_channels) { |
| 1004 | case 1: |
| 1005 | celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C; |
| 1006 | break; |
| 1007 | case 2: |
| 1008 | default: |
| 1009 | celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L; |
| 1010 | celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R; |
| 1011 | break; |
| 1012 | } |
| 1013 | |
| 1014 | celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t); |
| 1015 | |
| 1016 | celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size; |
| 1017 | celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity; |
| 1018 | celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode; |
| 1019 | celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag; |
| 1020 | celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate; |
| 1021 | |
| 1022 | ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg, |
| 1023 | sizeof(struct celt_enc_cfg_t)); |
| 1024 | if (ret != 0) { |
| 1025 | ALOGE("%s: Failed to set CELT encoder config", __func__); |
| 1026 | is_configured = false; |
| 1027 | goto fail; |
| 1028 | } |
| 1029 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_BIT_FORMAT); |
| 1030 | if (!ctrl_bit_format) { |
| 1031 | ALOGE(" ERROR bit format CONFIG data mixer control not identifed"); |
| 1032 | is_configured = false; |
| 1033 | goto fail; |
| 1034 | } |
| 1035 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE"); |
| 1036 | if (ret != 0) { |
| 1037 | ALOGE("%s: Failed to set bit format to encoder", __func__); |
| 1038 | is_configured = false; |
| 1039 | goto fail; |
| 1040 | } |
| 1041 | is_configured = true; |
| 1042 | a2dp.bt_encoder_format = ENC_CODEC_TYPE_CELT; |
| 1043 | a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate; |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 1044 | a2dp.enc_channels = celt_bt_cfg->channels; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 1045 | ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d", |
| 1046 | celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels); |
| 1047 | fail: |
| 1048 | return is_configured; |
| 1049 | } |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 1050 | |
| 1051 | bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg) |
| 1052 | { |
| 1053 | struct mixer_ctl *ldac_enc_data = NULL, *ctrl_bit_format = NULL; |
| 1054 | struct ldac_enc_cfg_t ldac_dsp_cfg; |
| 1055 | bool is_configured = false; |
| 1056 | int ret = 0; |
| 1057 | if(ldac_bt_cfg == NULL) |
| 1058 | return false; |
| 1059 | |
| 1060 | ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK); |
| 1061 | if (!ldac_enc_data) { |
| 1062 | ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed"); |
| 1063 | is_configured = false; |
| 1064 | goto fail; |
| 1065 | } |
| 1066 | memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t)); |
| 1067 | |
| 1068 | ldac_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_LDAC; |
| 1069 | ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate; |
| 1070 | ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode; |
| 1071 | switch(ldac_dsp_cfg.ldac_cfg.channel_mode) { |
| 1072 | case 4: |
| 1073 | ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C; |
| 1074 | ldac_dsp_cfg.custom_cfg.num_channels = 1; |
| 1075 | break; |
| 1076 | case 2: |
| 1077 | case 1: |
| 1078 | default: |
| 1079 | ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L; |
| 1080 | ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R; |
| 1081 | ldac_dsp_cfg.custom_cfg.num_channels = 2; |
| 1082 | break; |
| 1083 | } |
| 1084 | |
| 1085 | ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t); |
| 1086 | ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu; |
| 1087 | ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate; |
| 1088 | ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg, |
| 1089 | sizeof(struct ldac_enc_cfg_t)); |
| 1090 | if (ret != 0) { |
| 1091 | ALOGE("%s: Failed to set LDAC encoder config", __func__); |
| 1092 | is_configured = false; |
| 1093 | goto fail; |
| 1094 | } |
| 1095 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_BIT_FORMAT); |
| 1096 | if (!ctrl_bit_format) { |
| 1097 | ALOGE(" ERROR bit format CONFIG data mixer control not identifed"); |
| 1098 | is_configured = false; |
| 1099 | goto fail; |
| 1100 | } |
| 1101 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE"); |
| 1102 | if (ret != 0) { |
| 1103 | ALOGE("%s: Failed to set bit format to encoder", __func__); |
| 1104 | is_configured = false; |
| 1105 | goto fail; |
| 1106 | } |
| 1107 | is_configured = true; |
| 1108 | a2dp.bt_encoder_format = ENC_CODEC_TYPE_LDAC; |
| 1109 | a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate; |
| 1110 | a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels; |
| 1111 | ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d", |
| 1112 | ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels); |
| 1113 | fail: |
| 1114 | return is_configured; |
| 1115 | } |
| 1116 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1117 | bool configure_a2dp_encoder_format() |
| 1118 | { |
| 1119 | void *codec_info = NULL; |
| 1120 | uint8_t multi_cast = 0, num_dev = 1; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 1121 | enc_codec_t codec_type = ENC_CODEC_TYPE_INVALID; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1122 | bool is_configured = false; |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 1123 | audio_aptx_encoder_config aptx_encoder_cfg; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1124 | |
| 1125 | if (!a2dp.audio_get_codec_config) { |
| 1126 | ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config"); |
| 1127 | return false; |
| 1128 | } |
| 1129 | ALOGD("configure_a2dp_encoder_format start"); |
| 1130 | codec_info = a2dp.audio_get_codec_config(&multi_cast, &num_dev, |
| 1131 | &codec_type); |
| 1132 | |
| 1133 | switch(codec_type) { |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 1134 | case ENC_CODEC_TYPE_SBC: |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1135 | ALOGD(" Received SBC encoder supported BT device"); |
| 1136 | is_configured = |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 1137 | configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1138 | break; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 1139 | case ENC_CODEC_TYPE_APTX: |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1140 | ALOGD(" Received APTX encoder supported BT device"); |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 1141 | #ifndef LINUX_ENABLED |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 1142 | a2dp.is_aptx_dual_mono_supported = false; |
| 1143 | aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info; |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 1144 | #endif |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1145 | is_configured = |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 1146 | configure_aptx_enc_format(&aptx_encoder_cfg); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1147 | break; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 1148 | case ENC_CODEC_TYPE_APTX_HD: |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1149 | ALOGD(" Received APTX HD encoder supported BT device"); |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 1150 | #ifndef LINUX_ENABLED |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1151 | is_configured = |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 1152 | configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info); |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 1153 | #else |
| 1154 | is_configured = |
| 1155 | configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info); |
| 1156 | #endif |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 1157 | break; |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 1158 | #ifndef LINUX_ENABLED |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 1159 | case ENC_CODEC_TYPE_APTX_DUAL_MONO: |
| 1160 | ALOGD(" Received APTX dual mono encoder supported BT device"); |
| 1161 | a2dp.is_aptx_dual_mono_supported = true; |
| 1162 | aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info; |
| 1163 | is_configured = |
| 1164 | configure_aptx_enc_format(&aptx_encoder_cfg); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1165 | break; |
Manish Dewangan | 6a25263 | 2017-12-04 17:27:44 +0530 | [diff] [blame] | 1166 | #endif |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 1167 | case ENC_CODEC_TYPE_AAC: |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1168 | ALOGD(" Received AAC encoder supported BT device"); |
| 1169 | is_configured = |
| 1170 | configure_aac_enc_format((audio_aac_encoder_config *)codec_info); |
| 1171 | break; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 1172 | case ENC_CODEC_TYPE_CELT: |
| 1173 | ALOGD(" Received CELT encoder supported BT device"); |
| 1174 | is_configured = |
| 1175 | configure_celt_enc_format((audio_celt_encoder_config *)codec_info); |
| 1176 | break; |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 1177 | case ENC_CODEC_TYPE_LDAC: |
| 1178 | ALOGD(" Received LDAC encoder supported BT device"); |
| 1179 | is_configured = |
| 1180 | configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info); |
| 1181 | break; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1182 | default: |
| 1183 | ALOGD(" Received Unsupported encoder formar"); |
| 1184 | is_configured = false; |
| 1185 | break; |
| 1186 | } |
| 1187 | return is_configured; |
| 1188 | } |
| 1189 | |
| 1190 | int audio_extn_a2dp_start_playback() |
| 1191 | { |
| 1192 | int ret = 0; |
| 1193 | |
| 1194 | ALOGD("audio_extn_a2dp_start_playback start"); |
| 1195 | |
| 1196 | if(!(a2dp.bt_lib_handle && a2dp.audio_start_stream |
| 1197 | && a2dp.audio_get_codec_config)) { |
| 1198 | ALOGE("a2dp handle is not identified, Ignoring start request"); |
| 1199 | return -ENOSYS; |
| 1200 | } |
| 1201 | |
| 1202 | if(a2dp.a2dp_suspended == true) { |
| 1203 | //session will be restarted after suspend completion |
| 1204 | ALOGD("a2dp start requested during suspend state"); |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 1205 | return -ENOSYS; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | if (!a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) { |
| 1209 | ALOGD("calling BT module stream start"); |
| 1210 | /* This call indicates BT IPC lib to start playback */ |
| 1211 | ret = a2dp.audio_start_stream(); |
| 1212 | ALOGE("BT controller start return = %d",ret); |
| 1213 | if (ret != 0 ) { |
| 1214 | ALOGE("BT controller start failed"); |
| 1215 | a2dp.a2dp_started = false; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1216 | } else { |
| 1217 | if(configure_a2dp_encoder_format() == true) { |
| 1218 | a2dp.a2dp_started = true; |
| 1219 | ret = 0; |
| 1220 | ALOGD("Start playback successful to BT library"); |
| 1221 | } else { |
| 1222 | ALOGD(" unable to configure DSP encoder"); |
| 1223 | a2dp.a2dp_started = false; |
| 1224 | ret = -ETIMEDOUT; |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 1229 | if (a2dp.a2dp_started) { |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1230 | a2dp.a2dp_total_active_session_request++; |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 1231 | a2dp_check_and_set_scrambler(); |
| 1232 | a2dp_set_backend_cfg(); |
| 1233 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1234 | |
| 1235 | ALOGD("start A2DP playback total active sessions :%d", |
| 1236 | a2dp.a2dp_total_active_session_request); |
| 1237 | return ret; |
| 1238 | } |
| 1239 | |
Naresh Tanniru | 03f9dd5 | 2016-10-19 18:46:22 +0530 | [diff] [blame] | 1240 | static void reset_a2dp_enc_config_params() |
| 1241 | { |
| 1242 | int ret =0; |
| 1243 | |
| 1244 | struct mixer_ctl *ctl_enc_config, *ctrl_bit_format; |
| 1245 | struct sbc_enc_cfg_t dummy_reset_config; |
| 1246 | |
| 1247 | memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t)); |
| 1248 | ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 1249 | MIXER_ENC_CONFIG_BLOCK); |
| 1250 | if (!ctl_enc_config) { |
| 1251 | ALOGE(" ERROR a2dp encoder format mixer control not identifed"); |
| 1252 | } else { |
| 1253 | ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config, |
| 1254 | sizeof(struct sbc_enc_cfg_t)); |
| 1255 | a2dp.bt_encoder_format = ENC_MEDIA_FMT_NONE; |
| 1256 | } |
| 1257 | ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, |
| 1258 | MIXER_ENC_BIT_FORMAT); |
| 1259 | if (!ctrl_bit_format) { |
| 1260 | ALOGE(" ERROR bit format CONFIG data mixer control not identifed"); |
| 1261 | } else { |
| 1262 | ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE"); |
| 1263 | if (ret != 0) { |
| 1264 | ALOGE("%s: Failed to set bit format to encoder", __func__); |
| 1265 | } |
| 1266 | } |
| 1267 | } |
| 1268 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1269 | int audio_extn_a2dp_stop_playback() |
| 1270 | { |
| 1271 | int ret =0; |
| 1272 | |
| 1273 | ALOGV("audio_extn_a2dp_stop_playback start"); |
| 1274 | if(!(a2dp.bt_lib_handle && a2dp.audio_stop_stream)) { |
| 1275 | ALOGE("a2dp handle is not identified, Ignoring start request"); |
| 1276 | return -ENOSYS; |
| 1277 | } |
| 1278 | |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 1279 | if (a2dp.a2dp_total_active_session_request > 0) |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1280 | a2dp.a2dp_total_active_session_request--; |
| 1281 | |
| 1282 | if ( a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) { |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1283 | ALOGV("calling BT module stream stop"); |
| 1284 | ret = a2dp.audio_stop_stream(); |
| 1285 | if (ret < 0) |
| 1286 | ALOGE("stop stream to BT IPC lib failed"); |
| 1287 | else |
| 1288 | ALOGV("stop steam to BT IPC lib successful"); |
Naresh Tanniru | 03f9dd5 | 2016-10-19 18:46:22 +0530 | [diff] [blame] | 1289 | reset_a2dp_enc_config_params(); |
Preetam Singh Ranawat | c2bef79 | 2017-11-22 10:59:15 +0530 | [diff] [blame] | 1290 | a2dp_reset_backend_cfg(); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1291 | } |
| 1292 | if(!a2dp.a2dp_total_active_session_request) |
| 1293 | a2dp.a2dp_started = false; |
| 1294 | ALOGD("Stop A2DP playback total active sessions :%d", |
| 1295 | a2dp.a2dp_total_active_session_request); |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
| 1299 | void audio_extn_a2dp_set_parameters(struct str_parms *parms) |
| 1300 | { |
| 1301 | int ret, val; |
| 1302 | char value[32]={0}; |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 1303 | struct audio_usecase *uc_info; |
| 1304 | struct listnode *node; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1305 | |
| 1306 | if(a2dp.is_a2dp_offload_supported == false) { |
| 1307 | ALOGV("no supported encoders identified,ignoring a2dp setparam"); |
| 1308 | return; |
| 1309 | } |
| 1310 | |
| 1311 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, |
| 1312 | sizeof(value)); |
Zhou Song | 681350a | 2017-10-19 16:28:42 +0800 | [diff] [blame] | 1313 | if (ret >= 0) { |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1314 | val = atoi(value); |
Zhou Song | 681350a | 2017-10-19 16:28:42 +0800 | [diff] [blame] | 1315 | if (audio_is_a2dp_out_device(val)) { |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1316 | ALOGV("Received device connect request for A2DP"); |
| 1317 | open_a2dp_output(); |
| 1318 | } |
| 1319 | goto param_handled; |
| 1320 | } |
| 1321 | |
| 1322 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, |
| 1323 | sizeof(value)); |
| 1324 | |
Zhou Song | 681350a | 2017-10-19 16:28:42 +0800 | [diff] [blame] | 1325 | if (ret >= 0) { |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1326 | val = atoi(value); |
Zhou Song | 681350a | 2017-10-19 16:28:42 +0800 | [diff] [blame] | 1327 | if (audio_is_a2dp_out_device(val)) { |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1328 | ALOGV("Received device dis- connect request"); |
| 1329 | close_a2dp_output(); |
kunleiz | 4a1fad6 | 2018-02-08 18:00:16 +0800 | [diff] [blame] | 1330 | reset_a2dp_enc_config_params(); |
| 1331 | a2dp_reset_backend_cfg(); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1332 | } |
| 1333 | goto param_handled; |
| 1334 | } |
| 1335 | |
| 1336 | ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value)); |
| 1337 | if (ret >= 0) { |
| 1338 | if (a2dp.bt_lib_handle && (a2dp.bt_state != A2DP_STATE_DISCONNECTED) ) { |
| 1339 | if ((!strncmp(value,"true",sizeof(value)))) { |
| 1340 | ALOGD("Setting a2dp to suspend state"); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1341 | a2dp.a2dp_suspended = true; |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 1342 | list_for_each(node, &a2dp.adev->usecase_list) { |
| 1343 | uc_info = node_to_item(node, struct audio_usecase, list); |
Zhou Song | c66eb7e | 2017-08-08 18:29:07 +0800 | [diff] [blame] | 1344 | if (uc_info->type == PCM_PLAYBACK && |
| 1345 | (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) { |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 1346 | pthread_mutex_unlock(&a2dp.adev->lock); |
| 1347 | check_a2dp_restore(a2dp.adev, uc_info->stream.out, false); |
| 1348 | pthread_mutex_lock(&a2dp.adev->lock); |
| 1349 | } |
| 1350 | } |
Naresh Tanniru | 03f9dd5 | 2016-10-19 18:46:22 +0530 | [diff] [blame] | 1351 | reset_a2dp_enc_config_params(); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1352 | if(a2dp.audio_suspend_stream) |
| 1353 | a2dp.audio_suspend_stream(); |
| 1354 | } else if (a2dp.a2dp_suspended == true) { |
| 1355 | ALOGD("Resetting a2dp suspend state"); |
Zhou Song | 10617ed | 2017-05-26 13:28:48 +0800 | [diff] [blame] | 1356 | struct audio_usecase *uc_info; |
| 1357 | struct listnode *node; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1358 | if(a2dp.clear_a2dpsuspend_flag) |
| 1359 | a2dp.clear_a2dpsuspend_flag(); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1360 | a2dp.a2dp_suspended = false; |
Naresh Tanniru | 649871a | 2016-11-04 18:08:32 +0530 | [diff] [blame] | 1361 | /* |
| 1362 | * It is possible that before suspend,a2dp sessions can be active |
| 1363 | * for example during music + voice activation concurrency |
| 1364 | * a2dp suspend will be called & BT will change to sco mode |
| 1365 | * though music is paused as a part of voice activation |
| 1366 | * compress session close happens only after pause timeout(10secs) |
| 1367 | * so if resume request comes before pause timeout as a2dp session |
| 1368 | * is already active IPC start will not be called from APM/audio_hw |
| 1369 | * Fix is to call a2dp start for IPC library post suspend |
| 1370 | * based on number of active session count |
| 1371 | */ |
| 1372 | if (a2dp.a2dp_total_active_session_request > 0) { |
| 1373 | ALOGD(" Calling IPC lib start post suspend state"); |
| 1374 | if(a2dp.audio_start_stream) { |
| 1375 | ret = a2dp.audio_start_stream(); |
| 1376 | if (ret != 0) { |
| 1377 | ALOGE("BT controller start failed"); |
| 1378 | a2dp.a2dp_started = false; |
| 1379 | } |
| 1380 | } |
| 1381 | } |
Zhou Song | 10617ed | 2017-05-26 13:28:48 +0800 | [diff] [blame] | 1382 | list_for_each(node, &a2dp.adev->usecase_list) { |
| 1383 | uc_info = node_to_item(node, struct audio_usecase, list); |
Zhou Song | c66eb7e | 2017-08-08 18:29:07 +0800 | [diff] [blame] | 1384 | if (uc_info->type == PCM_PLAYBACK && |
| 1385 | (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) { |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 1386 | pthread_mutex_unlock(&a2dp.adev->lock); |
| 1387 | check_a2dp_restore(a2dp.adev, uc_info->stream.out, true); |
| 1388 | pthread_mutex_lock(&a2dp.adev->lock); |
| 1389 | } |
Zhou Song | 10617ed | 2017-05-26 13:28:48 +0800 | [diff] [blame] | 1390 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1391 | } |
| 1392 | } |
| 1393 | goto param_handled; |
| 1394 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1395 | param_handled: |
| 1396 | ALOGV("end of a2dp setparam"); |
| 1397 | } |
| 1398 | |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 1399 | void audio_extn_a2dp_set_handoff_mode(bool is_on) |
| 1400 | { |
| 1401 | a2dp.is_handoff_in_progress = is_on; |
| 1402 | } |
| 1403 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1404 | bool audio_extn_a2dp_is_force_device_switch() |
| 1405 | { |
| 1406 | //During encoder reconfiguration mode, force a2dp device switch |
Ashish Jain | c597d10 | 2016-12-12 10:31:34 +0530 | [diff] [blame] | 1407 | // Or if a2dp device is selected but earlier start failed ( as a2dp |
| 1408 | // was suspended, force retry. |
| 1409 | return a2dp.is_handoff_in_progress || !a2dp.a2dp_started; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1410 | } |
| 1411 | |
Preetam Singh Ranawat | b7475ba | 2017-02-24 18:17:06 +0530 | [diff] [blame] | 1412 | void audio_extn_a2dp_get_sample_rate(int *sample_rate) |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 1413 | { |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 1414 | *sample_rate = a2dp.enc_sampling_rate; |
| 1415 | } |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 1416 | |
| 1417 | bool audio_extn_a2dp_is_ready() |
| 1418 | { |
| 1419 | bool ret = false; |
| 1420 | |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 1421 | if (a2dp.a2dp_suspended) |
| 1422 | return ret; |
| 1423 | |
Aniket Kumar Lata | 901bcb8 | 2017-03-10 15:42:46 -0800 | [diff] [blame] | 1424 | if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) && |
| 1425 | (a2dp.is_a2dp_offload_supported) && |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 1426 | (a2dp.audio_check_a2dp_ready)) |
| 1427 | ret = a2dp.audio_check_a2dp_ready(); |
| 1428 | return ret; |
| 1429 | } |
| 1430 | |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 1431 | bool audio_extn_a2dp_is_suspended() |
| 1432 | { |
| 1433 | return a2dp.a2dp_suspended; |
| 1434 | } |
| 1435 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1436 | void audio_extn_a2dp_init (void *adev) |
| 1437 | { |
| 1438 | a2dp.adev = (struct audio_device*)adev; |
| 1439 | a2dp.bt_lib_handle = NULL; |
| 1440 | a2dp.a2dp_started = false; |
| 1441 | a2dp.bt_state = A2DP_STATE_DISCONNECTED; |
| 1442 | a2dp.a2dp_total_active_session_request = 0; |
| 1443 | a2dp.a2dp_suspended = false; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 1444 | a2dp.bt_encoder_format = ENC_CODEC_TYPE_INVALID; |
Naresh Tanniru | f5ba8d0 | 2016-09-29 18:06:37 +0530 | [diff] [blame] | 1445 | a2dp.enc_sampling_rate = 48000; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1446 | a2dp.is_a2dp_offload_supported = false; |
| 1447 | a2dp.is_handoff_in_progress = false; |
Aniket Kumar Lata | 53e54b1 | 2017-08-24 17:45:38 -0700 | [diff] [blame] | 1448 | a2dp.is_aptx_dual_mono_supported = false; |
kunleiz | 5a12726 | 2017-09-08 14:47:48 +0800 | [diff] [blame] | 1449 | reset_a2dp_enc_config_params(); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1450 | update_offload_codec_capabilities(); |
| 1451 | } |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 1452 | |
| 1453 | uint32_t audio_extn_a2dp_get_encoder_latency() |
| 1454 | { |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 1455 | uint32_t latency = 0; |
| 1456 | int avsync_runtime_prop = 0; |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 1457 | int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0, |
| 1458 | aac_offset = 0, celt_offset = 0, ldac_offset = 0; |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 1459 | char value[PROPERTY_VALUE_MAX]; |
| 1460 | |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 1461 | memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX); |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 1462 | 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] | 1463 | if (avsync_runtime_prop > 0) { |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 1464 | if (sscanf(value, "%d/%d/%d/%d/%d%d", |
| 1465 | &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) { |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 1466 | ALOGI("Failed to parse avsync offset params from '%s'.", value); |
| 1467 | avsync_runtime_prop = 0; |
| 1468 | } |
| 1469 | } |
| 1470 | |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame] | 1471 | uint32_t slatency = 0; |
| 1472 | if (a2dp.audio_get_a2dp_sink_latency && a2dp.bt_state != A2DP_STATE_DISCONNECTED) { |
| 1473 | slatency = a2dp.audio_get_a2dp_sink_latency(); |
| 1474 | } |
| 1475 | |
Aniket Kumar Lata | faaffde | 2017-03-22 19:18:15 -0700 | [diff] [blame] | 1476 | switch(a2dp.bt_encoder_format) { |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 1477 | case ENC_CODEC_TYPE_SBC: |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame] | 1478 | latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC; |
| 1479 | latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency; |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 1480 | break; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 1481 | case ENC_CODEC_TYPE_APTX: |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame] | 1482 | latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX; |
| 1483 | latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency; |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 1484 | break; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 1485 | case ENC_CODEC_TYPE_APTX_HD: |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame] | 1486 | latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD; |
| 1487 | latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency; |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 1488 | break; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 1489 | case ENC_CODEC_TYPE_AAC: |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame] | 1490 | latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC; |
| 1491 | latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency; |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 1492 | break; |
Preetam Singh Ranawat | 0d645ea | 2017-08-07 18:12:07 +0530 | [diff] [blame] | 1493 | case ENC_CODEC_TYPE_CELT: |
| 1494 | latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT; |
| 1495 | latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency; |
| 1496 | break; |
Preetam Singh Ranawat | d058a3d | 2017-10-25 17:31:37 +0530 | [diff] [blame] | 1497 | case ENC_CODEC_TYPE_LDAC: |
| 1498 | latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC; |
| 1499 | latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency; |
| 1500 | break; |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 1501 | default: |
| 1502 | latency = 200; |
| 1503 | break; |
| 1504 | } |
| 1505 | return latency; |
| 1506 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1507 | #endif // SPLIT_A2DP_ENABLED |