1 /*
2 * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3 * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4 *
5 * Version: MPL 1.1
6 *
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
16 *
17 * The Original Code is FreeSWITCH mod_fax.
18 *
19 * The Initial Developer of the Original Code is
20 * Massimo Cetra <devel@navynet.it>
21 *
22 * Portions created by the Initial Developer are Copyright (C)
23 * the Initial Developer. All Rights Reserved.
24 *
25 * Contributor(s):
26 *
27 * Brian West <brian@freeswitch.org>
28 * Anthony Minessale II <anthm@freeswitch.org>
29 * Steve Underwood <steveu@coppice.org>
30 * Antonio Gallo <agx@linux.it>
31 * mod_spandsp_fax.c -- Fax applications provided by SpanDSP
32 *
33 */
34
35 #include "mod_spandsp.h"
36
37 #include "udptl.h"
38
39 #define LOCAL_FAX_MAX_DATAGRAM 400
40 #define MAX_FEC_ENTRIES 4
41 #define MAX_FEC_SPAN 4
42 #define DEFAULT_FEC_ENTRIES 3
43 #define DEFAULT_FEC_SPAN 3
44
45 /*****************************************************************************
46 OUR DEFINES AND STRUCTS
47 *****************************************************************************/
48
49 typedef enum {
50 T38_MODE,
51 AUDIO_MODE,
52 T38_GATEWAY_MODE
53 } transport_mode_t;
54
55 typedef enum {
56 T38_MODE_UNKNOWN = 0,
57 T38_MODE_NEGOTIATED = 1,
58 T38_MODE_REQUESTED = 2,
59 T38_MODE_REFUSED = -1,
60 } t38_mode_t;
61
get_t38_status(t38_mode_t mode)62 const char * get_t38_status(t38_mode_t mode)
63 {
64 const char *str = "off";
65 switch(mode) {
66 case T38_MODE_NEGOTIATED:
67 str = "negotiated";
68 break;
69 case T38_MODE_REQUESTED:
70 str = "requested";
71 break;
72 case T38_MODE_REFUSED:
73 str = "refused";
74 break;
75 default:
76 break;
77 }
78 return str;
79 }
80
81
82 struct pvt_s {
83 switch_core_session_t *session;
84
85 mod_spandsp_fax_application_mode_t app_mode;
86
87 t30_state_t *t30;
88 fax_state_t *fax_state;
89 t38_terminal_state_t *t38_state;
90 t38_gateway_state_t *t38_gateway_state;
91 t38_core_state_t *t38_core;
92
93 udptl_state_t *udptl_state;
94
95 char *filename;
96 char *ident;
97 char *header;
98 char *timezone;
99
100 int use_ecm;
101 int disable_v17;
102 int enable_tep;
103 int enable_colour_fax;
104 int enable_image_resizing;
105 int enable_colour_to_bilevel;
106 int enable_grayscale_to_bilevel;
107 int verbose;
108 int caller;
109
110 int tx_page_start;
111 int tx_page_end;
112
113 int done;
114
115 t38_mode_t t38_mode;
116
117 struct pvt_s *next;
118 };
119
120 typedef struct pvt_s pvt_t;
121
122 static void launch_timer_thread(void);
123
124 static struct {
125 pvt_t *head;
126 switch_mutex_t *mutex;
127 switch_thread_t *thread;
128 int thread_running;
129 } t38_state_list;
130
131
132
wake_thread(int force)133 static void wake_thread(int force)
134 {
135 if (force) {
136 switch_thread_cond_signal(spandsp_globals.cond);
137 return;
138 }
139
140 if (switch_mutex_trylock(spandsp_globals.cond_mutex) == SWITCH_STATUS_SUCCESS) {
141 switch_thread_cond_signal(spandsp_globals.cond);
142 switch_mutex_unlock(spandsp_globals.cond_mutex);
143 }
144 }
145
add_pvt(pvt_t * pvt)146 static int add_pvt(pvt_t *pvt)
147 {
148 int r = 0;
149
150 if (t38_state_list.thread_running == 1) {
151 switch_mutex_lock(t38_state_list.mutex);
152 pvt->next = t38_state_list.head;
153 t38_state_list.head = pvt;
154 switch_mutex_unlock(t38_state_list.mutex);
155 r = 1;
156 wake_thread(0);
157 } else {
158 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error launching thread\n");
159 }
160
161 return r;
162
163 }
164
165
del_pvt(pvt_t * del_pvt)166 static int del_pvt(pvt_t *del_pvt)
167 {
168 pvt_t *p, *l = NULL;
169 int r = 0;
170
171
172 switch_mutex_lock(t38_state_list.mutex);
173
174 for (p = t38_state_list.head; p; p = p->next) {
175 if (p == del_pvt) {
176 if (l) {
177 l->next = p->next;
178 } else {
179 t38_state_list.head = p->next;
180 }
181 p->next = NULL;
182 r = 1;
183 break;
184 }
185
186 l = p;
187 }
188
189 switch_mutex_unlock(t38_state_list.mutex);
190
191 wake_thread(0);
192
193 return r;
194 }
195
timer_thread_run(switch_thread_t * thread,void * obj)196 static void *SWITCH_THREAD_FUNC timer_thread_run(switch_thread_t *thread, void *obj)
197 {
198 switch_timer_t timer = { 0 };
199 pvt_t *pvt;
200 int samples = 160;
201 int ms = 20;
202
203 if (switch_core_timer_init(&timer, "soft", ms, samples, NULL) != SWITCH_STATUS_SUCCESS) {
204 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "timer init failed.\n");
205 t38_state_list.thread_running = -1;
206 goto end;
207 }
208
209 t38_state_list.thread_running = 1;
210 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "FAX timer thread started.\n");
211
212 switch_mutex_lock(spandsp_globals.cond_mutex);
213
214 while(t38_state_list.thread_running == 1) {
215
216 switch_mutex_lock(t38_state_list.mutex);
217
218 if (!t38_state_list.head) {
219 switch_mutex_unlock(t38_state_list.mutex);
220 switch_thread_cond_wait(spandsp_globals.cond, spandsp_globals.cond_mutex);
221 switch_core_timer_sync(&timer);
222 continue;
223 }
224
225 for (pvt = t38_state_list.head; pvt; pvt = pvt->next) {
226 if (pvt->udptl_state && pvt->session && switch_channel_ready(switch_core_session_get_channel(pvt->session))) {
227 t38_terminal_send_timeout(pvt->t38_state, samples);
228 }
229 }
230
231 switch_mutex_unlock(t38_state_list.mutex);
232
233 switch_core_timer_next(&timer);
234 }
235
236 switch_mutex_unlock(spandsp_globals.cond_mutex);
237
238 end:
239 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "FAX timer thread ended.\n");
240
241 if (timer.timer_interface) {
242 switch_core_timer_destroy(&timer);
243 }
244
245 return NULL;
246 }
247
launch_timer_thread(void)248 static void launch_timer_thread(void)
249 {
250
251 switch_threadattr_t *thd_attr = NULL;
252
253 switch_threadattr_create(&thd_attr, spandsp_globals.pool);
254 switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
255 switch_thread_create(&t38_state_list.thread, thd_attr, timer_thread_run, NULL, spandsp_globals.pool);
256 }
257
258
259 /*****************************************************************************
260 LOGGING AND HELPER FUNCTIONS
261 *****************************************************************************/
262
counter_increment(void)263 static void counter_increment(void)
264 {
265 switch_mutex_lock(spandsp_globals.mutex);
266 spandsp_globals.total_sessions++;
267 switch_mutex_unlock(spandsp_globals.mutex);
268 }
269
mod_spandsp_log_message(void * user_data,int level,const char * msg)270 void mod_spandsp_log_message(void *user_data, int level, const char *msg)
271 {
272 int fs_log_level;
273 switch_core_session_t *session = (switch_core_session_t *)user_data;
274
275 switch (level) {
276 case SPAN_LOG_NONE:
277 return;
278 case SPAN_LOG_ERROR:
279 case SPAN_LOG_PROTOCOL_ERROR:
280 fs_log_level = SWITCH_LOG_ERROR;
281 break;
282 case SPAN_LOG_WARNING:
283 case SPAN_LOG_PROTOCOL_WARNING:
284 fs_log_level = SWITCH_LOG_WARNING;
285 break;
286 case SPAN_LOG_FLOW:
287 case SPAN_LOG_FLOW_2:
288 case SPAN_LOG_FLOW_3:
289 default: /* SPAN_LOG_DEBUG, SPAN_LOG_DEBUG_2, SPAN_LOG_DEBUG_3 */
290 fs_log_level = SWITCH_LOG_DEBUG;
291 break;
292 }
293
294 if (!zstr(msg)) {
295 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), fs_log_level, "%s", msg);
296 }
297 }
298
phase_b_handler(void * user_data,int result)299 static int phase_b_handler(void *user_data, int result)
300 {
301 t30_stats_t t30_stats;
302 switch_core_session_t *session;
303 switch_channel_t *channel;
304 const char *local_ident;
305 const char *far_ident;
306 char *fax_transfer_rate = NULL;
307 char *fax_document_total_pages = NULL;
308 pvt_t *pvt;
309 switch_event_t *event;
310
311 pvt = (pvt_t *) user_data;
312 switch_assert(pvt);
313
314 session = pvt->session;
315 switch_assert(session);
316
317 channel = switch_core_session_get_channel(session);
318 switch_assert(channel);
319
320 t30_get_transfer_statistics(pvt->t30, &t30_stats);
321
322 local_ident = switch_str_nil(t30_get_tx_ident(pvt->t30));
323 far_ident = switch_str_nil(t30_get_rx_ident(pvt->t30));
324
325 fax_transfer_rate = switch_core_session_sprintf(session, "%i", t30_stats.bit_rate);
326 if (fax_transfer_rate) {
327 switch_channel_set_variable(channel, "fax_transfer_rate", fax_transfer_rate);
328 }
329
330 if (pvt->app_mode == FUNCTION_TX) {
331 fax_document_total_pages = switch_core_session_sprintf(session, "%i", t30_stats.pages_in_file);
332 if (fax_document_total_pages) {
333 switch_channel_set_variable(channel, "fax_document_total_pages", fax_document_total_pages);
334 }
335 }
336
337 switch_channel_set_variable(channel, "fax_ecm_used", (t30_stats.error_correcting_mode) ? "on" : "off");
338 switch_channel_set_variable(channel, "fax_t38_status", get_t38_status(pvt->t38_mode));
339 switch_channel_set_variable(channel, "fax_local_station_id", local_ident);
340 switch_channel_set_variable(channel, "fax_remote_station_id", far_ident);
341 switch_channel_set_variable(channel, "fax_remote_country", switch_str_nil(t30_get_rx_country(pvt->t30)));
342 switch_channel_set_variable(channel, "fax_remote_vendor", switch_str_nil(t30_get_rx_vendor(pvt->t30)));
343 switch_channel_set_variable(channel, "fax_remote_model", switch_str_nil(t30_get_rx_model(pvt->t30)));
344
345
346 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "=== Negotiation Result =======================================================\n");
347 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Remote station id: %s\n", far_ident);
348 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Local station id: %s\n", local_ident);
349 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Transfer Rate: %i\n", t30_stats.bit_rate);
350 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "ECM status %s\n", (t30_stats.error_correcting_mode) ? "on" : "off");
351 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "T38 status %s\n", get_t38_status(pvt->t38_mode));
352 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "remote country: %s\n", switch_str_nil(t30_get_rx_country(pvt->t30)));
353 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "remote vendor: %s\n", switch_str_nil(t30_get_rx_vendor(pvt->t30)));
354 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "remote model: %s\n", switch_str_nil(t30_get_rx_model(pvt->t30)));
355 if (pvt->app_mode == FUNCTION_TX) {
356 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Total fax pages: %s\n", fax_document_total_pages);
357 }
358
359 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "==============================================================================\n");
360
361 switch_channel_execute_on(channel, "execute_on_fax_phase_b");
362
363 /* Fire event */
364
365 if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, pvt->app_mode == FUNCTION_TX ? SPANDSP_EVENT_TXFAXNEGOCIATERESULT : SPANDSP_EVENT_RXFAXNEGOCIATERESULT) == SWITCH_STATUS_SUCCESS) {
366 switch_channel_event_set_data(channel, event);
367
368 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "uuid", switch_core_session_get_uuid(session));
369 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-transfer-rate", fax_transfer_rate);
370 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-ecm-used", (t30_stats.error_correcting_mode) ? "on" : "off");
371 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-t38-status", get_t38_status(pvt->t38_mode));
372 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-local-station-id", local_ident);
373 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-remote-station-id", far_ident);
374 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-remote-country", switch_str_nil(t30_get_rx_country(pvt->t30)));
375 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-remote-vendor", switch_str_nil(t30_get_rx_vendor(pvt->t30)));
376 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-remote-model", switch_str_nil(t30_get_rx_model(pvt->t30)));
377 if (pvt->app_mode == FUNCTION_TX) {
378 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-document-total-pages", fax_document_total_pages);
379 }
380 switch_event_fire(&event);
381 }
382
383 return T30_ERR_OK;
384 }
385
phase_d_handler(void * user_data,int msg)386 static int phase_d_handler(void *user_data, int msg)
387 {
388 t30_stats_t t30_stats;
389 char *fax_file_image_resolution = NULL;
390 char *fax_line_image_resolution = NULL;
391 char *fax_file_image_pixel_size = NULL;
392 char *fax_line_image_pixel_size = NULL;
393 char *fax_image_size = NULL;
394 char *fax_bad_rows = NULL;
395 char *fax_encoding = NULL;
396 char *fax_longest_bad_row_run = NULL;
397 char *fax_document_transferred_pages = NULL;
398 char *fax_document_total_pages = NULL;
399 switch_core_session_t *session;
400 switch_channel_t *channel;
401 pvt_t *pvt;
402 switch_event_t *event;
403
404 pvt = (pvt_t *) user_data;
405 switch_assert(pvt);
406
407 session = pvt->session;
408 switch_assert(session);
409
410 channel = switch_core_session_get_channel(session);
411 switch_assert(channel);
412
413 t30_get_transfer_statistics(pvt->t30, &t30_stats);
414
415 /* Set Channel Variable */
416
417 fax_line_image_resolution = switch_core_session_sprintf(session, "%ix%i", t30_stats.x_resolution, t30_stats.y_resolution);
418 if (fax_line_image_resolution) {
419 switch_channel_set_variable(channel, "fax_image_resolution", fax_line_image_resolution);
420 }
421
422 fax_file_image_resolution = switch_core_session_sprintf(session, "%ix%i", t30_stats.image_x_resolution, t30_stats.image_y_resolution);
423 if (fax_file_image_resolution) {
424 switch_channel_set_variable(channel, "fax_file_image_resolution", fax_file_image_resolution);
425 }
426
427 fax_line_image_pixel_size = switch_core_session_sprintf(session, "%ix%i", t30_stats.width, t30_stats.length);
428 if (fax_line_image_pixel_size) {
429 switch_channel_set_variable(channel, "fax_image_pixel_size", fax_line_image_pixel_size);;
430 }
431
432 fax_file_image_pixel_size = switch_core_session_sprintf(session, "%ix%i", t30_stats.image_width, t30_stats.image_length);
433 if (fax_file_image_pixel_size) {
434 switch_channel_set_variable(channel, "fax_file_image_pixel_size", fax_file_image_pixel_size);;
435 }
436
437 fax_image_size = switch_core_session_sprintf(session, "%d", t30_stats.image_size);
438 if (fax_image_size) {
439 switch_channel_set_variable(channel, "fax_image_size", fax_image_size);
440 }
441
442 fax_bad_rows = switch_core_session_sprintf(session, "%d", t30_stats.bad_rows);
443 if (fax_bad_rows) {
444 switch_channel_set_variable(channel, "fax_bad_rows", fax_bad_rows);
445 }
446
447 fax_longest_bad_row_run = switch_core_session_sprintf(session, "%d", t30_stats.longest_bad_row_run);
448 if (fax_longest_bad_row_run) {
449 switch_channel_set_variable(channel, "fax_longest_bad_row_run", fax_longest_bad_row_run);
450 }
451
452 fax_encoding = switch_core_session_sprintf(session, "%d", t30_stats.compression);
453 if (fax_encoding) {
454 switch_channel_set_variable(channel, "fax_encoding", fax_encoding);
455 }
456
457 switch_channel_set_variable(channel, "fax_encoding_name", t4_compression_to_str(t30_stats.compression));
458
459 fax_document_transferred_pages = switch_core_session_sprintf(session, "%d", (pvt->app_mode == FUNCTION_TX) ? t30_stats.pages_tx : t30_stats.pages_rx);
460 if (fax_document_transferred_pages) {
461 switch_channel_set_variable(channel, "fax_document_transferred_pages", fax_document_transferred_pages);
462 }
463
464 if (pvt->app_mode == FUNCTION_TX) {
465 fax_document_total_pages = switch_core_session_sprintf(session, "%i", t30_stats.pages_in_file);
466 if (fax_document_total_pages) {
467 switch_channel_set_variable(channel, "fax_document_total_pages", fax_document_total_pages);
468 }
469 }
470
471 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "==== Page %s===========================================================\n", pvt->app_mode == FUNCTION_TX ? "Sent ====": "Received ");
472 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Page no = %d\n", (pvt->app_mode == FUNCTION_TX) ? t30_stats.pages_tx : t30_stats.pages_rx);
473 if (pvt->app_mode == FUNCTION_TX) {
474 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Total fax pages: %s\n", fax_document_total_pages);
475 }
476 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Image type = %s (%s in the file)\n", t4_image_type_to_str(t30_stats.type), t4_image_type_to_str(t30_stats.image_type));
477 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Image size = %d x %d pixels (%d x %d pixels in the file)\n", t30_stats.width, t30_stats.length, t30_stats.image_width, t30_stats.image_length);
478 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Image resolution = %d/m x %d/m (%d/m x %d/m in the file)\n", t30_stats.x_resolution, t30_stats.y_resolution, t30_stats.image_x_resolution, t30_stats.image_y_resolution);
479 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Compression = %s (%d)\n", t4_compression_to_str(t30_stats.compression), t30_stats.compression);
480 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Compressed image size = %d bytes\n", t30_stats.image_size);
481 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Bad rows = %d\n", t30_stats.bad_rows);
482 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Longest bad row run = %d\n", t30_stats.longest_bad_row_run);
483 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "==============================================================================\n");
484
485 switch_channel_execute_on(channel, "execute_on_fax_phase_d");
486
487 if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, pvt->app_mode == FUNCTION_TX ? SPANDSP_EVENT_TXFAXPAGERESULT : SPANDSP_EVENT_RXFAXPAGERESULT) == SWITCH_STATUS_SUCCESS) {
488 switch_channel_event_set_data(channel, event);
489
490 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "uuid", switch_core_session_get_uuid(session));
491 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-document-transferred-pages", fax_document_transferred_pages);
492 if (pvt->app_mode == FUNCTION_TX) {
493 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-document-total-pages", fax_document_total_pages);
494 }
495 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-image-resolution", fax_line_image_resolution);
496 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-file-image-resolution", fax_file_image_resolution);
497 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-image-size", fax_image_size);
498 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-image-pixel-size", fax_line_image_pixel_size);
499 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-file-image-pixel-size", fax_file_image_pixel_size);
500 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-bad-rows", fax_bad_rows);
501 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-longest-bad-row-run", fax_longest_bad_row_run);
502 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-encoding", fax_encoding);
503 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-encoding-name", t4_compression_to_str(t30_stats.compression));
504 switch_event_fire(&event);
505 }
506
507 return T30_ERR_OK; /* I don't think this does anything */
508 }
509
510 /*
511 * Called at the end of the document
512 */
phase_e_handler(void * user_data,int result)513 static void phase_e_handler(void *user_data, int result)
514 {
515 t30_stats_t t;
516 const char *local_ident;
517 const char *far_ident;
518 switch_core_session_t *session;
519 switch_channel_t *channel;
520 pvt_t *pvt;
521 char *fax_document_transferred_pages = NULL;
522 char *fax_document_total_pages = NULL;
523 char *fax_image_resolution = NULL;
524 char *fax_image_size = NULL;
525 char *fax_bad_rows = NULL;
526 char *fax_transfer_rate = NULL;
527 char *fax_result_code = NULL;
528 switch_event_t *event;
529 const char *var;
530 char *expanded;
531
532 pvt = (pvt_t *) user_data;
533 switch_assert(pvt);
534
535 session = pvt->session;
536 switch_assert(session);
537
538 channel = switch_core_session_get_channel(session);
539 switch_assert(channel);
540
541 t30_get_transfer_statistics(pvt->t30, &t);
542 local_ident = switch_str_nil(t30_get_tx_ident(pvt->t30));
543 far_ident = switch_str_nil(t30_get_rx_ident(pvt->t30));
544
545 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "==============================================================================\n");
546
547 if (result == T30_ERR_OK) {
548 if (pvt->app_mode == FUNCTION_TX) {
549 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Fax successfully sent.\n");
550 } else if (pvt->app_mode == FUNCTION_RX) {
551 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Fax successfully received.\n");
552 } else {
553 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Fax successfully managed. How ?\n");
554 }
555 switch_channel_set_variable(channel, "fax_success", "1");
556 } else {
557 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Fax processing not successful - result (%d) %s.\n", result,
558 t30_completion_code_to_str(result));
559 switch_channel_set_variable(channel, "fax_success", "0");
560 }
561
562 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Remote station id: %s\n", far_ident);
563 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Local station id: %s\n", local_ident);
564 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Pages transferred: %i\n",
565 pvt->app_mode == FUNCTION_TX ? t.pages_tx : t.pages_rx);
566 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Total fax pages: %i\n", t.pages_in_file);
567 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Image resolution: %ix%i\n", t.x_resolution, t.y_resolution);
568 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Transfer Rate: %i\n", t.bit_rate);
569
570 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "ECM status %s\n", (t.error_correcting_mode) ? "on" : "off");
571 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "T38 status %s\n", get_t38_status(pvt->t38_mode));
572 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "remote country: %s\n", switch_str_nil(t30_get_rx_country(pvt->t30)));
573 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "remote vendor: %s\n", switch_str_nil(t30_get_rx_vendor(pvt->t30)));
574 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "remote model: %s\n", switch_str_nil(t30_get_rx_model(pvt->t30)));
575
576 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "==============================================================================\n");
577
578 /*
579 Set our channel variables, variables are also used in event
580 */
581
582 fax_result_code = switch_core_session_sprintf(session, "%i", result);
583 if (fax_result_code) {
584 switch_channel_set_variable(channel, "fax_result_code", fax_result_code);
585 }
586
587 switch_channel_set_variable(channel, "fax_result_text", t30_completion_code_to_str(result));
588
589 switch_channel_set_variable(channel, "fax_ecm_used", (t.error_correcting_mode) ? "on" : "off");
590 switch_channel_set_variable(channel, "fax_t38_status", get_t38_status(pvt->t38_mode));
591 switch_channel_set_variable(channel, "fax_local_station_id", local_ident);
592 switch_channel_set_variable(channel, "fax_remote_station_id", far_ident);
593
594 fax_document_transferred_pages = switch_core_session_sprintf(session, "%i", pvt->app_mode == FUNCTION_TX ? t.pages_tx : t.pages_rx);
595 if (fax_document_transferred_pages) {
596 switch_channel_set_variable(channel, "fax_document_transferred_pages", fax_document_transferred_pages);
597 }
598
599 fax_document_total_pages = switch_core_session_sprintf(session, "%i", t.pages_in_file);
600 if (fax_document_total_pages) {
601 switch_channel_set_variable(channel, "fax_document_total_pages", fax_document_total_pages);
602 }
603
604 fax_image_resolution = switch_core_session_sprintf(session, "%ix%i", t.x_resolution, t.y_resolution);
605 if (fax_image_resolution) {
606 switch_channel_set_variable(channel, "fax_image_resolution", fax_image_resolution);
607 }
608
609 fax_image_size = switch_core_session_sprintf(session, "%d", t.image_size);
610 if (fax_image_size) {
611 switch_channel_set_variable(channel, "fax_image_size", fax_image_size);
612 }
613
614 fax_bad_rows = switch_core_session_sprintf(session, "%d", t.bad_rows);
615 if (fax_bad_rows) {
616 switch_channel_set_variable(channel, "fax_bad_rows", fax_bad_rows);
617 }
618
619 fax_transfer_rate = switch_core_session_sprintf(session, "%i", t.bit_rate);
620 if (fax_transfer_rate) {
621 switch_channel_set_variable(channel, "fax_transfer_rate", fax_transfer_rate);
622 }
623
624 /* switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING); */
625
626 pvt->done = 1;
627
628 /* Fire event */
629
630 if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, pvt->app_mode == FUNCTION_TX ? SPANDSP_EVENT_TXFAXRESULT : SPANDSP_EVENT_RXFAXRESULT) == SWITCH_STATUS_SUCCESS) {
631 switch_channel_event_set_data(channel, event);
632
633 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-success", (result == T30_ERR_OK) ? "1" : "0");
634 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-result-code", fax_result_code);
635 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-result-text", t30_completion_code_to_str(result));
636 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-document-transferred-pages", fax_document_transferred_pages);
637 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-document-total-pages", fax_document_total_pages);
638 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-image-resolution", fax_image_resolution);
639 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-image-size", fax_image_size);
640 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-bad-rows", fax_bad_rows);
641 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-transfer-rate", fax_transfer_rate);
642 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-ecm-used", (t.error_correcting_mode) ? "on" : "off");
643 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-t38-status", get_t38_status(pvt->t38_mode));
644 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-local-station-id", local_ident);
645 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "fax-remote-station-id", far_ident);
646 switch_event_fire(&event);
647 }
648
649 if ((var = switch_channel_get_variable(channel, "system_on_fax_result"))) {
650 expanded = switch_channel_expand_variables(channel, var);
651 switch_system(expanded, SWITCH_FALSE);
652 if (expanded != var) {
653 free(expanded);
654 }
655 }
656
657 switch_channel_execute_on(channel, "execute_on_fax_result");
658
659 if (result == T30_ERR_OK) {
660 if ((var = switch_channel_get_variable(channel, "system_on_fax_success"))) {
661 expanded = switch_channel_expand_variables(channel, var);
662 switch_system(expanded, SWITCH_FALSE);
663 if (expanded != var) {
664 free(expanded);
665 }
666 }
667 switch_channel_execute_on(channel, "execute_on_fax_success");
668 switch_channel_api_on(channel, "api_on_fax_success");
669 } else {
670 if ((var = switch_channel_get_variable(channel, "system_on_fax_failure"))) {
671 expanded = switch_channel_expand_variables(channel, var);
672 switch_system(expanded, SWITCH_FALSE);
673 if (expanded != var) {
674 free(expanded);
675 }
676 }
677 switch_channel_execute_on(channel, "execute_on_fax_failure");
678 switch_channel_api_on(channel, "api_on_fax_failure");
679 }
680 }
681
t38_tx_packet_handler(t38_core_state_t * s,void * user_data,const uint8_t * buf,int len,int count)682 static int t38_tx_packet_handler(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
683 {
684 switch_frame_t out_frame = { 0 };
685 switch_core_session_t *session;
686 pvt_t *pvt;
687 uint8_t pkt[LOCAL_FAX_MAX_DATAGRAM];
688 int x;
689 int r = 0;
690
691 pvt = (pvt_t *) user_data;
692 session = pvt->session;
693
694 /* we need to build a real packet here and make write_frame.packet and write_frame.packetlen point to it */
695 out_frame.flags = SFF_UDPTL_PACKET | SFF_PROXY_PACKET;
696 out_frame.packet = pkt;
697 out_frame.buflen = LOCAL_FAX_MAX_DATAGRAM;
698 if ((r = udptl_build_packet(pvt->udptl_state, pkt, buf, len)) > 0) {
699 out_frame.packetlen = r;
700 //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "WRITE %d udptl bytes\n", out_frame.packetlen);
701
702 for (x = 0; x < count; x++) {
703 if (switch_core_session_write_frame(session, &out_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
704 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "INVALID WRITE: %d:%d\n", out_frame.packetlen, count);
705 r = -1;
706 break;
707 }
708 }
709 } else {
710 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "INVALID PACKETLEN: %d PASSED: %d:%d\n", r, len, count);
711 }
712
713 if (r < 0) {
714 t30_state_t *t30;
715
716 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "TERMINATING T30 STATE\n");
717
718 if (pvt->t38_state && (t30 = t38_terminal_get_t30_state(pvt->t38_state))) {
719 t30_terminate(t30);
720 }
721 switch_yield(10000);
722 }
723
724 return r < 0 ? r : 0;
725 }
726
spanfax_init(pvt_t * pvt,transport_mode_t trans_mode)727 static switch_status_t spanfax_init(pvt_t *pvt, transport_mode_t trans_mode)
728 {
729 switch_core_session_t *session;
730 switch_channel_t *channel;
731 fax_state_t *fax;
732 t38_terminal_state_t *t38;
733 t30_state_t *t30;
734 const char *tmp;
735 const char *tz;
736 int fec_entries = DEFAULT_FEC_ENTRIES;
737 int fec_span = DEFAULT_FEC_SPAN;
738 int compressions;
739
740 session = (switch_core_session_t *) pvt->session;
741 switch_assert(session);
742
743 channel = switch_core_session_get_channel(session);
744 switch_assert(channel);
745
746 if ((tmp = switch_channel_get_variable(channel, "t38_gateway_redundancy"))) {
747 int tmp_value;
748 tmp_value = atoi(tmp);
749 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "FAX changing redundancy from %d:%d to %d:%d\n", fec_span, fec_entries, tmp_value, tmp_value );
750 fec_entries = tmp_value;
751 fec_span = tmp_value;
752 }
753
754 switch (trans_mode) {
755 case AUDIO_MODE:
756 if (pvt->fax_state == NULL) {
757 pvt->fax_state = (fax_state_t *) switch_core_session_alloc(pvt->session, sizeof(fax_state_t));
758 }
759 if (pvt->fax_state == NULL) {
760 return SWITCH_STATUS_FALSE;
761 }
762
763 fax = pvt->fax_state;
764 pvt->t30 = fax_get_t30_state(fax);
765 t30 = pvt->t30;
766
767 memset(fax, 0, sizeof(fax_state_t));
768 if (fax_init(fax, pvt->caller) == NULL) {
769 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot initialize my fax structs\n");
770 return SWITCH_STATUS_FALSE;
771 }
772
773 fax_set_transmit_on_idle(fax, TRUE);
774
775 span_log_set_message_handler(fax_get_logging_state(fax), mod_spandsp_log_message, pvt->session);
776 span_log_set_message_handler(t30_get_logging_state(t30), mod_spandsp_log_message, pvt->session);
777
778 if (pvt->verbose) {
779 span_log_set_level(fax_get_logging_state(fax), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
780 span_log_set_level(t30_get_logging_state(t30), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
781 }
782 break;
783 case T38_MODE:
784 {
785 switch_core_session_message_t msg = { 0 };
786
787 if (pvt->t38_state == NULL) {
788 pvt->t38_state = (t38_terminal_state_t *) switch_core_session_alloc(pvt->session, sizeof(t38_terminal_state_t));
789 }
790 if (pvt->t38_state == NULL) {
791 return SWITCH_STATUS_FALSE;
792 }
793 if (pvt->udptl_state == NULL) {
794 pvt->udptl_state = (udptl_state_t *) switch_core_session_alloc(pvt->session, sizeof(udptl_state_t));
795 }
796 if (pvt->udptl_state == NULL) {
797 t38_terminal_free(pvt->t38_state);
798 pvt->t38_state = NULL;
799 return SWITCH_STATUS_FALSE;
800 }
801
802 t38 = pvt->t38_state;
803 pvt->t30 = t38_terminal_get_t30_state(t38);
804 t30 = pvt->t30;
805
806 memset(t38, 0, sizeof(t38_terminal_state_t));
807
808 if (t38_terminal_init(t38, pvt->caller, t38_tx_packet_handler, pvt) == NULL) {
809 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot initialize my T.38 structs\n");
810 return SWITCH_STATUS_FALSE;
811 }
812
813 pvt->t38_core = t38_terminal_get_t38_core_state(pvt->t38_state);
814
815 if (udptl_init(pvt->udptl_state, UDPTL_ERROR_CORRECTION_REDUNDANCY, fec_span, fec_entries,
816 (udptl_rx_packet_handler_t *) t38_core_rx_ifp_packet, (void *) pvt->t38_core) == NULL) {
817 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot initialize my UDPTL structs\n");
818 return SWITCH_STATUS_FALSE;
819 }
820
821 msg.from = __FILE__;
822 msg.message_id = SWITCH_MESSAGE_INDICATE_UDPTL_MODE;
823 switch_core_session_receive_message(pvt->session, &msg);
824
825 /* add to timer thread processing */
826 if (!add_pvt(pvt)) {
827 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
828 }
829
830 span_log_set_message_handler(t38_terminal_get_logging_state(t38), mod_spandsp_log_message, pvt->session);
831 span_log_set_message_handler(t30_get_logging_state(t30), mod_spandsp_log_message, pvt->session);
832
833 if (pvt->verbose) {
834 span_log_set_level(t38_terminal_get_logging_state(t38), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
835 span_log_set_level(t30_get_logging_state(t30), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
836 }
837 }
838 break;
839 case T38_GATEWAY_MODE:
840 if (pvt->t38_gateway_state == NULL) {
841 pvt->t38_gateway_state = (t38_gateway_state_t *) switch_core_session_alloc(pvt->session, sizeof(t38_gateway_state_t));
842 }
843
844 if (pvt->udptl_state == NULL) {
845 pvt->udptl_state = (udptl_state_t *) switch_core_session_alloc(pvt->session, sizeof(udptl_state_t));
846 }
847
848 if (t38_gateway_init(pvt->t38_gateway_state, t38_tx_packet_handler, pvt) == NULL) {
849 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot initialize my T.38 structs\n");
850 t38_gateway_free(pvt->t38_gateway_state);
851 pvt->t38_gateway_state = NULL;
852
853 return SWITCH_STATUS_FALSE;
854 }
855
856 pvt->t38_core = t38_gateway_get_t38_core_state(pvt->t38_gateway_state);
857
858 if (udptl_init(pvt->udptl_state, UDPTL_ERROR_CORRECTION_REDUNDANCY, fec_span, fec_entries,
859 (udptl_rx_packet_handler_t *) t38_core_rx_ifp_packet, (void *) pvt->t38_core) == NULL) {
860 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot initialize my UDPTL structs\n");
861 t38_gateway_free(pvt->t38_gateway_state);
862 udptl_release(pvt->udptl_state);
863 pvt->udptl_state = NULL;
864 return SWITCH_STATUS_FALSE;
865 }
866
867 t38_gateway_set_transmit_on_idle(pvt->t38_gateway_state, TRUE);
868
869 if (switch_true(switch_channel_get_variable(channel, "fax_v17_disabled"))) {
870 t38_gateway_set_supported_modems(pvt->t38_gateway_state, T30_SUPPORT_V29 | T30_SUPPORT_V27TER);
871 } else {
872 t38_gateway_set_supported_modems(pvt->t38_gateway_state, T30_SUPPORT_V17 | T30_SUPPORT_V29 | T30_SUPPORT_V27TER);
873 }
874
875 t38_gateway_set_tep_mode(pvt->t38_gateway_state, pvt->enable_tep);
876
877 t38_gateway_set_ecm_capability(pvt->t38_gateway_state, pvt->use_ecm);
878 switch_channel_set_variable(channel, "fax_ecm_requested", pvt->use_ecm ? "true" : "false");
879
880 span_log_set_message_handler(t38_gateway_get_logging_state(pvt->t38_gateway_state), mod_spandsp_log_message, pvt->session);
881 span_log_set_message_handler(t38_core_get_logging_state(pvt->t38_core), mod_spandsp_log_message, pvt->session);
882
883 if (pvt->verbose) {
884 span_log_set_level(t38_gateway_get_logging_state(pvt->t38_gateway_state), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
885 span_log_set_level(t38_core_get_logging_state(pvt->t38_core), SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
886 }
887
888 t38_set_t38_version(pvt->t38_core, 0);
889
890 return SWITCH_STATUS_SUCCESS;
891 default:
892 assert(0); /* What? */
893 return SWITCH_STATUS_SUCCESS;
894 } /* Switch trans mode */
895
896 /* All the things which are common to audio and T.38 FAX setup */
897 t30_set_tx_ident(t30, pvt->ident);
898 t30_set_tx_page_header_info(t30, pvt->header);
899 if (pvt->timezone && pvt->timezone[0]) {
900 if ((tz = switch_lookup_timezone(pvt->timezone)))
901 t30_set_tx_page_header_tz(t30, tz);
902 else
903 t30_set_tx_page_header_tz(t30, pvt->timezone);
904 }
905
906 t30_set_phase_e_handler(t30, phase_e_handler, pvt);
907 t30_set_phase_d_handler(t30, phase_d_handler, pvt);
908 t30_set_phase_b_handler(t30, phase_b_handler, pvt);
909
910 t30_set_supported_image_sizes(t30,
911 T4_SUPPORT_LENGTH_US_LETTER
912 | T4_SUPPORT_LENGTH_US_LEGAL
913 | T4_SUPPORT_LENGTH_UNLIMITED
914 | T4_SUPPORT_WIDTH_215MM
915 | T4_SUPPORT_WIDTH_255MM
916 | T4_SUPPORT_WIDTH_303MM);
917 t30_set_supported_bilevel_resolutions(t30,
918 T4_RESOLUTION_R8_STANDARD
919 | T4_RESOLUTION_R8_FINE
920 | T4_RESOLUTION_R8_SUPERFINE
921 | T4_RESOLUTION_R16_SUPERFINE
922 | T4_RESOLUTION_200_100
923 | T4_RESOLUTION_200_200
924 | T4_RESOLUTION_200_400
925 | T4_RESOLUTION_400_400);
926 compressions = T4_COMPRESSION_T4_1D
927 | T4_COMPRESSION_T4_2D
928 | T4_COMPRESSION_T6
929 | T4_COMPRESSION_T85
930 | T4_COMPRESSION_T85_L0;
931 if (pvt->enable_colour_fax) {
932 t30_set_supported_colour_resolutions(t30, T4_RESOLUTION_100_100
933 | T4_RESOLUTION_200_200
934 | T4_RESOLUTION_300_300
935 | T4_RESOLUTION_400_400);
936 compressions |= (T4_COMPRESSION_COLOUR | T4_COMPRESSION_T42_T81);
937 } else {
938 t30_set_supported_colour_resolutions(t30, 0);
939 }
940 if (pvt->enable_image_resizing)
941 compressions |= T4_COMPRESSION_RESCALING;
942 if (pvt->enable_colour_to_bilevel)
943 compressions |= T4_COMPRESSION_COLOUR_TO_BILEVEL;
944 if (pvt->enable_grayscale_to_bilevel)
945 compressions |= T4_COMPRESSION_GRAY_TO_BILEVEL;
946
947 t30_set_supported_compressions(t30, compressions);
948
949 if (pvt->disable_v17) {
950 t30_set_supported_modems(t30, T30_SUPPORT_V29 | T30_SUPPORT_V27TER);
951 switch_channel_set_variable(channel, "fax_v17_disabled", "1");
952 } else {
953 t30_set_supported_modems(t30, T30_SUPPORT_V29 | T30_SUPPORT_V27TER | T30_SUPPORT_V17);
954 switch_channel_set_variable(channel, "fax_v17_disabled", "0");
955 }
956
957 if (pvt->use_ecm) {
958 t30_set_ecm_capability(t30, TRUE);
959 switch_channel_set_variable(channel, "fax_ecm_requested", "1");
960 } else {
961 t30_set_ecm_capability(t30, FALSE);
962 switch_channel_set_variable(channel, "fax_ecm_requested", "0");
963 }
964
965 if (pvt->app_mode == FUNCTION_TX) {
966 t30_set_tx_file(t30, pvt->filename, pvt->tx_page_start, pvt->tx_page_end);
967 } else {
968 t30_set_rx_file(t30, pvt->filename, -1);
969 }
970 switch_channel_set_variable(channel, "fax_filename", pvt->filename);
971
972 return SWITCH_STATUS_SUCCESS;
973 }
974
spanfax_destroy(pvt_t * pvt)975 static switch_status_t spanfax_destroy(pvt_t *pvt)
976 {
977 int terminate;
978 t30_state_t *t30;
979
980 if (!pvt) return SWITCH_STATUS_FALSE;
981
982 if (pvt->fax_state) {
983 if (pvt->t38_state) {
984 terminate = 0;
985 } else {
986 terminate = 1;
987 }
988
989 t30 = fax_get_t30_state(pvt->fax_state);
990 if (terminate && t30) {
991 t30_terminate(t30);
992 }
993
994 fax_release(pvt->fax_state);
995 }
996
997 if (pvt->t38_state) {
998
999 /* remove from timer thread processing */
1000 del_pvt(pvt);
1001
1002 if (pvt->t38_state) {
1003 terminate = 1;
1004 } else {
1005 terminate = 0;
1006 }
1007
1008 t30 = t38_terminal_get_t30_state(pvt->t38_state);
1009 if (terminate && t30) {
1010 t30_terminate(t30);
1011 }
1012
1013 t38_terminal_release(pvt->t38_state);
1014 }
1015
1016 if (pvt->t38_gateway_state) {
1017 t38_gateway_release(pvt->t38_gateway_state);
1018 }
1019
1020 if (pvt->udptl_state) {
1021 udptl_release(pvt->udptl_state);
1022 }
1023 return SWITCH_STATUS_SUCCESS;
1024 }
1025
configure_t38(pvt_t * pvt)1026 static t38_mode_t configure_t38(pvt_t *pvt)
1027 {
1028 switch_core_session_t *session;
1029 switch_channel_t *channel;
1030 switch_t38_options_t *t38_options;
1031 int method = 2;
1032
1033 switch_assert(pvt && pvt->session);
1034 session = pvt->session;
1035 channel = switch_core_session_get_channel(session);
1036 t38_options = switch_channel_get_private(channel, "t38_options");
1037
1038 if (!t38_options || !pvt->t38_core) {
1039 pvt->t38_mode = T38_MODE_REFUSED;
1040 return pvt->t38_mode;
1041 }
1042
1043 t38_set_t38_version(pvt->t38_core, t38_options->T38FaxVersion);
1044 t38_set_max_buffer_size(pvt->t38_core, t38_options->T38FaxMaxBuffer);
1045 t38_set_fastest_image_data_rate(pvt->t38_core, t38_options->T38MaxBitRate);
1046 t38_set_fill_bit_removal(pvt->t38_core, t38_options->T38FaxFillBitRemoval);
1047 t38_set_mmr_transcoding(pvt->t38_core, t38_options->T38FaxTranscodingMMR);
1048 t38_set_jbig_transcoding(pvt->t38_core, t38_options->T38FaxTranscodingJBIG);
1049 t38_set_max_datagram_size(pvt->t38_core, t38_options->T38FaxMaxDatagram);
1050
1051 if (t38_options->T38FaxRateManagement) {
1052 if (!strcasecmp(t38_options->T38FaxRateManagement, "transferredTCF")) {
1053 method = 2;
1054 } else {
1055 method = 1;
1056 }
1057 }
1058
1059 t38_set_data_rate_management_method(pvt->t38_core, method);
1060
1061
1062 //t38_set_data_transport_protocol(pvt->t38_core, int data_transport_protocol);
1063 //t38_set_redundancy_control(pvt->t38_core, int category, int setting);
1064
1065 return pvt->t38_mode;
1066 }
1067
negotiate_t38(pvt_t * pvt)1068 static t38_mode_t negotiate_t38(pvt_t *pvt)
1069 {
1070 switch_core_session_t *session = pvt->session;
1071 switch_channel_t *channel = switch_core_session_get_channel(session);
1072 switch_core_session_message_t msg = { 0 };
1073 switch_t38_options_t *t38_options = switch_channel_get_private(channel, "t38_options");
1074 int enabled = 0, insist = 0;
1075 const char *v;
1076
1077 pvt->t38_mode = T38_MODE_REFUSED;
1078
1079 if (pvt->app_mode == FUNCTION_GW) {
1080 enabled = 1;
1081 } else if ((v = switch_channel_get_variable(channel, "fax_enable_t38"))) {
1082 enabled = switch_true(v);
1083 } else {
1084 enabled = spandsp_globals.enable_t38;
1085 }
1086
1087 if (!(enabled && t38_options)) {
1088 /* if there is no t38_options the endpoint will refuse the transition */
1089 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "%s NO T38 options detected.\n", switch_channel_get_name(channel));
1090 switch_channel_set_private(channel, "t38_options", NULL);
1091 } else {
1092 pvt->t38_mode = T38_MODE_NEGOTIATED;
1093 switch_channel_set_app_flag_key("T38", channel, CF_APP_T38_NEGOTIATED);
1094
1095 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "T38 SDP Origin = %s\n", t38_options->sdp_o_line);
1096 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "T38FaxVersion = %d\n", t38_options->T38FaxVersion);
1097 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "T38MaxBitRate = %d\n", t38_options->T38MaxBitRate);
1098 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "T38FaxFillBitRemoval = %d\n", t38_options->T38FaxFillBitRemoval);
1099 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "T38FaxTranscodingMMR = %d\n", t38_options->T38FaxTranscodingMMR);
1100 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "T38FaxTranscodingJBIG = %d\n", t38_options->T38FaxTranscodingJBIG);
1101 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "T38FaxRateManagement = '%s'\n", t38_options->T38FaxRateManagement);
1102 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "T38FaxMaxBuffer = %d\n", t38_options->T38FaxMaxBuffer);
1103 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "T38FaxMaxDatagram = %d\n", t38_options->T38FaxMaxDatagram);
1104 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "T38FaxUdpEC = '%s'\n", t38_options->T38FaxUdpEC);
1105 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "T38VendorInfo = '%s'\n", switch_str_nil(t38_options->T38VendorInfo));
1106 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "ip = '%s'\n",
1107 t38_options->remote_ip ? t38_options->remote_ip : "Not specified");
1108 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "port = %d\n", t38_options->remote_port);
1109
1110 /* Time to practice our negotiating skills, by editing the t38_options */
1111
1112 if (t38_options->T38FaxVersion > 3) {
1113 t38_options->T38FaxVersion = 3;
1114 }
1115 t38_options->T38MaxBitRate = (pvt->disable_v17) ? 9600 : 14400;
1116
1117 /* cisco gets mad when we set this to one in a response where they set it to 0, are we allowed to hardcode this to 1 on responses? */
1118 /*
1119 if (!zstr(t38_options->sdp_o_line) && !switch_stristr("cisco", t38_options->sdp_o_line)) {
1120 t38_options->T38FaxFillBitRemoval = 1;
1121 }
1122 */
1123
1124 t38_options->T38FaxTranscodingMMR = 0;
1125 t38_options->T38FaxTranscodingJBIG = 0;
1126 t38_options->T38FaxRateManagement = "transferredTCF";
1127 if (!t38_options->T38FaxMaxBuffer) {
1128 t38_options->T38FaxMaxBuffer = 2000;
1129 }
1130 t38_options->T38FaxMaxDatagram = LOCAL_FAX_MAX_DATAGRAM;
1131 if (!zstr(t38_options->T38FaxUdpEC) &&
1132 (strcasecmp(t38_options->T38FaxUdpEC, "t38UDPRedundancy") == 0 ||
1133 strcasecmp(t38_options->T38FaxUdpEC, "t38UDPFEC") == 0)) {
1134 t38_options->T38FaxUdpEC = "t38UDPRedundancy";
1135 } else {
1136 t38_options->T38FaxUdpEC = NULL;
1137 }
1138 t38_options->T38VendorInfo = "0 0 0";
1139 }
1140
1141 if ((v = switch_channel_get_variable(channel, "fax_enable_t38_insist"))) {
1142 insist = switch_true(v);
1143 } else {
1144 insist = spandsp_globals.enable_t38_insist;
1145 }
1146
1147 /* This will send the options back in a response */
1148 msg.from = __FILE__;
1149 msg.message_id = SWITCH_MESSAGE_INDICATE_T38_DESCRIPTION;
1150 msg.numeric_arg = insist;
1151 switch_core_session_receive_message(session, &msg);
1152
1153 return pvt->t38_mode;
1154 }
1155
1156
1157
request_t38(pvt_t * pvt)1158 static t38_mode_t request_t38(pvt_t *pvt)
1159 {
1160 switch_core_session_t *session = pvt->session;
1161 switch_channel_t *channel = switch_core_session_get_channel(session);
1162 switch_core_session_message_t msg = { 0 };
1163 switch_t38_options_t *t38_options = NULL;
1164 int enabled = 0, insist = 0;
1165 const char *v;
1166
1167 pvt->t38_mode = T38_MODE_UNKNOWN;
1168
1169 if (pvt->app_mode == FUNCTION_GW) {
1170 enabled = 1;
1171 } else if ((v = switch_channel_get_variable(channel, "fax_enable_t38"))) {
1172 enabled = switch_true(v);
1173 } else {
1174 enabled = spandsp_globals.enable_t38;
1175 }
1176
1177 if (enabled) {
1178 if ((v = switch_channel_get_variable(channel, "fax_enable_t38_request"))) {
1179 enabled = switch_true(v);
1180 } else {
1181 enabled = spandsp_globals.enable_t38_request;
1182 }
1183 }
1184
1185
1186 if ((v = switch_channel_get_variable(channel, "fax_enable_t38_insist"))) {
1187 insist = switch_true(v);
1188 } else {
1189 insist = spandsp_globals.enable_t38_insist;
1190 }
1191
1192 if ((t38_options = switch_channel_get_private(channel, "t38_options"))) {
1193 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING,
1194 "%s already has T.38 data\n", switch_channel_get_name(channel));
1195 enabled = 0;
1196 }
1197
1198
1199
1200 if (enabled) {
1201
1202 if (!(t38_options = switch_channel_get_private(channel, "_preconfigured_t38_options"))) {
1203 t38_options = switch_core_session_alloc(session, sizeof(*t38_options));
1204 t38_options->T38MaxBitRate = (pvt->disable_v17) ? 9600 : 14400;
1205 t38_options->T38FaxVersion = 0;
1206 t38_options->T38FaxFillBitRemoval = 1;
1207 t38_options->T38FaxTranscodingMMR = 0;
1208 t38_options->T38FaxTranscodingJBIG = 0;
1209 t38_options->T38FaxRateManagement = "transferredTCF";
1210 t38_options->T38FaxMaxBuffer = 2000;
1211 t38_options->T38FaxMaxDatagram = LOCAL_FAX_MAX_DATAGRAM;
1212 t38_options->T38FaxUdpEC = "t38UDPRedundancy";
1213 t38_options->T38VendorInfo = "0 0 0";
1214 }
1215
1216 switch_channel_set_private(channel, "t38_options", t38_options);
1217 switch_channel_set_private(channel, "_preconfigured_t38_options", NULL);
1218
1219 pvt->t38_mode = T38_MODE_REQUESTED;
1220 switch_channel_set_app_flag_key("T38", channel, CF_APP_T38_REQ);
1221
1222 /* This will send a request for t.38 mode */
1223 msg.from = __FILE__;
1224 msg.message_id = SWITCH_MESSAGE_INDICATE_REQUEST_IMAGE_MEDIA;
1225 msg.numeric_arg = insist;
1226 switch_core_session_receive_message(session, &msg);
1227 }
1228
1229 return pvt->t38_mode;
1230 }
1231
1232 /*****************************************************************************
1233 MAIN FAX PROCESSING
1234 *****************************************************************************/
1235
pvt_init(switch_core_session_t * session,mod_spandsp_fax_application_mode_t app_mode)1236 static pvt_t *pvt_init(switch_core_session_t *session, mod_spandsp_fax_application_mode_t app_mode)
1237 {
1238 switch_channel_t *channel;
1239 pvt_t *pvt = NULL;
1240 const char *tmp;
1241
1242 /* Make sure we have a valid channel when starting the FAX application */
1243 channel = switch_core_session_get_channel(session);
1244 switch_assert(channel != NULL);
1245
1246 if (!switch_channel_media_ready(channel)) {
1247 switch_channel_answer(channel);
1248 }
1249
1250 /* Allocate our structs */
1251 pvt = switch_core_session_alloc(session, sizeof(pvt_t));
1252 pvt->session = session;
1253
1254 pvt->app_mode = app_mode;
1255
1256 pvt->tx_page_start = -1;
1257 pvt->tx_page_end = -1;
1258
1259
1260 switch(pvt->app_mode) {
1261
1262 case FUNCTION_TX:
1263 pvt->caller = 1;
1264 break;
1265 case FUNCTION_RX:
1266 pvt->caller = 0;
1267 break;
1268 case FUNCTION_GW:
1269 break;
1270 }
1271
1272 /* Retrieving our settings from the channel variables */
1273
1274 if ((tmp = switch_channel_get_variable(channel, "fax_use_ecm"))) {
1275 pvt->use_ecm = switch_true(tmp);
1276 } else {
1277 pvt->use_ecm = spandsp_globals.use_ecm;
1278 }
1279
1280 if ((tmp = switch_channel_get_variable(channel, "fax_enable_tep"))) {
1281 pvt->enable_tep = switch_true(tmp);
1282 } else {
1283 pvt->enable_tep = spandsp_globals.enable_tep;
1284 }
1285
1286 if ((tmp = switch_channel_get_variable(channel, "fax_disable_v17"))) {
1287 pvt->disable_v17 = switch_true(tmp);
1288 } else {
1289 pvt->disable_v17 = spandsp_globals.disable_v17;
1290 }
1291
1292 if ((tmp = switch_channel_get_variable(channel, "fax_enable_colour"))) {
1293 pvt->enable_colour_fax = switch_true(tmp);
1294 } else {
1295 pvt->enable_colour_fax = spandsp_globals.enable_colour_fax;
1296 }
1297
1298 if ((tmp = switch_channel_get_variable(channel, "fax_enable_image_resizing"))) {
1299 pvt->enable_image_resizing = switch_true(tmp);
1300 } else {
1301 pvt->enable_image_resizing = spandsp_globals.enable_image_resizing;
1302 }
1303
1304 if ((tmp = switch_channel_get_variable(channel, "fax_enable_colour_to_bilevel"))) {
1305 pvt->enable_colour_to_bilevel = switch_true(tmp);
1306 } else {
1307 pvt->enable_colour_to_bilevel = spandsp_globals.enable_colour_to_bilevel;
1308 }
1309
1310 if ((tmp = switch_channel_get_variable(channel, "fax_enable_grayscale_to_bilevel"))) {
1311 pvt->enable_grayscale_to_bilevel = switch_true(tmp);
1312 } else {
1313 pvt->enable_grayscale_to_bilevel = spandsp_globals.enable_grayscale_to_bilevel;
1314 }
1315
1316 if ((tmp = switch_channel_get_variable(channel, "fax_verbose"))) {
1317 pvt->verbose = switch_true(tmp);
1318 } else {
1319 pvt->verbose = spandsp_globals.verbose;
1320 }
1321
1322 if ((tmp = switch_channel_get_variable(channel, "fax_force_caller"))) {
1323 if (switch_true(tmp)) {
1324 pvt->caller = 1;
1325 } else {
1326 pvt->caller = 0;
1327 }
1328 }
1329
1330 if ((tmp = switch_channel_get_variable(channel, "fax_ident"))) {
1331 char *data = NULL;
1332
1333 data = strdup(tmp);
1334 switch_url_decode(data);
1335 pvt->ident = switch_core_session_strdup(session, data);
1336
1337 switch_safe_free(data);
1338 } else {
1339 pvt->ident = switch_core_session_strdup(session, spandsp_globals.ident);
1340 }
1341
1342 if ((tmp = switch_channel_get_variable(channel, "fax_header"))) {
1343 char *data = NULL;
1344
1345 data = strdup(tmp);
1346 switch_url_decode(data);
1347 pvt->header = switch_core_session_strdup(session, data);
1348
1349 switch_safe_free(data);
1350 } else {
1351 pvt->header = switch_core_session_strdup(session, spandsp_globals.header);
1352 }
1353
1354 if ((tmp = switch_channel_get_variable(channel, "fax_timezone"))) {
1355 char *data = NULL;
1356
1357 data = strdup(tmp);
1358 switch_url_decode(data);
1359 pvt->timezone = switch_core_session_strdup(session, data);
1360
1361 switch_safe_free(data);
1362 } else {
1363 pvt->timezone = switch_core_session_strdup(session, spandsp_globals.timezone);
1364 }
1365
1366 if (pvt->app_mode == FUNCTION_TX) {
1367 if ((tmp = switch_channel_get_variable(channel, "fax_start_page"))) {
1368 pvt->tx_page_start = atoi(tmp);
1369 }
1370
1371 if ((tmp = switch_channel_get_variable(channel, "fax_end_page"))) {
1372 pvt->tx_page_end = atoi(tmp);
1373 }
1374
1375 if (pvt->tx_page_end < -1) {
1376 pvt->tx_page_end = -1;
1377 }
1378
1379 if (pvt->tx_page_start < -1) {
1380 pvt->tx_page_start = -1;
1381 }
1382
1383 if ((pvt->tx_page_end < pvt->tx_page_start) && (pvt->tx_page_end != -1)) {
1384 pvt->tx_page_end = pvt->tx_page_start;
1385 }
1386 }
1387
1388 return pvt;
1389 }
1390
mod_spandsp_fax_stop_fax(switch_core_session_t * session)1391 void mod_spandsp_fax_stop_fax(switch_core_session_t *session)
1392 {
1393 pvt_t *pvt = switch_channel_get_private(switch_core_session_get_channel(session), "_fax_pvt");
1394 if (pvt) {
1395 pvt->done = 1;
1396 }
1397 }
1398
mod_spandsp_fax_process_fax(switch_core_session_t * session,const char * data,mod_spandsp_fax_application_mode_t app_mode)1399 void mod_spandsp_fax_process_fax(switch_core_session_t *session, const char *data, mod_spandsp_fax_application_mode_t app_mode)
1400 {
1401 pvt_t *pvt;
1402 switch_channel_t *channel = switch_core_session_get_channel(session);
1403 switch_codec_t read_codec = { 0 };
1404 switch_codec_t write_codec = { 0 };
1405 switch_frame_t *read_frame = { 0 };
1406 switch_frame_t write_frame = { 0 };
1407 switch_codec_implementation_t read_impl = { 0 };
1408 int16_t *buf = NULL;
1409 uint32_t req_counter = 0;
1410
1411 switch_core_session_get_read_impl(session, &read_impl);
1412
1413 counter_increment();
1414
1415 if (app_mode == FUNCTION_GW ||
1416 switch_channel_var_true(channel, "fax_enable_t38") ||
1417 switch_channel_var_true(channel, "fax_enable_t38_insist")) {
1418 switch_channel_set_app_flag_key("T38", channel, CF_APP_T38_POSSIBLE);
1419 }
1420
1421 pvt = pvt_init(session, app_mode);
1422 switch_channel_set_private(channel, "_fax_pvt", pvt);
1423
1424 buf = switch_core_session_alloc(session, SWITCH_RECOMMENDED_BUFFER_SIZE);
1425
1426 if (!zstr(data)) {
1427 pvt->filename = switch_core_session_strdup(session, data);
1428 if (pvt->app_mode == FUNCTION_TX) {
1429 if ((switch_file_exists(pvt->filename, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS)) {
1430 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot send non-existant fax file [%s]\n",
1431 switch_str_nil(pvt->filename));
1432 switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "Cannot send non-existant fax file");
1433 goto done;
1434 }
1435 }
1436 } else {
1437 if (pvt->app_mode == FUNCTION_TX) {
1438 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Fax TX filename not set.\n");
1439 switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "Fax TX filename not set");
1440 goto done;
1441 } else if (pvt->app_mode == FUNCTION_RX) {
1442 const char *spool, *prefix;
1443 switch_time_t time;
1444
1445 time = switch_time_now();
1446
1447 if (!(spool = switch_channel_get_variable(channel, "fax_spool"))) {
1448 spool = spandsp_globals.spool;
1449 }
1450
1451 if (!(prefix = switch_channel_get_variable(channel, "fax_prefix"))) {
1452 prefix = spandsp_globals.prepend_string;
1453 }
1454
1455 if (!(pvt->filename = switch_core_session_sprintf(session, "%s/%s-%ld-%" SWITCH_TIME_T_FMT ".tif", spool, prefix, spandsp_globals.total_sessions, time))) {
1456 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot automatically set fax RX destination file\n");
1457 switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "Cannot automatically set fax RX destination file");
1458 goto done;
1459 }
1460
1461 if (switch_dir_make_recursive(spool, SWITCH_DEFAULT_DIR_PERMS, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
1462 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot automatically set fax RX destination file\n");
1463 switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "Cannot automatically set fax RX destination file");
1464 goto done;
1465 }
1466
1467 } else {
1468 assert(0); /* UH ?? */
1469 }
1470 }
1471
1472 /*
1473 *** Initialize the SpanDSP elements ***
1474
1475 Note: we could analyze if a fax was already detected in previous stages
1476 and if so, when T.38 will be supported, send a reinvite in T38_MODE,
1477 bypassing AUDIO_MODE.
1478 */
1479
1480 if ((spanfax_init(pvt, AUDIO_MODE) != SWITCH_STATUS_SUCCESS)) {
1481 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot initialize Fax engine\n");
1482 switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "Cannot initialize Fax engine");
1483 return;
1484 }
1485
1486 /*
1487 Note: Disable echocan on the channel, remember to call app "disable_ec" in the dialplan
1488 before invoking fax applications
1489 */
1490
1491 /*
1492 Note: we are disabling the Jitterbuffer, here, before we answer.
1493 If you have set it to something else and the channel is pre-answered,
1494 it will have no effect. Make sure that if you want more reliable
1495 faxes, it is disabled.
1496 */
1497 switch_channel_set_variable(channel, "jitterbuffer_msec", NULL);
1498
1499
1500 /* We store the original channel codec before switching both
1501 * legs of the calls to a linear 16 bit codec that is the one
1502 * used internally by spandsp and FS will do the transcoding
1503 * from G.711 or any other original codec
1504 */
1505 if (switch_core_codec_init(&read_codec,
1506 "L16",
1507 NULL,
1508 NULL,
1509 read_impl.samples_per_second,
1510 read_impl.microseconds_per_packet / 1000,
1511 1,
1512 SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
1513 NULL, switch_core_session_get_pool(session)) == SWITCH_STATUS_SUCCESS) {
1514 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Raw read codec activation Success L16 %u\n",
1515 read_codec.implementation->microseconds_per_packet);
1516 switch_core_session_set_read_codec(session, &read_codec);
1517 } else {
1518 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Raw read codec activation Failed L16\n");
1519 switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "Raw read codec activation Failed L16");
1520 goto done;
1521 }
1522
1523 if (switch_core_codec_init(&write_codec,
1524 "L16",
1525 NULL,
1526 NULL,
1527 read_impl.samples_per_second,
1528 read_impl.microseconds_per_packet / 1000,
1529 1,
1530 SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
1531 NULL, switch_core_session_get_pool(session)) == SWITCH_STATUS_SUCCESS) {
1532 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Raw write codec activation Success L16\n");
1533 write_frame.codec = &write_codec;
1534 write_frame.data = buf;
1535 write_frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
1536 } else {
1537 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Raw write codec activation Failed L16\n");
1538 switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "Raw write codec activation Failed L16");
1539 goto done;
1540 }
1541
1542 switch_ivr_sleep(session, 250, SWITCH_TRUE, NULL);
1543
1544 if (pvt->app_mode == FUNCTION_TX) {
1545 const char *packet_count = switch_channel_get_variable(channel, "fax_t38_tx_reinvite_packet_count");
1546 if (!zstr(packet_count) && switch_is_number(packet_count)) {
1547 req_counter = atoi(packet_count);
1548 } else {
1549 req_counter = spandsp_globals.t38_tx_reinvite_packet_count;
1550 }
1551 } else {
1552 const char *packet_count = switch_channel_get_variable(channel, "fax_t38_rx_reinvite_packet_count");
1553 if (!zstr(packet_count) && switch_is_number(packet_count)) {
1554 req_counter = atoi(packet_count);
1555 } else {
1556 req_counter = spandsp_globals.t38_rx_reinvite_packet_count;
1557 }
1558 }
1559
1560 while (switch_channel_ready(channel)) {
1561 int tx = 0;
1562 switch_status_t status;
1563
1564 switch_ivr_parse_all_events(session);
1565
1566 /*
1567 if we are in T.38 mode, we should: 1- initialize the ptv->t38_state stuff, if not done
1568 and then set some callbacks when reading frames.
1569 The only thing we need, then, in this loop, is:
1570 - read a frame without blocking
1571 - eventually feed that frame in spandsp,
1572 - call t38_terminal_send_timeout(), sleep for a while
1573
1574 The T.38 stuff can be placed here (and the audio stuff can be skipped)
1575 */
1576
1577 /* read new audio frame from the channel */
1578 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1579
1580 if (!SWITCH_READ_ACCEPTABLE(status) || pvt->done) {
1581 /* Our duty is over */
1582 goto done;
1583 }
1584
1585 switch (pvt->t38_mode) {
1586 case T38_MODE_REQUESTED:
1587 {
1588 if (switch_channel_test_app_flag_key("T38", channel, CF_APP_T38_FAIL)) {
1589 pvt->t38_mode = T38_MODE_REFUSED;
1590 continue;
1591 } else if (switch_channel_test_app_flag_key("T38", channel, CF_APP_T38)) {
1592 switch_core_session_message_t msg = { 0 };
1593 pvt->t38_mode = T38_MODE_NEGOTIATED;
1594 switch_channel_set_app_flag_key("T38", channel, CF_APP_T38_NEGOTIATED);
1595 spanfax_init(pvt, T38_MODE);
1596 configure_t38(pvt);
1597
1598 /* This will change the rtp stack to udptl mode */
1599 msg.from = __FILE__;
1600 msg.message_id = SWITCH_MESSAGE_INDICATE_UDPTL_MODE;
1601 switch_core_session_receive_message(session, &msg);
1602 }
1603 continue;
1604 }
1605 break;
1606 case T38_MODE_UNKNOWN:
1607 {
1608 if (req_counter) {
1609 if (!--req_counter) {
1610 /* If you have the means, I highly recommend picking one up. ...*/
1611 request_t38(pvt);
1612 }
1613 }
1614
1615 if (switch_channel_test_app_flag_key("T38", channel, CF_APP_T38)) {
1616 if (negotiate_t38(pvt) == T38_MODE_NEGOTIATED) {
1617 /* is is safe to call this again, it was already called above in AUDIO_MODE */
1618 /* but this is the only way to set up the t38 stuff */
1619 spanfax_init(pvt, T38_MODE);
1620 continue;
1621 }
1622 }
1623 }
1624 break;
1625 case T38_MODE_NEGOTIATED:
1626 {
1627 /* do what we need to do when we are in t38 mode */
1628 if (switch_test_flag(read_frame, SFF_CNG)) {
1629 /* dunno what to do, most likely you will not get too many of these since we turn off the timer in udptl mode */
1630 continue;
1631 }
1632
1633 if (switch_test_flag(read_frame, SFF_UDPTL_PACKET) && read_frame->packet && read_frame->packetlen) {
1634 /* now we know we can cast frame->packet to a udptl structure */
1635 //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "READ %d udptl bytes\n", read_frame->packetlen);
1636
1637 udptl_rx_packet(pvt->udptl_state, read_frame->packet, read_frame->packetlen);
1638 }
1639 }
1640 continue;
1641 default:
1642 break;
1643 }
1644
1645 if (switch_test_flag(read_frame, SFF_CNG)) {
1646 /* We have no real signal data for the FAX software, but we have a space in time if we have a CNG indication.
1647 Do a fill-in operation in the FAX machine, to keep things rolling along. */
1648 if (fax_rx_fillin(pvt->fax_state, read_impl.samples_per_packet)) {
1649 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "fax_rx_fillin reported an error\n");
1650 continue;
1651 }
1652 } else {
1653 /* Pass the new incoming audio frame to the fax_rx function */
1654 if (fax_rx(pvt->fax_state, (int16_t *) read_frame->data, read_frame->samples)) {
1655 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "fax_rx reported an error\n");
1656 switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "fax_rx reported an error");
1657 goto done;
1658 }
1659 }
1660
1661 if ((tx = fax_tx(pvt->fax_state, buf, write_codec.implementation->samples_per_packet)) < 0) {
1662 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "fax_tx reported an error\n");
1663 switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "fax_tx reported an error");
1664 goto done;
1665 }
1666
1667 if (!tx) {
1668 /* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "No audio samples to send\n"); */
1669 continue;
1670 } else {
1671 /* Set our write_frame data */
1672 write_frame.datalen = tx * sizeof(int16_t);
1673 write_frame.samples = tx;
1674 }
1675
1676 if (switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
1677 goto done;
1678 }
1679
1680 }
1681
1682 done:
1683 /* Destroy the SpanDSP structures */
1684 spanfax_destroy(pvt);
1685
1686 switch_channel_clear_app_flag_key("T38", channel, CF_APP_T38_POSSIBLE);
1687
1688 /* restore the original codecs over the channel */
1689
1690 switch_core_session_set_read_codec(session, NULL);
1691
1692 if (switch_core_codec_ready(&read_codec)) {
1693 switch_core_codec_destroy(&read_codec);
1694 }
1695
1696 if (switch_core_codec_ready(&write_codec)) {
1697 switch_core_codec_destroy(&write_codec);
1698 }
1699 }
1700
mod_spandsp_fax_load(switch_memory_pool_t * pool)1701 void mod_spandsp_fax_load(switch_memory_pool_t *pool)
1702 {
1703 uint32_t sanity = 200;
1704
1705 memset(&t38_state_list, 0, sizeof(t38_state_list));
1706
1707 switch_mutex_init(&spandsp_globals.mutex, SWITCH_MUTEX_NESTED, spandsp_globals.pool);
1708 switch_mutex_init(&t38_state_list.mutex, SWITCH_MUTEX_NESTED, spandsp_globals.pool);
1709
1710 switch_mutex_init(&spandsp_globals.cond_mutex, SWITCH_MUTEX_NESTED, spandsp_globals.pool);
1711 switch_thread_cond_create(&spandsp_globals.cond, spandsp_globals.pool);
1712
1713 if (switch_core_test_flag(SCF_MINIMAL)) {
1714 return;
1715 }
1716
1717 launch_timer_thread();
1718
1719 while(--sanity && !t38_state_list.thread_running) {
1720 switch_yield(20000);
1721 }
1722 }
1723
mod_spandsp_fax_shutdown(void)1724 void mod_spandsp_fax_shutdown(void)
1725 {
1726 switch_status_t tstatus = SWITCH_STATUS_SUCCESS;
1727
1728 if (switch_core_test_flag(SCF_MINIMAL)) {
1729 return;
1730 }
1731
1732 t38_state_list.thread_running = 0;
1733 wake_thread(1);
1734 switch_thread_join(&tstatus, t38_state_list.thread);
1735 }
1736
1737 static const switch_state_handler_table_t t38_gateway_state_handlers;
1738
t38_gateway_on_soft_execute(switch_core_session_t * session)1739 static switch_status_t t38_gateway_on_soft_execute(switch_core_session_t *session)
1740 {
1741 switch_core_session_t *other_session;
1742
1743 switch_channel_t *other_channel, *channel = switch_core_session_get_channel(session);
1744 pvt_t *pvt;
1745 const char *peer_uuid = switch_channel_get_variable(channel, "t38_peer");
1746 switch_core_session_message_t msg = { 0 };
1747 switch_status_t status;
1748 switch_frame_t *read_frame = { 0 };
1749
1750 if (!(other_session = switch_core_session_locate(peer_uuid))) {
1751 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
1752 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "%s Cannot locate channel with uuid %s",
1753 switch_channel_get_name(channel), peer_uuid);
1754 goto end;
1755 }
1756
1757 other_channel = switch_core_session_get_channel(other_session);
1758
1759 pvt = pvt_init(session, FUNCTION_GW);
1760 request_t38(pvt);
1761
1762 msg.message_id = SWITCH_MESSAGE_INDICATE_BRIDGE;
1763 msg.from = __FILE__;
1764 msg.string_arg = peer_uuid;
1765 switch_core_session_receive_message(session, &msg);
1766
1767 while (switch_channel_ready(channel) && switch_channel_up(other_channel) && !switch_channel_test_app_flag_key("T38", channel, CF_APP_T38)) {
1768 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1769
1770 if (pvt->done) {
1771 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "%s Premature exit while negotiating\n", switch_channel_get_name(channel));
1772 /* Our duty is over */
1773 goto end_unlock;
1774 }
1775
1776 if (!SWITCH_READ_ACCEPTABLE(status)) {
1777 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "%s Read failed, status=%u\n", switch_channel_get_name(channel), status);
1778 goto end_unlock;
1779 }
1780
1781 if (switch_test_flag(read_frame, SFF_CNG)) {
1782 continue;
1783 }
1784
1785 if (switch_core_session_write_frame(other_session, read_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
1786 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "%s Write failed\n", switch_channel_get_name(channel));
1787 goto end_unlock;
1788 }
1789 }
1790
1791 if (!(switch_channel_ready(channel) && switch_channel_up(other_channel))) {
1792 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "%s Channel not ready\n", switch_channel_get_name(channel));
1793 goto end_unlock;
1794 }
1795
1796 if (!switch_channel_test_app_flag_key("T38", channel, CF_APP_T38)) {
1797 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
1798 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "%s Could not negotiate T38\n", switch_channel_get_name(channel));
1799 goto end_unlock;
1800 }
1801
1802 if (pvt->t38_mode == T38_MODE_REQUESTED) {
1803 spanfax_init(pvt, T38_GATEWAY_MODE);
1804 configure_t38(pvt);
1805 pvt->t38_mode = T38_MODE_NEGOTIATED;
1806 switch_channel_set_app_flag_key("T38", channel, CF_APP_T38_NEGOTIATED);
1807 } else {
1808 if (negotiate_t38(pvt) != T38_MODE_NEGOTIATED) {
1809 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "%s Could not negotiate T38\n", switch_channel_get_name(channel));
1810 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
1811 goto end_unlock;
1812 }
1813 switch_channel_set_app_flag_key("T38", channel, CF_APP_T38_NEGOTIATED);
1814 spanfax_init(pvt, T38_GATEWAY_MODE);
1815 }
1816
1817 /* This will change the rtp stack to udptl mode */
1818 msg.from = __FILE__;
1819 msg.message_id = SWITCH_MESSAGE_INDICATE_UDPTL_MODE;
1820 switch_core_session_receive_message(session, &msg);
1821
1822
1823 /* wake up the audio side */
1824 switch_channel_set_private(channel, "_t38_pvt", pvt);
1825 switch_channel_set_app_flag_key("T38", other_channel, CF_APP_T38);
1826
1827
1828 while (switch_channel_ready(channel) && switch_channel_up(other_channel)) {
1829
1830 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1831
1832 if (!SWITCH_READ_ACCEPTABLE(status) || pvt->done) {
1833 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "%s Premature exit while negotiating (%i)\n", switch_channel_get_name(channel), status);
1834 /* Our duty is over */
1835 goto end_unlock;
1836 }
1837
1838 if (switch_test_flag(read_frame, SFF_CNG)) {
1839 continue;
1840 }
1841
1842 if (switch_test_flag(read_frame, SFF_UDPTL_PACKET)) {
1843 if (udptl_rx_packet(pvt->udptl_state, read_frame->packet, read_frame->packetlen) < 0) {
1844 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s Error decoding UDPTL (%u bytes)\n", switch_channel_get_name(channel), read_frame->packetlen);
1845 }
1846 }
1847 }
1848
1849 end_unlock:
1850
1851
1852 msg.message_id = SWITCH_MESSAGE_INDICATE_UNBRIDGE;
1853 msg.from = __FILE__;
1854 msg.string_arg = peer_uuid;
1855 switch_core_session_receive_message(session, &msg);
1856
1857 switch_channel_hangup(other_channel, SWITCH_CAUSE_NORMAL_CLEARING);
1858 switch_core_session_rwunlock(other_session);
1859
1860 end:
1861
1862 switch_channel_clear_state_handler(channel, &t38_gateway_state_handlers);
1863 switch_channel_set_variable(channel, "t38_peer", NULL);
1864
1865 switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
1866 return SWITCH_STATUS_SUCCESS;
1867 }
1868
t38_gateway_on_consume_media(switch_core_session_t * session)1869 static switch_status_t t38_gateway_on_consume_media(switch_core_session_t *session)
1870 {
1871 switch_core_session_t *other_session;
1872 switch_channel_t *other_channel, *channel = switch_core_session_get_channel(session);
1873 const char *peer_uuid = switch_channel_get_variable(channel, "t38_peer");
1874 pvt_t *pvt = NULL;
1875 switch_codec_t read_codec = { 0 };
1876 switch_codec_t write_codec = { 0 };
1877 switch_frame_t *read_frame = { 0 };
1878 switch_frame_t write_frame = { 0 };
1879 switch_codec_implementation_t read_impl = { 0 };
1880 int16_t *buf = NULL;
1881 switch_status_t status;
1882 int tx;
1883 const char *t38_trace = switch_channel_get_variable(channel, "t38_trace");
1884 char *trace_read, *trace_write;
1885 zap_socket_t read_fd = FAX_INVALID_SOCKET, write_fd = FAX_INVALID_SOCKET;
1886 switch_core_session_message_t msg = { 0 };
1887 switch_event_t *event;
1888
1889 switch_core_session_get_read_impl(session, &read_impl);
1890
1891 buf = switch_core_session_alloc(session, SWITCH_RECOMMENDED_BUFFER_SIZE);
1892
1893 if (!(other_session = switch_core_session_locate(peer_uuid))) {
1894 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
1895 goto end;
1896 }
1897
1898 other_channel = switch_core_session_get_channel(other_session);
1899
1900 msg.message_id = SWITCH_MESSAGE_INDICATE_BRIDGE;
1901 msg.from = __FILE__;
1902 msg.string_arg = peer_uuid;
1903 switch_core_session_receive_message(session, &msg);
1904
1905 if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_BRIDGE) == SWITCH_STATUS_SUCCESS) {
1906 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridge-A-Unique-ID", switch_core_session_get_uuid(session));
1907 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridge-B-Unique-ID", peer_uuid);
1908 switch_channel_event_set_data(channel, event);
1909 switch_event_fire(&event);
1910 }
1911
1912 while (switch_channel_ready(channel) && switch_channel_up(other_channel) && !switch_channel_test_app_flag_key("T38", channel, CF_APP_T38)) {
1913 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1914
1915 if (!SWITCH_READ_ACCEPTABLE(status)) {
1916 /* Our duty is over */
1917 goto end_unlock;
1918 }
1919
1920 if (switch_test_flag(read_frame, SFF_CNG)) {
1921 continue;
1922 }
1923
1924 if (switch_core_session_write_frame(other_session, read_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
1925 goto end_unlock;
1926 }
1927 }
1928
1929 if (!(switch_channel_ready(channel) && switch_channel_up(other_channel))) {
1930 goto end_unlock;
1931 }
1932
1933 if (!switch_channel_test_app_flag_key("T38", channel, CF_APP_T38)) {
1934 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
1935 goto end_unlock;
1936 }
1937
1938 if (!(pvt = switch_channel_get_private(other_channel, "_t38_pvt"))) {
1939 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
1940 goto end_unlock;
1941 }
1942
1943 if (switch_core_codec_init(&read_codec,
1944 "L16",
1945 NULL,
1946 NULL,
1947 read_impl.samples_per_second,
1948 read_impl.microseconds_per_packet / 1000,
1949 1,
1950 SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
1951 NULL, switch_core_session_get_pool(session)) == SWITCH_STATUS_SUCCESS) {
1952 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Raw read codec activation Success L16 %u\n",
1953 read_codec.implementation->microseconds_per_packet);
1954 switch_core_session_set_read_codec(session, &read_codec);
1955 } else {
1956 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Raw read codec activation Failed L16\n");
1957 goto end_unlock;
1958 }
1959
1960 if (switch_core_codec_init(&write_codec,
1961 "L16",
1962 NULL,
1963 NULL,
1964 read_impl.samples_per_second,
1965 read_impl.microseconds_per_packet / 1000,
1966 1,
1967 SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
1968 NULL, switch_core_session_get_pool(session)) == SWITCH_STATUS_SUCCESS) {
1969 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Raw write codec activation Success L16\n");
1970 write_frame.codec = &write_codec;
1971 write_frame.data = buf;
1972 write_frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
1973 } else {
1974 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Raw write codec activation Failed L16\n");
1975 goto end_unlock;
1976 }
1977
1978 switch_ivr_sleep(session, 0, SWITCH_TRUE, NULL);
1979
1980 if (switch_true(t38_trace)) {
1981 trace_read = switch_core_session_sprintf(session, "%s%s%s_read.raw", SWITCH_GLOBAL_dirs.temp_dir,
1982 SWITCH_PATH_SEPARATOR, switch_core_session_get_uuid(session));
1983
1984 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Tracing inbound audio to %s\n", trace_read);
1985 switch_channel_set_variable(channel, "t38_trace_read", trace_read);
1986
1987 trace_write = switch_core_session_sprintf(session, "%s%s%s_write.raw", SWITCH_GLOBAL_dirs.temp_dir,
1988 SWITCH_PATH_SEPARATOR, switch_core_session_get_uuid(session));
1989
1990 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Tracing outbound audio to %s\n", trace_write);
1991 switch_channel_set_variable(channel, "t38_trace_read", trace_write);
1992
1993
1994 if ((write_fd = open(trace_read, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) != FAX_INVALID_SOCKET) {
1995 if ((read_fd = open(trace_write, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == FAX_INVALID_SOCKET) {
1996 close(write_fd);
1997 write_fd = FAX_INVALID_SOCKET;
1998 }
1999 }
2000 }
2001
2002 while (switch_channel_ready(channel) && switch_channel_up(other_channel)) {
2003 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
2004
2005 if (!SWITCH_READ_ACCEPTABLE(status) || pvt->done) {
2006 /* Our duty is over */
2007 goto end_unlock;
2008 }
2009
2010 if (switch_test_flag(read_frame, SFF_CNG)) {
2011 /* We have no real signal data for the FAX software, but we have a space in time if we have a CNG indication.
2012 Do a fill-in operation in the FAX machine, to keep things rolling along. */
2013 t38_gateway_rx_fillin(pvt->t38_gateway_state, read_impl.samples_per_packet);
2014 } else {
2015 if (read_fd != FAX_INVALID_SOCKET) {
2016 switch_ssize_t rv;
2017 do { rv = write(read_fd, read_frame->data, read_frame->datalen); } while (rv == -1 && errno == EINTR);
2018 }
2019 if (t38_gateway_rx(pvt->t38_gateway_state, (int16_t *) read_frame->data, read_frame->samples)) {
2020 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "fax_rx reported an error\n");
2021 goto end_unlock;
2022 }
2023 }
2024
2025 if ((tx = t38_gateway_tx(pvt->t38_gateway_state, buf, write_codec.implementation->samples_per_packet)) < 0) {
2026 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "fax_tx reported an error\n");
2027 goto end_unlock;
2028 }
2029
2030 if (!tx) {
2031 /* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "No audio samples to send\n"); */
2032 continue;
2033 } else {
2034 /* Set our write_frame data */
2035 write_frame.datalen = tx * sizeof(int16_t);
2036 write_frame.samples = tx;
2037 }
2038
2039 if (write_fd != FAX_INVALID_SOCKET) {
2040 switch_ssize_t rv;
2041 do { rv = write(write_fd, write_frame.data, write_frame.datalen); } while (rv == -1 && errno == EINTR);
2042 }
2043
2044 if (switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
2045 goto end_unlock;
2046 }
2047 }
2048
2049 end_unlock:
2050
2051 msg.message_id = SWITCH_MESSAGE_INDICATE_UNBRIDGE;
2052 msg.from = __FILE__;
2053 msg.string_arg = peer_uuid;
2054 switch_core_session_receive_message(session, &msg);
2055
2056
2057 if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_UNBRIDGE) == SWITCH_STATUS_SUCCESS) {
2058 switch_channel_event_set_data(channel, event);
2059 switch_event_fire(&event);
2060 }
2061
2062 if (read_fd != FAX_INVALID_SOCKET) {
2063 close(read_fd);
2064 }
2065
2066 if (write_fd != FAX_INVALID_SOCKET) {
2067 close(write_fd);
2068 }
2069
2070
2071 switch_channel_hangup(other_channel, SWITCH_CAUSE_NORMAL_CLEARING);
2072 switch_core_session_rwunlock(other_session);
2073
2074 switch_core_session_set_read_codec(session, NULL);
2075
2076 if (switch_core_codec_ready(&read_codec)) {
2077 switch_core_codec_destroy(&read_codec);
2078 }
2079
2080 if (switch_core_codec_ready(&write_codec)) {
2081 switch_core_codec_destroy(&write_codec);
2082 }
2083
2084
2085 end:
2086
2087 switch_channel_clear_state_handler(channel, &t38_gateway_state_handlers);
2088 switch_channel_set_variable(channel, "t38_peer", NULL);
2089 switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
2090
2091 return SWITCH_STATUS_SUCCESS;
2092 }
2093
t38_gateway_on_reset(switch_core_session_t * session)2094 static switch_status_t t38_gateway_on_reset(switch_core_session_t *session)
2095 {
2096 switch_channel_t *channel = switch_core_session_get_channel(session);
2097
2098 switch_channel_set_variable(channel, "rtp_autoflush_during_bridge", "false");
2099
2100 switch_channel_clear_flag(channel, CF_REDIRECT);
2101
2102 if (switch_channel_test_app_flag_key("T38", channel, CF_APP_TAGGED)) {
2103 switch_channel_clear_app_flag_key("T38", channel, CF_APP_TAGGED);
2104 switch_channel_set_state(channel, CS_CONSUME_MEDIA);
2105 } else {
2106 switch_channel_set_state(channel, CS_SOFT_EXECUTE);
2107 }
2108
2109 return SWITCH_STATUS_SUCCESS;
2110 }
2111
2112 static const switch_state_handler_table_t t38_gateway_state_handlers = {
2113 /*.on_init */ NULL,
2114 /*.on_routing */ NULL,
2115 /*.on_execute */ NULL,
2116 /*.on_hangup */ NULL,
2117 /*.on_exchange_media */ NULL,
2118 /*.on_soft_execute */ t38_gateway_on_soft_execute,
2119 /*.on_consume_media */ t38_gateway_on_consume_media,
2120 /*.on_hibernate */ NULL,
2121 /*.on_reset */ t38_gateway_on_reset,
2122 /*.on_park */ NULL,
2123 /*.on_reporting */ NULL,
2124 /*.on_destroy */ NULL,
2125 SSH_FLAG_STICKY
2126 };
2127
t38_gateway_start(switch_core_session_t * session,const char * app,const char * data)2128 switch_bool_t t38_gateway_start(switch_core_session_t *session, const char *app, const char *data)
2129 {
2130 switch_channel_t *other_channel = NULL, *channel = switch_core_session_get_channel(session);
2131 switch_core_session_t *other_session = NULL;
2132 int peer = app && !strcasecmp(app, "peer");
2133
2134 if (switch_core_session_get_partner(session, &other_session) == SWITCH_STATUS_SUCCESS) {
2135 other_channel = switch_core_session_get_channel(other_session);
2136
2137 switch_channel_set_variable(channel, "t38_peer", switch_core_session_get_uuid(other_session));
2138 switch_channel_set_variable(other_channel, "t38_peer", switch_core_session_get_uuid(session));
2139
2140 switch_channel_set_variable(peer ? other_channel : channel, "t38_gateway_format", "udptl");
2141 switch_channel_set_variable(peer ? channel : other_channel, "t38_gateway_format", "audio");
2142
2143
2144 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s starting gateway mode to %s\n",
2145 switch_channel_get_name(peer ? channel : other_channel),
2146 switch_channel_get_name(peer ? other_channel : channel));
2147
2148
2149 switch_channel_clear_state_handler(channel, NULL);
2150 switch_channel_clear_state_handler(other_channel, NULL);
2151
2152 switch_channel_add_state_handler(channel, &t38_gateway_state_handlers);
2153 switch_channel_add_state_handler(other_channel, &t38_gateway_state_handlers);
2154
2155 switch_channel_set_app_flag_key("T38", peer ? channel : other_channel, CF_APP_TAGGED);
2156 switch_channel_clear_app_flag_key("T38", peer ? other_channel : channel, CF_APP_TAGGED);
2157
2158 switch_channel_set_app_flag_key("T38", channel, CF_APP_T38_POSSIBLE);
2159 switch_channel_set_app_flag_key("T38", other_channel, CF_APP_T38_POSSIBLE);
2160
2161 switch_channel_set_flag(channel, CF_REDIRECT);
2162 switch_channel_set_state(channel, CS_RESET);
2163
2164 switch_channel_set_flag(other_channel, CF_REDIRECT);
2165 switch_channel_set_state(other_channel, CS_RESET);
2166
2167 switch_core_session_rwunlock(other_session);
2168
2169 }
2170
2171 return SWITCH_FALSE;
2172 }
2173
2174 typedef struct {
2175 char *app;
2176 char *data;
2177 char *key;
2178 int up;
2179 int tone_type;
2180 int total_hits;
2181 int hits;
2182 int sleep;
2183 int expires;
2184 int default_sleep;
2185 int default_expires;
2186 switch_tone_detect_callback_t callback;
2187 modem_connect_tones_rx_state_t rx_tones;
2188
2189 switch_media_bug_t *bug;
2190 switch_core_session_t *session;
2191 int bug_running;
2192
2193 } spandsp_fax_tone_container_t;
2194
tone_on_dtmf(switch_core_session_t * session,const switch_dtmf_t * dtmf,switch_dtmf_direction_t direction)2195 static switch_status_t tone_on_dtmf(switch_core_session_t *session, const switch_dtmf_t *dtmf, switch_dtmf_direction_t direction)
2196 {
2197 switch_channel_t *channel = switch_core_session_get_channel(session);
2198 spandsp_fax_tone_container_t *cont = switch_channel_get_private(channel, "_fax_tone_detect_");
2199
2200
2201 if (!cont || dtmf->digit != 'f') {
2202 return SWITCH_STATUS_SUCCESS;
2203 }
2204
2205 if (cont->callback) {
2206 cont->callback(cont->session, cont->app, cont->data);
2207 } else {
2208 switch_channel_execute_on(switch_core_session_get_channel(cont->session), "execute_on_fax_detect");
2209 if (cont->app) {
2210 switch_core_session_execute_application_async(cont->session, cont->app, cont->data);
2211 }
2212 }
2213
2214 return SWITCH_STATUS_SUCCESS;
2215
2216 }
2217
2218
tone_detect_callback(switch_media_bug_t * bug,void * user_data,switch_abc_type_t type)2219 static switch_bool_t tone_detect_callback(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type)
2220 {
2221 spandsp_fax_tone_container_t *cont = (spandsp_fax_tone_container_t *) user_data;
2222 switch_frame_t *frame = NULL;
2223 switch_bool_t rval = SWITCH_TRUE;
2224
2225 switch (type) {
2226 case SWITCH_ABC_TYPE_INIT:
2227 if (cont) {
2228 cont->bug_running = 1;
2229 modem_connect_tones_rx_init(&cont->rx_tones, cont->tone_type, NULL, NULL);
2230 }
2231 break;
2232 case SWITCH_ABC_TYPE_CLOSE:
2233 switch_channel_execute_on(switch_core_session_get_channel(cont->session), "execute_on_fax_close_detect");
2234 break;
2235 case SWITCH_ABC_TYPE_READ_REPLACE:
2236 case SWITCH_ABC_TYPE_WRITE_REPLACE:
2237 {
2238 int skip = 0;
2239
2240 if (type == SWITCH_ABC_TYPE_READ_REPLACE) {
2241 frame = switch_core_media_bug_get_read_replace_frame(bug);
2242 } else {
2243 frame = switch_core_media_bug_get_write_replace_frame(bug);
2244 }
2245
2246 if (cont->sleep) {
2247 cont->sleep--;
2248 if (cont->sleep) {
2249 skip = 1;
2250 }
2251 }
2252
2253 if (cont->expires) {
2254 cont->expires--;
2255 if (!cont->expires) {
2256 cont->hits = 0;
2257 cont->sleep = 0;
2258 cont->expires = 0;
2259 }
2260 }
2261
2262 if (!cont->up) {
2263 skip = 1;
2264 }
2265
2266 if (skip) {
2267 return SWITCH_TRUE;
2268 }
2269
2270 cont->hits = 0;
2271 modem_connect_tones_rx(&cont->rx_tones, frame->data, frame->samples);
2272 cont->hits = modem_connect_tones_rx_get(&cont->rx_tones);
2273
2274 if (cont->hits) {
2275 switch_event_t *event;
2276
2277 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(switch_core_media_bug_get_session(bug)), SWITCH_LOG_DEBUG,
2278 "Fax Tone Detected. [%s][%s]\n", cont->app, switch_str_nil(cont->data));
2279
2280 if (cont->callback) {
2281 cont->callback(cont->session, cont->app, cont->data);
2282 } else {
2283 switch_channel_execute_on(switch_core_session_get_channel(cont->session), "execute_on_fax_detect");
2284 if (cont->app) {
2285 switch_core_session_execute_application_async(cont->session, cont->app, cont->data);
2286 }
2287 }
2288
2289
2290 if (switch_event_create(&event, SWITCH_EVENT_DETECTED_TONE) == SWITCH_STATUS_SUCCESS) {
2291 switch_event_t *dup;
2292 switch_core_session_t *session = NULL;
2293 switch_channel_t *channel = NULL;
2294
2295 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Detected-Fax-Tone", "true");
2296
2297 session = switch_core_media_bug_get_session(bug);
2298 if (session) {
2299 channel = switch_core_session_get_channel(session);
2300 if (channel) switch_channel_event_set_data(channel, event);
2301 }
2302
2303 if (switch_event_dup(&dup, event) == SWITCH_STATUS_SUCCESS) {
2304 switch_event_fire(&dup);
2305 }
2306
2307 if (switch_core_session_queue_event(cont->session, &event) != SWITCH_STATUS_SUCCESS) {
2308 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(switch_core_media_bug_get_session(bug)), SWITCH_LOG_ERROR,
2309 "Event queue failed!\n");
2310 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "delivery-failure", "true");
2311 switch_event_fire(&event);
2312 }
2313 }
2314
2315 rval = SWITCH_FALSE;
2316 }
2317
2318 }
2319 break;
2320 case SWITCH_ABC_TYPE_WRITE:
2321 default:
2322 break;
2323 }
2324
2325 if (rval == SWITCH_FALSE) {
2326 cont->bug_running = 0;
2327 }
2328
2329 return rval;
2330 }
2331
spandsp_fax_stop_detect_session(switch_core_session_t * session)2332 switch_status_t spandsp_fax_stop_detect_session(switch_core_session_t *session)
2333 {
2334 switch_channel_t *channel = switch_core_session_get_channel(session);
2335 spandsp_fax_tone_container_t *cont = switch_channel_get_private(channel, "_fax_tone_detect_");
2336
2337 if (cont) {
2338 switch_channel_set_private(channel, "_fax_tone_detect_", NULL);
2339 cont->up = 0;
2340 switch_core_media_bug_remove(session, &cont->bug);
2341 return SWITCH_STATUS_SUCCESS;
2342 }
2343 return SWITCH_STATUS_FALSE;
2344 }
2345
spandsp_fax_detect_session(switch_core_session_t * session,const char * flags,int timeout,int tone_type,int hits,const char * app,const char * data,switch_tone_detect_callback_t callback)2346 switch_status_t spandsp_fax_detect_session(switch_core_session_t *session,
2347 const char *flags, int timeout, int tone_type,
2348 int hits, const char *app, const char *data, switch_tone_detect_callback_t callback)
2349 {
2350 switch_channel_t *channel = switch_core_session_get_channel(session);
2351 switch_status_t status;
2352 time_t to = 0;
2353 spandsp_fax_tone_container_t *cont = switch_channel_get_private(channel, "_fax_tone_detect_");
2354 switch_media_bug_flag_t bflags = 0;
2355 const char *var;
2356 switch_codec_implementation_t read_impl = { 0 };
2357 switch_core_session_get_read_impl(session, &read_impl);
2358
2359
2360 if (timeout) {
2361 to = switch_epoch_time_now(NULL) + timeout;
2362 }
2363
2364 if (cont) {
2365 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Max Tones Reached!\n");
2366 return SWITCH_STATUS_FALSE;
2367 }
2368
2369 if (!(cont = switch_core_session_alloc(session, sizeof(*cont)))) {
2370 return SWITCH_STATUS_MEMERR;
2371 }
2372
2373 switch_channel_set_app_flag_key("T38", channel, CF_APP_T38_POSSIBLE);
2374
2375 if (app) {
2376 cont->app = switch_core_session_strdup(session, app);
2377 }
2378
2379 if (data) {
2380 cont->data = switch_core_session_strdup(session, data);
2381 }
2382
2383 cont->tone_type = tone_type;
2384 cont->callback = callback;
2385 cont->up = 1;
2386 cont->session = session;
2387
2388 if (switch_channel_pre_answer(channel) != SWITCH_STATUS_SUCCESS) {
2389 return SWITCH_STATUS_FALSE;
2390 }
2391
2392 cont->default_sleep = 25;
2393 cont->default_expires = 250;
2394
2395 if ((var = switch_channel_get_variable(channel, "fax_tone_detect_sleep"))) {
2396 int tmp = atoi(var);
2397 if (tmp > 0) {
2398 cont->default_sleep = tmp;
2399 }
2400 }
2401
2402 if ((var = switch_channel_get_variable(channel, "fax_tone_detect_expires"))) {
2403 int tmp = atoi(var);
2404 if (tmp > 0) {
2405 cont->default_expires = tmp;
2406 }
2407 }
2408
2409 if (zstr(flags)) {
2410 bflags = SMBF_READ_REPLACE;
2411 } else {
2412 if (strchr(flags, 'r')) {
2413 bflags |= SMBF_READ_REPLACE;
2414 } else if (strchr(flags, 'w')) {
2415 bflags |= SMBF_WRITE_REPLACE;
2416 }
2417 }
2418
2419 bflags |= SMBF_NO_PAUSE;
2420
2421
2422 switch_core_event_hook_add_send_dtmf(session, tone_on_dtmf);
2423 switch_core_event_hook_add_recv_dtmf(session, tone_on_dtmf);
2424
2425
2426 if ((status = switch_core_media_bug_add(session, "fax_tone_detect", "",
2427 tone_detect_callback, cont, to, bflags, &cont->bug)) != SWITCH_STATUS_SUCCESS) {
2428 cont->bug_running = 0;
2429 return status;
2430 }
2431
2432 switch_channel_set_private(channel, "_fax_tone_detect_", cont);
2433
2434 return SWITCH_STATUS_SUCCESS;
2435 }
2436
2437
2438
2439
2440 /* For Emacs:
2441 * Local Variables:
2442 * mode:c
2443 * indent-tabs-mode:t
2444 * tab-width:4
2445 * c-basic-offset:4
2446 * End:
2447 * For VIM:
2448 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
2449 */
2450