hc
2025-02-14 bbb9540dc49f70f6b703d1c8d1b85fa5f602d86e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 *  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;
}