lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /*****************************************************************************
|
| 2 | ** °æÈ¨ËùÓÐ (C)2015, ÖÐÐËͨѶ¹É·ÝÓÐÏÞ¹«Ë¾¡£
|
| 3 | **
|
| 4 | ** ÎļþÃû³Æ: voice.c
|
| 5 | ** Îļþ±êʶ:
|
| 6 | ** ÄÚÈÝÕªÒª:
|
| 7 | ** ʹÓ÷½·¨:
|
| 8 | **
|
| 9 | ** ÐÞ¸ÄÈÕÆÚ °æ±¾ºÅ Ð޸ıê¼Ç ÐÞ¸ÄÈË ÐÞ¸ÄÄÚÈÝ
|
| 10 | ** -----------------------------------------------------------------------------
|
| 11 | ** 2019/09/25 V1.0 Create xxq ´´½¨
|
| 12 | **
|
| 13 | * ******************************************************************************/
|
| 14 |
|
| 15 | #include <stdio.h>
|
| 16 | #include <unistd.h>
|
| 17 | #include <string.h>
|
| 18 | #include <stdlib.h>
|
| 19 |
|
| 20 | #include <stdint.h>
|
| 21 | #include <linux/volte_drv.h>
|
| 22 | #include <sys/ioctl.h>
|
| 23 | #include <fcntl.h>
|
| 24 | #include <tinyalsa/audio_mixer_ctrl.h>
|
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 25 | #include "voice_lib.h"
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 26 | #include "voice_ipc.h"
|
| 27 |
|
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 28 |
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 29 |
|
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 30 | //#define VOICE_DEV_NAME "/dev/voice_device"
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 31 |
|
| 32 |
|
| 33 |
|
| 34 | typedef struct {
|
| 35 | int fd;
|
| 36 | unsigned int state; // 0 close;1 open
|
| 37 | T_Voice_Para vpara;
|
| 38 | } T_voiceInfo;
|
| 39 |
|
| 40 | T_voiceInfo *voiceinfo = NULL;
|
| 41 |
|
| 42 | static int slic_flag = 0;
|
| 43 | static int slic_flag_already = 0;// 0 not get ,1 already get
|
| 44 |
|
| 45 | struct pcm *volte_pcm_voice_out = NULL;
|
| 46 | struct pcm *volte_pcm_voice_in = NULL;
|
| 47 |
|
| 48 | #if 0
|
| 49 |
|
| 50 |
|
| 51 | T_Voice_Para gsmhwpara = {
|
| 52 | 8000, //8000;16000
|
| 53 | 0, //// 0 gsm;1 td;2 wcdma;3 lte
|
| 54 | 0//0 hardware dsp;1 soft amr lib
|
| 55 | };
|
| 56 |
|
| 57 | T_Voice_Para tdnbhwpara = {
|
| 58 | 8000, //8000;16000
|
| 59 | 1, //// 0 gsm;1 td;2 wcdma;3 lte
|
| 60 | 0,//0 amr-nb;1 amr-wb
|
| 61 | 0//0 hardware dsp;1 soft amr lib
|
| 62 | };
|
| 63 |
|
| 64 | T_Voice_Para tdnbsfpara = {
|
| 65 | 8000, //8000;16000
|
| 66 | 1, //// 0 gsm;1 td;2 wcdma;3 lte
|
| 67 | 0,//0 amr-nb;1 amr-wb
|
| 68 | 1//0 hardware dsp;1 soft amr lib
|
| 69 | };
|
| 70 |
|
| 71 | T_Voice_Para tdwbsfpara = {
|
| 72 | 8000, //8000;16000
|
| 73 | 1, //// 0 gsm;1 td;2 wcdma;3 lte
|
| 74 | 1,//0 amr-nb;1 amr-wb
|
| 75 | 1//0 hardware dsp;1 soft amr lib
|
| 76 | };
|
| 77 |
|
| 78 |
|
| 79 | #endif
|
| 80 | //static volatile int voiceinfo.fd = 0;
|
| 81 |
|
| 82 |
|
| 83 | int voice_open(T_Voice_Para *para)
|
| 84 | {
|
| 85 | int ret = 0;
|
| 86 | if (!para) {
|
| 87 |
|
| 88 | printf("voice: open para is NULL.\n");
|
| 89 |
|
| 90 | return -1;
|
| 91 | }
|
| 92 |
|
| 93 | voiceinfo = calloc(1, sizeof(T_voiceInfo));
|
| 94 | if (!voiceinfo) {
|
| 95 |
|
| 96 | printf("voice: open calloc fail!\n");
|
| 97 | return -1;
|
| 98 | }
|
| 99 |
|
| 100 | voiceinfo->vpara = *para;
|
| 101 |
|
| 102 | voiceinfo->fd = open(VOICE_DEV_NAME, O_RDONLY);
|
| 103 | if (voiceinfo->fd < 0) {
|
| 104 | printf("voice lib: open voice device error.\n");
|
| 105 | free(voiceinfo);
|
| 106 | return -1;
|
| 107 |
|
| 108 | }
|
| 109 |
|
| 110 |
|
| 111 | ret = ioctl(voiceinfo->fd, VOICE_IOCTL_START, para);
|
| 112 | if (ret) {
|
| 113 | printf("voice lib: voice start fd=%d,ret=%d.\n", voiceinfo->fd, ret);
|
| 114 | goto err;
|
| 115 | }
|
| 116 | printf("voice open end !\n");
|
| 117 |
|
| 118 | return 0;
|
| 119 |
|
| 120 | err:
|
| 121 | close(voiceinfo->fd);
|
| 122 | free(voiceinfo);
|
| 123 | printf("voice open err end !\n");
|
| 124 | return -1;
|
| 125 |
|
| 126 |
|
| 127 | }
|
| 128 |
|
| 129 |
|
| 130 | int voice_close(T_Voice_Para *para)
|
| 131 | {
|
| 132 | int ret = 0;
|
| 133 |
|
| 134 | if (!voiceinfo) {
|
| 135 |
|
| 136 | printf("voice: close voiceinfo is NUll!\n");
|
| 137 | return 0;
|
| 138 | }
|
| 139 |
|
| 140 | ret = ioctl(voiceinfo->fd, VOICE_IOCTL_STOP, para);
|
| 141 |
|
| 142 | if (ret) {
|
| 143 | printf("voice lib: voice stop fd=%d,ret=%d.\n", voiceinfo->fd, ret);
|
| 144 | }
|
| 145 |
|
| 146 | if (voiceinfo->fd >= 0) {
|
| 147 | close(voiceinfo->fd);
|
| 148 | }
|
| 149 | voiceinfo->fd = -1;
|
| 150 |
|
| 151 | free(voiceinfo);
|
| 152 | voiceinfo = NULL;
|
| 153 |
|
| 154 | printf("voice close end !\n");
|
| 155 |
|
| 156 | return 0;
|
| 157 | }
|
| 158 | int voice_Vploop(int *path)
|
| 159 | {
|
| 160 | int ret = 0;
|
| 161 | int fd = -1;
|
| 162 | printf("voice_Vploop: start path=%d!\n",*path);
|
| 163 | fd = open(VOICE_DEV_NAME, O_RDONLY);
|
| 164 | if (fd < 0) {
|
| 165 | printf("voice_Vploop: open voice device error.\n");
|
| 166 | return -1;
|
| 167 | }
|
| 168 | ret = ioctl(fd, VOICE_IOCTL_VPLOOP, path);
|
| 169 | if (ret) {
|
| 170 | printf("voice_Vploop: ret=%d,path=%p.\n", ret, path);
|
| 171 | close(fd);
|
| 172 | return -1;
|
| 173 | }
|
| 174 | close(fd);
|
| 175 | return 0;
|
| 176 |
|
| 177 | }
|
| 178 |
|
| 179 | int voice_GetSlicFlag(void)
|
| 180 | {
|
| 181 | int ret = 0;
|
| 182 | int fd = -1;
|
| 183 | int flag = 0;
|
| 184 | printf("voice_GetSlicFlag: start!\n");
|
| 185 | fd = open(VOICE_DEV_NAME, O_RDONLY);
|
| 186 | if (fd < 0) {
|
| 187 | printf("voice_GetSlicFlag: open voice device error.\n");
|
| 188 | return -1;
|
| 189 | }
|
| 190 | ret = ioctl(fd, VOICE_IOCTL_GET_SLIC_USE_FLAG, &flag);
|
| 191 | if (ret) {
|
| 192 | printf("voice_GetSlicFlag: ret=%d,flag=%d.\n", ret, flag);
|
| 193 | close(fd);
|
| 194 | return -1;
|
| 195 | }
|
| 196 | close(fd);
|
| 197 | slic_flag_already = 1;
|
| 198 | return flag;
|
| 199 |
|
| 200 | }
|
| 201 |
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 202 | static T_ZDrvVoice_Cfg s_cfgParam = {0};
|
| 203 | static int cur_vmode = MAX_AVOICE_MODE;
|
| 204 |
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 205 | int zDrvVolte_PreOpen(T_ZDrvVolte_Cfg *cfgParam)
|
| 206 | {
|
| 207 |
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 208 | struct mixer *voice_mixer = NULL;
|
| 209 | struct pcm_config config_voice = {0};
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 210 | int ret = 0;
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 211 | printf(" voice lib zDrvVolte_PreOpen!\n");
|
| 212 |
|
| 213 | if (slic_flag_already == 1) {
|
| 214 | printf(" voice slic flag already get, slic_flag=%d!\n", slic_flag);
|
| 215 | if (slic_flag == 1) {
|
| 216 | return 0;
|
| 217 | }
|
| 218 |
|
| 219 | } else {
|
| 220 | slic_flag = voice_GetSlicFlag();
|
| 221 | printf(" voice slic flag get, slic_flag=%d!\n", slic_flag);
|
| 222 | if (slic_flag == 1) {
|
| 223 | return 0;
|
| 224 | }
|
| 225 | }
|
xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 226 | #if defined(_VBUFF_IN_SINGLE_CORE) || defined(_VBUFF_IN_MULTI_CORE)
|
| 227 | printf("%s: use voice buffer,return!\n",__func__);
|
| 228 | return 0;
|
| 229 |
|
| 230 | #endif
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 231 | #if defined(_ALSA_CODEC_IN_CAP) && defined(_USE_ALSA_AT_INTF)
|
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 232 |
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 233 |
|
| 234 | printf("%s: i2s and codec not need config,return!\n",__func__);
|
| 235 | return 0;
|
| 236 |
|
| 237 | #elif defined(_ALSA_CODEC_IN_CAP)
|
| 238 |
|
| 239 |
|
| 240 | if(cfgParam->clock_rate == 8000){
|
| 241 |
|
| 242 | ret = cap_alsa_voice_open(AVOICE_4G_NB);
|
| 243 | printf("%s:cap_alsa_voice_open clock_rate(%d) ret=%d!\n",__func__,cfgParam->clock_rate,ret);
|
| 244 | cur_vmode = AVOICE_4G_NB;
|
| 245 |
|
| 246 | }
|
| 247 | else if(cfgParam->clock_rate == 16000){
|
| 248 |
|
| 249 | ret = cap_alsa_voice_open(AVOICE_4G_WB);
|
| 250 | printf("%s:cap_alsa_voice_open clock_rate(%d) ret=%d!\n",__func__,cfgParam->clock_rate,ret);
|
| 251 | cur_vmode = AVOICE_4G_WB;
|
| 252 | }
|
| 253 | else{
|
| 254 |
|
| 255 | printf("%s:cap_alsa_voice_open clock_rate(%d) not support!\n",__func__,cfgParam->clock_rate);
|
| 256 | return -2;
|
| 257 |
|
| 258 | }
|
| 259 | s_cfgParam = *cfgParam;
|
| 260 | return ret;
|
| 261 |
|
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 262 | #endif
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 263 |
|
| 264 |
|
| 265 | //open mixer dev for codec control
|
| 266 | voice_mixer = mixer_open(0);
|
| 267 | if (!voice_mixer) {
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 268 | printf("voice_mixer open failed!\n");
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 269 | return -1;
|
| 270 | }
|
| 271 |
|
| 272 | //config mixer dev
|
| 273 | mix_set_voice_path(voice_mixer, T_OUTPUT_SPEAKER);
|
| 274 | mix_set_voice_vol(voice_mixer, T_VOICE_VOL_3_LEVEL);
|
| 275 |
|
| 276 | //close mixer
|
| 277 | mixer_close(voice_mixer);
|
| 278 | voice_mixer = NULL;
|
| 279 |
|
| 280 | //open pcm dev for data tranf
|
| 281 | config_voice.channels = cfgParam->channel_count;
|
| 282 | config_voice.rate = cfgParam->clock_rate;
|
| 283 | //buffer num
|
| 284 | config_voice.period_count = 3;
|
| 285 | //buffer size
|
| 286 | config_voice.period_size = cfgParam->samples_per_frame * cfgParam->bits_per_sample / 8;
|
| 287 | //16-bit signed
|
| 288 | config_voice.format = PCM_FORMAT_S16_LE;
|
| 289 |
|
| 290 | //card 0 dev 1
|
| 291 | //23G card 0 dev 2
|
| 292 | volte_pcm_voice_out = pcm_open(0, 1, PCM_OUT, &config_voice);
|
| 293 | if (!pcm_is_ready(volte_pcm_voice_out)) {
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 294 | printf("volte_pcm_voice_out open failed!\n");
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 295 | goto err_ret;
|
| 296 | }
|
| 297 |
|
| 298 | volte_pcm_voice_in = pcm_open(0, 1, PCM_IN, &config_voice);
|
| 299 | if (!pcm_is_ready(volte_pcm_voice_in)) {
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 300 | printf("volte_pcm_voice_in open failed!\n");
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 301 | goto err_ret;
|
| 302 | }
|
| 303 |
|
| 304 | if (0 != pcm_prepare(volte_pcm_voice_out)) {
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 305 | printf("volte_pcm_voice_out pcm_prepare failed!\n");
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 306 | goto err_ret;
|
| 307 | }
|
| 308 |
|
| 309 | if (0 != pcm_prepare(volte_pcm_voice_in)) {
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 310 | printf("volte_pcm_voice_in pcm_prepare failed!\n");
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 311 | goto err_ret;
|
| 312 | }
|
| 313 | return 0;
|
| 314 | err_ret:
|
| 315 |
|
| 316 | pcm_close(volte_pcm_voice_out);
|
| 317 | volte_pcm_voice_out = NULL;
|
| 318 |
|
| 319 | if (volte_pcm_voice_in) {
|
| 320 | pcm_close(volte_pcm_voice_in);
|
| 321 | volte_pcm_voice_in = NULL;
|
| 322 | }
|
| 323 | return -1;
|
| 324 |
|
| 325 | }
|
| 326 |
|
| 327 | void zDrvVolte_PreClose(void)
|
| 328 | {
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 329 | int ret = 0;
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 330 |
|
| 331 | printf(" voice lib zDrvVolte_PreClose!\n");
|
| 332 |
|
| 333 | if (slic_flag_already == 1) {
|
| 334 | printf(" voice slic flag already get, slic_flag=%d!\n", slic_flag);
|
| 335 | if (slic_flag == 1) {
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 336 | return;
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 337 | }
|
| 338 |
|
| 339 | } else {
|
| 340 | slic_flag = voice_GetSlicFlag();
|
| 341 | printf(" voice slic flag get, slic_flag=%d!\n", slic_flag);
|
| 342 | if (slic_flag == 1) {
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 343 | return;
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 344 | }
|
| 345 | }
|
xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 346 | #if defined(_VBUFF_IN_SINGLE_CORE) || defined(_VBUFF_IN_MULTI_CORE)
|
| 347 | printf("%s: use voice buffer,return!\n",__func__);
|
| 348 | return 0;
|
| 349 |
|
| 350 | #endif
|
| 351 |
|
| 352 |
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 353 | #if defined(_ALSA_CODEC_IN_CAP) && defined(_USE_ALSA_AT_INTF)
|
| 354 |
|
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 355 | printf("%s: i2s and codec not need config,return!\n",__func__);
|
| 356 | return ;
|
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 357 | #elif defined(_ALSA_CODEC_IN_CAP)
|
| 358 | if(s_cfgParam.clock_rate == 8000){
|
| 359 |
|
| 360 | ret = cap_alsa_voice_close(AVOICE_4G_NB);
|
| 361 | printf("%s:cap_alsa_voice_close clock_rate(%d) ret=%d!\n",__func__,s_cfgParam.clock_rate,ret);
|
| 362 |
|
| 363 | }
|
| 364 | else if(s_cfgParam.clock_rate == 16000){
|
| 365 |
|
| 366 | ret = cap_alsa_voice_close(AVOICE_4G_WB);
|
| 367 | printf("%s:cap_alsa_voice_close clock_rate(%d) ret=%d!\n",__func__,s_cfgParam.clock_rate,ret);
|
| 368 |
|
| 369 | }
|
| 370 | else{
|
| 371 |
|
| 372 | printf("%s:cap_alsa_voice_close clock_rate(%d) not support!\n",__func__,s_cfgParam.clock_rate);
|
| 373 |
|
| 374 | }
|
| 375 | return;
|
| 376 |
|
| 377 |
|
| 378 |
|
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 379 | #endif
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 380 |
|
| 381 |
|
| 382 | if (volte_pcm_voice_out) {
|
| 383 | pcm_close(volte_pcm_voice_out);
|
| 384 | volte_pcm_voice_out = NULL;
|
| 385 | }
|
| 386 | if (volte_pcm_voice_in) {
|
| 387 | pcm_close(volte_pcm_voice_in);
|
| 388 | volte_pcm_voice_in = NULL;
|
| 389 | }
|
| 390 | }
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|