| #include "mbtk_type.h" |
| #include <fcntl.h> |
| #include <stdint.h> |
| #include <limits.h> |
| #include <termios.h> |
| #include <stdarg.h> |
| #include <fcntl.h> |
| #include <sys/time.h> |
| #include <pthread.h> |
| #include <signal.h> |
| #include <sys/stat.h> |
| |
| #include "ql/ql_audio.h" |
| |
| int handler = 0; |
| int s_play_hdl = -1; |
| int s_play_fd = -1; |
| static pthread_t play_thread_play; |
| |
| #define PROMPT_START_RECORD "/bin/start_record_test.wav" |
| #define RECORD_FILEPATH "/usrdata/test.wav" |
| #define PROMPT_START_PLAY "/bin/start_play_test.wav" |
| #define PROMPT_TEST_FINISH "/bin/test_end_test.wav" |
| |
| #if 1 |
| void dtmf_cb1(char dtmf) |
| { |
| printf("%s:%c\n", __FUNCTION__, dtmf); |
| } |
| #endif |
| |
| int Ql_cb_playback(int hdl, int result) |
| { |
| printf("%s: hdl=%d, result=%d\n\r", __func__, hdl, result); |
| if (result == AUD_PLAYER_FINISHED || result == AUD_PLAYER_NODATA) |
| { |
| printf("%s: play finished\n\r", __func__); |
| } |
| return 0; |
| } |
| |
| int MBTK_wav_pcm16Le_check(int fd) |
| { |
| struct wav_header hdr; |
| |
| if (fd <= 0) |
| return -1; |
| |
| if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) |
| { |
| printf("\n%s: cannot read header\n", __FUNCTION__); |
| return -1; |
| } |
| |
| if ((hdr.riff_id != ID_RIFF) || (hdr.riff_fmt != ID_WAVE) || (hdr.fmt_id != ID_FMT)) |
| { |
| printf("\n%s: is not a riff/wave file\n", __FUNCTION__); |
| return -1; |
| } |
| |
| if ((hdr.audio_format != FORMAT_PCM) || (hdr.fmt_sz != 16)) |
| { |
| printf("\n%s: is not pcm format\n", __FUNCTION__); |
| return -1; |
| } |
| |
| if (hdr.bits_per_sample != 16) |
| { |
| printf("\n%s: is not 16bit per sample\n", __FUNCTION__); |
| return -1; |
| } |
| |
| return 0; |
| } |
| |
| int PLT_Audio_StartPlay(char *filePath, int repeat_value) |
| { |
| s_play_hdl = Ql_AudPlayer_Open(NULL, Ql_cb_playback); |
| if (0 == s_play_hdl) { |
| printf("Ql_AudPlayer_Open Failed...\n"); |
| return -2; |
| } |
| |
| s_play_fd = open(filePath, O_RDWR); |
| if (s_play_fd <= 0){ |
| printf(" Open wavFilePath Failed\n"); |
| return -3 ; |
| } |
| |
| mbtk_audio_path_enable(1); |
| if (0 == MBTK_wav_pcm16Le_check(s_play_fd)) { |
| Ql_AudPlayer_PlayFrmFile(s_play_hdl, s_play_fd, 0); |
| } else { |
| printf("aplay FileType error...\n"); |
| close(s_play_fd); |
| return -4; |
| } |
| close(s_play_fd); |
| mbtk_audio_path_enable(0); |
| Ql_AudPlayer_Close(s_play_hdl); |
| s_play_hdl = -1; |
| |
| printf("aplay End...\n"); |
| return 0; |
| } |
| |
| static void audio_play_thread(void *arg) |
| { |
| PLT_Audio_StartPlay((char*)arg, 1); |
| } |
| |
| static void sig_handler(int sig) |
| { |
| if(s_play_hdl > 0) { |
| Ql_AudPlayer_Stop(s_play_hdl); |
| if (pthread_join(play_thread_play, NULL)) { |
| printf("error join play_recorder!\n"); |
| } |
| |
| mbtk_audio_mode_set(-2); |
| mbtk_audio_switch_pcm(0); |
| mbtk_audio_ubus_client_deinit(handler); |
| } |
| |
| printf("Success exit by signal...\n"); |
| exit(0); |
| } |
| |
| |
| int main(int argc, char* argv[]) |
| { |
| if(argc != 2) { |
| printf("audio_play_test <wav_file>\n"); |
| return -1; |
| } |
| |
| struct stat st; |
| const char *path = (const char *)argv[1]; |
| // mbtk_log_init("radio", "MBTK_AUDIO"); |
| |
| /* Check and open source file */ |
| if (access(path, F_OK) || stat(path, &st)) { |
| printf("%s: error reading from file %s\n", __FUNCTION__, path); |
| return -1; |
| } |
| |
| if (!st.st_size) { |
| printf("%s: empty file %s\n", __FUNCTION__, path); |
| return -1; |
| } |
| |
| signal(SIGINT, sig_handler); |
| signal(SIGTERM, sig_handler); |
| signal(SIGTSTP, sig_handler); |
| |
| mbtk_audio_ubus_client_init(&handler, dtmf_cb1); |
| mbtk_audio_switch_pcm(1); |
| mbtk_audio_mode_set(2); |
| |
| char cmd[100]; |
| bool running = TRUE; |
| while(running) |
| { |
| memset(cmd, 0, 100); |
| int err; |
| printf("1 : Play 2 : Pause 3 : Resume 4 : Stop Other : Exit\n"); |
| if(fgets(cmd, 100, stdin)) |
| { |
| if(cmd[0] == '\r' || cmd[0] == '\n') |
| continue; |
| int state = atoi(cmd); |
| switch(state){ |
| case 1: |
| { |
| if(s_play_hdl > 0) { |
| break; |
| } |
| pthread_attr_t thread_attr; |
| pthread_attr_init(&thread_attr); |
| if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED)) |
| { |
| printf("pthread_attr_setdetachstate() fail.\n"); |
| goto exit; |
| } |
| |
| if (pthread_create(&play_thread_play, NULL, (void *)&audio_play_thread, path) < 0) { |
| printf("%s: error creating thread_play!\n", __FUNCTION__); |
| goto exit; |
| } |
| |
| printf("Start play audio...\n"); |
| break; |
| } |
| case 2: |
| { |
| if(s_play_hdl <= 0) { |
| break; |
| } |
| Ql_AudPlayer_Pause(s_play_hdl); |
| printf("Audio pause.\n"); |
| break; |
| } |
| case 3: |
| { |
| if(s_play_hdl <= 0) { |
| break; |
| } |
| Ql_AudPlayer_Resume(s_play_hdl); |
| printf("Audio resume.\n"); |
| break; |
| } |
| case 4: |
| { |
| if(s_play_hdl <= 0) { |
| break; |
| } |
| Ql_AudPlayer_Stop(s_play_hdl); |
| if (pthread_join(play_thread_play, NULL)) { |
| printf("error join play_thread!\n"); |
| } |
| s_play_hdl = -1; |
| printf("Audio stop.\n"); |
| break; |
| } |
| default: |
| { |
| running = FALSE; |
| if(s_play_hdl <= 0) { |
| break; |
| } |
| Ql_AudPlayer_Stop(s_play_hdl); |
| if (pthread_join(play_thread_play, NULL)) { |
| printf("error join play_thread!\n"); |
| } |
| } |
| } |
| } |
| } |
| |
| exit: |
| |
| mbtk_audio_mode_set(-2); |
| mbtk_audio_switch_pcm(0); |
| mbtk_audio_ubus_client_deinit(handler); |
| |
| printf("Success exit.\n"); |
| return 0; |
| } |
| |