/******************************************************************************* * Copyright (C) 2018 Cadence Design Systems, Inc. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to use this Software with Cadence processor cores only and * not with any other processors and platforms, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ #define MODULE_TAG UTEST #include "xaf-utils-test.h" #include "audio/xa_vorbis_dec_api.h" #include "audio/xa-mixer-api.h" #include "audio/xa-audio-decoder-api.h" #include "xa_playback.h" #define PRINT_USAGE do { fprintf(stderr, "\nUsage: %s \n", argv[0]); \ fprintf(stderr, " Only .ogg and .pcm files are supported. \n"); \ fprintf(stderr, " Playback is configured @ 48kHz, 2 ch, 16 bits pcm. \n"); \ fprintf(stderr, " is optional. \n"); \ fprintf(stderr, " pcm output is written to dec-mix-out.pcm, by default. \n\n"); \ } while(0) #define MAX_INP_STRMS 2 #define NUM_COMP_IN_GRAPH 3 /* ...global variables */ int g_pthread_exit_code=0x12345678; /* ...playback format */ xaf_format_t pb_format; /* ...playback device parameters */ void *pb_handle = NULL; void thread_exit_handler(int sig) { /* ...unused arg */ (void) sig; pthread_exit(0); } static int vorbis_setup(void *p_decoder) { int param[2]; /* 1: Raw decode, 0: Ogg decode */ param[0] = XA_VORBISDEC_CONFIG_PARAM_RAW_VORBIS_FILE_MODE; param[1] = 0; XF_CHK_API(xaf_comp_set_config(p_decoder, 1, ¶m[0])); return 0; } static int pcm_setup(void *p_pcm) { int param[6]; param[0] = XA_CODEC_CONFIG_PARAM_SAMPLE_RATE; param[1] = pb_format.sample_rate; param[2] = XA_CODEC_CONFIG_PARAM_CHANNELS; param[3] = pb_format.channels; param[4] = XA_CODEC_CONFIG_PARAM_PCM_WIDTH; param[5] = pb_format.pcm_width; XF_CHK_API(xaf_comp_set_config(p_pcm, 3, ¶m[0])); return 0; } static int mixer_setup(void *p_decoder, xaf_format_t *p_format) { int param[6]; param[0] = XA_MIXER_CONFIG_PARAM_SAMPLE_RATE; param[1] = p_format->sample_rate; param[2] = XA_MIXER_CONFIG_PARAM_CHANNELS; param[3] = p_format->channels; param[4] = XA_MIXER_CONFIG_PARAM_PCM_WIDTH; param[5] = p_format->pcm_width; XF_CHK_API(xaf_comp_set_config(p_decoder, 3, ¶m[0])); return 0; } static int consume_output(void *p_buf, int buf_length, void *p_output) { XAF_CHK_PTR(p_buf); XAF_CHK_PTR(p_output); #if !defined BOARD FILE *fp = p_output; fwrite(p_buf, 1, buf_length, fp); if (xa_playback_buf(pb_handle, p_buf, buf_length)) { TRACE(ERROR, _b("Playback Failed \n")); return -1; } #else #endif return 0; } static int read_input(void *p_buf, int buf_length, int *read_length, void *p_input) { XAF_CHK_PTR(p_buf); XAF_CHK_PTR(read_length); XAF_CHK_PTR(p_input); #if !defined BOARD FILE *fp = p_input; *read_length = fread(p_buf, 1, buf_length, fp); #else #endif return 0; } static int comp_process_entry(void *arg) { void *p_comp; void *p_input, *p_output; xaf_comp_status comp_status; xaf_info_t comp_info; int input_over, read_length; void * (*arg_arr)[3]; void *pg_pthread_exit_code = (void*)&g_pthread_exit_code; XAF_CHK_PTR(arg); arg_arr = arg; p_comp = (*arg_arr)[0]; p_input = (*arg_arr)[1]; p_output = (*arg_arr)[2]; input_over = 0; XF_CHK_API(xaf_comp_process(NULL, p_comp, NULL, 0, XAF_EXEC_FLAG)); while (1) { XF_CHK_API(xaf_comp_get_status(NULL, p_comp, &comp_status, &comp_info)); if (comp_status == XAF_EXEC_DONE) break; if (comp_status == XAF_NEED_INPUT && !input_over) { void *p_buf = (void *) comp_info.buf; int size = comp_info.length; XF_CHK_API(read_input(p_buf, size, &read_length, p_input)); if (read_length) XF_CHK_API(xaf_comp_process(NULL, p_comp, (void *)comp_info.buf, read_length, XAF_INPUT_READY_FLAG)); else { XF_CHK_API(xaf_comp_process(NULL, p_comp, NULL, 0, XAF_INPUT_OVER_FLAG)); input_over = 1; } } if (comp_status == XAF_OUTPUT_READY) { void *p_buf = (void *) comp_info.buf; int size = comp_info.length; XF_CHK_API(consume_output(p_buf, size, p_output)); XF_CHK_API(xaf_comp_process(NULL, p_comp, (void *)comp_info.buf, comp_info.length, XAF_NEED_OUTPUT_FLAG)); } } pthread_exit(pg_pthread_exit_code); return 0; } int main(int argc, const char **argv) { void *p_adev = NULL; void *p_decoder[MAX_INP_STRMS]; void *p_mixer; mem_obj_t* mem_handle; int num_comp = NUM_COMP_IN_GRAPH; xaf_comp_status dec_status; xaf_info_t comp_info; void *p_input[MAX_INP_STRMS], *p_output; xf_id_t dec_id[MAX_INP_STRMS]; int (*dec_setup[MAX_INP_STRMS])(void *p_comp); pthread_t dec_thread[MAX_INP_STRMS]; pthread_t mixer_thread; void *dec_thread_args[MAX_INP_STRMS][3]; void *mixer_thread_args[3]; const char *ext; FILE *fp, *ofp; void *dec_inbuf[MAX_INP_STRMS][2]; int buf_length = XAF_INBUF_SIZE; int read_length; int input_over = 0; int i, j; int num_strms; unsigned int card = 0; unsigned int device = 0; unsigned int period_size = 1024; unsigned int period_count = 4; int pthread_error; void *pthread_exit_code[3]; struct sigaction actions; memset(&actions, 0, sizeof(actions)); sigemptyset(&actions.sa_mask); actions.sa_flags = 0; actions.sa_handler = thread_exit_handler; sigaction(SIGUSR1,&actions,NULL); /* ...initialize playback format */ pb_format.sample_rate = 48000; pb_format.channels = 2; pb_format.pcm_width = 16; audio_frmwk_buf_size = 0; //unused audio_comp_buf_size = 0; //unused print_banner("\'Audio decoder(PCM/Ogg-Vorbis) + Mixer\'"); /* ...initialize tracing facility */ TRACE_INIT("Xtensa Audio Framework - Sample Application"); #if !defined BOARD /* ...check input arguments */ if (argc < 2 || argc > (MAX_INP_STRMS+1)) { TRACE(ERROR, _b("Usage: ./xaf-test \n")); PRINT_USAGE; return 0; } argc--; for (i=0; i