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