liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | /** |
| 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 | |
| 42 | typedef 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 | |
zhangzh | 8711cea | 2023-10-20 10:47:43 +0800 | [diff] [blame] | 51 | typedef 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; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 60 | |
| 61 | struct 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 | }; |
| 76 | struct record_cb_s |
| 77 | { |
| 78 | mbtk_audio_record_cb_func _cb; |
| 79 | void *cb_data; |
| 80 | }; |
| 81 | |
| 82 | static struct mopen_audio_t *internal_hdl = NULL; |
| 83 | |
| 84 | |
| 85 | int 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 | |
| 126 | static 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 | } |
| 148 | static 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 | |
| 213 | void 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 | |
LUOJian | e9919ba | 2023-12-01 17:14:01 +0800 | [diff] [blame] | 223 | |
| 224 | int mbtk_audio_get_status(void* hdl) |
| 225 | { |
| 226 | struct mopen_audio_t *pcxt = (struct mopen_audio_t *)hdl; |
| 227 | if (NULL == hdl || NULL == internal_hdl) |
| 228 | return 0; |
| 229 | |
| 230 | return pcxt->state; |
| 231 | } |
| 232 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 233 | static void* mbtk_record_pthread(void* hdl) |
| 234 | { |
| 235 | struct mopen_audio_t *pcxt = (struct mopen_audio_t *)hdl; |
| 236 | struct record_cb_s *_usrData = (struct record_cb_s *)pcxt->usrData; |
| 237 | unsigned bufsize = 0; |
| 238 | char *data = NULL; |
| 239 | int len; |
| 240 | |
| 241 | if (NULL == hdl) |
| 242 | return NULL; |
| 243 | |
| 244 | bufsize = pcxt->pcm_packet_size; |
| 245 | data = calloc(1, bufsize); |
| 246 | if (!data) { |
| 247 | fprintf(stderr, "\n%s:could not allocate %d bytes\n", __FUNCTION__, bufsize); |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| 251 | mbtk_audio_set_status(hdl, AUDIO_RUNNING); |
| 252 | |
| 253 | while (pcxt->state != AUDIO_STOP) |
| 254 | { |
| 255 | len = pcxt->stream_in->read(pcxt->stream_in, data, bufsize); |
| 256 | if (len < 0) { |
| 257 | printf("%s: error reading!\n", __FUNCTION__); |
| 258 | break; |
| 259 | } |
| 260 | if ((bufsize > 0) && (NULL != _usrData->_cb)) |
| 261 | { |
| 262 | _usrData->_cb(2, data, bufsize); |
| 263 | } |
| 264 | } |
| 265 | pcxt->pid = 0; |
| 266 | if(pcxt->usrData) |
| 267 | { |
| 268 | free(pcxt->usrData); |
| 269 | pcxt->usrData = NULL; |
| 270 | } |
| 271 | free(data); |
| 272 | mbtk_audio_log("pcm pthread end-\n"); |
| 273 | return NULL; |
| 274 | } |
| 275 | |
| 276 | static void* mbtk_play_pthread(void* hdl) |
| 277 | { |
| 278 | struct mopen_audio_t *pcxt = (struct mopen_audio_t *)hdl; |
| 279 | _play_callback audio_play_cb = (_play_callback)pcxt->usrData; |
| 280 | unsigned bufsize = 0; |
| 281 | char *data = NULL; |
| 282 | int first_set = 0; |
| 283 | int ret = 0; |
| 284 | int rc; |
| 285 | |
| 286 | pthread_detach(pthread_self()); |
| 287 | if (NULL == hdl) |
| 288 | return NULL; |
| 289 | |
| 290 | bufsize = pcxt->pcm_packet_size; |
| 291 | data = calloc(1, bufsize); |
| 292 | if (!data) { |
| 293 | fprintf(stderr, "\n%s:could not allocate %d bytes\n",__FUNCTION__,bufsize); |
| 294 | pthread_exit(NULL); |
| 295 | } |
| 296 | |
| 297 | while (pcxt->state != AUDIO_STOP && 0 != pcxt->pipe_data) |
| 298 | { |
| 299 | ret = read(pcxt->fd[0], data, bufsize); |
| 300 | // printf("%s:read : %d bytes\n", __FUNCTION__, ret); |
| 301 | if(ret == 0) { |
| 302 | mbtk_audio_log("%s %d [%d]", __FUNCTION__, __LINE__, ret); |
| 303 | continue; |
| 304 | } |
| 305 | if(ret < 0) { |
| 306 | mbtk_audio_log("%s %d [%d]", __FUNCTION__, __LINE__, ret); |
| 307 | break; |
| 308 | } |
| 309 | if ((0 == first_set || AUDIO_RESUME == pcxt->state)) |
| 310 | { |
| 311 | first_set = 1; |
| 312 | mbtk_audio_set_status(hdl, AUDIO_RUNNING); |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 313 | audio_play_cb(pcxt, AUD_PLAYER_RESUME); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | pthread_mutex_lock(&pcxt->_stream_mutex); |
| 317 | pcxt->pipe_data -= ret; |
| 318 | pthread_mutex_unlock(&pcxt->_stream_mutex); |
| 319 | if(pcxt->pipe_data < 0) |
| 320 | pcxt->pipe_data = 0; |
| 321 | if(ret < pcxt->pcm_packet_size) |
| 322 | printf("pcm %d - %d\n", pcxt->pipe_data, ret); |
| 323 | |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 324 | rc = pcxt->stream_out->write(pcxt->stream_out, data, bufsize); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 325 | if (rc < 0) { |
| 326 | printf("%s: error writing (child).\n", __FUNCTION__); |
| 327 | break; |
| 328 | } else if (rc < (signed int)pcxt->pcm_packet_size) { |
| 329 | printf("%s: wrote less than buffer size, rc=%d.\n", __FUNCTION__, rc); |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | if (pcxt->fd[0] != 0 && pcxt->fd[1] != 0) |
| 335 | { |
| 336 | close(pcxt->fd[0]); |
| 337 | close(pcxt->fd[1]); |
| 338 | pcxt->fd[0] = 0; |
| 339 | pcxt->fd[1] = 0; |
| 340 | /* printf("close pcxt->fd!\n"); */ |
| 341 | } |
| 342 | if (audio_play_cb) |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 343 | audio_play_cb(pcxt, AUD_PLAYER_FINISHED); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 344 | pcxt->pid = 0; |
| 345 | // mbtk_audio_set_status(hdl, AUDIO_STOP); |
| 346 | mbtk_audio_set_status(hdl, AUDIO_OPEN); |
| 347 | free(data); |
| 348 | mbtk_audio_log("\n%s:exit!\n", __FUNCTION__); |
| 349 | pthread_exit(NULL); |
| 350 | |
| 351 | return NULL; |
| 352 | } |
| 353 | |
| 354 | static int audio_init_play_pthread(void* hdl) |
| 355 | { |
| 356 | struct mopen_audio_t *pcxt = (struct mopen_audio_t *)hdl; |
| 357 | _play_callback audio_play_cb = (_play_callback)pcxt->usrData; |
| 358 | int ret; |
| 359 | |
| 360 | if (pcxt->fd[0] == 0 && pcxt->fd[1] == 0) { |
| 361 | if(pipe(pcxt->fd) == 0) { |
| 362 | |
| 363 | pcxt->pipe_data = 0; |
| 364 | fcntl(pcxt->fd[0], F_SETFL, O_NONBLOCK); |
| 365 | fcntl(pcxt->fd[1], F_SETFL, O_NONBLOCK); |
| 366 | } else { |
| 367 | fprintf(stderr, "\n%d: create pipe error\n", __LINE__); |
| 368 | return -1; |
| 369 | } |
| 370 | } |
| 371 | if (pcxt->pid == 0) { |
| 372 | if (audio_play_cb) |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 373 | audio_play_cb(pcxt, AUD_PLAYER_START); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 374 | mbtk_audio_set_status(pcxt, AUDIO_RUNNING); |
| 375 | ret = pthread_create(&pcxt->pid, NULL, mbtk_play_pthread, hdl); |
| 376 | if (ret != 0) |
| 377 | { |
| 378 | fprintf(stderr, "\n%s: Failed create pthread\n",__FUNCTION__); |
| 379 | return ret; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | int mbtk_audio_play_wait_end(void* hdl) |
| 387 | { |
| 388 | struct mopen_audio_t *pcxt = (struct mopen_audio_t *)hdl; |
| 389 | |
| 390 | if (NULL == hdl) |
| 391 | { |
| 392 | fprintf(stderr, "\n%s: intput invalid params\n",__FUNCTION__); |
| 393 | return -1; |
| 394 | } |
| 395 | |
| 396 | if(pcxt->pid != 0) |
| 397 | { |
| 398 | do{ |
| 399 | usleep(10000); |
| 400 | } |
| 401 | while(pcxt->state != AUDIO_STOP); |
| 402 | pcxt->pid = 0; |
| 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int audio_open_pcm(void *dev_hdl, int num_channels, int rate, int in_out) |
| 409 | { |
| 410 | struct mopen_audio_t *pcxt = (struct mopen_audio_t *)dev_hdl; |
| 411 | int nb_wb; |
| 412 | |
| 413 | //Support 8k/16k & mono wave file |
| 414 | if (((rate != 8000) && (rate != 16000)) |
| 415 | || (num_channels != 1) ) { |
| 416 | printf("%s: error wave file:rate = %d, num_channels = %d!! \n", |
| 417 | __FUNCTION__,rate, num_channels); |
| 418 | return -1; |
| 419 | } |
| 420 | |
| 421 | printf("%s: success open, rate = %d, num_channels = %d.\n", |
| 422 | __FUNCTION__, rate, num_channels); |
| 423 | |
| 424 | if ((8000 == rate) && (1 == num_channels)) { |
| 425 | nb_wb = 0;//NB |
| 426 | pcxt->pcm_packet_size = PCM_NB_BUF_SIZE; |
| 427 | } else if ((16000 == rate) && (1 == num_channels)) { |
| 428 | nb_wb = 1;//WB |
| 429 | pcxt->pcm_packet_size = PCM_WB_BUF_SIZE; |
| 430 | } |
| 431 | |
| 432 | //config playback parameters. |
| 433 | config_parameters(in_out, nb_wb); |
| 434 | pcxt->device = in_out; |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | mbtk_audio_handle mbtk_audio_open(mbtk_audio_dev_enum dev, int flag, int rate, void *usrData) |
| 439 | { |
| 440 | int value = 1; |
| 441 | struct mopen_audio_t *aud_hdl = NULL; |
| 442 | int ret; |
| 443 | |
| 444 | // if (dev <= AUDIO_NULL || dev >= AUDIO_MAX) |
| 445 | // { |
| 446 | // printf("invaild PCM dev\n"); |
| 447 | // return NULL; |
| 448 | // } |
| 449 | if(internal_hdl) |
| 450 | { |
| 451 | printf("Audio device inited\n"); |
| 452 | return internal_hdl; |
| 453 | } |
| 454 | aud_hdl = (struct mopen_audio_t *) calloc(1, sizeof(struct mopen_audio_t)); |
| 455 | if (!aud_hdl) |
| 456 | { |
| 457 | fprintf(stderr, "\n%s:Failed to allocate audio hdl!, errno=%d\n", __FUNCTION__, errno); |
| 458 | return NULL; |
| 459 | } |
| 460 | memset(aud_hdl, 0, sizeof(struct mopen_audio_t)); |
| 461 | |
| 462 | //init global variables |
| 463 | aud_hdl->audio_ahw_dev_ubus = audio_hal_install(); |
| 464 | if (aud_hdl->audio_ahw_dev_ubus == NULL) { |
| 465 | printf("%s: audio_hal_install failed!\n", __FUNCTION__); |
| 466 | goto error; |
| 467 | } |
| 468 | |
| 469 | if(1 == dev) |
| 470 | { |
| 471 | //send the command of "AUDIOSTUB_PCMCTL" and "AUDIOSTUB_DTMFDETECTIONCTL" to simulate telephone offhook. |
| 472 | // ASR Question #80497 Using it causes the recording to crash |
| 473 | // simulateOffhook(aud_hdl, 1); |
| 474 | //config playback parameters. |
| 475 | config_parameters(0, 0); |
| 476 | } |
| 477 | ret = audio_open_pcm(aud_hdl, flag, rate, dev); |
| 478 | if (ret) |
| 479 | { |
| 480 | printf("\n%s: pcm open error, errno=%d\n", __FUNCTION__, errno); |
| 481 | goto error; |
| 482 | } |
| 483 | if(1 == dev) |
| 484 | { |
| 485 | //open the audiostub_ctl, prepare for record and playback |
| 486 | VCMInit(); |
| 487 | //open record stream |
| 488 | ret = aud_hdl->audio_ahw_dev_ubus->open_input_stream(aud_hdl->audio_ahw_dev_ubus, 0, |
| 489 | aud_hdl->audio_ahw_dev_ubus->get_supported_devices(aud_hdl->audio_ahw_dev_ubus), |
| 490 | NULL, &aud_hdl->stream_in, 0, 0, AUDIO_SOURCE_VOICE_CALL); |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | ret = aud_hdl->audio_ahw_dev_ubus->open_output_stream(aud_hdl->audio_ahw_dev_ubus, 0, |
| 495 | aud_hdl->audio_ahw_dev_ubus->get_supported_devices(aud_hdl->audio_ahw_dev_ubus), |
| 496 | AUDIO_OUTPUT_FLAG_DIRECT, NULL, &aud_hdl->stream_out, 0); |
| 497 | } |
| 498 | |
| 499 | if (ret < 0) { |
| 500 | printf("%s: error opening output device. rc = %d\n", __FUNCTION__, ret); |
| 501 | goto error; |
| 502 | } |
| 503 | pthread_mutex_init(&aud_hdl->_cond_mutex, NULL); |
| 504 | pthread_mutex_init(&aud_hdl->_stream_mutex, NULL); |
| 505 | aud_hdl->usrData = usrData; |
| 506 | aud_hdl->state = AUDIO_OPEN; |
| 507 | /* printf("Mbtk_Audio_Open aud_hdl[%x][%x][%x]\n", aud_hdl, aud_hdl->_mixer_ctl, aud_hdl->_pcm); */ |
| 508 | internal_hdl = aud_hdl; |
| 509 | |
| 510 | return (void *)aud_hdl; |
| 511 | |
| 512 | error: |
| 513 | value = 0; |
| 514 | free(aud_hdl); |
| 515 | return NULL; |
| 516 | } |
| 517 | |
| 518 | |
| 519 | int mbtk_audio_play_stream_old(void *dev_hdl, const void *pData, int len) |
| 520 | { |
| 521 | struct mopen_audio_t *pcxt = (struct mopen_audio_t *)dev_hdl; |
| 522 | |
| 523 | if (NULL == pcxt || NULL == internal_hdl) |
| 524 | return -1; |
| 525 | |
| 526 | if (len > 1024) { |
| 527 | printf("audio play stream max size ( < 1024): %d\n", len); |
| 528 | return -2; |
| 529 | } |
| 530 | |
| 531 | if (pcxt->state == AUDIO_STOP) { |
| 532 | return 0; |
| 533 | } |
| 534 | audio_init_play_pthread(pcxt); |
| 535 | |
| 536 | pthread_mutex_lock(&pcxt->_stream_mutex); |
| 537 | pcxt->pipe_data += len; |
| 538 | pthread_mutex_unlock(&pcxt->_stream_mutex); |
| 539 | while(pcxt->state != AUDIO_STOP && (pcxt->pipe_data > 40960 || pcxt->fd[1] < 0 || pcxt->fd[1] == 0)) { |
| 540 | // printf("%s:wait %d %d\n", __FUNCTION__, pcxt->pipe_data, pcxt->fd[1]); |
| 541 | usleep(50000); |
| 542 | } |
| 543 | |
| 544 | if(pcxt->fd[1] > 0) { |
| 545 | if (len != write(pcxt->fd[1], pData, len)) { |
| 546 | fprintf(stderr, "\n%s: pcm_write failed [%d %d]\n", __FUNCTION__, pcxt->fd[1], errno); |
| 547 | return -errno; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | int mbtk_audio_play_stream(void *dev_hdl, const void *pData, int len) |
| 555 | { |
| 556 | // printf("%s,--len:%d\n", __func__, len); |
| 557 | unsigned bufsize = 0; |
| 558 | char *data = NULL; |
| 559 | int first_set = 0; |
| 560 | int res = 0; |
| 561 | int ret = 0; |
| 562 | int read_size = 0; |
| 563 | char *p = (char *)pData; |
| 564 | |
| 565 | struct mopen_audio_t *pcxt = (struct mopen_audio_t *)dev_hdl; |
| 566 | |
| 567 | if (NULL == dev_hdl || NULL == internal_hdl || pData == NULL) |
| 568 | return -1; |
| 569 | |
| 570 | if(AUDIO_RUNNING == pcxt->state) |
| 571 | return -2; |
| 572 | |
| 573 | bufsize = pcxt->pcm_packet_size; |
| 574 | data = calloc(1, bufsize); |
| 575 | if (!data) { |
| 576 | fprintf(stderr, "\n%s:could not allocate %d bytes\n",__FUNCTION__,bufsize); |
| 577 | pthread_exit(NULL); |
| 578 | } |
| 579 | |
LUOJian | 71d5b92 | 2023-12-01 18:07:17 +0800 | [diff] [blame^] | 580 | if(pcxt->state == AUDIO_OPEN) |
| 581 | { |
| 582 | mbtk_audio_set_status(dev_hdl, AUDIO_RUNNING); |
| 583 | |
| 584 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 585 | // if(bufsize > len ) |
| 586 | // { |
| 587 | // bufsize = len; |
| 588 | // } |
| 589 | |
| 590 | while (pcxt->state != AUDIO_STOP) |
| 591 | { |
| 592 | |
| 593 | memcpy(data,p+read_size, bufsize ); |
| 594 | read_size += bufsize; |
| 595 | |
| 596 | if(read_size > len) |
| 597 | { |
| 598 | // printf(">[%d]\n", read_size); |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | while(AUDIO_PAUSE == pcxt->state) |
| 603 | { |
| 604 | usleep(80000); |
| 605 | } |
| 606 | |
| 607 | if ((0 == first_set || AUDIO_RESUME == pcxt->state)) |
| 608 | { |
| 609 | first_set = 1; |
| 610 | mbtk_audio_set_status(dev_hdl, AUDIO_RUNNING); |
| 611 | } |
| 612 | |
| 613 | ret = pcxt->stream_out->write(pcxt->stream_out, data, bufsize); |
| 614 | if (ret < 0) { |
| 615 | printf("%s: error writing (child).\n", __FUNCTION__); |
| 616 | break; |
| 617 | } else if (ret < (signed int)pcxt->pcm_packet_size) { |
| 618 | printf("%s: wrote less than buffer size, rc=%d.\n", __FUNCTION__, ret); |
| 619 | break; |
| 620 | } |
| 621 | |
| 622 | if(read_size == len) |
| 623 | { |
| 624 | // printf("=[%d]", read_size); |
| 625 | break; |
| 626 | } |
| 627 | |
| 628 | } |
LUOJian | 71d5b92 | 2023-12-01 18:07:17 +0800 | [diff] [blame^] | 629 | |
| 630 | if(pcxt->state != AUDIO_STOP) |
| 631 | { |
| 632 | mbtk_audio_set_status(dev_hdl, AUDIO_OPEN); |
| 633 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 634 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 635 | free(data); |
| 636 | return 0; |
| 637 | |
| 638 | } |
| 639 | |
| 640 | |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 641 | int mbtk_audio_play_file(void *dev_hdl, int file_fd, int offset) |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 642 | { |
| 643 | unsigned bufsize = 0; |
| 644 | char *data = NULL; |
| 645 | int first_set = 0; |
| 646 | int file_data_sz = 0; |
| 647 | int res = 0; |
| 648 | int ret; |
| 649 | |
| 650 | struct mopen_audio_t *pcxt = (struct mopen_audio_t *)dev_hdl; |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 651 | _play_callback audio_play_cb = (_play_callback)pcxt->usrData; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 652 | if (NULL == dev_hdl || NULL == internal_hdl) |
| 653 | return -1; |
| 654 | |
| 655 | if(AUDIO_RUNNING == pcxt->state) |
| 656 | return -2; |
| 657 | |
zhangzh | 8711cea | 2023-10-20 10:47:43 +0800 | [diff] [blame] | 658 | // file_data_sz = mbtk_wav_pcm16Le_check(file_fd); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 659 | bufsize = pcxt->pcm_packet_size; |
| 660 | data = calloc(1, bufsize); |
| 661 | if (!data) { |
| 662 | fprintf(stderr, "\n%s:could not allocate %d bytes\n",__FUNCTION__,bufsize); |
| 663 | pthread_exit(NULL); |
| 664 | } |
| 665 | if(offset) |
| 666 | { |
| 667 | lseek(file_fd, offset, SEEK_SET); |
| 668 | } |
| 669 | |
| 670 | mbtk_audio_set_status(dev_hdl, AUDIO_RUNNING); |
| 671 | int all_size = 0; |
| 672 | |
| 673 | while (pcxt->state != AUDIO_STOP) |
| 674 | { |
| 675 | res = read(file_fd, data, bufsize); |
| 676 | // printf("%s:read : %d bytes\n", __FUNCTION__, res); |
| 677 | if(res == 0 || res < 0) |
| 678 | { |
LUOJian | 1ce8d75 | 2023-09-11 11:24:43 +0800 | [diff] [blame] | 679 | printf("read:[%d]", res); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 680 | break; |
| 681 | } |
| 682 | |
| 683 | all_size += res; |
LUOJian | 1ce8d75 | 2023-09-11 11:24:43 +0800 | [diff] [blame] | 684 | // if (file_data_sz < all_size || file_data_sz == all_size) { |
| 685 | // printf("aplay size :%d - %d\n", file_data_sz, all_size); |
| 686 | // break; |
| 687 | // } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 688 | |
| 689 | while(AUDIO_PAUSE == pcxt->state) |
| 690 | { |
| 691 | usleep(80000); |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 692 | audio_play_cb(pcxt, AUD_PLAYER_PAUSE); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | if ((0 == first_set || AUDIO_RESUME == pcxt->state)) |
| 696 | { |
| 697 | first_set = 1; |
| 698 | mbtk_audio_set_status(dev_hdl, AUDIO_RUNNING); |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 699 | audio_play_cb(pcxt, AUD_PLAYER_RESUME); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 700 | } |
| 701 | |
zhangzh | 8711cea | 2023-10-20 10:47:43 +0800 | [diff] [blame] | 702 | ret = pcxt->stream_out->write(pcxt->stream_out, data, bufsize); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 703 | if (ret < 0) { |
| 704 | printf("%s: error writing (child).\n", __FUNCTION__); |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 705 | audio_play_cb(pcxt, AUD_PLAYER_ERROR); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 706 | break; |
| 707 | } else if (ret < (signed int)pcxt->pcm_packet_size) { |
| 708 | printf("%s: wrote less than buffer size, rc=%d.\n", __FUNCTION__, ret); |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 709 | audio_play_cb(pcxt, AUD_PLAYER_LESSDATA); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 710 | break; |
| 711 | } |
| 712 | } |
zhangzh | d04babd | 2023-10-24 16:55:57 +0800 | [diff] [blame] | 713 | if (audio_play_cb) |
| 714 | audio_play_cb(pcxt, AUD_PLAYER_FINISHED); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 715 | |
LUOJian | 1ce8d75 | 2023-09-11 11:24:43 +0800 | [diff] [blame] | 716 | printf("file_data_sz :%d - all_size: %d\n", file_data_sz, all_size); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 717 | mbtk_audio_set_status(dev_hdl, AUDIO_OPEN); |
| 718 | free(data); |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | int mbtk_audio_record(void *dev_hdl, mbtk_audio_record_cb_func cb_func, void *cb_data) |
| 724 | { |
| 725 | struct mopen_audio_t *pcxt = (struct mopen_audio_t *)dev_hdl; |
| 726 | int res = -1; |
| 727 | struct record_cb_s *_usrData = NULL; |
| 728 | |
| 729 | if (NULL == pcxt || NULL == internal_hdl || NULL == cb_func) |
| 730 | return -1; |
| 731 | |
| 732 | if(AUDIO_RUNNING == pcxt->state) |
| 733 | return -2; |
| 734 | |
| 735 | _usrData = malloc(sizeof(struct record_cb_s)); |
| 736 | _usrData->_cb = cb_func; |
| 737 | _usrData->cb_data = cb_data; |
| 738 | pcxt->usrData = (void *)_usrData; |
| 739 | res = pthread_create(&pcxt->pid, NULL, mbtk_record_pthread, dev_hdl); |
| 740 | if (res != 0) |
| 741 | { |
| 742 | if(pcxt->usrData) |
| 743 | { |
| 744 | free(pcxt->usrData); |
| 745 | pcxt->usrData = NULL; |
| 746 | } |
| 747 | fprintf(stderr, "\n%s: Failed to create pthread\n", __FUNCTION__); |
| 748 | } |
| 749 | // 防止重复录音 |
| 750 | usleep(500); |
| 751 | |
| 752 | return res; |
| 753 | } |
| 754 | |
| 755 | int mbtk_audio_close(void *dev_hdl) |
| 756 | { |
| 757 | printf("mbtk_audio_close()\n"); |
| 758 | int value = 0; |
| 759 | struct mopen_audio_t *_hdl = (struct mopen_audio_t *)dev_hdl; |
| 760 | |
| 761 | if (NULL == _hdl || NULL == internal_hdl ) |
| 762 | { |
| 763 | printf("mbtk_audio_close() fail dev_hdl is NULL\n"); |
| 764 | return -1; |
| 765 | } |
| 766 | |
| 767 | mbtk_audio_set_status(_hdl, AUDIO_STOP); |
| 768 | if(0 == _hdl->device) { |
| 769 | while (_hdl->pid != 0) { |
| 770 | usleep(10000); |
| 771 | } |
| 772 | |
| 773 | vcm_playback_drain(0);//wait for drain the AP audiostub queue. |
| 774 | usleep(80000);//delay 80ms until DSP play out its buffered data. |
| 775 | _hdl->stream_out->common.standby(&_hdl->stream_out->common); |
| 776 | _hdl->audio_ahw_dev_ubus->close_output_stream(_hdl->audio_ahw_dev_ubus, _hdl->stream_out); |
| 777 | } else { |
| 778 | |
| 779 | while (_hdl->pid != 0) { |
| 780 | sleep(1); |
| 781 | } |
| 782 | _hdl->stream_in->common.standby(&_hdl->stream_in->common); |
| 783 | _hdl->audio_ahw_dev_ubus->close_input_stream(_hdl->audio_ahw_dev_ubus, _hdl->stream_in); |
| 784 | VCMDeinit();//close the fd of audiostub_ctl when exit the thread. |
| 785 | } |
| 786 | |
| 787 | audio_hal_uninstall(); |
| 788 | pthread_mutex_destroy(&_hdl->_cond_mutex); |
| 789 | pthread_mutex_destroy(&_hdl->_stream_mutex); |
| 790 | free(_hdl); |
| 791 | _hdl = NULL; |
| 792 | internal_hdl = NULL; |
| 793 | |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | int mbtk_audio_pause(void* dev_hdl) |
| 798 | { |
| 799 | // struct pcm *_pcm = ((struct mopen_audio_t *)dev_hdl)->_pcm; |
| 800 | |
| 801 | if (NULL == dev_hdl || NULL == internal_hdl) |
| 802 | return -1; |
| 803 | |
| 804 | if (((struct mopen_audio_t *)dev_hdl)->state != AUDIO_RUNNING) |
| 805 | { |
| 806 | return -1; |
| 807 | } |
| 808 | |
| 809 | // if (ioctl(_pcm->fd, SNDRV_PCM_IOCTL_PAUSE, 1)) |
| 810 | // { |
| 811 | // printf("\n%s: cannot pause channel: errno =%d\n",__FUNCTION__,-errno); |
| 812 | // return -errno; |
| 813 | // } |
| 814 | mbtk_audio_set_status(dev_hdl, AUDIO_PAUSE); |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | int mbtk_audio_resume(void* dev_hdl) |
| 819 | { |
| 820 | if (NULL == dev_hdl || NULL == internal_hdl) |
| 821 | return -1; |
| 822 | |
| 823 | if (((struct mopen_audio_t *)dev_hdl)->state != AUDIO_PAUSE) |
| 824 | { |
| 825 | return -1; |
| 826 | } |
| 827 | |
| 828 | // if (ioctl(_pcm->fd, SNDRV_PCM_IOCTL_PAUSE, 0)) |
| 829 | // { |
| 830 | // printf("\n%s: cannot Resume channel: errno =%d\n", __FUNCTION__, -errno); |
| 831 | // return -errno; |
| 832 | // } |
| 833 | mbtk_audio_set_status(dev_hdl, AUDIO_RESUME); |
| 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | int mbtk_audio_stop(void* dev_hdl) |
| 838 | { |
| 839 | struct mopen_audio_t *_hdl = (struct mopen_audio_t *)dev_hdl; |
| 840 | |
| 841 | if (NULL == dev_hdl || NULL == internal_hdl) |
| 842 | return -1; |
| 843 | |
| 844 | // if (ioctl(_hdl->_pcm->fd, SNDRV_PCM_IOCTL_DROP)) |
| 845 | // { |
| 846 | // printf("\n%s: cannot Resume channel: errno =%d\n",__FUNCTION__,-errno); |
| 847 | // return -errno; |
| 848 | // } |
| 849 | |
| 850 | mbtk_audio_set_status(dev_hdl, AUDIO_STOP); |
| 851 | _hdl->pid = 0; |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | mbtk_audio_state_enum mbtk_audio_state_get(void *hdl) |
| 857 | { |
| 858 | return ((struct mopen_audio_t *)hdl)->state; |
| 859 | } |