comparison spandsp-0.0.3/spandsp-0.0.3/tests/t38_terminal_to_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_terminal_to_gateway_tests.c - Joint tests for the T.38 FoIP terminal and gateway modules.
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_terminal_to_gateway_tests.c,v 1.29 2006/12/09 04:56:20 steveu Exp $
26 */
27
28 /*! \file */
29
30 /*! \page t38_terminal_to_gateway_tests_page T.38 mixed gateway and termination tests
31 \section t38_terminal_to_gateway_tests_page_sec_1 What does it do?
32 These tests exercise the path
33
34 T.38 termination -> 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_terminal_to_gateway.wav"
73
74 t38_terminal_state_t t38_state_a;
75 t38_gateway_state_t t38_state_b;
76 fax_state_t fax_state_b;
77
78 ip_network_model_state_t *path_a_to_b;
79 ip_network_model_state_t *path_b_to_a;
80
81 int done[2] = {FALSE, FALSE};
82 int succeeded[2] = {FALSE, FALSE};
83
84 static void phase_b_handler(t30_state_t *s, void *user_data, int result)
85 {
86 int i;
87
88 i = (int) (intptr_t) user_data;
89 printf("%c: Phase B handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
90 }
91 /*- End of function --------------------------------------------------------*/
92
93 static void phase_d_handler(t30_state_t *s, void *user_data, int result)
94 {
95 int i;
96 t30_stats_t t;
97 char ident[21];
98
99 i = (int) (intptr_t) user_data;
100 printf("%c: Phase D handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
101 t30_get_transfer_statistics(s, &t);
102 printf("%c: Phase D: bit rate %d\n", i, t.bit_rate);
103 printf("%c: Phase D: ECM %s\n", i, (t.error_correcting_mode) ? "on" : "off");
104 printf("%c: Phase D: pages transferred %d\n", i, t.pages_transferred);
105 printf("%c: Phase D: image size %d x %d\n", i, t.width, t.length);
106 printf("%c: Phase D: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
107 printf("%c: Phase D: bad rows %d\n", i, t.bad_rows);
108 printf("%c: Phase D: longest bad row run %d\n", i, t.longest_bad_row_run);
109 printf("%c: Phase D: coding method %s\n", i, t4_encoding_to_str(t.encoding));
110 printf("%c: Phase D: image size %d\n", i, t.image_size);
111 t30_get_local_ident(s, ident);
112 printf("%c: Phase D: local ident '%s'\n", i, ident);
113 t30_get_far_ident(s, ident);
114 printf("%c: Phase D: remote ident '%s'\n", i, ident);
115 }
116 /*- End of function --------------------------------------------------------*/
117
118 static void phase_e_handler(t30_state_t *s, void *user_data, int result)
119 {
120 int i;
121 t30_stats_t t;
122 char ident[21];
123
124 i = (int) (intptr_t) user_data;
125 printf("%c: Phase E handler on channel %d - (%d) %s\n", i, i, result, t30_completion_code_to_str(result));
126 t30_get_transfer_statistics(s, &t);
127 printf("%c: Phase E: bit rate %d\n", i, t.bit_rate);
128 printf("%c: Phase E: ECM %s\n", i, (t.error_correcting_mode) ? "on" : "off");
129 printf("%c: Phase E: pages transferred %d\n", i, t.pages_transferred);
130 printf("%c: Phase E: image size %d x %d\n", i, t.width, t.length);
131 printf("%c: Phase E: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
132 printf("%c: Phase E: bad rows %d\n", i, t.bad_rows);
133 printf("%c: Phase E: longest bad row run %d\n", i, t.longest_bad_row_run);
134 printf("%c: Phase E: coding method %s\n", i, t4_encoding_to_str(t.encoding));
135 printf("%c: Phase E: image size %d bytes\n", i, t.image_size);
136 t30_get_local_ident(s, ident);
137 printf("%c: Phase E: local ident '%s'\n", i, ident);
138 t30_get_far_ident(s, ident);
139 printf("%c: Phase E: remote ident '%s'\n", i, ident);
140 succeeded[i - 'A'] = (result == T30_ERR_OK) && (t.pages_transferred == 12);
141 done[i - 'A'] = TRUE;
142 }
143 /*- End of function --------------------------------------------------------*/
144
145 static int tx_packet_handler_a(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
146 {
147 t38_gateway_state_t *t;
148
149 /* This routine queues messages between two instances of T.38 processing */
150 t = (t38_gateway_state_t *) user_data;
151 span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
152
153 ip_network_model_send(path_a_to_b, s->tx_seq_no, count, buf, len);
154 return 0;
155 }
156 /*- End of function --------------------------------------------------------*/
157
158 static int tx_packet_handler_b(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
159 {
160 t38_terminal_state_t *t;
161
162 /* This routine queues messages between two instances of T.38 processing */
163 t = (t38_terminal_state_t *) user_data;
164 span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
165
166 ip_network_model_send(path_b_to_a, s->tx_seq_no, count, buf, len);
167 return 0;
168 }
169 /*- End of function --------------------------------------------------------*/
170
171 int main(int argc, char *argv[])
172 {
173 int16_t t38_amp_b[SAMPLES_PER_CHUNK];
174 int16_t t30_amp_b[SAMPLES_PER_CHUNK];
175 int16_t out_amp[2*SAMPLES_PER_CHUNK];
176 int t38_len_b;
177 int t30_len_b;
178 int msg_len;
179 uint8_t msg[1024];
180 int log_audio;
181 int outframes;
182 AFfilesetup filesetup;
183 AFfilehandle wave_handle;
184 int t38_version;
185 int use_ecm;
186 const char *input_file_name;
187 int i;
188 int seq_no;
189
190 log_audio = FALSE;
191 t38_version = 1;
192 use_ecm = FALSE;
193 input_file_name = INPUT_FILE_NAME;
194 for (i = 1; i < argc; i++)
195 {
196 if (strcmp(argv[i], "-e") == 0)
197 {
198 use_ecm = TRUE;
199 continue;
200 }
201 if (strcmp(argv[i], "-i") == 0)
202 {
203 i++;
204 input_file_name = argv[i];
205 continue;
206 }
207 if (strcmp(argv[i], "-l") == 0)
208 {
209 log_audio = TRUE;
210 continue;
211 }
212 if (strcmp(argv[i], "-v") == 0)
213 {
214 i++;
215 t38_version = atoi(argv[i]);
216 continue;
217 }
218 }
219
220 printf("Using T.38 version %d\n", t38_version);
221 if (use_ecm)
222 printf("Using ECM\n");
223
224 filesetup = AF_NULL_FILESETUP;
225 wave_handle = AF_NULL_FILEHANDLE;
226 if (log_audio)
227 {
228 if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
229 {
230 fprintf(stderr, " Failed to create file setup\n");
231 exit(2);
232 }
233 afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
234 afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
235 afInitFileFormat(filesetup, AF_FILE_WAVE);
236 afInitChannels(filesetup, AF_DEFAULT_TRACK, 2);
237
238 if ((wave_handle = afOpenFile(OUTPUT_FILE_NAME_WAVE, "w", filesetup)) == AF_NULL_FILEHANDLE)
239 {
240 fprintf(stderr, " Cannot create wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
241 exit(2);
242 }
243 }
244
245 if ((path_a_to_b = ip_network_model_init(800, 2000, 0)) == NULL)
246 {
247 fprintf(stderr, "Failed to start IP network path model\n");
248 exit(2);
249 }
250 if ((path_b_to_a = ip_network_model_init(800, 2000, 0)) == NULL)
251 {
252 fprintf(stderr, "Failed to start IP network path model\n");
253 exit(2);
254 }
255
256 if (t38_terminal_init(&t38_state_a, TRUE, tx_packet_handler_a, &t38_state_b) == NULL)
257 {
258 fprintf(stderr, "Cannot start the T.38 channel\n");
259 exit(2);
260 }
261 t38_set_t38_version(&t38_state_a.t38, t38_version);
262 span_log_set_level(&t38_state_a.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
263 span_log_set_tag(&t38_state_a.logging, "T.38-A");
264 span_log_set_level(&t38_state_a.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
265 span_log_set_tag(&t38_state_a.t38.logging, "T.38-A");
266 span_log_set_level(&t38_state_a.t30_state.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
267 span_log_set_tag(&t38_state_a.t30_state.logging, "T.38-A");
268
269 t30_set_local_ident(&t38_state_a.t30_state, "11111111");
270 t30_set_tx_file(&t38_state_a.t30_state, input_file_name, -1, -1);
271 t30_set_phase_b_handler(&t38_state_a.t30_state, phase_b_handler, (void *) (intptr_t) 'A');
272 t30_set_phase_d_handler(&t38_state_a.t30_state, phase_d_handler, (void *) (intptr_t) 'A');
273 t30_set_phase_e_handler(&t38_state_a.t30_state, phase_e_handler, (void *) (intptr_t) 'A');
274 t30_set_ecm_capability(&t38_state_a.t30_state, use_ecm);
275 if (use_ecm)
276 t30_set_supported_compressions(&t38_state_a.t30_state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
277
278 if (t38_gateway_init(&t38_state_b, tx_packet_handler_b, &t38_state_a) == NULL)
279 {
280 fprintf(stderr, "Cannot start the T.38 channel\n");
281 exit(2);
282 }
283 t38_set_t38_version(&t38_state_b.t38, t38_version);
284 t38_gateway_ecm_control(&t38_state_b, use_ecm);
285 span_log_set_level(&t38_state_b.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
286 span_log_set_tag(&t38_state_b.logging, "T.38-B");
287 span_log_set_level(&t38_state_b.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
288 span_log_set_tag(&t38_state_b.t38.logging, "T.38-B");
289 memset(t38_amp_b, 0, sizeof(t38_amp_b));
290
291 fax_init(&fax_state_b, FALSE);
292 t30_set_local_ident(&fax_state_b.t30_state, "22222222");
293 t30_set_rx_file(&fax_state_b.t30_state, OUTPUT_FILE_NAME, -1);
294 t30_set_phase_b_handler(&fax_state_b.t30_state, phase_b_handler, (void *) (intptr_t) 'B');
295 t30_set_phase_d_handler(&fax_state_b.t30_state, phase_d_handler, (void *) (intptr_t) 'B');
296 t30_set_phase_e_handler(&fax_state_b.t30_state, phase_e_handler, (void *) (intptr_t) 'B');
297 t30_set_ecm_capability(&fax_state_b.t30_state, use_ecm);
298 if (use_ecm)
299 t30_set_supported_compressions(&fax_state_b.t30_state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
300 span_log_set_level(&fax_state_b.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
301 span_log_set_tag(&fax_state_b.logging, "FAX-B ");
302 span_log_set_level(&fax_state_b.t30_state.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
303 span_log_set_tag(&fax_state_b.t30_state.logging, "FAX-B ");
304 memset(t30_amp_b, 0, sizeof(t30_amp_b));
305
306 for (;;)
307 {
308 span_log_bump_samples(&t38_state_a.logging, SAMPLES_PER_CHUNK);
309 span_log_bump_samples(&t38_state_a.t38.logging, SAMPLES_PER_CHUNK);
310 span_log_bump_samples(&t38_state_a.t30_state.logging, SAMPLES_PER_CHUNK);
311 span_log_bump_samples(&t38_state_b.logging, SAMPLES_PER_CHUNK);
312 span_log_bump_samples(&t38_state_b.t38.logging, SAMPLES_PER_CHUNK);
313 span_log_bump_samples(&fax_state_b.logging, SAMPLES_PER_CHUNK);
314 span_log_bump_samples(&fax_state_b.t30_state.logging, SAMPLES_PER_CHUNK);
315 memset(out_amp, 0, sizeof(out_amp));
316
317 t38_terminal_send_timeout(&t38_state_a, SAMPLES_PER_CHUNK);
318
319 t30_len_b = fax_tx(&fax_state_b, t30_amp_b, SAMPLES_PER_CHUNK);
320 /* The receive side always expects a full block of samples, but the
321 transmit side may not be sending any when it doesn't need to. We
322 may need to pad with some silence. */
323 if (t30_len_b < SAMPLES_PER_CHUNK)
324 {
325 memset(t30_amp_b + t30_len_b, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t30_len_b));
326 t30_len_b = SAMPLES_PER_CHUNK;
327 }
328 if (log_audio)
329 {
330 for (i = 0; i < t30_len_b; i++)
331 out_amp[2*i + 1] = t30_amp_b[i];
332 }
333 if (t38_gateway_rx(&t38_state_b, t30_amp_b, t30_len_b))
334 break;
335
336 t38_len_b = t38_gateway_tx(&t38_state_b, t38_amp_b, SAMPLES_PER_CHUNK);
337 if (t38_len_b < SAMPLES_PER_CHUNK)
338 {
339 memset(t38_amp_b + t38_len_b, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t38_len_b));
340 t38_len_b = SAMPLES_PER_CHUNK;
341 }
342 if (log_audio)
343 {
344 for (i = 0; i < t38_len_b; i++)
345 out_amp[2*i] = t38_amp_b[i];
346 }
347 if (fax_rx(&fax_state_b, t38_amp_b, SAMPLES_PER_CHUNK))
348 break;
349
350 while ((msg_len = ip_network_model_get(path_a_to_b, SAMPLES_PER_CHUNK, msg, 1024, &seq_no)) >= 0)
351 t38_core_rx_ifp_packet(&t38_state_b.t38, seq_no, msg, msg_len);
352 while ((msg_len = ip_network_model_get(path_b_to_a, SAMPLES_PER_CHUNK, msg, 1024, &seq_no)) >= 0)
353 t38_core_rx_ifp_packet(&t38_state_a.t38, seq_no, msg, msg_len);
354
355 if (log_audio)
356 {
357 outframes = afWriteFrames(wave_handle, AF_DEFAULT_TRACK, out_amp, SAMPLES_PER_CHUNK);
358 if (outframes != SAMPLES_PER_CHUNK)
359 break;
360 }
361
362 if (done[0] && done[1])
363 break;
364 }
365 if (log_audio)
366 {
367 if (afCloseFile(wave_handle) != 0)
368 {
369 fprintf(stderr, " Cannot close wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
370 exit(2);
371 }
372 afFreeFileSetup(filesetup);
373 }
374 if (!succeeded[0] || !succeeded[1])
375 {
376 printf("Tests failed\n");
377 exit(2);
378 }
379 printf("Tests passed\n");
380 return 0;
381 }
382 /*- End of function --------------------------------------------------------*/
383 /*- End of file ------------------------------------------------------------*/

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