Debian Codegraph @

Xpjproject/pjsip-apps/src/samples/aectest.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20
21 /**
22 * \page page_pjmedia_samples_aectest_c Samples: AEC Test (aectest.c)
23 *
24 * Play a file to speaker, run AEC, and record the microphone input
25 * to see if echo is coming.
26 *
27 * This file is pjsip-apps/src/samples/aectest.c
28 *
29 * \includelineno aectest.c
30 */
31 #include <pjmedia.h>
32 #include <pjlib-util.h> /* pj_getopt */
33 #include <pjlib.h>
34
35 #define THIS_FILE "aectest.c"
36 #define PTIME 20
37 #define TAIL_LENGTH 200
38
39 static const char *desc =
40 " FILE \n"
41 " \n"
42 " aectest.c \n"
43 " \n"
44 " PURPOSE \n"
45 " \n"
46 " Test the AEC effectiveness. \n"
47 " \n"
48 " USAGE \n"
49 " \n"
50 " aectest [options] <PLAY.WAV> <REC.WAV> <OUTPUT.WAV> \n"
51 " \n"
52 " <PLAY.WAV> is the signal played to the speaker. \n"
53 " <REC.WAV> is the signal captured from the microphone. \n"
54 " <OUTPUT.WAV> is the output file to store the test result \n"
55 "\n"
56 " options:\n"
57 " -d The delay between playback and capture in ms, at least 25 ms.\n"
58 " Default is 25 ms. See note below. \n"
59 " -l Set the echo tail length in ms. Default is 200 ms \n"
60 " -r Set repeat count (default=1) \n"
61 " -a Algorithm: 0=default, 1=speex, 2=echo suppress, 3=WebRtc \n"
62 " 4=WebRtc-AEC3 \n"
63 " -i Interactive \n"
64 "\n"
65 " Note that for the AEC internal buffering mechanism, it is required\n"
66 " that the echoed signal (in REC.WAV) is delayed from the \n"
67 " corresponding reference signal (in PLAY.WAV) at least as much as \n"
68 " frame time + PJMEDIA_WSOLA_DELAY_MSEC. In this application, frame \n"
69 " time is 20 ms and default PJMEDIA_WSOLA_DELAY_MSEC is 5 ms, hence \n"
70 " 25 ms delay is the minimum value. \n";
71
72 /*
73 * Sample session:
74 *
75 * -d 100 -a 1 ../bin/orig8.wav ../bin/echo8.wav ../bin/result8.wav
76 */
77
78 static void app_perror(const char *sender, const char *title, pj_status_t st)
79 {
80 char errmsg[PJ_ERR_MSG_SIZE];
81
82 pj_strerror(st, errmsg, sizeof(errmsg));
83 PJ_LOG(3,(sender, "%s: %s", title, errmsg));
84 }
85
86
87 /*
88 * main()
89 */
90 int main(int argc, char *argv[])
91 {
92 pj_caching_pool cp;
93 pjmedia_endpt *med_endpt;
94 pj_pool_t *pool;
95 pjmedia_port *wav_play;
96 pjmedia_port *wav_rec;
97 pjmedia_port *wav_out;
98 pj_status_t status;
99 pjmedia_echo_state *ec;
100 pjmedia_frame play_frame, rec_frame;
101 unsigned opt = 0;
102 unsigned latency_ms = 25;
103 unsigned tail_ms = TAIL_LENGTH;
104 pj_timestamp t0, t1;
105 int i, repeat=1, interactive=0, c;
106
107 pj_optind = 0;
108 while ((c=pj_getopt(argc, argv, "d:l:a:r:i")) !=-1) {
109 switch (c) {
110 case 'd':
111 latency_ms = atoi(pj_optarg);
112 if (latency_ms < 25) {
113 puts("Invalid delay");
114 puts(desc);
115 }
116 break;
117 case 'l':
118 tail_ms = atoi(pj_optarg);
119 break;
120 case 'a':
121 {
122 int alg = atoi(pj_optarg);
123 switch (alg) {
124 case 0:
125 opt = 0;
126 break;
127 case 1:
128 opt = PJMEDIA_ECHO_SPEEX;
129 break;
130 case 2:
131 opt = PJMEDIA_ECHO_SIMPLE;
132 break;
133 case 3:
134 opt = PJMEDIA_ECHO_WEBRTC;
135 break;
136 case 4:
137 opt = PJMEDIA_ECHO_WEBRTC_AEC3;
138 break;
139 default:
140 puts("Invalid algorithm");
141 puts(desc);
142 return 1;
143 }
144 }
145 break;
146 case 'r':
147 repeat = atoi(pj_optarg);
148 if (repeat < 1) {
149 puts("Invalid repeat count");
150 puts(desc);
151 return 1;
152 }
153 break;
154 case 'i':
155 interactive = 1;
156 break;
157 }
158 }
159
160 if (argc - pj_optind != 3) {
161 puts("Error: missing argument(s)");
162 puts(desc);
163 return 1;
164 }
165
166 /* Must init PJLIB first: */
167 status = pj_init();
168 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
169
170 /* Must create a pool factory before we can allocate any memory. */
171 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
172
173 /*
174 * Initialize media endpoint.
175 * This will implicitly initialize PJMEDIA too.
176 */
177 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
178 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
179
180 /* Create memory pool for our file player */
181 pool = pj_pool_create( &cp.factory, /* pool factory */
182 "wav", /* pool name. */
183 4000, /* init size */
184 4000, /* increment size */
185 NULL /* callback on error */
186 );
187
188 /* Open wav_play */
189 status = pjmedia_wav_player_port_create(pool, argv[pj_optind], PTIME,
190 PJMEDIA_FILE_NO_LOOP, 0,
191 &wav_play);
192 if (status != PJ_SUCCESS) {
193 app_perror(THIS_FILE, "Error opening playback WAV file", status);
194 return 1;
195 }
196
197 /* Open recorded wav */
198 status = pjmedia_wav_player_port_create(pool, argv[pj_optind+1], PTIME,
199 PJMEDIA_FILE_NO_LOOP, 0,
200 &wav_rec);
201 if (status != PJ_SUCCESS) {
202 app_perror(THIS_FILE, "Error opening recorded WAV file", status);
203 return 1;
204 }
205
206 /* play and rec WAVs must have the same clock rate */
207 if (PJMEDIA_PIA_SRATE(&wav_play->info) != PJMEDIA_PIA_SRATE(&wav_rec->info)) {
208 puts("Error: clock rate mismatch in the WAV files");
209 return 1;
210 }
211
212 /* .. and channel count */
213 if (PJMEDIA_PIA_CCNT(&wav_play->info) != PJMEDIA_PIA_CCNT(&wav_rec->info)) {
214 puts("Error: clock rate mismatch in the WAV files");
215 return 1;
216 }
217
218 /* Create output wav */
219 status = pjmedia_wav_writer_port_create(pool, argv[pj_optind+2],
220 PJMEDIA_PIA_SRATE(&wav_play->info),
221 PJMEDIA_PIA_CCNT(&wav_play->info),
222 PJMEDIA_PIA_SPF(&wav_play->info),
223 PJMEDIA_PIA_BITS(&wav_play->info),
224 0, 0, &wav_out);
225 if (status != PJ_SUCCESS) {
226 app_perror(THIS_FILE, "Error opening output WAV file", status);
227 return 1;
228 }
229
230 /* Create echo canceller */
231 status = pjmedia_echo_create2(pool, PJMEDIA_PIA_SRATE(&wav_play->info),
232 PJMEDIA_PIA_CCNT(&wav_play->info),
233 PJMEDIA_PIA_SPF(&wav_play->info),
234 tail_ms, latency_ms,
235 opt, &ec);
236 if (status != PJ_SUCCESS) {
237 app_perror(THIS_FILE, "Error creating EC", status);
238 return 1;
239 }
240
241
242 /* Processing loop */
243 play_frame.buf = pj_pool_alloc(pool, PJMEDIA_PIA_SPF(&wav_play->info)<<1);
244 rec_frame.buf = pj_pool_alloc(pool, PJMEDIA_PIA_SPF(&wav_play->info)<<1);
245 pj_get_timestamp(&t0);
246 for (i=0; i < repeat; ++i) {
247 for (;;) {
248 play_frame.size = PJMEDIA_PIA_SPF(&wav_play->info) << 1;
249 status = pjmedia_port_get_frame(wav_play, &play_frame);
250 if (status != PJ_SUCCESS)
251 break;
252
253 status = pjmedia_echo_playback(ec, (short*)play_frame.buf);
254
255 rec_frame.size = PJMEDIA_PIA_SPF(&wav_play->info) << 1;
256 status = pjmedia_port_get_frame(wav_rec, &rec_frame);
257 if (status != PJ_SUCCESS)
258 break;
259
260 status = pjmedia_echo_capture(ec, (short*)rec_frame.buf, 0);
261 if (status != PJ_SUCCESS) {
262 app_perror(THIS_FILE, "pjmedia_echo_capture()", status);
263 break;
264 }
265
266 //status = pjmedia_echo_cancel(ec, (short*)rec_frame.buf,
267 // (short*)play_frame.buf, 0, NULL);
268
269 status = pjmedia_port_put_frame(wav_out, &rec_frame);
270 if (status != PJ_SUCCESS)
271 break;
272 }
273
274 pjmedia_wav_player_port_set_pos(wav_play, 0);
275 pjmedia_wav_player_port_set_pos(wav_rec, 0);
276 }
277 pj_get_timestamp(&t1);
278
279 i = (int)pjmedia_wav_writer_port_get_pos(wav_out) / sizeof(pj_int16_t) * 1000 /
280 (PJMEDIA_PIA_SRATE(&wav_out->info) * PJMEDIA_PIA_CCNT(&wav_out->info));
281 PJ_LOG(3,(THIS_FILE, "Processed %3d.%03ds audio",
282 i / 1000, i % 1000));
283 PJ_LOG(3,(THIS_FILE, "Completed in %u msec\n", pj_elapsed_msec(&t0, &t1)));
284
285 /* Destroy file port(s) */
286 status = pjmedia_port_destroy( wav_play );
287 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
288 status = pjmedia_port_destroy( wav_rec );
289 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
290 status = pjmedia_port_destroy( wav_out );
291 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
292
293 /* Destroy ec */
294 pjmedia_echo_destroy(ec);
295
296 /* Release application pool */
297 pj_pool_release( pool );
298
299 /* Destroy media endpoint. */
300 pjmedia_endpt_destroy( med_endpt );
301
302 /* Destroy pool factory */
303 pj_caching_pool_destroy( &cp );
304
305 /* Shutdown PJLIB */
306 pj_shutdown();
307
308 if (interactive) {
309 char s[10], *dummy;
310 puts("ENTER to quit");
311 dummy = fgets(s, sizeof(s), stdin);
312 PJ_UNUSED_ARG(dummy);
313 }
314
315 /* Done. */
316 return 0;
317 }
318