blob: f783f5f84f0929a15d2483c79b964b7b9ca2fb71 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/**
2 * \file lynq_audio_api.c
3 * \brief A Documented file.
4 *
5 * Detailed description
6 * \Author: luojian
7 * \Version: 1.0.0
8 * \Date: 2022-10-27
9 */
10#include <fcntl.h>
11#include <stdint.h>
12#include <limits.h>
13#include <termios.h>
14#include <stdarg.h>
15#include <dirent.h>
16#include <sys/stat.h>
17#include <sys/statfs.h>
18#include <sys/types.h>
19#include <string.h>
20#include <unistd.h>
21#include <pthread.h>
22#include <time.h>
23#include <sys/ioctl.h>
24#include <stdio.h>
25#include <signal.h>
26#include <errno.h>
27#include <stdlib.h>
28#include <pthread.h>
29
30#include "mbtk_log.h"
31#include "mbtk_type.h"
b.liu5fa9e772023-11-23 18:00:55 +080032
33#ifdef MBTK_PLATFORM_ASR1803
34
liubin281ac462023-07-19 14:22:54 +080035#include "mbtk_audio.h"
36
37static int rec_fd = 0;
38static int play_fd = 0;
39static mbtk_audio_handle play_hdl = NULL;
40static mbtk_audio_handle record_hdl = NULL;
41int volume_size = 0;
42pthread_t paly_thread;
43
44
45void dtmf_cb(char dtmf)
46{
47 printf("%s:%c\n", __FUNCTION__, dtmf);
48}
49
50void audio_volume_cb(int volume)
51{
52 volume_size = volume;
53 printf("%s:%d\n", __FUNCTION__, volume);
54}
55
56int lynq_memscpy
57(
58 void *dst,
59 int dst_size,
60 const void *src,
61 int src_size
62)
63{
64 if(dst_size == 0 || src_size == 0 || dst == NULL || src == NULL)
65 {
66 return 0;
67 }
68 else
69 {
70 return memcpy( dst, src,src_size);
71 }
72} /* dsatutil_free_memory() */
73
74static int lynq_get_path_name ( const char* path_name,char* path )
75{
76 int i=0;
77 int last = -1;
78 int len = strlen ( path_name );
79 if ( len > 0 )
80 {
81 for ( i=len - 1; i >= 0; i-- )
82 {
83 if ( path_name[i] == '/' )
84 {
85 last = i;
86 break;
87 }
88 }
89 if ( i != -1 )
90 {
91 lynq_memscpy ( path, ( i + 1 ) * sizeof ( char ), path_name, ( i + 1 ) * sizeof ( char ) );
92 printf ( "mbtk_get_path %s", path );
93 return 0;
94 }
95 }
96 return -1;
97}
98
99int lynq_create_audio_dir(const char *dirname)
100{
101 DIR *p_dir;
102 int res = -1, i = 0;;
103 char str[512];
104 strncpy(str, dirname, 512);
105 int len=strlen(str);
106
107 if(NULL == (p_dir = opendir((const char *)dirname)))
108 {
109 for(i=0; i<len; i++ )
110 {
111 if( str[i]=='/' )
112 {
113 str[i] = '\0';
114 if( access(str,0)!=0 )
115 {
116 if(mkdir(dirname, 0777) == 0)
117 {
118 res = 0;
119 }
120 else
121 {
122 fprintf(stderr, "create audio dir error \n");
123 res = -1;
124 }
125 }
126 str[i]='/';
127 }
128 }
129 if( len>0 && access(str,0)!=0 )
130 {
131 if(mkdir(dirname, 0777) == 0)
132 {
133 res = 0;
134 }
135 else
136 {
137 fprintf(stderr, "create audio dir error \n");
138 res = -1;
139 }
140 }
141 }
142 else
143 {
144 closedir(p_dir);
145 res = 0;
146 }
147 return res;
148}
149
150void lynq_record_cb_func(int cb_result, char* databuf, unsigned int len)
151{
152 int rc;
153 // printf("lynq_record_cb_func() len:%d, rec_fd:%d\n", len, rec_fd);
154 if(NULL == databuf)
155 {
156 printf("NULL == databuf\n");
157 }
158
159 if(NULL != databuf && len > 0 && rec_fd > 0)
160 {
161 //for debug:save into file
162 rc = write(rec_fd, databuf, len);
163 if (rc < 0) {
164 printf("%s: error writing to file!\n", __FUNCTION__);
165 } else if (rc < len) {
166 printf("%s: wrote less the buffer size!\n", __FUNCTION__);
167 }
168 }
169}
b.liu5fa9e772023-11-23 18:00:55 +0800170#else
171
172#endif
liubin281ac462023-07-19 14:22:54 +0800173
174int lynq_media_rec_audio(const char *path)
175{
b.liu5fa9e772023-11-23 18:00:55 +0800176#ifdef MBTK_PLATFORM_ASR1803
liubin281ac462023-07-19 14:22:54 +0800177 int ret = 0;
178 char audio_dir[50] ={0};
179 char audio_wav[10] = {0};
180 lynq_get_path_name(path, audio_dir);
181 printf("path:%s, audio_dir:%s\n", path, audio_dir);
182
183 record_hdl = mbtk_audio_open(MBTK_AUTIO_TYPE_IN, 1, 8000, NULL);
184 if (record_hdl == 0)
185 {
186 printf("AudRecorder open error\n");
187 return -1;
188 }
189
190 lynq_create_audio_dir(audio_dir);
191 rec_fd = open(path, O_RDWR|O_CREAT|O_TRUNC, 0644);
192 if (rec_fd <= 0)
193 {
194 printf("file open error\n");
195 goto err;
196 }
197
198 if(-1 == mbtk_audio_record(record_hdl, lynq_record_cb_func, NULL))
199 {
200 printf("file write error\n");
201 goto err;
202 }
203
204 return 0;
205// sleep(10);
206err:
207// Ql_AudRecorder_Close();
208 if(rec_fd > 0)
209 {
210 close(rec_fd);
211 rec_fd = 0;
212 }
213
214 return -1;
b.liu5fa9e772023-11-23 18:00:55 +0800215#else
216
217 return -1;
218#endif
liubin281ac462023-07-19 14:22:54 +0800219}
220
221
222
223//停止录制音频文件
224void lynq_media_rec_stop_audio(void)
225{
b.liu5fa9e772023-11-23 18:00:55 +0800226#ifdef MBTK_PLATFORM_ASR1803
liubin281ac462023-07-19 14:22:54 +0800227// sleep(10);
228 mbtk_audio_close(record_hdl);
229 if(rec_fd > 0)
230 {
231 close(rec_fd);
232 rec_fd = 0;
233 }
b.liu5fa9e772023-11-23 18:00:55 +0800234#else
235
236#endif
liubin281ac462023-07-19 14:22:54 +0800237}
238
b.liu5fa9e772023-11-23 18:00:55 +0800239#ifdef MBTK_PLATFORM_ASR1803
liubin281ac462023-07-19 14:22:54 +0800240//播放音频文件
241int lynq_media_play_audio_thread_handle(void *argv)
242{
243 char databuf[1024];
244 int size;
245
246 char *path = (char *)argv;
247 printf("lynq_media_play_audio() start \npath:%s\n",path);
248 LOGI("%s %d", __FUNCTION__, __LINE__);
249 play_hdl = mbtk_audio_open(MBTK_AUTIO_TYPE_OUT, 1, 8000, NULL);
250 if(NULL == play_hdl)
251 printf("mbtk_audio_open fail\n");
252
253 play_fd = open(path, O_RDWR);
254 if (play_fd <= 0)
255 {
256 printf("file open error\n");
257 goto err;
258 }
259 memset(databuf, 0, sizeof(databuf));
260 while(0 < (size = read(play_fd, databuf, sizeof(databuf))))
261 {
262 if(-1 == mbtk_audio_play_stream(play_hdl, databuf, size))
263 break;
264 }
265 printf("aplay Stream end \n");
266
267err:
268 if(play_fd > 0)
269 {
270 close(play_fd);
271 play_fd = 0;
272 }
273
274 pthread_exit(&paly_thread);
275 mbtk_audio_close(play_hdl);
276 return 0;
277}
b.liu5fa9e772023-11-23 18:00:55 +0800278#endif
liubin281ac462023-07-19 14:22:54 +0800279
280//创建线程播放音频文件
281int lynq_media_play_audio(const char *path)
282{
b.liu5fa9e772023-11-23 18:00:55 +0800283#ifdef MBTK_PLATFORM_ASR1803
liubin281ac462023-07-19 14:22:54 +0800284 int ret = pthread_create(&paly_thread, NULL, lynq_media_play_audio_thread_handle, (void *)path);
285 if (ret != 0) {
286 printf("create thread failed!\n");
287 return -1;
288 }
289
290 pthread_detach(paly_thread);
291 return 0;
b.liu5fa9e772023-11-23 18:00:55 +0800292#else
293
294 return 0;
295#endif
liubin281ac462023-07-19 14:22:54 +0800296}
297
298
299//停止播放音频文件
300void lynq_media_stop_audio(void)
301{
b.liu5fa9e772023-11-23 18:00:55 +0800302#ifdef MBTK_PLATFORM_ASR1803
liubin281ac462023-07-19 14:22:54 +0800303 printf("lynq_media_stop_audio()----\n");
304 if(play_fd > 0)
305 {
306 int ret = pthread_cancel(paly_thread);
307 if (ret != 0) {
308 printf("cancle paly_thread fail\n");
309 return ;
310 }
311 close(play_fd);
312 play_fd = 0;
313 }
314 mbtk_audio_close(play_hdl);
b.liu5fa9e772023-11-23 18:00:55 +0800315#else
316
317#endif
liubin281ac462023-07-19 14:22:54 +0800318}
319
b.liu5fa9e772023-11-23 18:00:55 +0800320#ifdef MBTK_PLATFORM_ASR1803
liubin281ac462023-07-19 14:22:54 +0800321int lynq_audio_ubus_client_init(mbtk_audio_client_handle_type *ph_audio, mbtk_dtmf_cb cb)
322{
323 if(rec_fd > 0 || play_fd > 0)
324 {
325 printf("rec or play need close\n");
326 return -1;
327 }
328 return mbtk_audio_ubus_client_init(ph_audio, cb);
329}
330
331int lynq_audio_ubus_client_deinit(mbtk_audio_client_handle_type h_audio)
332{
333 if(rec_fd > 0 || play_fd > 0)
334 {
335 printf("rec or play need close\n");
336 return -1;
337 }
338 return mbtk_audio_ubus_client_deinit(h_audio);
339}
b.liu5fa9e772023-11-23 18:00:55 +0800340#endif
liubin281ac462023-07-19 14:22:54 +0800341
342int lynq_get_spk_volume(int * volume)
343{
b.liu5fa9e772023-11-23 18:00:55 +0800344#ifdef MBTK_PLATFORM_ASR1803
liubin281ac462023-07-19 14:22:54 +0800345 mbtk_audio_ubus_volume_get(audio_volume_cb);
346 *volume = volume_size;
347 return 0;
b.liu5fa9e772023-11-23 18:00:55 +0800348#else
349 return 0;
350#endif
liubin281ac462023-07-19 14:22:54 +0800351}
352
353
354int lynq_set_spk_volume(const int volume)
355{
b.liu5fa9e772023-11-23 18:00:55 +0800356#ifdef MBTK_PLATFORM_ASR1803
liubin281ac462023-07-19 14:22:54 +0800357 mbtk_audio_ubus_volume_set(volume);
358 return 0;
b.liu5fa9e772023-11-23 18:00:55 +0800359#else
360 return 0;
361#endif
liubin281ac462023-07-19 14:22:54 +0800362}
363