blob: 13f8637a788c9d8f7659476fc948c0c75f0957a0 [file] [log] [blame]
/******************************************************************************
*(C) Copyright 2019 ASR.
* All Rights Reserved
******************************************************************************/
/* -------------------------------------------------------------------------------------------------------------------
*
* Filename:VoIP_app_DEMO.c
*
* Authors:
*
* Description: The DEMO will record and playback PCM stream based on ASR1826.
*
*
* 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"
extern void* audio_hal_install(void);
extern void audio_hal_uninstall(void);
extern void configure_vcm(unsigned int data[]);
/******************************************************************************
* Globals
******************************************************************************/
static audio_hw_device_t *VoIP_ahw_dev_ubus;
struct audio_stream_in *stream_in = NULL;
struct audio_stream_out *stream_out = NULL;
bool go_on_pcmloopback = true;
unsigned int pcm_record_size = 0; //320:NB, 640:WB
unsigned int pcm_playback_size = 0; //320:NB, 640:WB
/////////////////////////////////////////////////////////////////////////////
static int config_parameters(int in_out)
{
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 = 0; /* 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: VoIP_thread
* Description:This function will main thread to record the PCM stream from
* ASR1826 and playback the PCM stream to ASR1826.
*
* NOTE:
sample rate is 8000
channel number is 1
bit of sample is 16
Due to modem limited, only support 8k/16k sample rate
* Returns: void
\*******************************************************************************/
static void VoIP_thread(void *arg)
{
int rc, len, fd;
char buffer[PCM_WB_BUF_SIZE];
unsigned int frames = 0;
struct wav_header header;
char path[10];
memset(path, 0x00, sizeof(path));
memcpy(path, "./nb.wav", 9);
printf("path is %s. \n", path);
header.riff_id = ID_RIFF;
header.riff_sz = 0;
header.riff_fmt = ID_WAVE;
header.fmt_id = ID_FMT;
header.fmt_sz = 16;
header.audio_format = 1; //FORMAT_PCM;
header.num_channels = 1; //Modem ONLY support mono recording
header.sample_rate = (pcm_record_size == PCM_NB_BUF_SIZE ? 8000 : 16000);
header.bits_per_sample = 16; //PCM_SAMPLEBITS_S16_LE;
header.byte_rate = (header.bits_per_sample / 8) * header.num_channels * header.sample_rate;
header.block_align = header.num_channels * (header.bits_per_sample / 8);
header.data_id = ID_DATA;
printf("enter VoIP_thread.\n");
//Must set vcm_configure before pcmloopback
if((pcm_record_size != PCM_NB_BUF_SIZE) && (pcm_record_size != PCM_WB_BUF_SIZE)){
printf("%s: Please use vcm_configure to set pcm_record_size!!\n", __FUNCTION__);
return;
}
//Must set vcm_configure before playback
if((pcm_playback_size != PCM_NB_BUF_SIZE) && (pcm_playback_size != PCM_WB_BUF_SIZE)){
printf("%s: Please use vcm_configure to set pcm_playback_size!!\n", __FUNCTION__);
return;
}
//Must set vcm_configure before playback
if(pcm_record_size != pcm_playback_size){
printf("%s: Please configure pcm_record_size = pcm_playback_size!!\n", __FUNCTION__);
return;
}
fd = open(path, O_WRONLY | O_CREAT);
if (fd < 0) {
printf("%s: error opening file %s!\n", __FUNCTION__, path);
return;
}
//leave enough room for header
lseek(fd, sizeof(struct wav_header), SEEK_SET);
//open the audiostub_ctl, prepare for record and playback
VCMInit();
//open record stream
rc = VoIP_ahw_dev_ubus->open_input_stream(VoIP_ahw_dev_ubus, 0,
VoIP_ahw_dev_ubus->get_supported_devices(VoIP_ahw_dev_ubus),
NULL, &stream_in, 0, 0, AUDIO_SOURCE_VOICE_CALL);
if (rc < 0) {
printf("%s: error opening input device. rc = %d!\n", __FUNCTION__, rc);
goto bad_stream;
}
//open playback stream
rc = VoIP_ahw_dev_ubus->open_output_stream(VoIP_ahw_dev_ubus, 0,
VoIP_ahw_dev_ubus->get_supported_devices(VoIP_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 pcmloopback %d bytes every 20ms!\n", __FUNCTION__, pcm_record_size);
go_on_pcmloopback = true;
while (go_on_pcmloopback) {
//record the needed format stream from the device.
//only read pcm stream, no send command.
len = stream_in->read(stream_in, buffer, pcm_record_size);
if (len < 0) {
printf("%s: error reading!\n", __FUNCTION__);
goto end_pcmloopback;
}
//printf("%s: record from Dev size is %d!\n", __FUNCTION__, len);
//
//TODO:send the above IP package to far-end
//
//for debug:save into file
rc = write(fd, buffer, len);
if (rc < 0) {
printf("%s: error writing to file!\n", __FUNCTION__);
goto end_pcmloopback;
} else if (rc < len) {
printf("%s: wrote less the buffer size!\n", __FUNCTION__);
}
//printf("%s: write to file len is %d.\n", __FUNCTION__, rc);
//playback the needed format stream to device.
//only write pcm stream, no send command.
rc = stream_out->write(stream_out, buffer, len);
if (rc < 0) {
printf("%s: error writing (child)!\n", __FUNCTION__);
goto end_pcmloopback;
} else if (rc < len) {
printf("%s: wrote less than buffer size!\n", __FUNCTION__);
goto end_pcmloopback;
}
// printf("%s: playback to Dev len is %d.\n", __FUNCTION__, rc);
printf("%s: No.%d frame loopback!\n", __FUNCTION__, ++frames);
}
end_pcmloopback:
//write header now all information is known
header.data_sz = pcm_record_size * frames;
header.riff_sz = header.data_sz + sizeof(header) - 8;
lseek(fd, 0, SEEK_SET);
write(fd, &header, sizeof(struct wav_header));
printf("%s: save wave file:%s. sample_rate = %d, num_channels = %d!\n", __FUNCTION__, path, header.sample_rate, header.num_channels);
stream_in->common.standby(&stream_in->common);
VoIP_ahw_dev_ubus->close_input_stream(VoIP_ahw_dev_ubus, stream_in);
stream_out->common.standby(&stream_out->common);
VoIP_ahw_dev_ubus->close_output_stream(VoIP_ahw_dev_ubus, stream_out);
VCMDeinit();//close the fd of audiostub_ctl when exit the thread.
go_on_pcmloopback = false;
bad_stream:
if (close(fd))
printf("%s: error closing file!\n", __FUNCTION__);
printf("%s: finished pcm loopback!\n", __FUNCTION__);
printf("exit VoIP_thread!\n");
return;
}
static void sig_handler(int sig)
{
go_on_pcmloopback = false;
printf("output signal number: %d.\n", sig);
}
/*******************************************************************************\
* Function: main
\*******************************************************************************/
int main()
{
pthread_t thread_start;
pthread_t thread_stop;
int rc = 0;
//use the VoIP device file.
//use test device node
config_VoIP_device(2);
/* install signal handler and begin to capture signal for close */
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
//init global variables
VoIP_ahw_dev_ubus = audio_hal_install();
if (VoIP_ahw_dev_ubus == NULL) {
printf("%s: audio_hal_install failed!\n", __FUNCTION__);
exit (-1);
}
//The following config parameters are needed for main thread.
//config record parameters.
config_parameters(1);
//config playback parameters.
config_parameters(0);
rc = pthread_create(&thread_start, NULL, (void *)&VoIP_thread, NULL);
if (rc < 0) {
printf("%s: error creating thread_start!\n", __FUNCTION__);
rc = UBUS_STATUS_PERMISSION_DENIED;
}
if (pthread_join(thread_start, NULL)){
printf("error join thread!\n");
abort();
}
audio_hal_uninstall();
return 0;
}