comparison spandsp-0.0.3/spandsp-0.0.3/tests/t38_gateway_to_terminal_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_to_terminal_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_gateway_to_terminal_tests.c,v 1.31 2006/12/09 04:56:20 steveu Exp $
26 */
27
28 /*! \file */
29
30 /*! \page t38_gateway_to_terminal_tests_page T.38 mixed gateway and termination tests
31 \section t38_gateway_to_terminal_tests_page_sec_1 What does it do?
32 These tests exercise the path
33
34 FAX machine -> T.38 gateway -> T.38 termination
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_to_terminal.wav"
73
74 fax_state_t fax_state_a;
75 t38_gateway_state_t t38_state_a;
76 t38_terminal_state_t t38_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 %c - (%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_terminal_state_t *t;
148
149 /* This routine queues messages between two instances of T.38 processing */
150 t = (t38_terminal_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_gateway_state_t *t;
161
162 /* This routine queues messages between two instances of T.38 processing */
163 t = (t38_gateway_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_a[SAMPLES_PER_CHUNK];
174 int16_t t30_amp_a[SAMPLES_PER_CHUNK];
175 int16_t out_amp[2*SAMPLES_PER_CHUNK];
176 int t38_len_a;
177 int t30_len_a;
178 int msg_len;
179 uint8_t msg[1024];
180 int log_audio;
181 int outframes;
182 int t38_version;
183 int use_ecm;
184 AFfilesetup filesetup;
185 AFfilehandle wave_handle;
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 wave_handle = afOpenFile(OUTPUT_FILE_NAME_WAVE, "w", filesetup);
239 if (wave_handle == AF_NULL_FILEHANDLE)
240 {
241 fprintf(stderr, " Cannot create wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
242 exit(2);
243 }
244 }
245
246 if ((path_a_to_b = ip_network_model_init(800, 2000, 0)) == NULL)
247 {
248 fprintf(stderr, "Failed to start IP network path model\n");
249 exit(2);
250 }
251 if ((path_b_to_a = ip_network_model_init(800, 2000, 0)) == NULL)
252 {
253 fprintf(stderr, "Failed to start IP network path model\n");
254 exit(2);
255 }
256
257 fax_init(&fax_state_a, TRUE);
258 t30_set_local_ident(&fax_state_a.t30_state, "11111111");
259 t30_set_tx_file(&fax_state_a.t30_state, input_file_name, -1, -1);
260 t30_set_phase_b_handler(&fax_state_a.t30_state, phase_b_handler, (void *) (intptr_t) 'A');
261 t30_set_phase_d_handler(&fax_state_a.t30_state, phase_d_handler, (void *) (intptr_t) 'A');
262 t30_set_phase_e_handler(&fax_state_a.t30_state, phase_e_handler, (void *) (intptr_t) 'A');
263 t30_set_ecm_capability(&fax_state_a.t30_state, use_ecm);
264 if (use_ecm)
265 t30_set_supported_compressions(&fax_state_a.t30_state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
266 span_log_set_level(&fax_state_a.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
267 span_log_set_tag(&fax_state_a.logging, "FAX-A ");
268 span_log_set_level(&fax_state_a.t30_state.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
269 span_log_set_tag(&fax_state_a.t30_state.logging, "FAX-A ");
270 memset(t30_amp_a, 0, sizeof(t30_amp_a));
271
272 if (t38_gateway_init(&t38_state_a, tx_packet_handler_a, &t38_state_b) == NULL)
273 {
274 fprintf(stderr, "Cannot start the T.38 channel\n");
275 exit(2);
276 }
277 t38_set_t38_version(&t38_state_a.t38, t38_version);
278 t38_gateway_ecm_control(&t38_state_a, use_ecm);
279 span_log_set_level(&t38_state_a.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
280 span_log_set_tag(&t38_state_a.logging, "T.38-A");
281 span_log_set_level(&t38_state_a.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
282 span_log_set_tag(&t38_state_a.t38.logging, "T.38-A");
283 memset(t38_amp_a, 0, sizeof(t38_amp_a));
284
285 if (t38_terminal_init(&t38_state_b, FALSE, tx_packet_handler_b, &t38_state_a) == NULL)
286 {
287 fprintf(stderr, "Cannot start the T.38 channel\n");
288 exit(2);
289 }
290 t38_set_t38_version(&t38_state_b.t38, t38_version);
291 span_log_set_level(&t38_state_b.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
292 span_log_set_tag(&t38_state_b.logging, "T.38-B");
293 span_log_set_level(&t38_state_b.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
294 span_log_set_tag(&t38_state_b.t38.logging, "T.38-B");
295 span_log_set_level(&t38_state_b.t30_state.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
296 span_log_set_tag(&t38_state_b.t30_state.logging, "T.38-B");
297
298 t30_set_local_ident(&t38_state_b.t30_state, "22222222");
299 t30_set_rx_file(&t38_state_b.t30_state, OUTPUT_FILE_NAME, -1);
300 t30_set_phase_b_handler(&t38_state_b.t30_state, phase_b_handler, (void *) (intptr_t) 'B');
301 t30_set_phase_d_handler(&t38_state_b.t30_state, phase_d_handler, (void *) (intptr_t) 'B');
302 t30_set_phase_e_handler(&t38_state_b.t30_state, phase_e_handler, (void *) (intptr_t) 'B');
303 t30_set_ecm_capability(&t38_state_b.t30_state, use_ecm);
304 if (use_ecm)
305 t30_set_supported_compressions(&t38_state_b.t30_state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
306
307 for (;;)
308 {
309 span_log_bump_samples(&fax_state_a.logging, SAMPLES_PER_CHUNK);
310 span_log_bump_samples(&fax_state_a.t30_state.logging, SAMPLES_PER_CHUNK);
311 span_log_bump_samples(&t38_state_a.logging, SAMPLES_PER_CHUNK);
312 span_log_bump_samples(&t38_state_a.t38.logging, SAMPLES_PER_CHUNK);
313 span_log_bump_samples(&t38_state_b.logging, SAMPLES_PER_CHUNK);
314 span_log_bump_samples(&t38_state_b.t38.logging, SAMPLES_PER_CHUNK);
315 span_log_bump_samples(&t38_state_b.t30_state.logging, SAMPLES_PER_CHUNK);
316 memset(out_amp, 0, sizeof(out_amp));
317
318 t38_terminal_send_timeout(&t38_state_b, SAMPLES_PER_CHUNK);
319
320 t30_len_a = fax_tx(&fax_state_a, t30_amp_a, SAMPLES_PER_CHUNK);
321 /* The receive side always expects a full block of samples, but the
322 transmit side may not be sending any when it doesn't need to. We
323 may need to pad with some silence. */
324 if (t30_len_a < SAMPLES_PER_CHUNK)
325 {
326 memset(t30_amp_a + t30_len_a, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t30_len_a));
327 t30_len_a = SAMPLES_PER_CHUNK;
328 }
329 if (log_audio)
330 {
331 for (i = 0; i < t30_len_a; i++)
332 out_amp[2*i] = t30_amp_a[i];
333 }
334 if (t38_gateway_rx(&t38_state_a, t30_amp_a, t30_len_a))
335 break;
336
337 t38_len_a = t38_gateway_tx(&t38_state_a, t38_amp_a, SAMPLES_PER_CHUNK);
338 if (t38_len_a < SAMPLES_PER_CHUNK)
339 {
340 memset(t38_amp_a + t38_len_a, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t38_len_a));
341 t38_len_a = SAMPLES_PER_CHUNK;
342 }
343 if (log_audio)
344 {
345 for (i = 0; i < t38_len_a; i++)
346 out_amp[2*i + 1] = t38_amp_a[i];
347 }
348 if (fax_rx(&fax_state_a, t38_amp_a, SAMPLES_PER_CHUNK))
349 break;
350
351 while ((msg_len = ip_network_model_get(path_a_to_b, SAMPLES_PER_CHUNK, msg, 1024, &seq_no)) >= 0)
352 t38_core_rx_ifp_packet(&t38_state_b.t38, seq_no, msg, msg_len);
353 while ((msg_len = ip_network_model_get(path_b_to_a, SAMPLES_PER_CHUNK, msg, 1024, &seq_no)) >= 0)
354 t38_core_rx_ifp_packet(&t38_state_a.t38, seq_no, msg, msg_len);
355
356 if (log_audio)
357 {
358 outframes = afWriteFrames(wave_handle, AF_DEFAULT_TRACK, out_amp, SAMPLES_PER_CHUNK);
359 if (outframes != SAMPLES_PER_CHUNK)
360 break;
361 }
362
363 if (done[0] && done[1])
364 break;
365 }
366 if (log_audio)
367 {
368 if (afCloseFile(wave_handle) != 0)
369 {
370 fprintf(stderr, " Cannot close wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
371 exit(2);
372 }
373 afFreeFileSetup(filesetup);
374 }
375 if (!succeeded[0] || !succeeded[1])
376 {
377 printf("Tests failed\n");
378 exit(2);
379 }
380 printf("Tests passed\n");
381 return 0;
382 }
383 /*- End of function --------------------------------------------------------*/
384 /*- End of file ------------------------------------------------------------*/

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