comparison spandsp-0.0.3/spandsp-0.0.3/tests/t38_gateway_tests.c @ 5:f762bf195c4b

import spandsp-0.0.3
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 16:00:21 +0200
parents
children
comparison
equal deleted inserted replaced
4:26cd8f1ef0b1 5:f762bf195c4b
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * t38_gateway_tests.c - Tests for the T.38 FoIP gateway module.
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2005, 2006 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * $Id: t38_gateway_tests.c,v 1.38 2006/12/09 04:56:20 steveu Exp $
26 */
27
28 /*! \file */
29
30 /*! \page t38_gateway_tests_page T.38 gateway tests tests
31 \section t38_gateway_tests_page_sec_1 What does it do?
32 These tests exercise the path
33
34 FAX machine <-> T.38 gateway <-> T.38 gateway <-> FAX machine
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <inttypes.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <fcntl.h>
45 #include <string.h>
46 #if defined(HAVE_TGMATH_H)
47 #include <tgmath.h>
48 #endif
49 #if defined(HAVE_MATH_H)
50 #include <math.h>
51 #endif
52 #include <assert.h>
53 #include <errno.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <netinet/tcp.h>
57 #define __USE_MISC
58 #include <arpa/inet.h>
59 #include <sys/select.h>
60 #include <sys/time.h>
61 #include <audiofile.h>
62 #include <tiffio.h>
63
64 #include "spandsp.h"
65
66 #include "ip_network_model.h"
67
68 #define SAMPLES_PER_CHUNK 160
69
70 #define INPUT_FILE_NAME "../itutests/fax/itutests.tif"
71 #define OUTPUT_FILE_NAME "t38.tif"
72 #define OUTPUT_FILE_NAME_WAVE "t38_gateway.wav"
73 #define OUTPUT_FILE_NAME_T30A "t38_gateway_t30a.wav"
74 #define OUTPUT_FILE_NAME_T38A "t38_gateway_t38a.wav"
75 #define OUTPUT_FILE_NAME_T30B "t38_gateway_t30b.wav"
76 #define OUTPUT_FILE_NAME_T38B "t38_gateway_t38b.wav"
77
78 fax_state_t fax_state_a;
79 t38_gateway_state_t t38_state_a;
80 t38_gateway_state_t t38_state_b;
81 fax_state_t fax_state_b;
82
83 ip_network_model_state_t *path_a_to_b;
84 ip_network_model_state_t *path_b_to_a;
85
86 int done[2] = {FALSE, FALSE};
87 int succeeded[2] = {FALSE, FALSE};
88
89 static void phase_b_handler(t30_state_t *s, void *user_data, int result)
90 {
91 int i;
92
93 i = (int) (intptr_t) user_data;
94 printf("%c: Phase B handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
95 }
96 /*- End of function --------------------------------------------------------*/
97
98 static void phase_d_handler(t30_state_t *s, void *user_data, int result)
99 {
100 int i;
101 t30_stats_t t;
102 char ident[21];
103
104 i = (int) (intptr_t) user_data;
105 printf("%c: Phase D handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
106 t30_get_transfer_statistics(s, &t);
107 printf("%c: Phase D: bit rate %d\n", i, t.bit_rate);
108 printf("%c: Phase D: ECM %s\n", i, (t.error_correcting_mode) ? "on" : "off");
109 printf("%c: Phase D: pages transferred %d\n", i, t.pages_transferred);
110 printf("%c: Phase D: image size %d x %d\n", i, t.width, t.length);
111 printf("%c: Phase D: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
112 printf("%c: Phase D: bad rows %d\n", i, t.bad_rows);
113 printf("%c: Phase D: longest bad row run %d\n", i, t.longest_bad_row_run);
114 printf("%c: Phase D: coding method %s\n", i, t4_encoding_to_str(t.encoding));
115 printf("%c: Phase D: image size %d\n", i, t.image_size);
116 t30_get_local_ident(s, ident);
117 printf("%c: Phase D: local ident '%s'\n", i, ident);
118 t30_get_far_ident(s, ident);
119 printf("%c: Phase D: remote ident '%s'\n", i, ident);
120 }
121 /*- End of function --------------------------------------------------------*/
122
123 static void phase_e_handler(t30_state_t *s, void *user_data, int result)
124 {
125 int i;
126 t30_stats_t t;
127 char ident[21];
128
129 i = (int) (intptr_t) user_data;
130 printf("%c: Phase E handler on channel %c - (%d) %s\n", i, i, result, t30_completion_code_to_str(result));
131 t30_get_transfer_statistics(s, &t);
132 printf("%c: Phase E: bit rate %d\n", i, t.bit_rate);
133 printf("%c: Phase E: ECM %s\n", i, (t.error_correcting_mode) ? "on" : "off");
134 printf("%c: Phase E: pages transferred %d\n", i, t.pages_transferred);
135 printf("%c: Phase E: image size %d x %d\n", i, t.width, t.length);
136 printf("%c: Phase E: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
137 printf("%c: Phase E: bad rows %d\n", i, t.bad_rows);
138 printf("%c: Phase E: longest bad row run %d\n", i, t.longest_bad_row_run);
139 printf("%c: Phase E: coding method %s\n", i, t4_encoding_to_str(t.encoding));
140 printf("%c: Phase E: image size %d bytes\n", i, t.image_size);
141 t30_get_local_ident(s, ident);
142 printf("%c: Phase E: local ident '%s'\n", i, ident);
143 t30_get_far_ident(s, ident);
144 printf("%c: Phase E: remote ident '%s'\n", i, ident);
145 succeeded[i - 'A'] = (result == T30_ERR_OK) && (t.pages_transferred == 12);
146 done[i - 'A'] = TRUE;
147 }
148 /*- End of function --------------------------------------------------------*/
149
150 static int tx_packet_handler_a(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
151 {
152 t38_gateway_state_t *t;
153
154 /* This routine queues messages between two instances of T.38 processing */
155 t = (t38_gateway_state_t *) user_data;
156 span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
157
158 ip_network_model_send(path_a_to_b, s->tx_seq_no, count, buf, len);
159 return 0;
160 }
161 /*- End of function --------------------------------------------------------*/
162
163 static int tx_packet_handler_b(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
164 {
165 t38_gateway_state_t *t;
166
167 /* This routine queues messages between two instances of T.38 processing */
168 t = (t38_gateway_state_t *) user_data;
169 span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
170
171 ip_network_model_send(path_b_to_a, s->tx_seq_no, count, buf, len);
172 return 0;
173 }
174 /*- End of function --------------------------------------------------------*/
175
176 int main(int argc, char *argv[])
177 {
178 int16_t silence[SAMPLES_PER_CHUNK];
179 int16_t t30_amp_a[SAMPLES_PER_CHUNK];
180 int16_t t38_amp_a[SAMPLES_PER_CHUNK];
181 int16_t t38_amp_b[SAMPLES_PER_CHUNK];
182 int16_t t30_amp_b[SAMPLES_PER_CHUNK];
183 int16_t out_amp[SAMPLES_PER_CHUNK*4];
184 int t30_len_a;
185 int t38_len_a;
186 int t38_len_b;
187 int t30_len_b;
188 int log_audio;
189 int msg_len;
190 uint8_t msg[1024];
191 int outframes;
192 AFfilesetup filesetup;
193 AFfilehandle wave_handle;
194 int use_ecm;
195 int t38_version;
196 const char *input_file_name;
197 int i;
198 int seq_no;
199
200 log_audio = FALSE;
201 use_ecm = FALSE;
202 t38_version = 1;
203 input_file_name = INPUT_FILE_NAME;
204 for (i = 1; i < argc; i++)
205 {
206 if (strcmp(argv[i], "-e") == 0)
207 {
208 use_ecm = TRUE;
209 continue;
210 }
211 if (strcmp(argv[i], "-i") == 0)
212 {
213 i++;
214 input_file_name = argv[i];
215 continue;
216 }
217 if (strcmp(argv[i], "-l") == 0)
218 {
219 log_audio = TRUE;
220 continue;
221 }
222 if (strcmp(argv[i], "-v") == 0)
223 {
224 i++;
225 t38_version = atoi(argv[i]);
226 continue;
227 }
228 }
229
230 printf("Using T.38 version %d\n", t38_version);
231 if (use_ecm)
232 printf("Using ECM\n");
233
234 filesetup = AF_NULL_FILESETUP;
235 wave_handle = AF_NULL_FILEHANDLE;
236 if (log_audio)
237 {
238 if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
239 {
240 fprintf(stderr, " Failed to create file setup\n");
241 exit(2);
242 }
243 afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
244 afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
245 afInitFileFormat(filesetup, AF_FILE_WAVE);
246 afInitChannels(filesetup, AF_DEFAULT_TRACK, 4);
247 if ((wave_handle = afOpenFile(OUTPUT_FILE_NAME_WAVE, "w", filesetup)) == AF_NULL_FILEHANDLE)
248 {
249 fprintf(stderr, " Cannot create wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
250 exit(2);
251 }
252 }
253 memset(silence, 0, sizeof(silence));
254
255 if ((path_a_to_b = ip_network_model_init(800, 2000, 0)) == NULL)
256 {
257 fprintf(stderr, "Failed to start IP network path model\n");
258 exit(2);
259 }
260 if ((path_b_to_a = ip_network_model_init(800, 2000, 0)) == NULL)
261 {
262 fprintf(stderr, "Failed to start IP network path model\n");
263 exit(2);
264 }
265
266 fax_init(&fax_state_a, TRUE);
267 t30_set_local_ident(&fax_state_a.t30_state, "11111111");
268 t30_set_tx_file(&fax_state_a.t30_state, input_file_name, -1, -1);
269 t30_set_phase_b_handler(&fax_state_a.t30_state, phase_b_handler, (void *) (intptr_t) 'A');
270 t30_set_phase_d_handler(&fax_state_a.t30_state, phase_d_handler, (void *) (intptr_t) 'A');
271 t30_set_phase_e_handler(&fax_state_a.t30_state, phase_e_handler, (void *) (intptr_t) 'A');
272 t30_set_ecm_capability(&fax_state_a.t30_state, use_ecm);
273 if (use_ecm)
274 t30_set_supported_compressions(&fax_state_a.t30_state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
275 span_log_set_level(&fax_state_a.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
276 span_log_set_tag(&fax_state_a.logging, "FAX-A ");
277 span_log_set_level(&fax_state_a.t30_state.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
278 span_log_set_tag(&fax_state_a.t30_state.logging, "FAX-A ");
279 memset(t30_amp_a, 0, sizeof(t30_amp_a));
280
281 if (t38_gateway_init(&t38_state_a, tx_packet_handler_a, &t38_state_b) == NULL)
282 {
283 fprintf(stderr, "Cannot start the T.38 channel\n");
284 exit(2);
285 }
286 t38_set_t38_version(&t38_state_a.t38, t38_version);
287 t38_gateway_ecm_control(&t38_state_a, use_ecm);
288 span_log_set_level(&t38_state_a.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
289 span_log_set_tag(&t38_state_a.logging, "T.38-A");
290 span_log_set_level(&t38_state_a.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
291 span_log_set_tag(&t38_state_a.t38.logging, "T.38-A");
292 memset(t38_amp_a, 0, sizeof(t38_amp_a));
293
294 if (t38_gateway_init(&t38_state_b, tx_packet_handler_b, &t38_state_a) == NULL)
295 {
296 fprintf(stderr, "Cannot start the T.38 channel\n");
297 exit(2);
298 }
299 t38_set_t38_version(&t38_state_b.t38, t38_version);
300 t38_gateway_ecm_control(&t38_state_b, use_ecm);
301 span_log_set_level(&t38_state_b.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
302 span_log_set_tag(&t38_state_b.logging, "T.38-B");
303 span_log_set_level(&t38_state_b.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
304 span_log_set_tag(&t38_state_b.t38.logging, "T.38-B");
305 memset(t38_amp_b, 0, sizeof(t38_amp_b));
306
307 fax_init(&fax_state_b, FALSE);
308 t30_set_local_ident(&fax_state_b.t30_state, "22222222");
309 t30_set_rx_file(&fax_state_b.t30_state, OUTPUT_FILE_NAME, -1);
310 t30_set_phase_b_handler(&fax_state_b.t30_state, phase_b_handler, (void *) (intptr_t) 'B');
311 t30_set_phase_d_handler(&fax_state_b.t30_state, phase_d_handler, (void *) (intptr_t) 'B');
312 t30_set_phase_e_handler(&fax_state_b.t30_state, phase_e_handler, (void *) (intptr_t) 'B');
313 t30_set_ecm_capability(&fax_state_b.t30_state, use_ecm);
314 if (use_ecm)
315 t30_set_supported_compressions(&fax_state_b.t30_state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
316 span_log_set_level(&fax_state_b.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
317 span_log_set_tag(&fax_state_b.logging, "FAX-B ");
318 span_log_set_level(&fax_state_b.t30_state.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
319 span_log_set_tag(&fax_state_b.t30_state.logging, "FAX-B ");
320 memset(t30_amp_b, 0, sizeof(t30_amp_b));
321
322 for (;;)
323 {
324 span_log_bump_samples(&fax_state_a.logging, SAMPLES_PER_CHUNK);
325 span_log_bump_samples(&fax_state_a.t30_state.logging, SAMPLES_PER_CHUNK);
326 span_log_bump_samples(&t38_state_a.logging, SAMPLES_PER_CHUNK);
327 span_log_bump_samples(&t38_state_a.t38.logging, SAMPLES_PER_CHUNK);
328 span_log_bump_samples(&t38_state_b.logging, SAMPLES_PER_CHUNK);
329 span_log_bump_samples(&t38_state_b.t38.logging, SAMPLES_PER_CHUNK);
330 span_log_bump_samples(&fax_state_b.logging, SAMPLES_PER_CHUNK);
331 span_log_bump_samples(&fax_state_b.t30_state.logging, SAMPLES_PER_CHUNK);
332 memset(out_amp, 0, sizeof(out_amp));
333
334 t30_len_a = fax_tx(&fax_state_a, t30_amp_a, SAMPLES_PER_CHUNK);
335 /* The receive side always expects a full block of samples, but the
336 transmit side may not be sending any when it doesn't need to. We
337 may need to pad with some silence. */
338 if (t30_len_a < SAMPLES_PER_CHUNK)
339 {
340 memset(t30_amp_a + t30_len_a, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t30_len_a));
341 t30_len_a = SAMPLES_PER_CHUNK;
342 }
343 if (log_audio)
344 {
345 for (i = 0; i < t30_len_a; i++)
346 out_amp[i*4] = t30_amp_a[i];
347 }
348 if (t38_gateway_rx(&t38_state_a, t30_amp_a, t30_len_a))
349 break;
350
351 t38_len_a = t38_gateway_tx(&t38_state_a, t38_amp_a, SAMPLES_PER_CHUNK);
352 if (t38_len_a < SAMPLES_PER_CHUNK)
353 {
354 memset(t38_amp_a + t38_len_a, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t38_len_a));
355 t38_len_a = SAMPLES_PER_CHUNK;
356 }
357 if (log_audio)
358 {
359 for (i = 0; i < t38_len_a; i++)
360 out_amp[i*4 + 1] = t38_amp_a[i];
361 }
362 if (fax_rx(&fax_state_a, t38_amp_a, SAMPLES_PER_CHUNK))
363 break;
364
365 t30_len_b = fax_tx(&fax_state_b, t30_amp_b, SAMPLES_PER_CHUNK);
366 /* The receive side always expects a full block of samples, but the
367 transmit side may not be sending any when it doesn't need to. We
368 may need to pad with some silence. */
369 if (t30_len_b < SAMPLES_PER_CHUNK)
370 {
371 memset(t30_amp_b + t30_len_b, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t30_len_b));
372 t30_len_b = SAMPLES_PER_CHUNK;
373 }
374 if (log_audio)
375 {
376 for (i = 0; i < t30_len_b; i++)
377 out_amp[i*4 + 3] = t30_amp_b[i];
378 }
379 if (t38_gateway_rx(&t38_state_b, t30_amp_b, t30_len_b))
380 break;
381
382 t38_len_b = t38_gateway_tx(&t38_state_b, t38_amp_b, SAMPLES_PER_CHUNK);
383 if (t38_len_b < SAMPLES_PER_CHUNK)
384 {
385 memset(t38_amp_b + t38_len_b, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t38_len_b));
386 t38_len_b = SAMPLES_PER_CHUNK;
387 }
388 if (log_audio)
389 {
390 for (i = 0; i < t38_len_b; i++)
391 out_amp[i*4 + 2] = t38_amp_b[i];
392 }
393 if (fax_rx(&fax_state_b, t38_amp_b, SAMPLES_PER_CHUNK))
394 break;
395
396 while ((msg_len = ip_network_model_get(path_a_to_b, SAMPLES_PER_CHUNK, msg, 1024, &seq_no)) >= 0)
397 t38_core_rx_ifp_packet(&t38_state_b.t38, seq_no, msg, msg_len);
398 while ((msg_len = ip_network_model_get(path_b_to_a, SAMPLES_PER_CHUNK, msg, 1024, &seq_no)) >= 0)
399 t38_core_rx_ifp_packet(&t38_state_a.t38, seq_no, msg, msg_len);
400
401 if (log_audio)
402 {
403 outframes = afWriteFrames(wave_handle, AF_DEFAULT_TRACK, out_amp, SAMPLES_PER_CHUNK);
404 if (outframes != SAMPLES_PER_CHUNK)
405 break;
406 }
407
408 if (done[0] && done[1])
409 break;
410 }
411 if (log_audio)
412 {
413 if (afCloseFile(wave_handle) != 0)
414 {
415 fprintf(stderr, " Cannot close wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
416 exit(2);
417 }
418 afFreeFileSetup(filesetup);
419 }
420 if (!succeeded[0] || !succeeded[1])
421 {
422 printf("Tests failed\n");
423 exit(2);
424 }
425 printf("Tests passed\n");
426 return 0;
427 }
428 /*- End of function --------------------------------------------------------*/
429 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.