blob: edc1e75ffdb3845528a6dc311a71f3ae04aa7928 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*****************************************************************************
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>
25#include "voice_lib.h"
26
27#define VOICE_DEV_NAME "/dev/voice_device"
28
29
30
31typedef struct {
32 int fd;
33 unsigned int state; // 0 close;1 open
34 T_Voice_Para vpara;
35} T_voiceInfo;
36
37T_voiceInfo *voiceinfo = NULL;
38
39static int slic_flag = 0;
40static int slic_flag_already = 0;// 0 not get ,1 already get
41
42struct pcm *volte_pcm_voice_out = NULL;
43struct pcm *volte_pcm_voice_in = NULL;
44
45#if 0
46
47
48T_Voice_Para gsmhwpara = {
49 8000, //8000;16000
50 0, //// 0 gsm;1 td;2 wcdma;3 lte
51 0//0 hardware dsp;1 soft amr lib
52};
53
54T_Voice_Para tdnbhwpara = {
55 8000, //8000;16000
56 1, //// 0 gsm;1 td;2 wcdma;3 lte
57 0,//0 amr-nb;1 amr-wb
58 0//0 hardware dsp;1 soft amr lib
59};
60
61T_Voice_Para tdnbsfpara = {
62 8000, //8000;16000
63 1, //// 0 gsm;1 td;2 wcdma;3 lte
64 0,//0 amr-nb;1 amr-wb
65 1//0 hardware dsp;1 soft amr lib
66};
67
68T_Voice_Para tdwbsfpara = {
69 8000, //8000;16000
70 1, //// 0 gsm;1 td;2 wcdma;3 lte
71 1,//0 amr-nb;1 amr-wb
72 1//0 hardware dsp;1 soft amr lib
73};
74
75
76#endif
77//static volatile int voiceinfo.fd = 0;
78
79
80int voice_open(T_Voice_Para *para)
81{
82 int ret = 0;
83 if (!para) {
84
85 printf("voice: open para is NULL.\n");
86
87 return -1;
88 }
89
90 voiceinfo = calloc(1, sizeof(T_voiceInfo));
91 if (!voiceinfo) {
92
93 printf("voice: open calloc fail!\n");
94 return -1;
95 }
96
97 voiceinfo->vpara = *para;
98
99 voiceinfo->fd = open(VOICE_DEV_NAME, O_RDONLY);
100 if (voiceinfo->fd < 0) {
101 printf("voice lib: open voice device error.\n");
102 free(voiceinfo);
103 return -1;
104
105 }
106
107
108 ret = ioctl(voiceinfo->fd, VOICE_IOCTL_START, para);
109 if (ret) {
110 printf("voice lib: voice start fd=%d,ret=%d.\n", voiceinfo->fd, ret);
111 goto err;
112 }
113 printf("voice open end !\n");
114
115 return 0;
116
117err:
118 close(voiceinfo->fd);
119 free(voiceinfo);
120 printf("voice open err end !\n");
121 return -1;
122
123
124}
125
126
127int voice_close(T_Voice_Para *para)
128{
129 int ret = 0;
130
131 if (!voiceinfo) {
132
133 printf("voice: close voiceinfo is NUll!\n");
134 return 0;
135 }
136
137 ret = ioctl(voiceinfo->fd, VOICE_IOCTL_STOP, para);
138
139 if (ret) {
140 printf("voice lib: voice stop fd=%d,ret=%d.\n", voiceinfo->fd, ret);
141 }
142
143 if (voiceinfo->fd >= 0) {
144 close(voiceinfo->fd);
145 }
146 voiceinfo->fd = -1;
147
148 free(voiceinfo);
149 voiceinfo = NULL;
150
151 printf("voice close end !\n");
152
153 return 0;
154}
155int voice_Vploop(int *path)
156{
157 int ret = 0;
158 int fd = -1;
159 printf("voice_Vploop: start path=%d!\n",*path);
160 fd = open(VOICE_DEV_NAME, O_RDONLY);
161 if (fd < 0) {
162 printf("voice_Vploop: open voice device error.\n");
163 return -1;
164 }
165 ret = ioctl(fd, VOICE_IOCTL_VPLOOP, path);
166 if (ret) {
167 printf("voice_Vploop: ret=%d,path=%p.\n", ret, path);
168 close(fd);
169 return -1;
170 }
171 close(fd);
172 return 0;
173
174}
175
176int voice_GetSlicFlag(void)
177{
178 int ret = 0;
179 int fd = -1;
180 int flag = 0;
181 printf("voice_GetSlicFlag: start!\n");
182 fd = open(VOICE_DEV_NAME, O_RDONLY);
183 if (fd < 0) {
184 printf("voice_GetSlicFlag: open voice device error.\n");
185 return -1;
186 }
187 ret = ioctl(fd, VOICE_IOCTL_GET_SLIC_USE_FLAG, &flag);
188 if (ret) {
189 printf("voice_GetSlicFlag: ret=%d,flag=%d.\n", ret, flag);
190 close(fd);
191 return -1;
192 }
193 close(fd);
194 slic_flag_already = 1;
195 return flag;
196
197}
198
199int zDrvVolte_PreOpen(T_ZDrvVolte_Cfg *cfgParam)
200{
201
202
203 struct mixer *voice_mixer = NULL;
204 struct pcm_config config_voice = {0};
205 printf(" voice lib zDrvVolte_PreOpen!\n");
206
207 if (slic_flag_already == 1) {
208 printf(" voice slic flag already get, slic_flag=%d!\n", slic_flag);
209 if (slic_flag == 1) {
210 return 0;
211 }
212
213 } else {
214 slic_flag = voice_GetSlicFlag();
215 printf(" voice slic flag get, slic_flag=%d!\n", slic_flag);
216 if (slic_flag == 1) {
217 return 0;
218 }
219 }
220#if defined(_VBUFF_IN_SINGLE_CORE) || defined(_VBUFF_IN_MULTI_CORE)
221 printf("%s: use voice buffer,return!\n",__func__);
222 return 0;
223
224#endif
225
226 //open mixer dev for codec control
227 voice_mixer = mixer_open(0);
228 if (!voice_mixer) {
229 printf("voice_mixer open failed!\n");
230 return -1;
231 }
232
233 //config mixer dev
234 mix_set_voice_path(voice_mixer, T_OUTPUT_SPEAKER);
235 mix_set_voice_vol(voice_mixer, T_VOICE_VOL_3_LEVEL);
236
237 //close mixer
238 mixer_close(voice_mixer);
239 voice_mixer = NULL;
240
241 //open pcm dev for data tranf
242 config_voice.channels = cfgParam->channel_count;
243 config_voice.rate = cfgParam->clock_rate;
244 //buffer num
245 config_voice.period_count = 3;
246 //buffer size
247 config_voice.period_size = cfgParam->samples_per_frame * cfgParam->bits_per_sample / 8;
248 //16-bit signed
249 config_voice.format = PCM_FORMAT_S16_LE;
250
251 //card 0 dev 1
252 //23G card 0 dev 2
253 volte_pcm_voice_out = pcm_open(0, 1, PCM_OUT, &config_voice);
254 if (!pcm_is_ready(volte_pcm_voice_out)) {
255 printf("volte_pcm_voice_out open failed!\n");
256 goto err_ret;
257 }
258
259 volte_pcm_voice_in = pcm_open(0, 1, PCM_IN, &config_voice);
260 if (!pcm_is_ready(volte_pcm_voice_in)) {
261 printf("volte_pcm_voice_in open failed!\n");
262 goto err_ret;
263 }
264
265 if (0 != pcm_prepare(volte_pcm_voice_out)) {
266 printf("volte_pcm_voice_out pcm_prepare failed!\n");
267 goto err_ret;
268 }
269
270 if (0 != pcm_prepare(volte_pcm_voice_in)) {
271 printf("volte_pcm_voice_in pcm_prepare failed!\n");
272 goto err_ret;
273 }
274 return 0;
275err_ret:
276
277 pcm_close(volte_pcm_voice_out);
278 volte_pcm_voice_out = NULL;
279
280 if (volte_pcm_voice_in) {
281 pcm_close(volte_pcm_voice_in);
282 volte_pcm_voice_in = NULL;
283 }
284 return -1;
285
286}
287
288void zDrvVolte_PreClose(void)
289{
290
291 printf(" voice lib zDrvVolte_PreClose!\n");
292
293 if (slic_flag_already == 1) {
294 printf(" voice slic flag already get, slic_flag=%d!\n", slic_flag);
295 if (slic_flag == 1) {
296 return ;
297 }
298
299 } else {
300 slic_flag = voice_GetSlicFlag();
301 printf(" voice slic flag get, slic_flag=%d!\n", slic_flag);
302 if (slic_flag == 1) {
303 return ;
304 }
305 }
306#if defined(_VBUFF_IN_SINGLE_CORE) || defined(_VBUFF_IN_MULTI_CORE)
307 printf("%s: use voice buffer,return!\n",__func__);
308 return 0;
309
310#endif
311
312
313 if (volte_pcm_voice_out) {
314 pcm_close(volte_pcm_voice_out);
315 volte_pcm_voice_out = NULL;
316 }
317 if (volte_pcm_voice_in) {
318 pcm_close(volte_pcm_voice_in);
319 volte_pcm_voice_in = NULL;
320 }
321}
322
323
324
325
326