/*
|
* echo_audio_linein_test.c -- Audio line in test application
|
*
|
* Copyright (c) 2018 Rockchip Electronics Co. Ltd.
|
* Author: chad.ma <chad.ma@rock-chips.com>
|
*
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
* you may not use this file except in compliance with the License.
|
* You may obtain a copy of the License at
|
*
|
* http://www.apache.org/licenses/LICENSE-2.0
|
*
|
* Unless required by applicable law or agreed to in writing, software
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* See the License for the specific language governing permissions and
|
* limitations under the License.
|
*/
|
|
#include <stdio.h>
|
#include <malloc.h>
|
#include <unistd.h>
|
#include <stdlib.h>
|
#include <string.h>
|
#include <getopt.h>
|
#include <fcntl.h>
|
#include <ctype.h>
|
#include <errno.h>
|
#include <limits.h>
|
#include <time.h>
|
#include <locale.h>
|
#include <sys/unistd.h>
|
#include <sys/stat.h>
|
#include <sys/types.h>
|
#include <alsa/asoundlib.h>
|
#include <assert.h>
|
|
#include <stdbool.h>
|
#include <signal.h>
|
#include "audio_test.h"
|
|
#define LOG_TAG "audio_linein_test"
|
#include "common.h"
|
|
#if 0
|
snd_pcm_t *open_sound_dev(snd_pcm_stream_t type)
|
{
|
int err;
|
snd_pcm_t *handle;
|
snd_pcm_hw_params_t *hw_params;
|
unsigned int buffer_time;
|
unsigned int period_time;
|
unsigned int rate = 44100;
|
|
if ((err = snd_pcm_open (&handle, "hw:0,0", type, 0)) < 0) //´ò¿ªÉ豸£¬µÚ2¸ö²ÎÊýÊÇָʹÓÃĬÈ쵀 É豸Ãû³Æ
|
return NULL;
|
|
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) { //·ÖÅä²ÎÊý
|
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
|
snd_strerror (err));
|
return NULL;
|
}
|
|
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) { //³õʼ»¯²ÎÊý
|
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
|
snd_strerror (err));
|
return NULL;
|
}
|
|
if (snd_pcm_hw_params_get_buffer_time_max(hw_params, &buffer_time, 0) < 0) {
|
fprintf(stderr, "Error snd_pcm_hw_params_get_buffer_time_max\n");
|
log_err("Error snd_pcm_hw_params_get_buffer_time_max(%s)\n",snd_strerror (err));
|
}
|
|
if (buffer_time > 500000) buffer_time = 500000;
|
period_time = buffer_time / 4;
|
|
log_info("buff_time = %d, period_time =%d \n",buffer_time, period_time);
|
if (snd_pcm_hw_params_set_buffer_time_near(handle, hw_params, &buffer_time, 0) < 0) {
|
fprintf(stderr, "Error snd_pcm_hw_params_set_buffer_time_near\n");
|
log_err("Error snd_pcm_hw_params_set_buffer_time_near(%s)\n",snd_strerror (err));
|
|
return NULL;
|
}
|
|
if (snd_pcm_hw_params_set_period_time_near(handle, hw_params, &period_time, 0) < 0) {
|
fprintf(stderr, "Error snd_pcm_hw_params_set_buffer_time_near\n");
|
log_err("Error snd_pcm_hw_params_set_buffer_time_near(%s)\n",snd_strerror (err));
|
|
return NULL;
|
}
|
|
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { //ÉèÖòÎÊý£¬Êý¾Ý´æ·Å·½Ê½Êǽ»²æ´æ·ÅSND_PCM_ACCESS_RW_INTERLEAVED£¨ÏÈ·Å×óÉùµÀÔÙ·ÅÓÒÉùµÀ£©
|
fprintf (stderr, "cannot set access type (%s)\n",
|
snd_strerror (err));
|
log_err("cannot set access type (%s)\n", snd_strerror (err));
|
return NULL;
|
}
|
|
if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
|
fprintf (stderr, "cannot set sample format (%s)\n", //ÉèÖøñʽSND_PCM_FORMAT_S16_LEÊÇ16λµÄ
|
snd_strerror (err));
|
log_err("cannot set sample format (%s)\n", snd_strerror (err));
|
return NULL;
|
}
|
|
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &rate, 0)) < 0) { //ÉèÖòÉÑùÂÊ
|
fprintf (stderr, "cannot set sample rate (%s)\n",
|
snd_strerror (err));
|
log_err("cannot set sample rate (%s)\n", snd_strerror (err));
|
return NULL;
|
}
|
|
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, 2)) < 0) { //ÉèÖÃͨµÀÊý
|
fprintf (stderr, "cannot set channel count (%s)\n",
|
snd_strerror (err));
|
log_err("cannot set channel count (%s)\n",snd_strerror (err));
|
return NULL;
|
}
|
|
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) { //°Ñ¹¹ÔìµÄ½á¹¹ÌåµÄÊý¾Ýдµ½Ó²¼þÉÏÈ¥
|
fprintf (stderr, "cannot set parameters (%s)\n",
|
snd_strerror (err));
|
log_err("cannot set parameters (%s)\n",snd_strerror (err));
|
return NULL;
|
}
|
|
snd_pcm_hw_params_free (hw_params); //ÊͷꬲÎÊýµÄ½á¹¹Ìå
|
|
return handle; //³É¹¦Ê±·µ»Ø¾ä±ú
|
}
|
|
|
void close_sound_dev(snd_pcm_t *handle) //¹Ø±ÕÉùÒôÉ豸
|
{
|
snd_pcm_close (handle);
|
}
|
|
snd_pcm_t *open_playback(void) //´ò¿ª²¥·ÅÉ豸
|
{
|
return open_sound_dev(SND_PCM_STREAM_PLAYBACK);
|
}
|
|
snd_pcm_t *open_capture(void) //´ò¿ªÂ¼ÒôÉ豸
|
{
|
return open_sound_dev(SND_PCM_STREAM_CAPTURE);
|
}
|
|
int main (int argc, char *argv[])
|
{
|
int err;
|
char buf[512];
|
snd_pcm_t *playback_handle;
|
snd_pcm_t *capture_handle;
|
playback_handle = open_playback();// ´ò¿ª²¥·ÅÉ豸
|
|
if (!playback_handle) {
|
fprintf (stderr, "cannot open for playback\n");
|
log_err("cannot open for playback\n");
|
return -1;
|
}
|
|
capture_handle = open_capture();//´ò¿ªÂ¼ÒôÉ豸
|
|
if (!capture_handle) {
|
fprintf (stderr, "cannot open for capture\n");
|
log_err("cannot open for capture\n");
|
return -1;
|
}
|
|
if ((err = snd_pcm_prepare (playback_handle)) < 0) { //×¼±¸²¥·Å²Ù×÷
|
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
|
snd_strerror (err));
|
log_err("cannot prepare audio interface for use (%s)\n",snd_strerror (err));
|
return -1;
|
}
|
|
if ((err = snd_pcm_prepare (capture_handle)) < 0) { //×¼±¸Â¼Òô²Ù×÷
|
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
|
snd_strerror (err));
|
log_err("cannot prepare audio interface for use (%s)\n",snd_strerror (err));
|
return -1;
|
}
|
|
while (1) {
|
if ((err = snd_pcm_readi (capture_handle, buf, 128)) != 128) { //¶Á²Ù×÷£¨°ÑÊý¾Ý¶Áµ½buffer£©
|
fprintf (stderr, "read from audio interface failed (%s)\n",
|
snd_strerror (err));
|
log_err("read from audio interface failed (%s)\n",snd_strerror (err));
|
return -1;
|
}
|
|
if ((err = snd_pcm_writei (playback_handle, buf, 128)) != 128) { //д²Ù×÷(°ÑÊý¾Ýдµ½buffer)
|
fprintf (stderr, "write to audio interface failed (%s)\n",
|
snd_strerror (err));
|
log_err("write to audio interface failed (%s)\n",snd_strerror (err));
|
return -1;
|
}
|
}
|
|
snd_pcm_close (playback_handle);//¹Ø±Õ²¥·ÅÉ豸
|
snd_pcm_close (capture_handle);//¹Ø±Õ¼ÒôÉ豸
|
return 0;
|
}
|
#endif
|
|
#define LINEIN_TIME 20
|
|
static bool gIslinein = false;
|
|
int switch_to_linein(bool sw_to_linein)
|
{
|
int ret = 0;
|
int status = 0;
|
|
if (sw_to_linein) {
|
log_info("-----------> swtich to line in <-----------\n");
|
status = system("/tmp/switch_inoutput.sh li");
|
} else {
|
log_info("-----------> swtich to mic in <-----------\n");
|
status = system("/tmp/switch_inoutput.sh mi");
|
}
|
|
if (status == -1) {
|
log_info("system cmd run error...\n");
|
ret = -1;
|
}
|
|
return ret;
|
}
|
|
//*Audio line in test
|
void *audio_line_in_test(void *argv)
|
{
|
char cmd[128];
|
|
fprintf(stderr,"=========function :%s start=============\n",__func__);
|
|
gIslinein = true;
|
switch_to_linein(gIslinein);
|
|
//Ö¸¶¨Card 0£¬device 0£¬Â¼Òô10Ãë
|
fprintf(stderr,"recording\n ");
|
sprintf(cmd,"arecord -D hw:0,0 -f S16_LE -c 2 -r 44100 -d %d %s/%s", \
|
LINEIN_TIME,TEST_RESULT_SAVE_PATH,AUDIO_LINEIN_FILE);
|
system(cmd);
|
|
fprintf(stderr,"playing\n ");
|
sprintf(cmd,"aplay -f S16_LE -c 2 -r 44100 -d %d %s/%s", \
|
LINEIN_TIME,TEST_RESULT_SAVE_PATH,AUDIO_LINEIN_FILE); //·ÅÒô
|
system(cmd);
|
|
//remove line-in test file
|
sprintf(cmd,"rm %s/%s",TEST_RESULT_SAVE_PATH,AUDIO_LINEIN_FILE);
|
system(cmd);
|
fprintf(stderr,"=========Audio Line in test finished=============\n");
|
}
|
|
//* ÐźŴ¦Àíº¯Êý£¬ÔÚ½áÊø½ø³Ìǰ£¬É¾³ý¼ÒôµÄpcmÎļþ
|
static int del_line_in_pcm(int sign_no)
|
{
|
char cmd[128];
|
|
printf("====================function : %s start =================\n",__func__);
|
|
sprintf(cmd,"%s/%s",TEST_RESULT_SAVE_PATH,AUDIO_LINEIN_FILE);
|
if(0 == access(cmd,F_OK)){
|
remove(cmd);
|
}
|
|
printf("====================function : %s finished =================\n",__func__);
|
exit(0);
|
}
|
|
|
|
/*Ö÷º¯ÊýÈë¿Ú*/
|
int main(int argc, char *argv[])
|
{
|
int delay_t = 0,err_code = 0;
|
struct timeval t1, t2;
|
char buf[COMMAND_VALUESIZE] = "audio_linein_test";
|
char result[COMMAND_VALUESIZE] = RESULT_PASS;
|
|
system("amixer cset name='Master Playback Volume' 80%");
|
log_info("audio record test start...\n");
|
|
//* ×¢²áÐźŴ¦Àíº¯Êý
|
signal(SIGTERM, (__sighandler_t)del_line_in_pcm);
|
|
gettimeofday(&t1, NULL);
|
while(1)
|
{
|
audio_line_in_test(argv[0]);
|
gettimeofday(&t2, NULL);
|
delay_t = (t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec);
|
if (delay_t > MANUAL_TEST_TIMEOUT) {
|
log_warn("audio record test end, timeout 60s\n");
|
err_code = AUDIO_LINEIN_TIMEOUT;
|
//break;
|
}
|
}
|
|
if (gIslinein) {
|
gIslinein = false;
|
switch_to_linein(gIslinein); //acodec switch to mic-->input
|
}
|
|
if (err_code)
|
strcpy(result, RESULT_FAIL);
|
send_msg_to_server(buf, result, err_code);
|
return 0;
|
}
|
|