xref: /OK3568_Linux_fs/external/rk_pcba_test/echo_audio_linein_test.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  *  echo_audio_linein_test.c  --  Audio line in test application
3  *
4  *  Copyright (c) 2018 Rockchip Electronics Co. Ltd.
5  *  Author: chad.ma <chad.ma@rock-chips.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #include <stdio.h>
21 #include <malloc.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <getopt.h>
26 #include <fcntl.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <limits.h>
30 #include <time.h>
31 #include <locale.h>
32 #include <sys/unistd.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <alsa/asoundlib.h>
36 #include <assert.h>
37 
38 #include <stdbool.h>
39 #include <signal.h>
40 #include "audio_test.h"
41 
42 #define LOG_TAG "audio_linein_test"
43 #include "common.h"
44 
45 #if 0
46 snd_pcm_t *open_sound_dev(snd_pcm_stream_t type)
47 {
48     int err;
49     snd_pcm_t *handle;
50     snd_pcm_hw_params_t *hw_params;
51     unsigned int buffer_time;
52     unsigned int period_time;
53     unsigned int rate = 44100;
54 
55     if ((err = snd_pcm_open (&handle, "hw:0,0", type, 0)) < 0)     //���豸����2��������ָʹ��Ĭ�ϵ� �豸����
56         return NULL;
57 
58     if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {  //�������
59         fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
60                  snd_strerror (err));
61         return NULL;
62     }
63 
64     if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {  //��ʼ������
65         fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
66                  snd_strerror (err));
67         return NULL;
68     }
69 
70     if (snd_pcm_hw_params_get_buffer_time_max(hw_params, &buffer_time, 0) < 0) {
71         fprintf(stderr, "Error snd_pcm_hw_params_get_buffer_time_max\n");
72         log_err("Error snd_pcm_hw_params_get_buffer_time_max(%s)\n",snd_strerror (err));
73     }
74 
75     if (buffer_time > 500000)  buffer_time = 500000;
76     period_time = buffer_time / 4;
77 
78     log_info("buff_time = %d, period_time =%d \n",buffer_time, period_time);
79     if (snd_pcm_hw_params_set_buffer_time_near(handle, hw_params, &buffer_time, 0) < 0) {
80         fprintf(stderr, "Error snd_pcm_hw_params_set_buffer_time_near\n");
81         log_err("Error snd_pcm_hw_params_set_buffer_time_near(%s)\n",snd_strerror (err));
82 
83         return NULL;
84     }
85 
86     if (snd_pcm_hw_params_set_period_time_near(handle, hw_params, &period_time, 0) < 0) {
87         fprintf(stderr, "Error snd_pcm_hw_params_set_buffer_time_near\n");
88         log_err("Error snd_pcm_hw_params_set_buffer_time_near(%s)\n",snd_strerror (err));
89 
90         return NULL;
91     }
92 
93     if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { //���ò��������ݴ�ŷ�ʽ�ǽ�����SND_PCM_ACCESS_RW_INTERLEAVED���ȷ��������ٷ���������
94         fprintf (stderr, "cannot set access type (%s)\n",
95                  snd_strerror (err));
96         log_err("cannot set access type (%s)\n", snd_strerror (err));
97         return NULL;
98     }
99 
100     if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
101         fprintf (stderr, "cannot set sample format (%s)\n", //���ø�ʽSND_PCM_FORMAT_S16_LE��16λ��
102                  snd_strerror (err));
103         log_err("cannot set sample format (%s)\n", snd_strerror (err));
104         return NULL;
105     }
106 
107     if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &rate, 0)) < 0) { //���ò�����
108         fprintf (stderr, "cannot set sample rate (%s)\n",
109                  snd_strerror (err));
110         log_err("cannot set sample rate (%s)\n", snd_strerror (err));
111         return NULL;
112     }
113 
114     if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, 2)) < 0) { //����ͨ����
115         fprintf (stderr, "cannot set channel count (%s)\n",
116                  snd_strerror (err));
117         log_err("cannot set channel count (%s)\n",snd_strerror (err));
118         return NULL;
119     }
120 
121     if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) { //�ѹ���Ľṹ�������д��Ӳ����ȥ
122         fprintf (stderr, "cannot set parameters (%s)\n",
123                  snd_strerror (err));
124         log_err("cannot set parameters (%s)\n",snd_strerror (err));
125         return NULL;
126     }
127 
128     snd_pcm_hw_params_free (hw_params); //�ͷź������Ľṹ��
129 
130     return handle;  //�ɹ�ʱ���ؾ��
131 }
132 
133 
134 void close_sound_dev(snd_pcm_t *handle) //�ر������豸
135 {
136     snd_pcm_close (handle);
137 }
138 
139 snd_pcm_t *open_playback(void) //�������豸
140 {
141     return open_sound_dev(SND_PCM_STREAM_PLAYBACK);
142 }
143 
144 snd_pcm_t *open_capture(void) //��¼���豸
145 {
146     return open_sound_dev(SND_PCM_STREAM_CAPTURE);
147 }
148 
149 int main (int argc, char *argv[])
150 {
151     int err;
152     char buf[512];
153     snd_pcm_t *playback_handle;
154     snd_pcm_t *capture_handle;
155     playback_handle = open_playback();// �������豸
156 
157     if (!playback_handle) {
158         fprintf (stderr, "cannot open for playback\n");
159         log_err("cannot open for playback\n");
160         return -1;
161     }
162 
163     capture_handle = open_capture();//��¼���豸
164 
165     if (!capture_handle) {
166         fprintf (stderr, "cannot open for capture\n");
167         log_err("cannot open for capture\n");
168         return -1;
169     }
170 
171     if ((err = snd_pcm_prepare (playback_handle)) < 0) { //׼�����Ų���
172         fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
173                  snd_strerror (err));
174         log_err("cannot prepare audio interface for use (%s)\n",snd_strerror (err));
175         return -1;
176     }
177 
178     if ((err = snd_pcm_prepare (capture_handle)) < 0) { //׼��¼������
179         fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
180                  snd_strerror (err));
181         log_err("cannot prepare audio interface for use (%s)\n",snd_strerror (err));
182         return -1;
183     }
184 
185     while (1) {
186         if ((err = snd_pcm_readi (capture_handle, buf, 128)) != 128) { //�������������ݶ���buffer��
187             fprintf (stderr, "read from audio interface failed (%s)\n",
188                      snd_strerror (err));
189             log_err("read from audio interface failed (%s)\n",snd_strerror (err));
190             return -1;
191         }
192 
193         if ((err = snd_pcm_writei (playback_handle, buf, 128)) != 128) { //д����(������д��buffer)
194             fprintf (stderr, "write to audio interface failed (%s)\n",
195                      snd_strerror (err));
196             log_err("write to audio interface failed (%s)\n",snd_strerror (err));
197             return -1;
198         }
199     }
200 
201     snd_pcm_close (playback_handle);//�رղ����豸
202     snd_pcm_close (capture_handle);//�ر�¼���豸
203     return 0;
204 }
205 #endif
206 
207 #define LINEIN_TIME 20
208 
209 static bool gIslinein = false;
210 
switch_to_linein(bool sw_to_linein)211 int switch_to_linein(bool sw_to_linein)
212 {
213     int ret = 0;
214     int status = 0;
215 
216     if (sw_to_linein) {
217         log_info("-----------> swtich to line in <-----------\n");
218         status = system("/tmp/switch_inoutput.sh li");
219     } else {
220         log_info("-----------> swtich to mic in <-----------\n");
221         status = system("/tmp/switch_inoutput.sh mi");
222     }
223 
224     if (status == -1) {
225         log_info("system cmd run error...\n");
226         ret = -1;
227     }
228 
229     return ret;
230 }
231 
232 //*Audio line in test
audio_line_in_test(void * argv)233 void *audio_line_in_test(void *argv)
234 {
235     char cmd[128];
236 
237     fprintf(stderr,"=========function :%s start=============\n",__func__);
238 
239     gIslinein = true;
240     switch_to_linein(gIslinein);
241 
242     //ָ��Card 0��device 0��¼��10��
243     fprintf(stderr,"recording\n ");
244     sprintf(cmd,"arecord -D hw:0,0 -f S16_LE -c 2 -r 44100 -d %d %s/%s", \
245                 LINEIN_TIME,TEST_RESULT_SAVE_PATH,AUDIO_LINEIN_FILE);
246     system(cmd);
247 
248     fprintf(stderr,"playing\n ");
249     sprintf(cmd,"aplay -f S16_LE -c 2 -r 44100 -d %d %s/%s", \
250                 LINEIN_TIME,TEST_RESULT_SAVE_PATH,AUDIO_LINEIN_FILE); //����
251     system(cmd);
252 
253     //remove line-in test file
254     sprintf(cmd,"rm %s/%s",TEST_RESULT_SAVE_PATH,AUDIO_LINEIN_FILE);
255     system(cmd);
256     fprintf(stderr,"=========Audio Line in test finished=============\n");
257 }
258 
259 //* �źŴ��������ڽ�������ǰ��ɾ��¼����pcm�ļ�
del_line_in_pcm(int sign_no)260 static int del_line_in_pcm(int sign_no)
261 {
262     char cmd[128];
263 
264     printf("====================function : %s start =================\n",__func__);
265 
266     sprintf(cmd,"%s/%s",TEST_RESULT_SAVE_PATH,AUDIO_LINEIN_FILE);
267     if(0 == access(cmd,F_OK)){
268        remove(cmd);
269     }
270 
271     printf("====================function : %s finished =================\n",__func__);
272     exit(0);
273 }
274 
275 
276 
277 /*���������*/
main(int argc,char * argv[])278 int main(int argc, char *argv[])
279 {
280     int delay_t = 0,err_code = 0;
281 	struct timeval t1, t2;
282 	char buf[COMMAND_VALUESIZE] = "audio_linein_test";
283     char result[COMMAND_VALUESIZE] = RESULT_PASS;
284 
285     system("amixer cset name='Master Playback Volume' 80%");
286 	log_info("audio record test start...\n");
287 
288 	//* ע���źŴ�����
289 	signal(SIGTERM, (__sighandler_t)del_line_in_pcm);
290 
291 	gettimeofday(&t1, NULL);
292     while(1)
293     {
294         audio_line_in_test(argv[0]);
295         gettimeofday(&t2, NULL);
296 		delay_t = (t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec);
297 		if (delay_t > MANUAL_TEST_TIMEOUT) {
298 			log_warn("audio record test end, timeout 60s\n");
299 			err_code = AUDIO_LINEIN_TIMEOUT;
300 			//break;
301 		}
302     }
303 
304     if (gIslinein) {
305         gIslinein = false;
306         switch_to_linein(gIslinein);    //acodec switch to mic-->input
307     }
308 
309     if (err_code)
310 		strcpy(result, RESULT_FAIL);
311     send_msg_to_server(buf, result, err_code);
312     return 0;
313 }
314 
315