blob: 773f7c9486184f22f3c7da020db61da374928bf0 [file] [log] [blame]
Benny Prijono69f73122006-08-04 11:08:00 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono69f73122006-08-04 11:08:00 +00004 *
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
Benny Prijono95250c72008-08-09 05:40:22 +000035#define THIS_FILE "aectest.c"
Benny Prijono69f73122006-08-04 11:08:00 +000036#define PTIME 20
Benny Prijono95250c72008-08-09 05:40:22 +000037#define TAIL_LENGTH 200
Benny Prijono69f73122006-08-04 11:08:00 +000038
39static 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"
Benny Prijono95250c72008-08-09 05:40:22 +000050" aectest [options] <PLAY.WAV> <REC.WAV> <OUTPUT.WAV> \n"
Benny Prijono69f73122006-08-04 11:08:00 +000051" \n"
Benny Prijono95250c72008-08-09 05:40:22 +000052" <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. Default is zero.\n"
58" -l Set the echo tail length in ms. Default is 200 ms \n"
Benny Prijonoa7908d72008-08-10 16:15:14 +000059" -r Set repeat count (default=1) \n"
Benny Prijono95250c72008-08-09 05:40:22 +000060" -a Algorithm: 0=default, 1=speex, 3=echo suppress \n";
Benny Prijono69f73122006-08-04 11:08:00 +000061
Benny Prijono95250c72008-08-09 05:40:22 +000062/*
63 * Sample session:
64 *
65 * -d 100 -a 1 ../bin/orig8.wav ../bin/echo8.wav ../bin/result8.wav
66 */
Benny Prijono69f73122006-08-04 11:08:00 +000067
68static void app_perror(const char *sender, const char *title, pj_status_t st)
69{
70 char errmsg[PJ_ERR_MSG_SIZE];
71
72 pj_strerror(st, errmsg, sizeof(errmsg));
73 PJ_LOG(3,(sender, "%s: %s", title, errmsg));
74}
75
76
77/*
78 * main()
79 */
80int main(int argc, char *argv[])
81{
82 pj_caching_pool cp;
83 pjmedia_endpt *med_endpt;
84 pj_pool_t *pool;
Benny Prijono95250c72008-08-09 05:40:22 +000085 pjmedia_port *wav_play;
86 pjmedia_port *wav_rec;
87 pjmedia_port *wav_out;
Benny Prijono69f73122006-08-04 11:08:00 +000088 pj_status_t status;
Benny Prijono95250c72008-08-09 05:40:22 +000089 pjmedia_echo_state *ec;
90 pjmedia_frame play_frame, rec_frame;
91 unsigned opt = 0;
92 unsigned latency_ms = 0;
93 unsigned tail_ms = TAIL_LENGTH;
94 pj_timestamp t0, t1;
Benny Prijonoa7908d72008-08-10 16:15:14 +000095 int i, repeat=1, c;
Benny Prijono69f73122006-08-04 11:08:00 +000096
Benny Prijono95250c72008-08-09 05:40:22 +000097 pj_optind = 0;
Benny Prijonoa7908d72008-08-10 16:15:14 +000098 while ((c=pj_getopt(argc, argv, "d:l:a:r:")) !=-1) {
Benny Prijono95250c72008-08-09 05:40:22 +000099 switch (c) {
100 case 'd':
101 latency_ms = atoi(pj_optarg);
102 break;
103 case 'l':
104 tail_ms = atoi(pj_optarg);
105 break;
106 case 'a':
107 {
108 int alg = atoi(pj_optarg);
109 switch (alg) {
110 case 0:
111 opt = 0;
112 case 1:
113 opt = PJMEDIA_ECHO_SPEEX;
114 break;
115 case 3:
116 opt = PJMEDIA_ECHO_SIMPLE;
117 break;
118 default:
119 puts("Invalid algorithm");
120 puts(desc);
121 return 1;
122 }
123 }
124 break;
Benny Prijonoa7908d72008-08-10 16:15:14 +0000125 case 'r':
126 repeat = atoi(pj_optarg);
127 if (repeat < 1) {
128 puts("Invalid algorithm");
129 puts(desc);
130 return 1;
131 }
132 break;
Benny Prijono95250c72008-08-09 05:40:22 +0000133 }
134 }
Benny Prijono69f73122006-08-04 11:08:00 +0000135
Benny Prijono95250c72008-08-09 05:40:22 +0000136 if (argc - pj_optind != 3) {
137 puts("Error: missing argument(s)");
Benny Prijono69f73122006-08-04 11:08:00 +0000138 puts(desc);
139 return 1;
140 }
141
Benny Prijono69f73122006-08-04 11:08:00 +0000142 /* Must init PJLIB first: */
143 status = pj_init();
144 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
145
146 /* Must create a pool factory before we can allocate any memory. */
147 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
148
149 /*
150 * Initialize media endpoint.
151 * This will implicitly initialize PJMEDIA too.
152 */
153 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
154 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
155
156 /* Create memory pool for our file player */
157 pool = pj_pool_create( &cp.factory, /* pool factory */
158 "wav", /* pool name. */
159 4000, /* init size */
160 4000, /* increment size */
161 NULL /* callback on error */
162 );
163
Benny Prijono95250c72008-08-09 05:40:22 +0000164 /* Open wav_play */
165 status = pjmedia_wav_player_port_create(pool, argv[pj_optind], PTIME,
166 PJMEDIA_FILE_NO_LOOP, 0,
167 &wav_play);
Benny Prijono69f73122006-08-04 11:08:00 +0000168 if (status != PJ_SUCCESS) {
Benny Prijono95250c72008-08-09 05:40:22 +0000169 app_perror(THIS_FILE, "Error opening playback WAV file", status);
Benny Prijono69f73122006-08-04 11:08:00 +0000170 return 1;
171 }
Benny Prijono69f73122006-08-04 11:08:00 +0000172
Benny Prijono95250c72008-08-09 05:40:22 +0000173 /* Open recorded wav */
174 status = pjmedia_wav_player_port_create(pool, argv[pj_optind+1], PTIME,
175 PJMEDIA_FILE_NO_LOOP, 0,
176 &wav_rec);
177 if (status != PJ_SUCCESS) {
178 app_perror(THIS_FILE, "Error opening recorded WAV file", status);
179 return 1;
180 }
Benny Prijono69f73122006-08-04 11:08:00 +0000181
Benny Prijono95250c72008-08-09 05:40:22 +0000182 /* play and rec WAVs must have the same clock rate */
183 if (wav_play->info.clock_rate != wav_rec->info.clock_rate) {
184 puts("Error: clock rate mismatch in the WAV files");
185 return 1;
186 }
Benny Prijono69f73122006-08-04 11:08:00 +0000187
Benny Prijono95250c72008-08-09 05:40:22 +0000188 /* .. and channel count */
189 if (wav_play->info.channel_count != wav_rec->info.channel_count) {
190 puts("Error: clock rate mismatch in the WAV files");
191 return 1;
192 }
193
194 /* Create output wav */
195 status = pjmedia_wav_writer_port_create(pool, argv[pj_optind+2],
196 wav_play->info.clock_rate,
197 wav_play->info.channel_count,
198 wav_play->info.samples_per_frame,
199 wav_play->info.bits_per_sample,
200 0, 0, &wav_out);
201 if (status != PJ_SUCCESS) {
202 app_perror(THIS_FILE, "Error opening output WAV file", status);
203 return 1;
204 }
205
206 /* Create echo canceller */
207 status = pjmedia_echo_create2(pool, wav_play->info.clock_rate,
208 wav_play->info.channel_count,
209 wav_play->info.samples_per_frame,
210 tail_ms, latency_ms,
211 opt, &ec);
212 if (status != PJ_SUCCESS) {
213 app_perror(THIS_FILE, "Error creating EC", status);
214 return 1;
215 }
216
217
218 /* Processing loop */
219 play_frame.buf = pj_pool_alloc(pool, wav_play->info.samples_per_frame<<1);
220 rec_frame.buf = pj_pool_alloc(pool, wav_play->info.samples_per_frame<<1);
221 pj_get_timestamp(&t0);
Benny Prijonoa7908d72008-08-10 16:15:14 +0000222 for (i=0; i < repeat; ++i) {
223 for (;;) {
224 play_frame.size = wav_play->info.samples_per_frame << 1;
225 status = pjmedia_port_get_frame(wav_play, &play_frame);
226 if (status != PJ_SUCCESS)
227 break;
Benny Prijono95250c72008-08-09 05:40:22 +0000228
Benny Prijonoa7908d72008-08-10 16:15:14 +0000229 status = pjmedia_echo_playback(ec, (short*)play_frame.buf);
Benny Prijono95250c72008-08-09 05:40:22 +0000230
Benny Prijonoa7908d72008-08-10 16:15:14 +0000231 rec_frame.size = wav_play->info.samples_per_frame << 1;
232 status = pjmedia_port_get_frame(wav_rec, &rec_frame);
233 if (status != PJ_SUCCESS)
234 break;
Benny Prijono95250c72008-08-09 05:40:22 +0000235
Benny Prijonoa7908d72008-08-10 16:15:14 +0000236 status = pjmedia_echo_capture(ec, (short*)rec_frame.buf, 0);
Benny Prijono95250c72008-08-09 05:40:22 +0000237
Benny Prijonoa7908d72008-08-10 16:15:14 +0000238 //status = pjmedia_echo_cancel(ec, (short*)rec_frame.buf,
239 // (short*)play_frame.buf, 0, NULL);
Benny Prijono95250c72008-08-09 05:40:22 +0000240
Benny Prijonoa7908d72008-08-10 16:15:14 +0000241 pjmedia_port_put_frame(wav_out, &rec_frame);
242 }
243
244 pjmedia_wav_player_port_set_pos(wav_play, 0);
245 pjmedia_wav_player_port_set_pos(wav_rec, 0);
Benny Prijono95250c72008-08-09 05:40:22 +0000246 }
247 pj_get_timestamp(&t1);
248
249 PJ_LOG(3,(THIS_FILE, "Completed in %u msec\n", pj_elapsed_msec(&t0, &t1)));
Benny Prijono69f73122006-08-04 11:08:00 +0000250
251 /* Destroy file port(s) */
Benny Prijono95250c72008-08-09 05:40:22 +0000252 status = pjmedia_port_destroy( wav_play );
Benny Prijono69f73122006-08-04 11:08:00 +0000253 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
Benny Prijono95250c72008-08-09 05:40:22 +0000254 status = pjmedia_port_destroy( wav_rec );
255 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
256 status = pjmedia_port_destroy( wav_out );
Benny Prijono69f73122006-08-04 11:08:00 +0000257 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
258
Benny Prijono95250c72008-08-09 05:40:22 +0000259 /* Destroy ec */
260 pjmedia_echo_destroy(ec);
Benny Prijono69f73122006-08-04 11:08:00 +0000261
262 /* Release application pool */
263 pj_pool_release( pool );
264
265 /* Destroy media endpoint. */
266 pjmedia_endpt_destroy( med_endpt );
267
268 /* Destroy pool factory */
269 pj_caching_pool_destroy( &cp );
270
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000271 /* Shutdown PJLIB */
272 pj_shutdown();
Benny Prijono69f73122006-08-04 11:08:00 +0000273
Benny Prijonoa7908d72008-08-10 16:15:14 +0000274#if 0
275 {
276 char s[10];
277 puts("ENTER to quit");
278 fgets(s, sizeof(s), stdin);
279 }
280#endif
281
Benny Prijono69f73122006-08-04 11:08:00 +0000282 /* Done. */
283 return 0;
284}
285