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