liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | /** |
| 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" |
| 32 | #include "mbtk_audio.h" |
| 33 | |
| 34 | static int rec_fd = 0; |
| 35 | static int play_fd = 0; |
| 36 | static mbtk_audio_handle play_hdl = NULL; |
| 37 | static mbtk_audio_handle record_hdl = NULL; |
| 38 | int volume_size = 0; |
| 39 | pthread_t paly_thread; |
| 40 | |
| 41 | |
| 42 | void dtmf_cb(char dtmf) |
| 43 | { |
| 44 | printf("%s:%c\n", __FUNCTION__, dtmf); |
| 45 | } |
| 46 | |
| 47 | void audio_volume_cb(int volume) |
| 48 | { |
| 49 | volume_size = volume; |
| 50 | printf("%s:%d\n", __FUNCTION__, volume); |
| 51 | } |
| 52 | |
| 53 | int lynq_memscpy |
| 54 | ( |
| 55 | void *dst, |
| 56 | int dst_size, |
| 57 | const void *src, |
| 58 | int src_size |
| 59 | ) |
| 60 | { |
| 61 | if(dst_size == 0 || src_size == 0 || dst == NULL || src == NULL) |
| 62 | { |
| 63 | return 0; |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | return memcpy( dst, src,src_size); |
| 68 | } |
| 69 | } /* dsatutil_free_memory() */ |
| 70 | |
| 71 | static int lynq_get_path_name ( const char* path_name,char* path ) |
| 72 | { |
| 73 | int i=0; |
| 74 | int last = -1; |
| 75 | int len = strlen ( path_name ); |
| 76 | if ( len > 0 ) |
| 77 | { |
| 78 | for ( i=len - 1; i >= 0; i-- ) |
| 79 | { |
| 80 | if ( path_name[i] == '/' ) |
| 81 | { |
| 82 | last = i; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | if ( i != -1 ) |
| 87 | { |
| 88 | lynq_memscpy ( path, ( i + 1 ) * sizeof ( char ), path_name, ( i + 1 ) * sizeof ( char ) ); |
| 89 | printf ( "mbtk_get_path %s", path ); |
| 90 | return 0; |
| 91 | } |
| 92 | } |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | int lynq_create_audio_dir(const char *dirname) |
| 97 | { |
| 98 | DIR *p_dir; |
| 99 | int res = -1, i = 0;; |
| 100 | char str[512]; |
| 101 | strncpy(str, dirname, 512); |
| 102 | int len=strlen(str); |
| 103 | |
| 104 | if(NULL == (p_dir = opendir((const char *)dirname))) |
| 105 | { |
| 106 | for(i=0; i<len; i++ ) |
| 107 | { |
| 108 | if( str[i]=='/' ) |
| 109 | { |
| 110 | str[i] = '\0'; |
| 111 | if( access(str,0)!=0 ) |
| 112 | { |
| 113 | if(mkdir(dirname, 0777) == 0) |
| 114 | { |
| 115 | res = 0; |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | fprintf(stderr, "create audio dir error \n"); |
| 120 | res = -1; |
| 121 | } |
| 122 | } |
| 123 | str[i]='/'; |
| 124 | } |
| 125 | } |
| 126 | if( len>0 && access(str,0)!=0 ) |
| 127 | { |
| 128 | if(mkdir(dirname, 0777) == 0) |
| 129 | { |
| 130 | res = 0; |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | fprintf(stderr, "create audio dir error \n"); |
| 135 | res = -1; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | closedir(p_dir); |
| 142 | res = 0; |
| 143 | } |
| 144 | return res; |
| 145 | } |
| 146 | |
| 147 | void lynq_record_cb_func(int cb_result, char* databuf, unsigned int len) |
| 148 | { |
| 149 | int rc; |
| 150 | // printf("lynq_record_cb_func() len:%d, rec_fd:%d\n", len, rec_fd); |
| 151 | if(NULL == databuf) |
| 152 | { |
| 153 | printf("NULL == databuf\n"); |
| 154 | } |
| 155 | |
| 156 | if(NULL != databuf && len > 0 && rec_fd > 0) |
| 157 | { |
| 158 | //for debug:save into file |
| 159 | rc = write(rec_fd, databuf, len); |
| 160 | if (rc < 0) { |
| 161 | printf("%s: error writing to file!\n", __FUNCTION__); |
| 162 | } else if (rc < len) { |
| 163 | printf("%s: wrote less the buffer size!\n", __FUNCTION__); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | int lynq_media_rec_audio(const char *path) |
| 169 | { |
| 170 | int ret = 0; |
| 171 | char audio_dir[50] ={0}; |
| 172 | char audio_wav[10] = {0}; |
| 173 | lynq_get_path_name(path, audio_dir); |
| 174 | printf("path:%s, audio_dir:%s\n", path, audio_dir); |
| 175 | |
| 176 | record_hdl = mbtk_audio_open(MBTK_AUTIO_TYPE_IN, 1, 8000, NULL); |
| 177 | if (record_hdl == 0) |
| 178 | { |
| 179 | printf("AudRecorder open error\n"); |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | lynq_create_audio_dir(audio_dir); |
| 184 | rec_fd = open(path, O_RDWR|O_CREAT|O_TRUNC, 0644); |
| 185 | if (rec_fd <= 0) |
| 186 | { |
| 187 | printf("file open error\n"); |
| 188 | goto err; |
| 189 | } |
| 190 | |
| 191 | if(-1 == mbtk_audio_record(record_hdl, lynq_record_cb_func, NULL)) |
| 192 | { |
| 193 | printf("file write error\n"); |
| 194 | goto err; |
| 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | // sleep(10); |
| 199 | err: |
| 200 | // Ql_AudRecorder_Close(); |
| 201 | if(rec_fd > 0) |
| 202 | { |
| 203 | close(rec_fd); |
| 204 | rec_fd = 0; |
| 205 | } |
| 206 | |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | |
| 212 | //停止录制音频文件 |
| 213 | void lynq_media_rec_stop_audio(void) |
| 214 | { |
| 215 | // sleep(10); |
| 216 | mbtk_audio_close(record_hdl); |
| 217 | if(rec_fd > 0) |
| 218 | { |
| 219 | close(rec_fd); |
| 220 | rec_fd = 0; |
| 221 | } |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | //播放音频文件 |
| 226 | int lynq_media_play_audio_thread_handle(void *argv) |
| 227 | { |
| 228 | char databuf[1024]; |
| 229 | int size; |
| 230 | |
| 231 | char *path = (char *)argv; |
| 232 | printf("lynq_media_play_audio() start \npath:%s\n",path); |
| 233 | LOGI("%s %d", __FUNCTION__, __LINE__); |
| 234 | play_hdl = mbtk_audio_open(MBTK_AUTIO_TYPE_OUT, 1, 8000, NULL); |
| 235 | if(NULL == play_hdl) |
| 236 | printf("mbtk_audio_open fail\n"); |
| 237 | |
| 238 | play_fd = open(path, O_RDWR); |
| 239 | if (play_fd <= 0) |
| 240 | { |
| 241 | printf("file open error\n"); |
| 242 | goto err; |
| 243 | } |
| 244 | memset(databuf, 0, sizeof(databuf)); |
| 245 | while(0 < (size = read(play_fd, databuf, sizeof(databuf)))) |
| 246 | { |
| 247 | if(-1 == mbtk_audio_play_stream(play_hdl, databuf, size)) |
| 248 | break; |
| 249 | } |
| 250 | printf("aplay Stream end \n"); |
| 251 | |
| 252 | err: |
| 253 | if(play_fd > 0) |
| 254 | { |
| 255 | close(play_fd); |
| 256 | play_fd = 0; |
| 257 | } |
| 258 | |
| 259 | pthread_exit(&paly_thread); |
| 260 | mbtk_audio_close(play_hdl); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | //创建线程播放音频文件 |
| 265 | int lynq_media_play_audio(const char *path) |
| 266 | { |
| 267 | int ret = pthread_create(&paly_thread, NULL, lynq_media_play_audio_thread_handle, (void *)path); |
| 268 | if (ret != 0) { |
| 269 | printf("create thread failed!\n"); |
| 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | pthread_detach(paly_thread); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | //停止播放音频文件 |
| 279 | void lynq_media_stop_audio(void) |
| 280 | { |
| 281 | printf("lynq_media_stop_audio()----\n"); |
| 282 | if(play_fd > 0) |
| 283 | { |
| 284 | int ret = pthread_cancel(paly_thread); |
| 285 | if (ret != 0) { |
| 286 | printf("cancle paly_thread fail\n"); |
| 287 | return ; |
| 288 | } |
| 289 | close(play_fd); |
| 290 | play_fd = 0; |
| 291 | } |
| 292 | mbtk_audio_close(play_hdl); |
| 293 | } |
| 294 | |
| 295 | |
| 296 | int lynq_audio_ubus_client_init(mbtk_audio_client_handle_type *ph_audio, mbtk_dtmf_cb cb) |
| 297 | { |
| 298 | if(rec_fd > 0 || play_fd > 0) |
| 299 | { |
| 300 | printf("rec or play need close\n"); |
| 301 | return -1; |
| 302 | } |
| 303 | return mbtk_audio_ubus_client_init(ph_audio, cb); |
| 304 | } |
| 305 | |
| 306 | int lynq_audio_ubus_client_deinit(mbtk_audio_client_handle_type h_audio) |
| 307 | { |
| 308 | if(rec_fd > 0 || play_fd > 0) |
| 309 | { |
| 310 | printf("rec or play need close\n"); |
| 311 | return -1; |
| 312 | } |
| 313 | return mbtk_audio_ubus_client_deinit(h_audio); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | int lynq_get_spk_volume(int * volume) |
| 318 | { |
| 319 | mbtk_audio_ubus_volume_get(audio_volume_cb); |
| 320 | *volume = volume_size; |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | |
| 325 | int lynq_set_spk_volume(const int volume) |
| 326 | { |
| 327 | mbtk_audio_ubus_volume_set(volume); |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | |
| 332 | |
| 333 | |