blob: 20504d2e58bc98a3f7b7542724b40aa153877dd0 [file] [log] [blame]
/******************************************************************************
*(C) Copyright 2019 ASR.
* All Rights Reserved
******************************************************************************/
/* -------------------------------------------------------------------------
*
* Filename:audio_player_wav.c
*
* Authors:
*
* Description: The DEMO will playback wav based on PCM stream.
*
*
* HISTORY:
*
*
*
* Notes:
*
******************************************************************************/
/******************************************************************************
* Include files
******************************************************************************/
#include "audio_if_types.h"
#include "audio_if_ubus.h"
#include "audio_if_parameter.h"
#include "audio_if.h"
#include "audio_if_api.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "telatparamdef_ubus.h"
#include <libubox/blobmsg_json.h>
#include "libubus.h"
#include <string.h>
#include "audio_if_audio_hw_mrvl.h"
#include "utlEventHandler.h"
#include "udev_monitor.h"
#include "audio_hw_mrvl.h"
#include <stdlib.h>
#include <stdio.h>
#include <cutils/str_parms.h>
#include "vcm.h"
#include "voice_control.h"
extern void* audio_hal_install(void);
extern void audio_hal_uninstall(void);
extern void configure_vcm(unsigned int data[]);
extern void set_pcm_config(int role, int rate);
extern int mrvl_hw_dev_config_pcm(struct audio_hw_device *dev, unsigned int pcm);
/******************************************************************************
* Globals
******************************************************************************/
static audio_hw_device_t *audio_player_ahw_dev_ubus;
struct audio_stream_in *stream_in = NULL;
struct audio_stream_out *stream_out = NULL;
bool go_on_play = true;
unsigned int pcm_record_size = 0; //320:NB, 640:WB
int pcm_playback_size = 0; //320:NB, 640:WB
/////////////////////////////////////////////////////////////////////////////
static int config_parameters(int in_out, int NBWB)
{
unsigned int direction = 0xFF, type, srcdst, priority, dest;
char kvpair[128];
struct str_parms *param = NULL;
int data[5];
const char *key = NULL;
bool update_vcm = false;
direction = in_out;/* 0-play, 1-record */
type = NBWB; /* 0:PCM_NB_BUF_SIZE, 1:PCM_WB_BUF_SIZE */
srcdst = 1;/* 0-None, 1-Near end, 2-Far end, 3-Both ends */
priority = 1;/* 0-Do not combine(override), 1-Combine */
dest = 1;/* 0-Near codec, 1-Near Vocoder */
if(direction == 0){//output
if(type == 0)
pcm_playback_size = PCM_NB_BUF_SIZE;
else
pcm_playback_size = PCM_WB_BUF_SIZE;
printf("config playback parameters.\n");
}
else if(direction == 1){//input
if(type == 0)
pcm_record_size = PCM_NB_BUF_SIZE;
else
pcm_record_size = PCM_WB_BUF_SIZE;
printf("config record parameters.\n");
}
memset(kvpair, 0x00, sizeof(kvpair));
sprintf(kvpair, "%s=%d;%s=%d;%s=%d;%s=%d;%s=%d", VCM_CONFIG_DIRECTION, direction,
VCM_CONFIG_TYPE, type, VCM_CONFIG_SRC_DST, srcdst,
VCM_CONFIG_PRIORITY, priority, VCM_CONFIG_DEST, dest);
printf("%s: config information kvpair is %s.\n", __FUNCTION__, kvpair);
//extract the parameter and config from string
param = str_parms_create_str(kvpair);
if (!param) {
printf("%s: param create str is null!", __FUNCTION__);
return -1;
}
//set vcm configurations
key = VCM_CONFIG_DIRECTION;
if (str_parms_get_int(param, key, &data[0]) == 0) {
update_vcm = true;
str_parms_del(param, key);
}
key = VCM_CONFIG_TYPE;
if (str_parms_get_int(param, key, &data[1]) == 0) {
update_vcm = true;
str_parms_del(param, key);
}
key = VCM_CONFIG_SRC_DST;
if (str_parms_get_int(param, key, &data[2]) == 0) {
update_vcm = true;
str_parms_del(param, key);
}
key = VCM_CONFIG_PRIORITY;
if (str_parms_get_int(param, key, &data[3]) == 0) {
update_vcm = true;
str_parms_del(param, key);
}
key = VCM_CONFIG_DEST;
if (str_parms_get_int(param, key, &data[4]) == 0) {
update_vcm = true;
str_parms_del(param, key);
}
//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]);
if (update_vcm) {
configure_vcm(data); /*TODO check if all inputs got all values successfully*/
}
return 0;
}
/*******************************************************************************\
* Function: audio_play_thread
* Description:This function will main thread to play the PCM stream
*
*
* NOTE:
sample rate is 8000/16000
channel number is 1:MONO
bit of sample is 16bits
Due to modem limited, only support 8k/16k sample rate
* Returns: void
\*******************************************************************************/
static void audio_play_thread(void *arg)
{
int rc, len, fd, frames = 0;
char buf[PCM_WB_BUF_SIZE];
char *path = (char *)arg;
struct stat st;
struct riff_wave_header riff_wave_header;
struct chunk_header chunk_header;
struct chunk_fmt chunk_fmt = {0};
unsigned int more_chunks = 1;
int NBWB = 0;
/* Check and open source file */
if (access(path, F_OK) || stat(path, &st)) {
printf("%s: error reading from file %s\n", __FUNCTION__, path);
return;
}
if (!st.st_size) {
printf("%s: empty file %s\n", __FUNCTION__, path);
return;
}
fd = open(path, O_RDONLY);
if (fd < 0) {
printf("%s: error opening file %s\n", __FUNCTION__, path);
return;
}
read(fd, &riff_wave_header, sizeof(riff_wave_header));
if ((riff_wave_header.riff_id != ID_RIFF) || (riff_wave_header.wave_id != ID_WAVE)) {
printf("Error: '%s' is not a riff/wave file\n", path);
close(fd);
return;
}
do {
read(fd, &chunk_header, sizeof(chunk_header));
switch (chunk_header.id) {
case ID_FMT:
read(fd, &chunk_fmt, sizeof(chunk_fmt));
/* If the format header is larger, skip the rest */
if (chunk_header.sz > sizeof(chunk_fmt))
lseek(fd, chunk_header.sz - sizeof(chunk_fmt), SEEK_CUR);
break;
case ID_DATA:
/* Stop looking for chunks */
more_chunks = 0;
break;
default:
/* Unknown chunk, skip bytes */
lseek(fd, chunk_header.sz, SEEK_CUR);
}
} while (more_chunks);
//Support 8k/16k & mono wave file
if (((chunk_fmt.sample_rate != 8000) && (chunk_fmt.sample_rate != 16000))
|| (chunk_fmt.num_channels != 1) ) {
printf("%s: error wave file:sample_rate = %d, num_channels = %d!! \n",
__FUNCTION__,chunk_fmt.sample_rate, chunk_fmt.num_channels);
close(fd);
return;
}
printf("%s: success open wave file:%s, sample_rate = %d, num_channels = %d.\n",
__FUNCTION__, path, chunk_fmt.sample_rate, chunk_fmt.num_channels);
if ((8000 == chunk_fmt.sample_rate) && (1 == chunk_fmt.num_channels)) {
NBWB = 0;//NB
} else if ((16000 == chunk_fmt.sample_rate) && (1 == chunk_fmt.num_channels)) {
NBWB = 1;//WB
}
//config playback parameters.
config_parameters(0, NBWB);
rc = audio_player_ahw_dev_ubus->open_output_stream(audio_player_ahw_dev_ubus, 0,
audio_player_ahw_dev_ubus->get_supported_devices(audio_player_ahw_dev_ubus),
AUDIO_OUTPUT_FLAG_DIRECT, NULL, &stream_out, 0);
if (rc < 0) {
printf("%s: error opening output device. rc = %d\n", __FUNCTION__, rc);
goto bad_stream;
}
printf("%s: starting playback %d bytes every 20ms.\n", __FUNCTION__, pcm_playback_size);
go_on_play = true;
while (go_on_play) {
/* Playback loop */
memset(buf, 0x00, sizeof(buf));
len = read(fd, buf, pcm_playback_size);
if (len == -1) {
printf("%s: error reading from file\n", __FUNCTION__);
goto end_thread;
}
if (len == 0) {
/* reached EOF */
printf("%s: nothing to read\n", __FUNCTION__);
goto end_thread;
}
if (len < (signed int)pcm_playback_size) {
printf("%s: len %d is smaller than needed %d, so fill the buffer with 0.\n", __FUNCTION__, len, pcm_playback_size);
}
rc = stream_out->write(stream_out, buf, pcm_playback_size);
if (rc < 0) {
printf("%s: error writing (child).\n", __FUNCTION__);
goto end_thread;
} else if (rc < (signed int)pcm_playback_size) {
printf("%s: wrote less than buffer size, rc=%d.\n", __FUNCTION__, rc);
goto end_thread;
}
printf("%s: No.%d frame playback\n", __FUNCTION__, ++frames);
}
end_thread:
vcm_playback_drain(80); //wait 80ms for drain the AP audiostub queue.
usleep(80000); //delay 80ms until DSP play out its buffered data.
stream_out->common.standby(&stream_out->common);
audio_player_ahw_dev_ubus->close_output_stream(audio_player_ahw_dev_ubus, stream_out);
go_on_play = false;
bad_stream:
if (close(fd))
printf("%s: error closing file\n", __FUNCTION__);
printf("%s: finished pcm playback.\n", __FUNCTION__);
return;
}
static void sig_handler(int sig)
{
go_on_play = false;
printf("output signal number: %d.\n", sig);
}
/* Hold path in a global string*/
char *gPath_play = NULL;
/*******************************************************************************\
* Function: main
\*******************************************************************************/
int main(int argc, char **argv)
{
pthread_t thread_play;
int rc = 0;
char *filename;
if (argc < 2) {
fprintf(stderr, "Usage: %s file.wav.\n", argv[0]);
return 1;
}
filename = argv[1];
printf("command line is %s %s.\n", argv[0], filename);
/* malloc new memory */
gPath_play = malloc(strlen(filename) + 1); /* ending with /0 */
memset(gPath_play, 0x00, strlen(filename) + 1);
if(gPath_play == NULL) {
printf("%s: error malloc\n", __FUNCTION__);
abort();
}
else {
memcpy(gPath_play, filename, strlen(filename));
}
/* install signal handler and begin to capture signal for close */
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
//init global variables
audio_player_ahw_dev_ubus = audio_hal_install();
if (audio_player_ahw_dev_ubus == NULL) {
printf("%s: audio_hal_install failed!\n", __FUNCTION__);
exit (-1);
}
rc = pthread_create(&thread_play, NULL, (void *)&audio_play_thread, gPath_play);
if (rc < 0) {
printf("%s: error creating thread_play!\n", __FUNCTION__);
rc = UBUS_STATUS_PERMISSION_DENIED;
}
if (pthread_join(thread_play, NULL)) {
printf("error join thread_play!\n");
abort();
}
audio_hal_uninstall();
return 0;
}