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_spandsp.
18 *
19 * The Initial Developer of the Original Code is
20 * Michael Jerris <mike@jerris.com
21 *
22 * Portions created by the Initial Developer are Copyright (C)
23 * the Initial Developer. All Rights Reserved.
24 *
25 * Contributor(s):
26 *
27 * Massimo Cetra <devel@navynet.it>
28 * Anthony Minessale II <anthm@freeswitch.org>
29 * Brian West <brian@freeswitch.org>
30 * Steve Underwood <steveu@coppice.org>
31 * Antonio Gallo <agx@linux.it>
32 * Christopher M. Rienzo <chris@rienzo.com>
33 * mod_spandsp.c -- Module implementing spandsp fax, dsp, and codec functionality
34 *
35 */
36
37
38
39 #include "mod_spandsp.h"
40 #include <spandsp/version.h>
41 #include "mod_spandsp_modem.h"
42
43 /* **************************************************************************
44 FREESWITCH MODULE DEFINITIONS
45 ************************************************************************* */
46
47 struct spandsp_globals spandsp_globals = { 0 };
48
49 #define SPANFAX_RX_USAGE "<filename>"
50 #define SPANFAX_TX_USAGE "<filename>"
51
52 SWITCH_MODULE_LOAD_FUNCTION(mod_spandsp_init);
53 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_spandsp_shutdown);
54 SWITCH_MODULE_DEFINITION(mod_spandsp, mod_spandsp_init, mod_spandsp_shutdown, NULL);
55
56
SWITCH_STANDARD_APP(spanfax_tx_function)57 SWITCH_STANDARD_APP(spanfax_tx_function)
58 {
59 mod_spandsp_fax_process_fax(session, data, FUNCTION_TX);
60 }
61
SWITCH_STANDARD_APP(spanfax_rx_function)62 SWITCH_STANDARD_APP(spanfax_rx_function)
63 {
64 mod_spandsp_fax_process_fax(session, data, FUNCTION_RX);
65 }
66
SWITCH_STANDARD_APP(spanfax_stop_function)67 SWITCH_STANDARD_APP(spanfax_stop_function)
68 {
69 mod_spandsp_fax_stop_fax(session);
70 }
71
SWITCH_STANDARD_APP(dtmf_session_function)72 SWITCH_STANDARD_APP(dtmf_session_function)
73 {
74 spandsp_inband_dtmf_session(session);
75 }
76
SWITCH_STANDARD_APP(stop_dtmf_session_function)77 SWITCH_STANDARD_APP(stop_dtmf_session_function)
78 {
79 spandsp_stop_inband_dtmf_session(session);
80 }
81
82
SWITCH_STANDARD_APP(tdd_encode_function)83 SWITCH_STANDARD_APP(tdd_encode_function)
84 {
85 char *text = (char *) data;
86
87 if (!zstr(text)) {
88 spandsp_tdd_encode_session(session, text);
89 } else {
90 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Missing text data\n");
91 }
92 }
93
SWITCH_STANDARD_APP(tdd_send_function)94 SWITCH_STANDARD_APP(tdd_send_function)
95 {
96 char *text = (char *) data;
97
98 if (!zstr(text)) {
99 spandsp_tdd_send_session(session, text);
100 } else {
101 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Missing text data\n");
102 }
103 }
104
SWITCH_STANDARD_APP(stop_tdd_encode_function)105 SWITCH_STANDARD_APP(stop_tdd_encode_function)
106 {
107 spandsp_stop_tdd_encode_session(session);
108 }
109
110
111
112
SWITCH_STANDARD_APP(tdd_decode_function)113 SWITCH_STANDARD_APP(tdd_decode_function)
114 {
115 spandsp_tdd_decode_session(session);
116 }
117
SWITCH_STANDARD_APP(stop_tdd_decode_function)118 SWITCH_STANDARD_APP(stop_tdd_decode_function)
119 {
120 spandsp_stop_tdd_decode_session(session);
121 }
122
123
SWITCH_STANDARD_APP(spandsp_fax_detect_session_function)124 SWITCH_STANDARD_APP(spandsp_fax_detect_session_function)
125 {
126 int argc = 0;
127 char *argv[4] = { 0 };
128 char *dupdata;
129 const char *app = NULL, *arg = NULL;
130 int timeout = 0;
131 int tone_type = MODEM_CONNECT_TONES_FAX_CNG;
132
133 if (!zstr(data) && (dupdata = switch_core_session_strdup(session, data))) {
134 if ((argc = switch_split(dupdata, ' ', argv)) >= 2) {
135 app = argv[0];
136 arg = argv[1];
137 if (argc > 2) {
138 timeout = atoi(argv[2]);
139 if (timeout < 0) {
140 timeout = 0;
141 }
142 }
143 if (argc > 3) {
144 if (!strcmp(argv[3], "ced")) {
145 tone_type = MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE;
146 } else {
147 tone_type = MODEM_CONNECT_TONES_FAX_CNG;
148 }
149 }
150 }
151 }
152
153 if (app) {
154 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Enabling fax detection '%s' '%s'\n", argv[0], argv[1]);
155 spandsp_fax_detect_session(session, "rw", timeout, tone_type, 1, app, arg, NULL);
156 } else {
157 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot Enable fax detection '%s' '%s'\n", argv[0], argv[1]);
158 }
159 }
160
SWITCH_STANDARD_APP(spandsp_stop_fax_detect_session_function)161 SWITCH_STANDARD_APP(spandsp_stop_fax_detect_session_function)
162 {
163 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Disabling fax detection\n");
164 spandsp_fax_stop_detect_session(session);
165 }
166
tdd_event_handler(switch_event_t * event)167 static void tdd_event_handler(switch_event_t *event)
168 {
169 const char *uuid = switch_event_get_header(event, "tdd-uuid");
170 const char *message = switch_event_get_body(event);
171 switch_core_session_t *session;
172
173 if (zstr(message)) {
174 message = switch_event_get_header(event, "tdd-message");
175 }
176
177 if (zstr(message)) {
178 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "No message for tdd handler\n");
179 return;
180 }
181
182 if (zstr(uuid)) {
183 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "No uuid for tdd handler\n");
184 return;
185 }
186
187 if ((session = switch_core_session_locate(uuid))) {
188
189 spandsp_tdd_encode_session(session, message);
190
191 switch_core_session_rwunlock(session);
192 } else {
193 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "No session for supplied uuid.\n");
194 }
195 }
196
event_handler(switch_event_t * event)197 static void event_handler(switch_event_t *event)
198 {
199 load_configuration(1);
200 }
201
SWITCH_STANDARD_APP(t38_gateway_function)202 SWITCH_STANDARD_APP(t38_gateway_function)
203 {
204 switch_channel_t *channel = switch_core_session_get_channel(session);
205 int timeout = 20;
206 const char *var;
207 int argc = 0;
208 char *argv[2] = { 0 };
209 char *dupdata;
210 const char *direction = NULL, *flags = NULL;
211
212 if (!zstr(data) && (dupdata = switch_core_session_strdup(session, data))) {
213 if ((argc = switch_split(dupdata, ' ', argv))) {
214 if (argc > 0) {
215 direction = argv[0];
216 }
217
218 if (argc > 1) {
219 flags = argv[1];
220 }
221 }
222 }
223
224 switch_channel_set_app_flag_key("T38", channel, CF_APP_T38_POSSIBLE);
225
226 if (zstr(direction) || strcasecmp(direction, "self")) {
227 direction = "peer";
228 }
229
230 switch_channel_set_variable(channel, "t38_leg", direction);
231
232 if (!zstr(flags) && !strcasecmp(flags, "nocng")) {
233 t38_gateway_start(session, direction, NULL);
234 } else {
235 if ((var = switch_channel_get_variable(channel, "t38_gateway_detect_timeout"))) {
236 int to = atoi(var);
237 if (to > -1) {
238 timeout = to;
239 } else {
240 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "%s invalid timeout value.\n", switch_channel_get_name(channel));
241 }
242 }
243
244 //switch_ivr_tone_detect_session(session, "t38", "1100.0", "rw", timeout, 1, direction, NULL, t38_gateway_start);
245 spandsp_fax_detect_session(session, "rw", timeout, MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE, 1, direction, NULL, t38_gateway_start);
246 }
247 }
248
249 /**
250 * Start tone detector application
251 *
252 * @param data the command string
253 */
SWITCH_STANDARD_APP(start_tone_detect_app)254 SWITCH_STANDARD_APP(start_tone_detect_app)
255 {
256 switch_channel_t *channel;
257 if (!session) {
258 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No session\n");
259 return;
260 }
261 channel = switch_core_session_get_channel(session);
262 if (zstr(data)) {
263 switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "-ERR missing descriptor name");
264 } else if (callprogress_detector_start(session, data) != SWITCH_STATUS_SUCCESS) {
265 switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "-ERR failed to start tone detector");
266 } else {
267 switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "+OK started");
268 }
269 }
270
271
SWITCH_STANDARD_API(start_tone_detect_api)272 SWITCH_STANDARD_API(start_tone_detect_api)
273 {
274 switch_status_t status = SWITCH_STATUS_SUCCESS;
275 switch_core_session_t *psession = NULL;
276 char *puuid = NULL, *descriptor = NULL;
277
278 if (zstr(cmd)) {
279 stream->write_function(stream, "-ERR missing uuid\n");
280 return SWITCH_STATUS_SUCCESS;
281 }
282
283 puuid = strdup((char *)cmd);
284
285 switch_assert(puuid);
286
287 if ((descriptor = strchr(puuid, ' '))) {
288 *descriptor++ = '\0';
289 }
290
291 if (zstr(descriptor)) {
292 stream->write_function(stream, "-ERR missing descriptor name\n");
293 goto end;
294 }
295
296 if (!(psession = switch_core_session_locate(puuid))) {
297 stream->write_function(stream, "-ERR Cannot locate session\n");
298 goto end;
299 }
300
301 status = callprogress_detector_start(psession, descriptor);
302
303 if (status == SWITCH_STATUS_SUCCESS) {
304 stream->write_function(stream, "+OK started\n");
305 } else {
306 stream->write_function(stream, "-ERR failed to start tone detector\n");
307 }
308
309 switch_core_session_rwunlock(psession);
310
311 end:
312
313 switch_safe_free(puuid);
314
315 return status;
316 }
317
318 /**
319 * Stop tone detector application
320 *
321 * @param data the command string
322 */
SWITCH_STANDARD_APP(stop_tone_detect_app)323 SWITCH_STANDARD_APP(stop_tone_detect_app)
324 {
325 switch_channel_t *channel;
326 if (!session) {
327 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No session\n");
328 return;
329 }
330 channel = switch_core_session_get_channel(session);
331 callprogress_detector_stop(session);
332 switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "+OK stopped");
333 }
334
335 /**
336 * Stop tone detector API
337 */
SWITCH_STANDARD_API(stop_tone_detect_api)338 SWITCH_STANDARD_API(stop_tone_detect_api)
339 {
340 switch_status_t status = SWITCH_STATUS_SUCCESS;
341 switch_core_session_t *psession = NULL;
342
343 if (zstr(cmd)) {
344 stream->write_function(stream, "-ERR missing session UUID\n");
345 return SWITCH_STATUS_SUCCESS;
346 }
347
348 if (!(psession = switch_core_session_locate(cmd))) {
349 stream->write_function(stream, "-ERR Cannot locate session\n");
350 return SWITCH_STATUS_SUCCESS;
351 }
352
353 callprogress_detector_stop(psession);
354 stream->write_function(stream, "+OK stopped\n");
355 switch_core_session_rwunlock(psession);
356
357 return status;
358 }
359
360
361
SWITCH_STANDARD_API(start_tdd_detect_api)362 SWITCH_STANDARD_API(start_tdd_detect_api)
363 {
364 switch_status_t status = SWITCH_STATUS_SUCCESS;
365 switch_core_session_t *psession = NULL;
366
367 if (!(psession = switch_core_session_locate(cmd))) {
368 stream->write_function(stream, "-ERR Cannot locate session\n");
369 return SWITCH_STATUS_SUCCESS;
370 }
371
372 status = spandsp_tdd_decode_session(psession);
373
374 if (status == SWITCH_STATUS_SUCCESS) {
375 stream->write_function(stream, "+OK started\n");
376 } else {
377 stream->write_function(stream, "-ERR failed to start tdd detector\n");
378 }
379
380 switch_core_session_rwunlock(psession);
381
382 return status;
383 }
384
385
SWITCH_STANDARD_API(stop_tdd_detect_api)386 SWITCH_STANDARD_API(stop_tdd_detect_api)
387 {
388 switch_status_t status = SWITCH_STATUS_SUCCESS;
389 switch_core_session_t *psession = NULL;
390
391
392 if (!(psession = switch_core_session_locate(cmd))) {
393 stream->write_function(stream, "-ERR Cannot locate session\n");
394 return SWITCH_STATUS_SUCCESS;
395 }
396
397 spandsp_stop_tdd_decode_session(psession);
398
399 stream->write_function(stream, "+OK stopped\n");
400 switch_core_session_rwunlock(psession);
401
402 return status;
403 }
404
405
SWITCH_STANDARD_API(start_send_tdd_api)406 SWITCH_STANDARD_API(start_send_tdd_api)
407 {
408 switch_core_session_t *psession = NULL;
409 char *puuid = NULL, *text = NULL;
410
411 if (zstr(cmd)) {
412 stream->write_function(stream, "-ERR missing uuid\n");
413 return SWITCH_STATUS_SUCCESS;
414 }
415
416 puuid = strdup((char *)cmd);
417
418 switch_assert(puuid);
419
420 if ((text = strchr(puuid, ' '))) {
421 *text++ = '\0';
422 }
423
424 if (zstr(text)) {
425 stream->write_function(stream, "-ERR missing text\n");
426 goto end;
427 }
428
429
430 if (!(psession = switch_core_session_locate(puuid))) {
431 stream->write_function(stream, "-ERR Cannot locate session\n");
432 goto end;
433 }
434
435
436 spandsp_tdd_encode_session(psession, text);
437
438 stream->write_function(stream, "+OK\n");
439 switch_core_session_rwunlock(psession);
440
441 end:
442
443 switch_safe_free(puuid);
444
445 return SWITCH_STATUS_SUCCESS;
446 }
447
mod_spandsp_indicate_data(switch_core_session_t * session,switch_bool_t self,switch_bool_t on)448 void mod_spandsp_indicate_data(switch_core_session_t *session, switch_bool_t self, switch_bool_t on)
449 {
450 switch_core_session_t *target_session = NULL;
451 int locked = 0;
452
453 if (self) {
454 target_session = session;
455 } else {
456 if (switch_core_session_get_partner(session, &target_session) == SWITCH_STATUS_SUCCESS) {
457 locked = 1;
458 } else {
459 target_session = NULL;
460 }
461 }
462
463 if (target_session) {
464 switch_core_session_message_t *msg;
465
466 msg = switch_core_session_alloc(target_session, sizeof(*msg));
467 MESSAGE_STAMP_FFL(msg);
468 msg->message_id = SWITCH_MESSAGE_INDICATE_AUDIO_DATA;
469 msg->from = __FILE__;
470 msg->numeric_arg = on;
471
472 switch_core_session_queue_message(target_session, msg);
473
474 if (locked) {
475 switch_core_session_rwunlock(target_session);
476 }
477 }
478 }
479
480
481 /* **************************************************************************
482 CONFIGURATION
483 ************************************************************************* */
destroy_descriptor(void * ptr)484 static void destroy_descriptor(void *ptr)
485 {
486 tone_descriptor_t *d = (tone_descriptor_t *) ptr;
487
488 tone_descriptor_destroy(d);
489 }
490
load_configuration(switch_bool_t reload)491 switch_status_t load_configuration(switch_bool_t reload)
492 {
493 switch_xml_t xml = NULL, x_lists = NULL, x_list = NULL, cfg = NULL, callprogress = NULL, xdescriptor = NULL;
494 switch_status_t status = SWITCH_STATUS_FALSE;
495
496 switch_mutex_lock(spandsp_globals.mutex);
497
498 if (spandsp_globals.tones) {
499 switch_core_hash_destroy(&spandsp_globals.tones);
500 }
501
502 if (spandsp_globals.config_pool) {
503 switch_core_destroy_memory_pool(&spandsp_globals.config_pool);
504 }
505
506 switch_core_new_memory_pool(&spandsp_globals.config_pool);
507 switch_core_hash_init(&spandsp_globals.tones);
508
509 spandsp_globals.modem_dialplan = "XML";
510 spandsp_globals.modem_context = "default";
511 spandsp_globals.modem_directory = "/dev";
512 spandsp_globals.modem_count = 0;
513
514
515 spandsp_globals.enable_t38 = 1;
516 spandsp_globals.enable_tep = 0;
517 spandsp_globals.total_sessions = 0;
518 spandsp_globals.verbose = 0;
519 spandsp_globals.use_ecm = 1;
520 spandsp_globals.disable_v17 = 0;
521 spandsp_globals.prepend_string = switch_core_strdup(spandsp_globals.config_pool, "fax");
522 spandsp_globals.spool = switch_core_strdup(spandsp_globals.config_pool, "/tmp");
523 spandsp_globals.ident = "SpanDSP Fax Ident";
524 spandsp_globals.header = "SpanDSP Fax Header";
525 spandsp_globals.timezone = "";
526 spandsp_globals.tonedebug = 0;
527 spandsp_globals.t38_tx_reinvite_packet_count = 100;
528 spandsp_globals.t38_rx_reinvite_packet_count = 50;
529
530 if ((xml = switch_xml_open_cfg("spandsp.conf", &cfg, NULL)) || (xml = switch_xml_open_cfg("fax.conf", &cfg, NULL))) {
531 status = SWITCH_STATUS_SUCCESS;
532
533 if ((x_lists = switch_xml_child(cfg, "modem-settings"))) {
534 for (x_list = switch_xml_child(x_lists, "param"); x_list; x_list = x_list->next) {
535 const char *name = switch_xml_attr(x_list, "name");
536 const char *value = switch_xml_attr(x_list, "value");
537
538 if (zstr(name)) {
539 continue;
540 }
541
542 if (zstr(value)) {
543 continue;
544 }
545
546
547 if (!reload && !strcmp(name, "total-modems")) {
548 int tmp = atoi(value);
549
550 if (tmp > -1 && tmp < MAX_MODEMS) {
551 spandsp_globals.modem_count = tmp;
552 } else {
553 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid value [%d] for total-modems\n", tmp);
554 }
555 } else if (!strcmp(name, "directory")) {
556 spandsp_globals.modem_directory = switch_core_strdup(spandsp_globals.config_pool, value);
557 } else if (!strcmp(name, "dialplan")) {
558 spandsp_globals.modem_dialplan = switch_core_strdup(spandsp_globals.config_pool, value);
559 } else if (!strcmp(name, "context")) {
560 spandsp_globals.modem_context = switch_core_strdup(spandsp_globals.config_pool, value);
561 } else if (!strcmp(name, "verbose")) {
562 if (switch_true(value)) {
563 spandsp_globals.modem_verbose = 1;
564 } else {
565 spandsp_globals.modem_verbose = 0;
566 }
567 }
568 }
569 }
570
571 if ((x_lists = switch_xml_child(cfg, "fax-settings")) || (x_lists = switch_xml_child(cfg, "settings"))) {
572 for (x_list = switch_xml_child(x_lists, "param"); x_list; x_list = x_list->next) {
573 const char *name = switch_xml_attr(x_list, "name");
574 const char *value = switch_xml_attr(x_list, "value");
575
576 if (zstr(name)) {
577 continue;
578 }
579
580 if (zstr(value)) {
581 continue;
582 }
583
584 if (!strcmp(name, "use-ecm")) {
585 if (switch_true(value))
586 spandsp_globals.use_ecm = 1;
587 else
588 spandsp_globals.use_ecm = 0;
589 } else if (!strcmp(name, "verbose")) {
590 if (switch_true(value))
591 spandsp_globals.verbose = 1;
592 else
593 spandsp_globals.verbose = 0;
594 } else if (!strcmp(name, "disable-v17")) {
595 if (switch_true(value))
596 spandsp_globals.disable_v17 = 1;
597 else
598 spandsp_globals.disable_v17 = 0;
599 } else if (!strcmp(name, "enable-colour")) {
600 if (switch_true(value))
601 spandsp_globals.enable_colour_fax = 1;
602 else
603 spandsp_globals.enable_colour_fax = 0;
604 } else if (!strcmp(name, "enable-image-resizing")) {
605 if (switch_true(value))
606 spandsp_globals.enable_image_resizing = 1;
607 else
608 spandsp_globals.enable_image_resizing = 0;
609 } else if (!strcmp(name, "enable-colour-to-bilevel")) {
610 if (switch_true(value))
611 spandsp_globals.enable_colour_to_bilevel = 1;
612 else
613 spandsp_globals.enable_colour_to_bilevel = 0;
614 } else if (!strcmp(name, "enable-grayscale-to-bilevel")) {
615 if (switch_true(value))
616 spandsp_globals.enable_grayscale_to_bilevel = 1;
617 else
618 spandsp_globals.enable_grayscale_to_bilevel = 0;
619 } else if (!strcmp(name, "enable-tep")) {
620 if (switch_true(value)) {
621 spandsp_globals.enable_tep= 1;
622 } else {
623 spandsp_globals.enable_tep = 0;
624 }
625 } else if (!strcmp(name, "enable-t38")) {
626 if (switch_true(value)) {
627 spandsp_globals.enable_t38= 1;
628 } else {
629 spandsp_globals.enable_t38 = 0;
630 }
631 } else if (!strcmp(name, "enable-t38-request")) {
632 if (switch_true(value)) {
633 spandsp_globals.enable_t38_request = 1;
634 } else {
635 spandsp_globals.enable_t38_request = 0;
636 }
637 } else if (!strcmp(name, "t38-tx-reinvite-packet-count")) {
638 int delay = atoi(value);
639
640 if (delay >= 0 && delay < 1000) {
641 spandsp_globals.t38_tx_reinvite_packet_count = delay;
642 } else {
643 spandsp_globals.t38_tx_reinvite_packet_count = 100;
644 }
645 } else if (!strcmp(name, "t38-rx-reinvite-packet-count")) {
646 int delay = atoi(value);
647
648 if (delay >= 0 && delay < 1000) {
649 spandsp_globals.t38_rx_reinvite_packet_count = delay;
650 } else {
651 spandsp_globals.t38_rx_reinvite_packet_count = 0;
652 }
653 } else if (!strcmp(name, "ident")) {
654 if (!strcmp(value, "_undef_")) {
655 spandsp_globals.ident = "";
656 } else {
657 spandsp_globals.ident = switch_core_strdup(spandsp_globals.config_pool, value);
658 }
659 } else if (!strcmp(name, "header")) {
660 if (!strcmp(value, "_undef_")) {
661 spandsp_globals.header = "";
662 } else {
663 spandsp_globals.header = switch_core_strdup(spandsp_globals.config_pool, value);
664 }
665 } else if (!strcmp(name, "spool-dir")) {
666 spandsp_globals.spool = switch_core_strdup(spandsp_globals.config_pool, value);
667 } else if (!strcmp(name, "file-prefix")) {
668 spandsp_globals.prepend_string = switch_core_strdup(spandsp_globals.config_pool, value);
669 } else {
670 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Unknown parameter %s\n", name);
671 }
672
673 }
674 }
675
676 /* Configure call progress detector */
677 if ((callprogress = switch_xml_child(cfg, "descriptors"))) {
678 /* check if debugging is enabled */
679 const char *debug = switch_xml_attr(callprogress, "debug-level");
680 if (!zstr(debug) && switch_is_number(debug)) {
681 int debug_val = atoi(debug);
682 if (debug_val > 0) {
683 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Setting tone detector debug-level to : %d\n", debug_val);
684 spandsp_globals.tonedebug = debug_val;
685 }
686 }
687
688 for (xdescriptor = switch_xml_child(callprogress, "descriptor"); xdescriptor; xdescriptor = switch_xml_next(xdescriptor)) {
689 const char *name = switch_xml_attr(xdescriptor, "name");
690 const char *tone_name = NULL;
691 switch_xml_t tone = NULL, element = NULL;
692 tone_descriptor_t *descriptor = NULL;
693
694 /* create descriptor */
695 if (zstr(name)) {
696 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Missing <descriptor> name\n");
697 switch_goto_status(SWITCH_STATUS_FALSE, done);
698 }
699 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "Adding tone_descriptor: %s\n", name);
700 if (tone_descriptor_create(&descriptor, name, spandsp_globals.config_pool) != SWITCH_STATUS_SUCCESS) {
701 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Unable to allocate tone_descriptor: %s\n", name);
702 switch_goto_status(SWITCH_STATUS_FALSE, done);
703 }
704
705 switch_core_hash_insert_destructor(spandsp_globals.tones, name, descriptor, destroy_descriptor);
706
707 /* add tones to descriptor */
708 for (tone = switch_xml_child(xdescriptor, "tone"); tone; tone = switch_xml_next(tone)) {
709 int id = 0;
710 tone_name = switch_xml_attr(tone, "name");
711 if (zstr(tone_name)) {
712 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Missing <tone> name for <descriptor> %s\n", name);
713 switch_goto_status(SWITCH_STATUS_FALSE, done);
714 }
715 id = tone_descriptor_add_tone(descriptor, tone_name);
716 if (id == -1) {
717 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
718 "Unable to add tone_descriptor: %s, tone: %s. (too many tones)\n", name, tone_name);
719 switch_goto_status(SWITCH_STATUS_FALSE, done);
720 }
721 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10,
722 "Adding tone_descriptor: %s, tone: %s(%d)\n", name, tone_name, id);
723 /* add elements to tone */
724 for (element = switch_xml_child(tone, "element"); element; element = switch_xml_next(element)) {
725 const char *freq1_attr = switch_xml_attr(element, "freq1");
726 const char *freq2_attr = switch_xml_attr(element, "freq2");
727 const char *min_attr = switch_xml_attr(element, "min");
728 const char *max_attr = switch_xml_attr(element, "max");
729 int freq1, freq2, min, max;
730 if (zstr(freq1_attr)) {
731 freq1 = 0;
732 } else {
733 freq1 = atoi(freq1_attr);
734 }
735 if (zstr(freq2_attr)) {
736 freq2 = 0;
737 } else {
738 freq2 = atoi(freq2_attr);
739 }
740 if (zstr(min_attr)) {
741 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
742 "Missing min in <element> of <descriptor> %s <tone> %s(%d)\n", name, tone_name, id);
743 switch_goto_status(SWITCH_STATUS_FALSE, done);
744 }
745 min = atoi(min_attr);
746 if (zstr(max_attr)) {
747 max = 0;
748 } else {
749 max = atoi(max_attr);
750 }
751 /* check params */
752 if ((freq1 < 0 || freq2 < 0 || min < 0 || max < 0) || (freq1 == 0 && min == 0 && max == 0)) {
753 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid element param.\n");
754 switch_goto_status(SWITCH_STATUS_FALSE, done);
755 }
756 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10,
757 "Adding tone_descriptor: %s, tone: %s(%d), element (%d, %d, %d, %d)\n", name, tone_name, id, freq1, freq2, min, max);
758 tone_descriptor_add_tone_element(descriptor, id, freq1, freq2, min, max);
759 }
760 }
761 }
762 }
763
764 done:
765
766 switch_xml_free(xml);
767 }
768
769 switch_mutex_unlock(spandsp_globals.mutex);
770
771 return status;
772 }
773
774
SWITCH_MODULE_LOAD_FUNCTION(mod_spandsp_init)775 SWITCH_MODULE_LOAD_FUNCTION(mod_spandsp_init)
776 {
777 switch_application_interface_t *app_interface;
778 switch_api_interface_t *api_interface;
779
780
781 if (switch_event_reserve_subclass(MY_EVENT_TDD_RECV_MESSAGE) != SWITCH_STATUS_SUCCESS) {
782 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't register subclass %s!\n", MY_EVENT_TDD_RECV_MESSAGE);
783 return SWITCH_STATUS_TERM;
784 }
785
786 if (switch_event_reserve_subclass(SPANDSP_EVENT_TXFAXNEGOCIATERESULT) != SWITCH_STATUS_SUCCESS) {
787 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't register subclass %s!\n", SPANDSP_EVENT_TXFAXNEGOCIATERESULT);
788 return SWITCH_STATUS_TERM;
789 }
790
791 if (switch_event_reserve_subclass(SPANDSP_EVENT_RXFAXNEGOCIATERESULT) != SWITCH_STATUS_SUCCESS) {
792 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't register subclass %s!\n", SPANDSP_EVENT_RXFAXNEGOCIATERESULT);
793 return SWITCH_STATUS_TERM;
794 }
795
796 if (switch_event_reserve_subclass(SPANDSP_EVENT_TXFAXPAGERESULT) != SWITCH_STATUS_SUCCESS) {
797 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't register subclass %s!\n", SPANDSP_EVENT_TXFAXPAGERESULT);
798 return SWITCH_STATUS_TERM;
799 }
800
801 if (switch_event_reserve_subclass(SPANDSP_EVENT_RXFAXPAGERESULT) != SWITCH_STATUS_SUCCESS) {
802 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't register subclass %s!\n", SPANDSP_EVENT_RXFAXPAGERESULT);
803 return SWITCH_STATUS_TERM;
804 }
805
806 if (switch_event_reserve_subclass(SPANDSP_EVENT_TXFAXRESULT) != SWITCH_STATUS_SUCCESS) {
807 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't register subclass %s!\n", SPANDSP_EVENT_TXFAXRESULT);
808 return SWITCH_STATUS_TERM;
809 }
810
811 if (switch_event_reserve_subclass(SPANDSP_EVENT_RXFAXRESULT) != SWITCH_STATUS_SUCCESS) {
812 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't register subclass %s!\n", SPANDSP_EVENT_RXFAXRESULT);
813 return SWITCH_STATUS_TERM;
814 }
815
816 memset(&spandsp_globals, 0, sizeof(spandsp_globals));
817 spandsp_globals.pool = pool;
818
819 *module_interface = switch_loadable_module_create_module_interface(pool, modname);
820 switch_mutex_init(&spandsp_globals.mutex, SWITCH_MUTEX_NESTED, pool);
821
822 SWITCH_ADD_APP(app_interface, "t38_gateway", "Convert to T38 Gateway if tones are heard", "Convert to T38 Gateway if tones are heard",
823 t38_gateway_function, "", SAF_MEDIA_TAP);
824
825 SWITCH_ADD_APP(app_interface, "rxfax", "FAX Receive Application", "FAX Receive Application", spanfax_rx_function, SPANFAX_RX_USAGE,
826 SAF_SUPPORT_NOMEDIA | SAF_NO_LOOPBACK);
827 SWITCH_ADD_APP(app_interface, "txfax", "FAX Transmit Application", "FAX Transmit Application", spanfax_tx_function, SPANFAX_TX_USAGE,
828 SAF_SUPPORT_NOMEDIA | SAF_NO_LOOPBACK);
829 SWITCH_ADD_APP(app_interface, "stopfax", "Stop FAX Application", "Stop FAX Application", spanfax_stop_function, "", SAF_NONE);
830
831 SWITCH_ADD_APP(app_interface, "spandsp_stop_dtmf", "stop inband dtmf", "Stop detecting inband dtmf.", stop_dtmf_session_function, "", SAF_NONE);
832 SWITCH_ADD_APP(app_interface, "spandsp_start_dtmf", "Detect dtmf", "Detect inband dtmf on the session", dtmf_session_function, "", SAF_MEDIA_TAP);
833
834
835 SWITCH_ADD_APP(app_interface, "spandsp_stop_inject_tdd", "stop sending tdd", "", stop_tdd_encode_function, "", SAF_NONE);
836 SWITCH_ADD_APP(app_interface, "spandsp_inject_tdd", "Send TDD data", "Send TDD data", tdd_encode_function, "", SAF_MEDIA_TAP);
837
838 SWITCH_ADD_APP(app_interface, "spandsp_stop_detect_tdd", "stop sending tdd", "", stop_tdd_decode_function, "", SAF_NONE);
839 SWITCH_ADD_APP(app_interface, "spandsp_detect_tdd", "Detect TDD data", "Detect TDD data", tdd_decode_function, "", SAF_MEDIA_TAP);
840
841
842 SWITCH_ADD_APP(app_interface, "spandsp_send_tdd", "Send TDD data", "Send TDD data", tdd_send_function, "", SAF_NONE);
843
844 SWITCH_ADD_APP(app_interface, "spandsp_start_fax_detect", "start fax detect", "start fax detect", spandsp_fax_detect_session_function,
845 "<app>[ <arg>][ <timeout>][ <tone_type>]", SAF_NONE);
846
847 SWITCH_ADD_APP(app_interface, "spandsp_stop_fax_detect", "stop fax detect", "stop fax detect", spandsp_stop_fax_detect_session_function, "", SAF_NONE);
848
849 load_configuration(0);
850
851 mod_spandsp_fax_load(pool);
852 mod_spandsp_codecs_load(module_interface, pool);
853
854
855 if (mod_spandsp_dsp_load(module_interface, pool) != SWITCH_STATUS_SUCCESS) {
856 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't load or process spandsp.conf, not adding tone_detect applications\n");
857 } else {
858 SWITCH_ADD_APP(app_interface, "spandsp_start_tone_detect", "Start background tone detection with cadence", "", start_tone_detect_app, "<name>", SAF_NONE);
859 SWITCH_ADD_APP(app_interface, "spandsp_stop_tone_detect", "Stop background tone detection with cadence", "", stop_tone_detect_app, "", SAF_NONE);
860 SWITCH_ADD_API(api_interface, "spandsp_start_tone_detect", "Start background tone detection with cadence", start_tone_detect_api, "<uuid> <name>");
861 SWITCH_ADD_API(api_interface, "spandsp_stop_tone_detect", "Stop background tone detection with cadence", stop_tone_detect_api, "<uuid>");
862 switch_console_set_complete("add spandsp_start_tone_detect ::console::list_uuid");
863 switch_console_set_complete("add spandsp_stop_tone_detect ::console::list_uuid");
864 }
865
866 SWITCH_ADD_API(api_interface, "start_tdd_detect", "Start background tdd detection", start_tdd_detect_api, "<uuid>");
867 SWITCH_ADD_API(api_interface, "stop_tdd_detect", "Stop background tdd detection", stop_tdd_detect_api, "<uuid>");
868
869 SWITCH_ADD_API(api_interface, "uuid_send_tdd", "send tdd data to a uuid", start_send_tdd_api, "<uuid> <text>");
870
871 switch_console_set_complete("add uuid_send_tdd ::console::list_uuid");
872
873
874
875 if ((switch_event_bind(modname, SWITCH_EVENT_RELOADXML, NULL, event_handler, NULL) != SWITCH_STATUS_SUCCESS)) {
876 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind our reloadxml handler!\n");
877 /* Not such severe to prevent loading */
878 }
879
880
881 if (switch_event_bind(modname, SWITCH_EVENT_CUSTOM, MY_EVENT_TDD_SEND_MESSAGE, tdd_event_handler, NULL) != SWITCH_STATUS_SUCCESS) {
882 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind!\n");
883 }
884
885 #if defined(MODEM_SUPPORT)
886 modem_global_init(module_interface, pool);
887 #endif
888
889 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "mod_spandsp loaded, using spandsp library version [%s]\n", SPANDSP_RELEASE_DATETIME_STRING);
890
891 return SWITCH_STATUS_SUCCESS;
892 }
893
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_spandsp_shutdown)894 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_spandsp_shutdown)
895 {
896 switch_event_unbind_callback(event_handler);
897 switch_event_unbind_callback(tdd_event_handler);
898
899 switch_event_free_subclass(MY_EVENT_TDD_RECV_MESSAGE);
900 switch_event_free_subclass(SPANDSP_EVENT_TXFAXNEGOCIATERESULT);
901 switch_event_free_subclass(SPANDSP_EVENT_RXFAXNEGOCIATERESULT);
902 switch_event_free_subclass(SPANDSP_EVENT_TXFAXPAGERESULT);
903 switch_event_free_subclass(SPANDSP_EVENT_RXFAXPAGERESULT);
904 switch_event_free_subclass(SPANDSP_EVENT_TXFAXRESULT);
905 switch_event_free_subclass(SPANDSP_EVENT_RXFAXRESULT);
906
907 mod_spandsp_fax_shutdown();
908 mod_spandsp_dsp_shutdown();
909 #if defined(MODEM_SUPPORT)
910 modem_global_shutdown();
911 #endif
912
913 if (spandsp_globals.tones) {
914 switch_core_hash_destroy(&spandsp_globals.tones);
915 }
916
917 if (spandsp_globals.config_pool) {
918 switch_core_destroy_memory_pool(&spandsp_globals.config_pool);
919 }
920
921 memset(&spandsp_globals, 0, sizeof(spandsp_globals));
922
923 return SWITCH_STATUS_UNLOAD;
924 }
925
926 /* For Emacs:
927 * Local Variables:
928 * mode:c
929 * indent-tabs-mode:t
930 * tab-width:4
931 * c-basic-offset:4
932 * End:
933 * For VIM:
934 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
935 */
936