blob: 09a745fd68d9101fd4457345182186467087d05f [file] [log] [blame]
Benny Prijono69f73122006-08-04 11:08:00 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono69f73122006-08-04 11:08:00 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21
22/**
23 * \page page_pjmedia_samples_aectest_c Samples: AEC Test (aectest.c)
24 *
25 * Play a file to speaker, run AEC, and record the microphone input
26 * to see if echo is coming.
27 *
28 * This file is pjsip-apps/src/samples/aectest.c
29 *
30 * \includelineno aectest.c
31 */
32#include <pjmedia.h>
33#include <pjlib-util.h> /* pj_getopt */
34#include <pjlib.h>
35
Benny Prijono95250c72008-08-09 05:40:22 +000036#define THIS_FILE "aectest.c"
Benny Prijono69f73122006-08-04 11:08:00 +000037#define PTIME 20
Benny Prijono95250c72008-08-09 05:40:22 +000038#define TAIL_LENGTH 200
Benny Prijono69f73122006-08-04 11:08:00 +000039
40static const char *desc =
41" FILE \n"
42" \n"
43" aectest.c \n"
44" \n"
45" PURPOSE \n"
46" \n"
47" Test the AEC effectiveness. \n"
48" \n"
49" USAGE \n"
50" \n"
Benny Prijono95250c72008-08-09 05:40:22 +000051" aectest [options] <PLAY.WAV> <REC.WAV> <OUTPUT.WAV> \n"
Benny Prijono69f73122006-08-04 11:08:00 +000052" \n"
Benny Prijono95250c72008-08-09 05:40:22 +000053" <PLAY.WAV> is the signal played to the speaker. \n"
54" <REC.WAV> is the signal captured from the microphone. \n"
55" <OUTPUT.WAV> is the output file to store the test result \n"
56"\n"
57" options:\n"
58" -d The delay between playback and capture in ms. Default is zero.\n"
59" -l Set the echo tail length in ms. Default is 200 ms \n"
Benny Prijonoa7908d72008-08-10 16:15:14 +000060" -r Set repeat count (default=1) \n"
Benny Prijono6a237632008-08-13 13:53:18 +000061" -a Algorithm: 0=default, 1=speex, 3=echo suppress \n"
62" -i Interactive \n";
Benny Prijono69f73122006-08-04 11:08:00 +000063
Benny Prijono95250c72008-08-09 05:40:22 +000064/*
65 * Sample session:
66 *
67 * -d 100 -a 1 ../bin/orig8.wav ../bin/echo8.wav ../bin/result8.wav
68 */
Benny Prijono69f73122006-08-04 11:08:00 +000069
70static void app_perror(const char *sender, const char *title, pj_status_t st)
71{
72 char errmsg[PJ_ERR_MSG_SIZE];
73
74 pj_strerror(st, errmsg, sizeof(errmsg));
75 PJ_LOG(3,(sender, "%s: %s", title, errmsg));
76}
77
78
79/*
80 * main()
81 */
82int main(int argc, char *argv[])
83{
84 pj_caching_pool cp;
85 pjmedia_endpt *med_endpt;
86 pj_pool_t *pool;
Benny Prijono95250c72008-08-09 05:40:22 +000087 pjmedia_port *wav_play;
88 pjmedia_port *wav_rec;
89 pjmedia_port *wav_out;
Benny Prijono69f73122006-08-04 11:08:00 +000090 pj_status_t status;
Benny Prijono95250c72008-08-09 05:40:22 +000091 pjmedia_echo_state *ec;
92 pjmedia_frame play_frame, rec_frame;
93 unsigned opt = 0;
94 unsigned latency_ms = 0;
95 unsigned tail_ms = TAIL_LENGTH;
96 pj_timestamp t0, t1;
Benny Prijono6a237632008-08-13 13:53:18 +000097 int i, repeat=1, interactive=0, c;
Benny Prijono69f73122006-08-04 11:08:00 +000098
Benny Prijono95250c72008-08-09 05:40:22 +000099 pj_optind = 0;
Benny Prijono6a237632008-08-13 13:53:18 +0000100 while ((c=pj_getopt(argc, argv, "d:l:a:r:i")) !=-1) {
Benny Prijono95250c72008-08-09 05:40:22 +0000101 switch (c) {
102 case 'd':
103 latency_ms = atoi(pj_optarg);
104 break;
105 case 'l':
106 tail_ms = atoi(pj_optarg);
107 break;
108 case 'a':
109 {
110 int alg = atoi(pj_optarg);
111 switch (alg) {
112 case 0:
113 opt = 0;
114 case 1:
115 opt = PJMEDIA_ECHO_SPEEX;
116 break;
117 case 3:
118 opt = PJMEDIA_ECHO_SIMPLE;
119 break;
120 default:
121 puts("Invalid algorithm");
122 puts(desc);
123 return 1;
124 }
125 }
126 break;
Benny Prijonoa7908d72008-08-10 16:15:14 +0000127 case 'r':
128 repeat = atoi(pj_optarg);
129 if (repeat < 1) {
130 puts("Invalid algorithm");
131 puts(desc);
132 return 1;
133 }
134 break;
Benny Prijono6a237632008-08-13 13:53:18 +0000135 case 'i':
136 interactive = 1;
137 break;
Benny Prijono95250c72008-08-09 05:40:22 +0000138 }
139 }
Benny Prijono69f73122006-08-04 11:08:00 +0000140
Benny Prijono95250c72008-08-09 05:40:22 +0000141 if (argc - pj_optind != 3) {
142 puts("Error: missing argument(s)");
Benny Prijono69f73122006-08-04 11:08:00 +0000143 puts(desc);
144 return 1;
145 }
146
Benny Prijono69f73122006-08-04 11:08:00 +0000147 /* Must init PJLIB first: */
148 status = pj_init();
149 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
150
151 /* Must create a pool factory before we can allocate any memory. */
152 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
153
154 /*
155 * Initialize media endpoint.
156 * This will implicitly initialize PJMEDIA too.
157 */
158 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
159 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
160
161 /* Create memory pool for our file player */
162 pool = pj_pool_create( &cp.factory, /* pool factory */
163 "wav", /* pool name. */
164 4000, /* init size */
165 4000, /* increment size */
166 NULL /* callback on error */
167 );
168
Benny Prijono95250c72008-08-09 05:40:22 +0000169 /* Open wav_play */
170 status = pjmedia_wav_player_port_create(pool, argv[pj_optind], PTIME,
171 PJMEDIA_FILE_NO_LOOP, 0,
172 &wav_play);
Benny Prijono69f73122006-08-04 11:08:00 +0000173 if (status != PJ_SUCCESS) {
Benny Prijono95250c72008-08-09 05:40:22 +0000174 app_perror(THIS_FILE, "Error opening playback WAV file", status);
Benny Prijono69f73122006-08-04 11:08:00 +0000175 return 1;
176 }
Benny Prijono69f73122006-08-04 11:08:00 +0000177
Benny Prijono95250c72008-08-09 05:40:22 +0000178 /* Open recorded wav */
179 status = pjmedia_wav_player_port_create(pool, argv[pj_optind+1], PTIME,
180 PJMEDIA_FILE_NO_LOOP, 0,
181 &wav_rec);
182 if (status != PJ_SUCCESS) {
183 app_perror(THIS_FILE, "Error opening recorded WAV file", status);
184 return 1;
185 }
Benny Prijono69f73122006-08-04 11:08:00 +0000186
Benny Prijono95250c72008-08-09 05:40:22 +0000187 /* play and rec WAVs must have the same clock rate */
188 if (wav_play->info.clock_rate != wav_rec->info.clock_rate) {
189 puts("Error: clock rate mismatch in the WAV files");
190 return 1;
191 }
Benny Prijono69f73122006-08-04 11:08:00 +0000192
Benny Prijono95250c72008-08-09 05:40:22 +0000193 /* .. and channel count */
194 if (wav_play->info.channel_count != wav_rec->info.channel_count) {
195 puts("Error: clock rate mismatch in the WAV files");
196 return 1;
197 }
198
199 /* Create output wav */
200 status = pjmedia_wav_writer_port_create(pool, argv[pj_optind+2],
201 wav_play->info.clock_rate,
202 wav_play->info.channel_count,
203 wav_play->info.samples_per_frame,
204 wav_play->info.bits_per_sample,
205 0, 0, &wav_out);
206 if (status != PJ_SUCCESS) {
207 app_perror(THIS_FILE, "Error opening output WAV file", status);
208 return 1;
209 }
210
211 /* Create echo canceller */
212 status = pjmedia_echo_create2(pool, wav_play->info.clock_rate,
213 wav_play->info.channel_count,
214 wav_play->info.samples_per_frame,
215 tail_ms, latency_ms,
216 opt, &ec);
217 if (status != PJ_SUCCESS) {
218 app_perror(THIS_FILE, "Error creating EC", status);
219 return 1;
220 }
221
222
223 /* Processing loop */
224 play_frame.buf = pj_pool_alloc(pool, wav_play->info.samples_per_frame<<1);
225 rec_frame.buf = pj_pool_alloc(pool, wav_play->info.samples_per_frame<<1);
226 pj_get_timestamp(&t0);
Benny Prijonoa7908d72008-08-10 16:15:14 +0000227 for (i=0; i < repeat; ++i) {
228 for (;;) {
229 play_frame.size = wav_play->info.samples_per_frame << 1;
230 status = pjmedia_port_get_frame(wav_play, &play_frame);
231 if (status != PJ_SUCCESS)
232 break;
Benny Prijono95250c72008-08-09 05:40:22 +0000233
Benny Prijonoa7908d72008-08-10 16:15:14 +0000234 status = pjmedia_echo_playback(ec, (short*)play_frame.buf);
Benny Prijono95250c72008-08-09 05:40:22 +0000235
Benny Prijonoa7908d72008-08-10 16:15:14 +0000236 rec_frame.size = wav_play->info.samples_per_frame << 1;
237 status = pjmedia_port_get_frame(wav_rec, &rec_frame);
238 if (status != PJ_SUCCESS)
239 break;
Benny Prijono95250c72008-08-09 05:40:22 +0000240
Benny Prijonoa7908d72008-08-10 16:15:14 +0000241 status = pjmedia_echo_capture(ec, (short*)rec_frame.buf, 0);
Benny Prijono95250c72008-08-09 05:40:22 +0000242
Benny Prijonoa7908d72008-08-10 16:15:14 +0000243 //status = pjmedia_echo_cancel(ec, (short*)rec_frame.buf,
244 // (short*)play_frame.buf, 0, NULL);
Benny Prijono95250c72008-08-09 05:40:22 +0000245
Benny Prijonoa7908d72008-08-10 16:15:14 +0000246 pjmedia_port_put_frame(wav_out, &rec_frame);
247 }
248
249 pjmedia_wav_player_port_set_pos(wav_play, 0);
250 pjmedia_wav_player_port_set_pos(wav_rec, 0);
Benny Prijono95250c72008-08-09 05:40:22 +0000251 }
252 pj_get_timestamp(&t1);
253
Benny Prijono6a237632008-08-13 13:53:18 +0000254 i = pjmedia_wav_writer_port_get_pos(wav_out) * 1000 /
255 (wav_out->info.clock_rate * wav_out->info.channel_count);
256 PJ_LOG(3,(THIS_FILE, "Processed %3d.%03ds audio",
257 i / 1000, i % 1000));
Benny Prijono95250c72008-08-09 05:40:22 +0000258 PJ_LOG(3,(THIS_FILE, "Completed in %u msec\n", pj_elapsed_msec(&t0, &t1)));
Benny Prijono69f73122006-08-04 11:08:00 +0000259
260 /* Destroy file port(s) */
Benny Prijono95250c72008-08-09 05:40:22 +0000261 status = pjmedia_port_destroy( wav_play );
Benny Prijono69f73122006-08-04 11:08:00 +0000262 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
Benny Prijono95250c72008-08-09 05:40:22 +0000263 status = pjmedia_port_destroy( wav_rec );
264 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
265 status = pjmedia_port_destroy( wav_out );
Benny Prijono69f73122006-08-04 11:08:00 +0000266 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
267
Benny Prijono95250c72008-08-09 05:40:22 +0000268 /* Destroy ec */
269 pjmedia_echo_destroy(ec);
Benny Prijono69f73122006-08-04 11:08:00 +0000270
271 /* Release application pool */
272 pj_pool_release( pool );
273
274 /* Destroy media endpoint. */
275 pjmedia_endpt_destroy( med_endpt );
276
277 /* Destroy pool factory */
278 pj_caching_pool_destroy( &cp );
279
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000280 /* Shutdown PJLIB */
281 pj_shutdown();
Benny Prijono69f73122006-08-04 11:08:00 +0000282
Benny Prijono6a237632008-08-13 13:53:18 +0000283 if (interactive) {
Benny Prijonoa7908d72008-08-10 16:15:14 +0000284 char s[10];
285 puts("ENTER to quit");
286 fgets(s, sizeof(s), stdin);
287 }
Benny Prijonoa7908d72008-08-10 16:15:14 +0000288
Benny Prijono69f73122006-08-04 11:08:00 +0000289 /* Done. */
290 return 0;
291}
292