Debian Codegraph @

tests/t38_terminal_to_gateway_tests.c

raw symbols SCIP
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 <[email protected]>
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
26 /*! \file */
27
28 /*! \page t38_terminal_to_gateway_tests_page T.38 mixed gateway and termination tests
29 \section t38_terminal_to_gateway_tests_page_sec_1 What does it do?
30 These tests exercise the path
31
32 T.38 termination -> T.38 gateway -> FAX machine
33 */
34
35 /* Enable the following definition to enable direct probing into the FAX structures */
36 //#define WITH_SPANDSP_INTERNALS
37
38 #if defined(HAVE_CONFIG_H)
39 #include <config.h>
40 #endif
41
42 #if defined(HAVE_FL_FL_H) && defined(HAVE_FL_FL_CARTESIAN_H) && defined(HAVE_FL_FL_AUDIO_METER_H)
43 #define ENABLE_GUI
44 #endif
45
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <fcntl.h>
49 #include <string.h>
50 #include <assert.h>
51 #include <errno.h>
52 #include <sndfile.h>
53 #if !defined(_WIN32)
54 #include <unistd.h>
55 #endif
56
57 //#if defined(WITH_SPANDSP_INTERNALS)
58 #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
59 //#endif
60
61 #include "spandsp.h"
62 #include "spandsp-sim.h"
63
64 #if defined(ENABLE_GUI)
65 #include "media_monitor.h"
66 #endif
67 #include "fax_utils.h"
68
69 #define SAMPLES_PER_CHUNK 160
70
71 #define INPUT_FILE_NAME "../test-data/itu/fax/itutests.tif"
72 #define OUTPUT_FILE_NAME "t38.tif"
73 #define OUTPUT_FILE_NAME_WAVE "t38_terminal_to_gateway.wav"
74
75 t38_terminal_state_t *t38_state_a;
76 t38_gateway_state_t *t38_state_b;
77 fax_state_t *fax_state_b;
78
79 g1050_state_t *path_a_to_b;
80 g1050_state_t *path_b_to_a;
81
82 double when = 0.0;
83
84 int done[2] = {FALSE, FALSE};
85 int succeeded[2] = {FALSE, FALSE};
86
87 int simulate_incrementing_repeats = FALSE;
88
89 static int phase_b_handler(t30_state_t *s, void *user_data, int result)
90 {
91 int i;
92 char tag[20];
93
94 i = (int) (intptr_t) user_data;
95 snprintf(tag, sizeof(tag), "%c: Phase B", i);
96 printf("%c: Phase B handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
97 fax_log_rx_parameters(s, tag);
98 return T30_ERR_OK;
99 }
100 /*- End of function --------------------------------------------------------*/
101
102 static int phase_d_handler(t30_state_t *s, void *user_data, int result)
103 {
104 int i;
105 char tag[20];
106
107 i = (int) (intptr_t) user_data;
108 snprintf(tag, sizeof(tag), "%c: Phase D", i);
109 printf("%c: Phase D handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
110 fax_log_page_transfer_statistics(s, tag);
111 fax_log_tx_parameters(s, tag);
112 fax_log_rx_parameters(s, tag);
113 return T30_ERR_OK;
114 }
115 /*- End of function --------------------------------------------------------*/
116
117 static void phase_e_handler(t30_state_t *s, void *user_data, int result)
118 {
119 int i;
120 t30_stats_t t;
121 char tag[20];
122
123 i = (int) (intptr_t) user_data;
124 snprintf(tag, sizeof(tag), "%c: Phase E", i);
125 printf("%c: Phase E handler on channel %c - (%d) %s\n", i, i, result, t30_completion_code_to_str(result));
126 fax_log_final_transfer_statistics(s, tag);
127 fax_log_tx_parameters(s, tag);
128 fax_log_rx_parameters(s, tag);
129 t30_get_transfer_statistics(s, &t);
130 succeeded[i - 'A'] = (result == T30_ERR_OK) && (t.pages_tx == 12 || t.pages_rx == 12);
131 done[i - 'A'] = TRUE;
132 }
133 /*- End of function --------------------------------------------------------*/
134
135 static int tx_packet_handler_a(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
136 {
137 int i;
138 static int subst_seq = 0;
139
140 /* This routine queues messages between two instances of T.38 processing */
141 if (simulate_incrementing_repeats)
142 {
143 for (i = 0; i < count; i++)
144 {
145 span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d\n", subst_seq, len);
146
147 g1050_put(path_a_to_b, buf, len, subst_seq, when);
148 subst_seq = (subst_seq + 1) & 0xFFFF;
149 }
150 }
151 else
152 {
153 span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
154
155 for (i = 0; i < count; i++)
156 g1050_put(path_a_to_b, buf, len, s->tx_seq_no, when);
157 }
158 return 0;
159 }
160 /*- End of function --------------------------------------------------------*/
161
162 static int tx_packet_handler_b(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
163 {
164 int i;
165 static int subst_seq = 0;
166
167 /* This routine queues messages between two instances of T.38 processing */
168 if (simulate_incrementing_repeats)
169 {
170 for (i = 0; i < count; i++)
171 {
172 span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d\n", subst_seq, len);
173
174 g1050_put(path_b_to_a, buf, len, subst_seq, when);
175 subst_seq = (subst_seq + 1) & 0xFFFF;
176 }
177 }
178 else
179 {
180 span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
181
182 for (i = 0; i < count; i++)
183 g1050_put(path_b_to_a, buf, len, s->tx_seq_no, when);
184 }
185 return 0;
186 }
187 /*- End of function --------------------------------------------------------*/
188
189 int main(int argc, char *argv[])
190 {
191 int16_t t38_amp_b[SAMPLES_PER_CHUNK];
192 int16_t t30_amp_b[SAMPLES_PER_CHUNK];
193 int16_t out_amp[2*SAMPLES_PER_CHUNK];
194 int t38_len_b;
195 int t30_len_b;
196 int msg_len;
197 uint8_t msg[1024];
198 int log_audio;
199 int outframes;
200 SNDFILE *wave_handle;
201 int t38_version;
202 int use_ecm;
203 int use_tep;
204 int feedback_audio;
205 int use_transmit_on_idle;
206 const char *input_file_name;
207 int i;
208 int seq_no;
209 int g1050_model_no;
210 int g1050_speed_pattern_no;
211 double tx_when;
212 double rx_when;
213 int use_gui;
214 int supported_modems;
215 int opt;
216 t30_state_t *t30;
217 t38_core_state_t *t38_core;
218 logging_state_t *logging;
219
220 log_audio = FALSE;
221 t38_version = 1;
222 use_ecm = FALSE;
223 input_file_name = INPUT_FILE_NAME;
224 simulate_incrementing_repeats = FALSE;
225 g1050_model_no = 0;
226 g1050_speed_pattern_no = 1;
227 use_gui = FALSE;
228 use_tep = FALSE;
229 feedback_audio = FALSE;
230 use_transmit_on_idle = TRUE;
231 supported_modems = T30_SUPPORT_V27TER | T30_SUPPORT_V29 | T30_SUPPORT_V17;
232 while ((opt = getopt(argc, argv, "efgi:Ilm:M:s:tv:")) != -1)
233 {
234 switch (opt)
235 {
236 case 'e':
237 use_ecm = TRUE;
238 break;
239 case 'f':
240 feedback_audio = TRUE;
241 break;
242 case 'g':
243 #if defined(ENABLE_GUI)
244 use_gui = TRUE;
245 #else
246 fprintf(stderr, "Graphical monitoring not available\n");
247 exit(2);
248 #endif
249 break;
250 case 'i':
251 input_file_name = optarg;
252 break;
253 case 'I':
254 simulate_incrementing_repeats = TRUE;
255 break;
256 case 'l':
257 log_audio = TRUE;
258 break;
259 case 'm':
260 supported_modems = atoi(optarg);
261 break;
262 case 'M':
263 g1050_model_no = optarg[0] - 'A' + 1;
264 break;
265 case 's':
266 g1050_speed_pattern_no = atoi(optarg);
267 break;
268 case 't':
269 use_tep = TRUE;
270 break;
271 case 'v':
272 t38_version = atoi(optarg);
273 break;
274 default:
275 //usage();
276 exit(2);
277 break;
278 }
279 }
280
281 printf("Using T.38 version %d\n", t38_version);
282 if (use_ecm)
283 printf("Using ECM\n");
284
285 wave_handle = NULL;
286 if (log_audio)
287 {
288 if ((wave_handle = sf_open_telephony_write(OUTPUT_FILE_NAME_WAVE, 2)) == NULL)
289 {
290 fprintf(stderr, " Cannot create audio file '%s'\n", OUTPUT_FILE_NAME_WAVE);
291 exit(2);
292 }
293 }
294
295 srand48(0x1234567);
296 if ((path_a_to_b = g1050_init(g1050_model_no, g1050_speed_pattern_no, 100, 33)) == NULL)
297 {
298 fprintf(stderr, "Failed to start IP network path model\n");
299 exit(2);
300 }
301 if ((path_b_to_a = g1050_init(g1050_model_no, g1050_speed_pattern_no, 100, 33)) == NULL)
302 {
303 fprintf(stderr, "Failed to start IP network path model\n");
304 exit(2);
305 }
306
307 if ((t38_state_a = t38_terminal_init(NULL, TRUE, tx_packet_handler_a, &t38_state_b)) == NULL)
308 {
309 fprintf(stderr, "Cannot start the T.38 channel\n");
310 exit(2);
311 }
312 t30 = t38_terminal_get_t30_state(t38_state_a);
313 t38_core = t38_terminal_get_t38_core_state(t38_state_a);
314 t38_set_t38_version(t38_core, t38_version);
315
316 logging = t38_terminal_get_logging_state(t38_state_a);
317 span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
318 span_log_set_tag(logging, "T.38-A");
319
320 logging = t38_core_get_logging_state(t38_core);
321 span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
322 span_log_set_tag(logging, "T.38-A");
323
324 logging = t30_get_logging_state(t30);
325 span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
326 span_log_set_tag(logging, "T.38-A");
327
328 t30_set_supported_modems(t30, supported_modems);
329 t30_set_tx_ident(t30, "11111111");
330 t30_set_tx_file(t30, input_file_name, -1, -1);
331 t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) 'A');
332 t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) 'A');
333 t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) 'A');
334 t30_set_ecm_capability(t30, use_ecm);
335 if (use_ecm)
336 t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
337
338 if ((t38_state_b = t38_gateway_init(NULL, tx_packet_handler_b, &t38_state_a)) == NULL)
339 {
340 fprintf(stderr, "Cannot start the T.38 channel\n");
341 exit(2);
342 }
343 t38_core = t38_gateway_get_t38_core_state(t38_state_b);
344 t38_gateway_set_transmit_on_idle(t38_state_b, use_transmit_on_idle);
345 t38_set_t38_version(t38_core, t38_version);
346 t38_gateway_set_ecm_capability(t38_state_b, use_ecm);
347
348 logging = t38_gateway_get_logging_state(t38_state_b);
349 span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
350 span_log_set_tag(logging, "T.38-B");
351
352 logging = t38_core_get_logging_state(t38_core);
353 span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
354 span_log_set_tag(logging, "T.38-B");
355
356 memset(t38_amp_b, 0, sizeof(t38_amp_b));
357
358 if ((fax_state_b = fax_init(NULL, FALSE)) == NULL)
359 {
360 fprintf(stderr, "Cannot start FAX\n");
361 exit(2);
362 }
363 t30 = fax_get_t30_state(fax_state_b);
364 fax_set_transmit_on_idle(fax_state_b, use_transmit_on_idle);
365 fax_set_tep_mode(fax_state_b, use_tep);
366 t30_set_supported_modems(t30, supported_modems);
367 t30_set_tx_ident(t30, "22222222");
368 t30_set_tx_nsf(t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp\x00", 12);
369 t30_set_rx_file(t30, OUTPUT_FILE_NAME, -1);
370 t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) 'B');
371 t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) 'B');
372 t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) 'B');
373 t30_set_ecm_capability(t30, use_ecm);
374 if (use_ecm)
375 t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
376
377 logging = fax_get_logging_state(fax_state_b);
378 span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
379 span_log_set_tag(logging, "FAX-B ");
380
381 logging = t30_get_logging_state(t30);
382 span_log_set_level(logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
383 span_log_set_tag(logging, "FAX-B ");
384
385 memset(t30_amp_b, 0, sizeof(t30_amp_b));
386
387 #if defined(ENABLE_GUI)
388 if (use_gui)
389 start_media_monitor();
390 #endif
391 for (;;)
392 {
393 logging = t38_terminal_get_logging_state(t38_state_a);
394 span_log_bump_samples(logging, SAMPLES_PER_CHUNK);
395 t38_core = t38_terminal_get_t38_core_state(t38_state_a);
396 logging = t38_core_get_logging_state(t38_core);
397 span_log_bump_samples(logging, SAMPLES_PER_CHUNK);
398 t30 = t38_terminal_get_t30_state(t38_state_a);
399 logging = t30_get_logging_state(t30);
400 span_log_bump_samples(logging, SAMPLES_PER_CHUNK);
401 logging = t38_gateway_get_logging_state(t38_state_b);
402 span_log_bump_samples(logging, SAMPLES_PER_CHUNK);
403 t38_core = t38_gateway_get_t38_core_state(t38_state_b);
404 logging = t38_core_get_logging_state(t38_core);
405 span_log_bump_samples(logging, SAMPLES_PER_CHUNK);
406 logging = fax_get_logging_state(fax_state_b);
407 span_log_bump_samples(logging, SAMPLES_PER_CHUNK);
408 t30 = fax_get_t30_state(fax_state_b);
409 logging = t30_get_logging_state(t30);
410 span_log_bump_samples(logging, SAMPLES_PER_CHUNK);
411 memset(out_amp, 0, sizeof(out_amp));
412
413 t38_terminal_send_timeout(t38_state_a, SAMPLES_PER_CHUNK);
414
415 t30_len_b = fax_tx(fax_state_b, t30_amp_b, SAMPLES_PER_CHUNK);
416 if (!use_transmit_on_idle)
417 {
418 /* The receive side always expects a full block of samples, but the
419 transmit side may not be sending any when it doesn't need to. We
420 may need to pad with some silence. */
421 if (t30_len_b < SAMPLES_PER_CHUNK)
422 {
423 memset(t30_amp_b + t30_len_b, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t30_len_b));
424 t30_len_b = SAMPLES_PER_CHUNK;
425 }
426 }
427 if (feedback_audio)
428 {
429 for (i = 0; i < t30_len_b; i++)
430 t30_amp_b[i] += t38_amp_b[i] >> 1;
431 }
432 if (log_audio)
433 {
434 for (i = 0; i < t30_len_b; i++)
435 out_amp[2*i + 1] = t30_amp_b[i];
436 }
437 if (t38_gateway_rx(t38_state_b, t30_amp_b, t30_len_b))
438 break;
439
440 t38_len_b = t38_gateway_tx(t38_state_b, t38_amp_b, SAMPLES_PER_CHUNK);
441 if (!use_transmit_on_idle)
442 {
443 if (t38_len_b < SAMPLES_PER_CHUNK)
444 {
445 memset(t38_amp_b + t38_len_b, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t38_len_b));
446 t38_len_b = SAMPLES_PER_CHUNK;
447 }
448 }
449 if (log_audio)
450 {
451 for (i = 0; i < t38_len_b; i++)
452 out_amp[2*i] = t38_amp_b[i];
453 }
454 if (fax_rx(fax_state_b, t38_amp_b, SAMPLES_PER_CHUNK))
455 break;
456
457 when += (float) SAMPLES_PER_CHUNK/(float) SAMPLE_RATE;
458
459 while ((msg_len = g1050_get(path_a_to_b, msg, 1024, when, &seq_no, &tx_when, &rx_when)) >= 0)
460 {
461 #if defined(ENABLE_GUI)
462 if (use_gui)
463 media_monitor_rx(seq_no, tx_when, rx_when);
464 #endif
465 t38_core = t38_gateway_get_t38_core_state(t38_state_b);
466 t38_core_rx_ifp_packet(t38_core, msg, msg_len, seq_no);
467 }
468 while ((msg_len = g1050_get(path_b_to_a, msg, 1024, when, &seq_no, &tx_when, &rx_when)) >= 0)
469 {
470 #if defined(ENABLE_GUI)
471 if (use_gui)
472 media_monitor_rx(seq_no, tx_when, rx_when);
473 #endif
474 t38_core = t38_terminal_get_t38_core_state(t38_state_a);
475 t38_core_rx_ifp_packet(t38_core, msg, msg_len, seq_no);
476 }
477 if (log_audio)
478 {
479 outframes = sf_writef_short(wave_handle, out_amp, SAMPLES_PER_CHUNK);
480 if (outframes != SAMPLES_PER_CHUNK)
481 break;
482 }
483
484 if (done[0] && done[1])
485 break;
486 #if defined(ENABLE_GUI)
487 if (use_gui)
488 media_monitor_update_display();
489 #endif
490 }
491 t38_terminal_release(t38_state_a);
492 fax_release(fax_state_b);
493 if (log_audio)
494 {
495 if (sf_close(wave_handle) != 0)
496 {
497 fprintf(stderr, " Cannot close audio file '%s'\n", OUTPUT_FILE_NAME_WAVE);
498 exit(2);
499 }
500 }
501 if (!succeeded[0] || !succeeded[1])
502 {
503 printf("Tests failed\n");
504 exit(2);
505 }
506 printf("Tests passed\n");
507 return 0;
508 }
509 /*- End of function --------------------------------------------------------*/
510 /*- End of file ------------------------------------------------------------*/