blob: 83d25858497c269f22643f886d6b45f4a91e27e8 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/**
2 * \file mbtk_audio_alsa.c
3 * \brief A Documented file.
4 *
5 * Detailed description
6 * \Author: js.wang <js.wang@mobiletek.cn>
7 * \Version: 1.0.0
8 * \Date: 2020-09-21
9 */
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <unistd.h>
14#include <sys/types.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <pthread.h>
18
19#include "audio_if_types.h"
20#include "audio_if_ubus.h"
21#include "audio_if_parameter.h"
22#include "audio_if.h"
23#include "audio_if_api.h"
24#include <unistd.h>
25#include <sys/stat.h>
26#include "audio_if_audio_hw_mrvl.h"
27#include "audio_hw_mrvl.h"
28#include <cutils/str_parms.h>
29#include "vcm.h"
30#include "voice_control.h"
31#include "mbtk_audio.h"
32
33
34// #define DEBUG 1
35
36#ifdef DEBUG
37 #define mbtk_audio_log(...) printf(__VA_ARGS__)
38#else
39 #define mbtk_audio_log(...)
40#endif
41
42typedef int (*_play_callback)(int hdl, int result);
43
44#define FAILURE -1
45#define ID_RIFF 0x46464952
46#define ID_WAVE 0x45564157
47#define ID_FMT 0x20746d66
48#define ID_DATA 0x61746164
49#define FORMAT_PCM 1
50
zhangzh8711cea2023-10-20 10:47:43 +080051typedef enum {
52 AUD_PLAYER_ERROR = -1,
53 AUD_PLAYER_START = 0,
54 AUD_PLAYER_PAUSE,
55 AUD_PLAYER_RESUME,
56 AUD_PLAYER_NODATA, //Buff no data and play tread will sleep
57 AUD_PLAYER_LESSDATA, //Buff has less data
58 AUD_PLAYER_FINISHED,
59} Enum_AudPlayer_State;
liubin281ac462023-07-19 14:22:54 +080060
61struct mopen_audio_t
62{
63 int device;
64 audio_hw_device_t *audio_ahw_dev_ubus;
65 struct audio_stream_in *stream_in;
66 struct audio_stream_out *stream_out;
67 int pcm_packet_size; //320:NB, 640:WB
68 int pipe_data;
69 int fd[2];
70 pthread_t pid;
71 mbtk_audio_state_enum state;
72 pthread_mutex_t _cond_mutex;
73 pthread_mutex_t _stream_mutex;
74 void *usrData;
75};
76struct record_cb_s
77{
78 mbtk_audio_record_cb_func _cb;
79 void *cb_data;
80};
81
zhangzh8711cea2023-10-20 10:47:43 +080082struct player_cb_s
83{
84 mbtk_audio_player_cb_func _cb;
85 void *cb_data;
86};
87
liubin281ac462023-07-19 14:22:54 +080088static struct mopen_audio_t *internal_hdl = NULL;
89
90
91int mbtk_wav_pcm16Le_check(int fd)
92{
93 printf("MBTK_wav_pcm16Le_check()----------strart5, fd:%d\n", fd);
94 struct wav_header hdr;
95
96 if (fd <= 0)
97 return -1;
98
99 if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
100 {
101 printf("\n%s: cannot read header\n", __FUNCTION__);
102 return -1;
103 }
104
105 printf("hdr.riff_id:%X, hdr.riff_fmt:%X, hdr.fmt_id:%X", hdr.riff_id, hdr.riff_fmt, hdr.fmt_id);
106
107 if ((hdr.riff_id != ID_RIFF)
108 || (hdr.riff_fmt != ID_WAVE)
109 || (hdr.fmt_id != ID_FMT))
110 {
111 printf("\n%s: is not a riff/wave file\n", __FUNCTION__);
112 return -1;
113 }
114
115 if ((hdr.audio_format != FORMAT_PCM) || (hdr.fmt_sz != 16)) {
116 printf("\n%s: is not pcm format\n", __FUNCTION__);
117 return -1;
118 }
119
120 if (hdr.bits_per_sample != 16) {
121 printf("\n%s: is not 16bit per sample\n", __FUNCTION__);
122 return -1;
123 }
124
125 printf("audio_format: %d,num_channels: %d,sample_rate: %d,byte_rate: %d,bits_per_sample: %d data_sz: %d\n",
126 hdr.audio_format, hdr.num_channels, hdr.sample_rate, hdr.byte_rate, hdr.bits_per_sample, hdr.data_sz);
127
128 return hdr.data_sz;
129}
130
131
132static void simulateOffhook(struct mopen_audio_t *aud_hdl, unsigned int onoff)
133{
134 unsigned int pcm_on;
135 unsigned int DTMFDetectiononoff;
136 unsigned int dialToneToOthersTones;
137 unsigned int dialTonesToOthersDialTones;
138 unsigned int dialVadDuration;
139
140 pcm_on = onoff;
141 //send the command of "AUDIOSTUB_PCMCTL"
142 aud_hdl->audio_ahw_dev_ubus->switch_pcm(aud_hdl->audio_ahw_dev_ubus, pcm_on);
143
144 DTMFDetectiononoff = onoff;
145 dialToneToOthersTones = 50;
146 dialTonesToOthersDialTones = 4;
147 dialVadDuration = 3;
148 //send the command of "AUDIOSTUB_DTMFDETECTIONCTL"
149 //vcm_DTMFDetection(1, 50, 4, 3);
150 vcm_DTMFDetection(DTMFDetectiononoff, dialToneToOthersTones, dialTonesToOthersDialTones, dialVadDuration);
151
152 return;
153}
154static int config_parameters(int in_out, int NBWB)
155{
156 unsigned int direction = 0xFF, type, srcdst, priority, dest;
157 char kvpair[128];
158 struct str_parms *param = NULL;
159 int data[5];
160 const char *key = NULL;
161 bool update_vcm = false;
162
163 direction = in_out;/* 0-play, 1-record */
164 type = NBWB; /* 0:PCM_NB_BUF_SIZE, 1:PCM_WB_BUF_SIZE */
165 srcdst = 1;/* 0-None, 1-Near end, 2-Far end, 3-Both ends */
166 priority = 1;/* 0-Do not combine(override), 1-Combine */
167 dest = 1;/* 0-Near codec, 1-Near Vocoder */
168
169 memset(kvpair, 0x00, sizeof(kvpair));
170 sprintf(kvpair, "%s=%d;%s=%d;%s=%d;%s=%d;%s=%d", VCM_CONFIG_DIRECTION, direction,
171 VCM_CONFIG_TYPE, type, VCM_CONFIG_SRC_DST, srcdst,
172 VCM_CONFIG_PRIORITY, priority, VCM_CONFIG_DEST, dest);
173
174 mbtk_audio_log("%s: config information kvpair is %s.\n", __FUNCTION__, kvpair);
175
176 //extract the parameter and config from string
177 param = str_parms_create_str(kvpair);
178 if (!param) {
179 printf("%s: param create str is null!", __FUNCTION__);
180 return -1;
181 }
182
183 //set vcm configurations
184 key = VCM_CONFIG_DIRECTION;
185 if (str_parms_get_int(param, key, &data[0]) == 0) {
186 update_vcm = true;
187 str_parms_del(param, key);
188 }
189 key = VCM_CONFIG_TYPE;
190 if (str_parms_get_int(param, key, &data[1]) == 0) {
191 update_vcm = true;
192 str_parms_del(param, key);
193 }
194 key = VCM_CONFIG_SRC_DST;
195 if (str_parms_get_int(param, key, &data[2]) == 0) {
196 update_vcm = true;
197 str_parms_del(param, key);
198 }
199 key = VCM_CONFIG_PRIORITY;
200 if (str_parms_get_int(param, key, &data[3]) == 0) {
201 update_vcm = true;
202 str_parms_del(param, key);
203 }
204 key = VCM_CONFIG_DEST;
205 if (str_parms_get_int(param, key, &data[4]) == 0) {
206 update_vcm = true;
207 str_parms_del(param, key);
208 }
209
210 //printf("Direction is %d, Type is %d, Src_Dst is %d, Priority is %d, Dest is %d. \n",data[0], data[1], data[2], data[3], data[4]);
211
212 if (update_vcm) {
213 configure_vcm(data); /*TODO check if all inputs got all values successfully*/
214 }
215
216 return 0;
217}
218
219void mbtk_audio_set_status(void* hdl, mbtk_audio_state_enum _status)
220{
221 struct mopen_audio_t *pcxt = (struct mopen_audio_t *)hdl;
222 if (NULL == hdl || NULL == internal_hdl)
223 return 0;
224 pthread_mutex_lock(&pcxt->_cond_mutex);
225 pcxt->state = _status;
226 pthread_mutex_unlock(&pcxt->_cond_mutex);
227}
228
229static void* mbtk_record_pthread(void* hdl)
230{
231 struct mopen_audio_t *pcxt = (struct mopen_audio_t *)hdl;
232 struct record_cb_s *_usrData = (struct record_cb_s *)pcxt->usrData;
233 unsigned bufsize = 0;
234 char *data = NULL;
235 int len;
236
237 if (NULL == hdl)
238 return NULL;
239
240 bufsize = pcxt->pcm_packet_size;
241 data = calloc(1, bufsize);
242 if (!data) {
243 fprintf(stderr, "\n%s:could not allocate %d bytes\n", __FUNCTION__, bufsize);
244 return NULL;
245 }
246
247 mbtk_audio_set_status(hdl, AUDIO_RUNNING);
248
249 while (pcxt->state != AUDIO_STOP)
250 {
251 len = pcxt->stream_in->read(pcxt->stream_in, data, bufsize);
252 if (len < 0) {
253 printf("%s: error reading!\n", __FUNCTION__);
254 break;
255 }
256 if ((bufsize > 0) && (NULL != _usrData->_cb))
257 {
258 _usrData->_cb(2, data, bufsize);
259 }
260 }
261 pcxt->pid = 0;
262 if(pcxt->usrData)
263 {
264 free(pcxt->usrData);
265 pcxt->usrData = NULL;
266 }
267 free(data);
268 mbtk_audio_log("pcm pthread end-\n");
269 return NULL;
270}
271
272static void* mbtk_play_pthread(void* hdl)
273{
274 struct mopen_audio_t *pcxt = (struct mopen_audio_t *)hdl;
275 _play_callback audio_play_cb = (_play_callback)pcxt->usrData;
276 unsigned bufsize = 0;
277 char *data = NULL;
278 int first_set = 0;
279 int ret = 0;
280 int rc;
281
282 pthread_detach(pthread_self());
283 if (NULL == hdl)
284 return NULL;
285
286 bufsize = pcxt->pcm_packet_size;
287 data = calloc(1, bufsize);
288 if (!data) {
289 fprintf(stderr, "\n%s:could not allocate %d bytes\n",__FUNCTION__,bufsize);
290 pthread_exit(NULL);
291 }
292
293 while (pcxt->state != AUDIO_STOP && 0 != pcxt->pipe_data)
294 {
295 ret = read(pcxt->fd[0], data, bufsize);
296 // printf("%s:read : %d bytes\n", __FUNCTION__, ret);
297 if(ret == 0) {
298 mbtk_audio_log("%s %d [%d]", __FUNCTION__, __LINE__, ret);
299 continue;
300 }
301 if(ret < 0) {
302 mbtk_audio_log("%s %d [%d]", __FUNCTION__, __LINE__, ret);
303 break;
304 }
305 if ((0 == first_set || AUDIO_RESUME == pcxt->state))
306 {
307 first_set = 1;
308 mbtk_audio_set_status(hdl, AUDIO_RUNNING);
309 }
310
311 pthread_mutex_lock(&pcxt->_stream_mutex);
312 pcxt->pipe_data -= ret;
313 pthread_mutex_unlock(&pcxt->_stream_mutex);
314 if(pcxt->pipe_data < 0)
315 pcxt->pipe_data = 0;
316 if(ret < pcxt->pcm_packet_size)
317 printf("pcm %d - %d\n", pcxt->pipe_data, ret);
318
319 rc = pcxt->stream_out->write(pcxt->stream_out, data, ret);
320 if (rc < 0) {
321 printf("%s: error writing (child).\n", __FUNCTION__);
322 break;
323 } else if (rc < (signed int)pcxt->pcm_packet_size) {
324 printf("%s: wrote less than buffer size, rc=%d.\n", __FUNCTION__, rc);
325 break;
326 }
327 }
328
329 if (pcxt->fd[0] != 0 && pcxt->fd[1] != 0)
330 {
331 close(pcxt->fd[0]);
332 close(pcxt->fd[1]);
333 pcxt->fd[0] = 0;
334 pcxt->fd[1] = 0;
335 /* printf("close pcxt->fd!\n"); */
336 }
337 if (audio_play_cb)
338 audio_play_cb(pcxt, 5);
339 pcxt->pid = 0;
340// mbtk_audio_set_status(hdl, AUDIO_STOP);
341 mbtk_audio_set_status(hdl, AUDIO_OPEN);
342 free(data);
343 mbtk_audio_log("\n%s:exit!\n", __FUNCTION__);
344 pthread_exit(NULL);
345
346 return NULL;
347}
348
349static int audio_init_play_pthread(void* hdl)
350{
351 struct mopen_audio_t *pcxt = (struct mopen_audio_t *)hdl;
352 _play_callback audio_play_cb = (_play_callback)pcxt->usrData;
353 int ret;
354
355 if (pcxt->fd[0] == 0 && pcxt->fd[1] == 0) {
356 if(pipe(pcxt->fd) == 0) {
357
358 pcxt->pipe_data = 0;
359 fcntl(pcxt->fd[0], F_SETFL, O_NONBLOCK);
360 fcntl(pcxt->fd[1], F_SETFL, O_NONBLOCK);
361 } else {
362 fprintf(stderr, "\n%d: create pipe error\n", __LINE__);
363 return -1;
364 }
365 }
366 if (pcxt->pid == 0) {
367 if (audio_play_cb)
368 audio_play_cb(pcxt, 0);
369 mbtk_audio_set_status(pcxt, AUDIO_RUNNING);
370 ret = pthread_create(&pcxt->pid, NULL, mbtk_play_pthread, hdl);
371 if (ret != 0)
372 {
373 fprintf(stderr, "\n%s: Failed create pthread\n",__FUNCTION__);
374 return ret;
375 }
376 }
377
378 return 0;
379}
380
381int mbtk_audio_play_wait_end(void* hdl)
382{
383 struct mopen_audio_t *pcxt = (struct mopen_audio_t *)hdl;
384
385 if (NULL == hdl)
386 {
387 fprintf(stderr, "\n%s: intput invalid params\n",__FUNCTION__);
388 return -1;
389 }
390
391 if(pcxt->pid != 0)
392 {
393 do{
394 usleep(10000);
395 }
396 while(pcxt->state != AUDIO_STOP);
397 pcxt->pid = 0;
398 }
399
400 return 0;
401}
402
403static int audio_open_pcm(void *dev_hdl, int num_channels, int rate, int in_out)
404{
405 struct mopen_audio_t *pcxt = (struct mopen_audio_t *)dev_hdl;
406 int nb_wb;
407
408 //Support 8k/16k & mono wave file
409 if (((rate != 8000) && (rate != 16000))
410 || (num_channels != 1) ) {
411 printf("%s: error wave file:rate = %d, num_channels = %d!! \n",
412 __FUNCTION__,rate, num_channels);
413 return -1;
414 }
415
416 printf("%s: success open, rate = %d, num_channels = %d.\n",
417 __FUNCTION__, rate, num_channels);
418
419 if ((8000 == rate) && (1 == num_channels)) {
420 nb_wb = 0;//NB
421 pcxt->pcm_packet_size = PCM_NB_BUF_SIZE;
422 } else if ((16000 == rate) && (1 == num_channels)) {
423 nb_wb = 1;//WB
424 pcxt->pcm_packet_size = PCM_WB_BUF_SIZE;
425 }
426
427 //config playback parameters.
428 config_parameters(in_out, nb_wb);
429 pcxt->device = in_out;
430 return 0;
431}
432
433mbtk_audio_handle mbtk_audio_open(mbtk_audio_dev_enum dev, int flag, int rate, void *usrData)
434{
435 int value = 1;
436 struct mopen_audio_t *aud_hdl = NULL;
437 int ret;
438
439 // if (dev <= AUDIO_NULL || dev >= AUDIO_MAX)
440 // {
441 // printf("invaild PCM dev\n");
442 // return NULL;
443 // }
444 if(internal_hdl)
445 {
446 printf("Audio device inited\n");
447 return internal_hdl;
448 }
449 aud_hdl = (struct mopen_audio_t *) calloc(1, sizeof(struct mopen_audio_t));
450 if (!aud_hdl)
451 {
452 fprintf(stderr, "\n%s:Failed to allocate audio hdl!, errno=%d\n", __FUNCTION__, errno);
453 return NULL;
454 }
455 memset(aud_hdl, 0, sizeof(struct mopen_audio_t));
456
457 //init global variables
458 aud_hdl->audio_ahw_dev_ubus = audio_hal_install();
459 if (aud_hdl->audio_ahw_dev_ubus == NULL) {
460 printf("%s: audio_hal_install failed!\n", __FUNCTION__);
461 goto error;
462 }
463
464 if(1 == dev)
465 {
466 //send the command of "AUDIOSTUB_PCMCTL" and "AUDIOSTUB_DTMFDETECTIONCTL" to simulate telephone offhook.
467 // ASR Question #80497 Using it causes the recording to crash
468 // simulateOffhook(aud_hdl, 1);
469 //config playback parameters.
470 config_parameters(0, 0);
471 }
472 ret = audio_open_pcm(aud_hdl, flag, rate, dev);
473 if (ret)
474 {
475 printf("\n%s: pcm open error, errno=%d\n", __FUNCTION__, errno);
476 goto error;
477 }
478 if(1 == dev)
479 {
480 //open the audiostub_ctl, prepare for record and playback
481 VCMInit();
482 //open record stream
483 ret = aud_hdl->audio_ahw_dev_ubus->open_input_stream(aud_hdl->audio_ahw_dev_ubus, 0,
484 aud_hdl->audio_ahw_dev_ubus->get_supported_devices(aud_hdl->audio_ahw_dev_ubus),
485 NULL, &aud_hdl->stream_in, 0, 0, AUDIO_SOURCE_VOICE_CALL);
486 }
487 else
488 {
489 ret = aud_hdl->audio_ahw_dev_ubus->open_output_stream(aud_hdl->audio_ahw_dev_ubus, 0,
490 aud_hdl->audio_ahw_dev_ubus->get_supported_devices(aud_hdl->audio_ahw_dev_ubus),
491 AUDIO_OUTPUT_FLAG_DIRECT, NULL, &aud_hdl->stream_out, 0);
492 }
493
494 if (ret < 0) {
495 printf("%s: error opening output device. rc = %d\n", __FUNCTION__, ret);
496 goto error;
497 }
498 pthread_mutex_init(&aud_hdl->_cond_mutex, NULL);
499 pthread_mutex_init(&aud_hdl->_stream_mutex, NULL);
500 aud_hdl->usrData = usrData;
501 aud_hdl->state = AUDIO_OPEN;
502 /* printf("Mbtk_Audio_Open aud_hdl[%x][%x][%x]\n", aud_hdl, aud_hdl->_mixer_ctl, aud_hdl->_pcm); */
503 internal_hdl = aud_hdl;
504
505 return (void *)aud_hdl;
506
507error:
508 value = 0;
509 free(aud_hdl);
510 return NULL;
511}
512
513
514int mbtk_audio_play_stream_old(void *dev_hdl, const void *pData, int len)
515{
516 struct mopen_audio_t *pcxt = (struct mopen_audio_t *)dev_hdl;
517
518 if (NULL == pcxt || NULL == internal_hdl)
519 return -1;
520
521 if (len > 1024) {
522 printf("audio play stream max size ( < 1024): %d\n", len);
523 return -2;
524 }
525
526 if (pcxt->state == AUDIO_STOP) {
527 return 0;
528 }
529 audio_init_play_pthread(pcxt);
530
531 pthread_mutex_lock(&pcxt->_stream_mutex);
532 pcxt->pipe_data += len;
533 pthread_mutex_unlock(&pcxt->_stream_mutex);
534 while(pcxt->state != AUDIO_STOP && (pcxt->pipe_data > 40960 || pcxt->fd[1] < 0 || pcxt->fd[1] == 0)) {
535 // printf("%s:wait %d %d\n", __FUNCTION__, pcxt->pipe_data, pcxt->fd[1]);
536 usleep(50000);
537 }
538
539 if(pcxt->fd[1] > 0) {
540 if (len != write(pcxt->fd[1], pData, len)) {
541 fprintf(stderr, "\n%s: pcm_write failed [%d %d]\n", __FUNCTION__, pcxt->fd[1], errno);
542 return -errno;
543 }
544 }
545
546 return 0;
547}
548
549int mbtk_audio_play_stream(void *dev_hdl, const void *pData, int len)
550{
551// printf("%s,--len:%d\n", __func__, len);
552 unsigned bufsize = 0;
553 char *data = NULL;
554 int first_set = 0;
555 int res = 0;
556 int ret = 0;
557 int read_size = 0;
558 char *p = (char *)pData;
559
560 struct mopen_audio_t *pcxt = (struct mopen_audio_t *)dev_hdl;
561
562 if (NULL == dev_hdl || NULL == internal_hdl || pData == NULL)
563 return -1;
564
565 if(AUDIO_RUNNING == pcxt->state)
566 return -2;
567
568 bufsize = pcxt->pcm_packet_size;
569 data = calloc(1, bufsize);
570 if (!data) {
571 fprintf(stderr, "\n%s:could not allocate %d bytes\n",__FUNCTION__,bufsize);
572 pthread_exit(NULL);
573 }
574
575 mbtk_audio_set_status(dev_hdl, AUDIO_RUNNING);
576// if(bufsize > len )
577// {
578// bufsize = len;
579// }
580
581 while (pcxt->state != AUDIO_STOP)
582 {
583
584 memcpy(data,p+read_size, bufsize );
585 read_size += bufsize;
586
587 if(read_size > len)
588 {
589 // printf(">[%d]\n", read_size);
590 break;
591 }
592
593 while(AUDIO_PAUSE == pcxt->state)
594 {
595 usleep(80000);
596 }
597
598 if ((0 == first_set || AUDIO_RESUME == pcxt->state))
599 {
600 first_set = 1;
601 mbtk_audio_set_status(dev_hdl, AUDIO_RUNNING);
602 }
603
604 ret = pcxt->stream_out->write(pcxt->stream_out, data, bufsize);
605 if (ret < 0) {
606 printf("%s: error writing (child).\n", __FUNCTION__);
607 break;
608 } else if (ret < (signed int)pcxt->pcm_packet_size) {
609 printf("%s: wrote less than buffer size, rc=%d.\n", __FUNCTION__, ret);
610 break;
611 }
612
613 if(read_size == len)
614 {
615 // printf("=[%d]", read_size);
616 break;
617 }
618
619 }
620
621 mbtk_audio_set_status(dev_hdl, AUDIO_OPEN);
622 free(data);
623 return 0;
624
625}
626
627
zhangzh8711cea2023-10-20 10:47:43 +0800628int mbtk_audio_play_file(void *dev_hdl, int file_fd, int offset, mbtk_audio_player_cb_func cb_func)
liubin281ac462023-07-19 14:22:54 +0800629{
630 unsigned bufsize = 0;
631 char *data = NULL;
632 int first_set = 0;
633 int file_data_sz = 0;
634 int res = 0;
635 int ret;
636
637 struct mopen_audio_t *pcxt = (struct mopen_audio_t *)dev_hdl;
zhangzh8711cea2023-10-20 10:47:43 +0800638 struct player_cb_s *_usrData = NULL;
liubin281ac462023-07-19 14:22:54 +0800639 if (NULL == dev_hdl || NULL == internal_hdl)
640 return -1;
641
642 if(AUDIO_RUNNING == pcxt->state)
643 return -2;
644
zhangzh8711cea2023-10-20 10:47:43 +0800645 // file_data_sz = mbtk_wav_pcm16Le_check(file_fd);
646 _usrData = malloc(sizeof(struct player_cb_s));
647 _usrData->_cb = cb_func;
648 _usrData->cb_data = NULL;
liubin281ac462023-07-19 14:22:54 +0800649 bufsize = pcxt->pcm_packet_size;
650 data = calloc(1, bufsize);
651 if (!data) {
652 fprintf(stderr, "\n%s:could not allocate %d bytes\n",__FUNCTION__,bufsize);
653 pthread_exit(NULL);
654 }
655 if(offset)
656 {
657 lseek(file_fd, offset, SEEK_SET);
658 }
659
660 mbtk_audio_set_status(dev_hdl, AUDIO_RUNNING);
661 int all_size = 0;
662
663 while (pcxt->state != AUDIO_STOP)
664 {
665 res = read(file_fd, data, bufsize);
666 // printf("%s:read : %d bytes\n", __FUNCTION__, res);
667 if(res == 0 || res < 0)
668 {
LUOJian1ce8d752023-09-11 11:24:43 +0800669 printf("read:[%d]", res);
liubin281ac462023-07-19 14:22:54 +0800670 break;
671 }
672
673 all_size += res;
LUOJian1ce8d752023-09-11 11:24:43 +0800674// if (file_data_sz < all_size || file_data_sz == all_size) {
675// printf("aplay size :%d - %d\n", file_data_sz, all_size);
676 // break;
677 // }
liubin281ac462023-07-19 14:22:54 +0800678
679 while(AUDIO_PAUSE == pcxt->state)
680 {
681 usleep(80000);
zhangzh8711cea2023-10-20 10:47:43 +0800682 _usrData->_cb((int)pcxt, AUD_PLAYER_PAUSE);
liubin281ac462023-07-19 14:22:54 +0800683 }
684
685 if ((0 == first_set || AUDIO_RESUME == pcxt->state))
686 {
687 first_set = 1;
688 mbtk_audio_set_status(dev_hdl, AUDIO_RUNNING);
zhangzh8711cea2023-10-20 10:47:43 +0800689 _usrData->_cb((int)pcxt, AUD_PLAYER_RESUME);
liubin281ac462023-07-19 14:22:54 +0800690 }
691
zhangzh8711cea2023-10-20 10:47:43 +0800692 ret = pcxt->stream_out->write(pcxt->stream_out, data, bufsize);
liubin281ac462023-07-19 14:22:54 +0800693 if (ret < 0) {
694 printf("%s: error writing (child).\n", __FUNCTION__);
zhangzh8711cea2023-10-20 10:47:43 +0800695 _usrData->_cb((int)pcxt, AUD_PLAYER_ERROR);
liubin281ac462023-07-19 14:22:54 +0800696 break;
697 } else if (ret < (signed int)pcxt->pcm_packet_size) {
698 printf("%s: wrote less than buffer size, rc=%d.\n", __FUNCTION__, ret);
zhangzh8711cea2023-10-20 10:47:43 +0800699 _usrData->_cb((int)pcxt, AUD_PLAYER_LESSDATA);
liubin281ac462023-07-19 14:22:54 +0800700 break;
701 }
702 }
zhangzh8711cea2023-10-20 10:47:43 +0800703 _usrData->_cb((int)pcxt, AUD_PLAYER_FINISHED);
liubin281ac462023-07-19 14:22:54 +0800704
LUOJian1ce8d752023-09-11 11:24:43 +0800705 printf("file_data_sz :%d - all_size: %d\n", file_data_sz, all_size);
liubin281ac462023-07-19 14:22:54 +0800706 mbtk_audio_set_status(dev_hdl, AUDIO_OPEN);
707 free(data);
708
709 return 0;
710}
711
712int mbtk_audio_record(void *dev_hdl, mbtk_audio_record_cb_func cb_func, void *cb_data)
713{
714 struct mopen_audio_t *pcxt = (struct mopen_audio_t *)dev_hdl;
715 int res = -1;
716 struct record_cb_s *_usrData = NULL;
717
718 if (NULL == pcxt || NULL == internal_hdl || NULL == cb_func)
719 return -1;
720
721 if(AUDIO_RUNNING == pcxt->state)
722 return -2;
723
724 _usrData = malloc(sizeof(struct record_cb_s));
725 _usrData->_cb = cb_func;
726 _usrData->cb_data = cb_data;
727 pcxt->usrData = (void *)_usrData;
728 res = pthread_create(&pcxt->pid, NULL, mbtk_record_pthread, dev_hdl);
729 if (res != 0)
730 {
731 if(pcxt->usrData)
732 {
733 free(pcxt->usrData);
734 pcxt->usrData = NULL;
735 }
736 fprintf(stderr, "\n%s: Failed to create pthread\n", __FUNCTION__);
737 }
738 // 防止重复录音
739 usleep(500);
740
741 return res;
742}
743
744int mbtk_audio_close(void *dev_hdl)
745{
746 printf("mbtk_audio_close()\n");
747 int value = 0;
748 struct mopen_audio_t *_hdl = (struct mopen_audio_t *)dev_hdl;
749
750 if (NULL == _hdl || NULL == internal_hdl )
751 {
752 printf("mbtk_audio_close() fail dev_hdl is NULL\n");
753 return -1;
754 }
755
756 mbtk_audio_set_status(_hdl, AUDIO_STOP);
757 if(0 == _hdl->device) {
758 while (_hdl->pid != 0) {
759 usleep(10000);
760 }
761
762 vcm_playback_drain(0);//wait for drain the AP audiostub queue.
763 usleep(80000);//delay 80ms until DSP play out its buffered data.
764 _hdl->stream_out->common.standby(&_hdl->stream_out->common);
765 _hdl->audio_ahw_dev_ubus->close_output_stream(_hdl->audio_ahw_dev_ubus, _hdl->stream_out);
766 } else {
767
768 while (_hdl->pid != 0) {
769 sleep(1);
770 }
771 _hdl->stream_in->common.standby(&_hdl->stream_in->common);
772 _hdl->audio_ahw_dev_ubus->close_input_stream(_hdl->audio_ahw_dev_ubus, _hdl->stream_in);
773 VCMDeinit();//close the fd of audiostub_ctl when exit the thread.
774 }
775
776 audio_hal_uninstall();
777 pthread_mutex_destroy(&_hdl->_cond_mutex);
778 pthread_mutex_destroy(&_hdl->_stream_mutex);
779 free(_hdl);
780 _hdl = NULL;
781 internal_hdl = NULL;
782
783 return 0;
784}
785
786int mbtk_audio_pause(void* dev_hdl)
787{
788 // struct pcm *_pcm = ((struct mopen_audio_t *)dev_hdl)->_pcm;
789
790 if (NULL == dev_hdl || NULL == internal_hdl)
791 return -1;
792
793 if (((struct mopen_audio_t *)dev_hdl)->state != AUDIO_RUNNING)
794 {
795 return -1;
796 }
797
798 // if (ioctl(_pcm->fd, SNDRV_PCM_IOCTL_PAUSE, 1))
799 // {
800 // printf("\n%s: cannot pause channel: errno =%d\n",__FUNCTION__,-errno);
801 // return -errno;
802 // }
803 mbtk_audio_set_status(dev_hdl, AUDIO_PAUSE);
804 return 0;
805}
806
807int mbtk_audio_resume(void* dev_hdl)
808{
809 if (NULL == dev_hdl || NULL == internal_hdl)
810 return -1;
811
812 if (((struct mopen_audio_t *)dev_hdl)->state != AUDIO_PAUSE)
813 {
814 return -1;
815 }
816
817 // if (ioctl(_pcm->fd, SNDRV_PCM_IOCTL_PAUSE, 0))
818 // {
819 // printf("\n%s: cannot Resume channel: errno =%d\n", __FUNCTION__, -errno);
820 // return -errno;
821 // }
822 mbtk_audio_set_status(dev_hdl, AUDIO_RESUME);
823 return 0;
824}
825
826int mbtk_audio_stop(void* dev_hdl)
827{
828 struct mopen_audio_t *_hdl = (struct mopen_audio_t *)dev_hdl;
829
830 if (NULL == dev_hdl || NULL == internal_hdl)
831 return -1;
832
833 // if (ioctl(_hdl->_pcm->fd, SNDRV_PCM_IOCTL_DROP))
834 // {
835 // printf("\n%s: cannot Resume channel: errno =%d\n",__FUNCTION__,-errno);
836 // return -errno;
837 // }
838
839 mbtk_audio_set_status(dev_hdl, AUDIO_STOP);
840 _hdl->pid = 0;
841
842 return 0;
843}
844
845mbtk_audio_state_enum mbtk_audio_state_get(void *hdl)
846{
847 return ((struct mopen_audio_t *)hdl)->state;
848}