diff --git a/src/include/switch_core.h b/src/include/switch_core.h index dc9318e251..6ff20b410f 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -103,9 +103,10 @@ struct switch_core_runtime; ///\{ /*! \brief Initilize the core + \param console optional FILE stream for output \note to be called at application startup */ -SWITCH_DECLARE(switch_status) switch_core_init(void); +SWITCH_DECLARE(switch_status) switch_core_init(FILE *console); /*! \brief Destroy the core @@ -883,6 +884,12 @@ SWITCH_DECLARE(switch_status) switch_core_directory_close(switch_directory_handl */ SWITCH_DECLARE(FILE *) switch_core_data_channel(switch_text_channel channel); +/*! + \brief Set the output console to the desired FILE stream + \param handle the FILE stream +*/ +SWITCH_DECLARE(void) switch_core_set_console(FILE *handle); + /*! \brief Launch a thread */ diff --git a/src/mod/endpoints/mod_wanpipe/mod_wanpipe.c b/src/mod/endpoints/mod_wanpipe/mod_wanpipe.c index 2d40b743ee..fd21795624 100644 --- a/src/mod/endpoints/mod_wanpipe/mod_wanpipe.c +++ b/src/mod/endpoints/mod_wanpipe/mod_wanpipe.c @@ -63,7 +63,7 @@ typedef enum { TFLAG_SWITCH = (1 << 9), } TFLAGS; -#define PACKET_LEN 160 + #define DEFAULT_MTU 160 static struct { @@ -205,7 +205,7 @@ static int check_flags(struct sangoma_pri *spri); static int on_restart(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event); static int on_anything(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event); static void *pri_thread_run(switch_thread *thread, void *obj); -static int config_wanpipe(int reload); +static switch_status config_wanpipe(int reload); /* @@ -219,6 +219,7 @@ static switch_status wanpipe_on_init(switch_core_session *session) switch_channel *channel = NULL; wanpipe_tdm_api_t tdm_api; int err = 0; + int mtu_mru; unsigned int rate = 8000; channel = switch_core_session_get_channel(session); @@ -229,10 +230,15 @@ static switch_status wanpipe_on_init(switch_core_session *session) tech_pvt->read_frame.data = tech_pvt->databuf; - switch_console_printf(SWITCH_CHANNEL_CONSOLE, "WANPIPE INIT\n"); + err = sangoma_tdm_set_codec(tech_pvt->socket, &tdm_api, WP_SLINEAR); + mtu_mru = sangoma_tdm_get_usr_mtu_mru(tech_pvt->socket, &tdm_api); + switch_console_printf(SWITCH_CHANNEL_CONSOLE, "WANPIPE INIT MTU is %d\n", mtu_mru); - err = sangoma_tdm_set_codec(tech_pvt->socket, &tdm_api, WP_SLINEAR); - + if (mtu_mru != globals.mtu) { + sangoma_tdm_set_usr_period(tech_pvt->socket, &tdm_api, (globals.mtu / 8) / 2); + mtu_mru = sangoma_tdm_get_usr_mtu_mru(tech_pvt->socket, &tdm_api); + switch_console_printf(SWITCH_CHANNEL_CONSOLE, "ADJUSTED MTU is %d\n", mtu_mru); + } if (switch_core_codec_init (&tech_pvt->read_codec, "L16", rate, 20, 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, @@ -455,8 +461,6 @@ static switch_status wanpipe_outgoing_channel(switch_core_session *session, swit if (spri && (tech_pvt->call = pri_new_call(spri->pri))) { struct pri_sr *sr; - int mtu_mru; - wanpipe_tdm_api_t tdm_api; sr = pri_sr_new(); pri_sr_set_channel(sr, channo, 0, 0); @@ -487,9 +491,6 @@ static switch_status wanpipe_outgoing_channel(switch_core_session *session, swit pri_sr_free(sr); chanmap->map[channo] = *new_session; tech_pvt->spri = spri; - - mtu_mru = sangoma_tdm_get_usr_mtu_mru(tech_pvt->socket, &tdm_api); - switch_console_printf(SWITCH_CHANNEL_CONSOLE, "MTU is %d\n", mtu_mru); } } else { @@ -608,8 +609,8 @@ static switch_status wanpipe_write_frame(switch_core_session *session, switch_fr assert(tech_pvt != NULL); while (tech_pvt->dtmf_buffer && bwrote < frame->datalen && bytes > 0 && (inuse = switch_buffer_inuse(tech_pvt->dtmf_buffer)) > 0) { - if ((bread = switch_buffer_read(tech_pvt->dtmf_buffer, dtmf, PACKET_LEN)) < PACKET_LEN) { - while (bread < PACKET_LEN) { + if ((bread = switch_buffer_read(tech_pvt->dtmf_buffer, dtmf, globals.mtu)) < globals.mtu) { + while (bread < globals.mtu) { dtmf[bread++] = 0; } } @@ -637,11 +638,11 @@ static switch_status wanpipe_write_frame(switch_core_session *session, switch_fr while (bytes > 0) { sangoma_socket_waitfor(tech_pvt->socket, -1, POLLOUT | POLLERR | POLLHUP); res = sangoma_sendmsg_socket(tech_pvt->socket, - &tech_pvt->hdrframe, sizeof(tech_pvt->hdrframe), bp, PACKET_LEN, 0); + &tech_pvt->hdrframe, sizeof(tech_pvt->hdrframe), bp, globals.mtu, 0); if (res < 0) { switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Bad Write frame len %d write %d bytes returned %d (%s)!\n", frame->datalen, - PACKET_LEN, res, strerror(errno)); + globals.mtu, res, strerror(errno)); if (errno == EBUSY) { continue; } @@ -753,8 +754,19 @@ static const switch_loadable_module_interface wanpipe_module_interface = { /*.application_interface */ NULL }; +static void s_pri_error(struct pri *pri, char *s) +{ + switch_console_printf(SWITCH_CHANNEL_CONSOLE, s); +} + +static void s_pri_message(struct pri *pri, char *s) +{ + s_pri_error(pri, s); +} + SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename) { + switch_status status = SWITCH_STATUS_SUCCESS; memset(SPANS, 0, sizeof(SPANS)); @@ -764,13 +776,19 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_modul } /* start the pri's */ - config_wanpipe(0); + if ((status = config_wanpipe(0) != SWITCH_STATUS_SUCCESS)) { + return status; + } + + pri_set_error(s_pri_error); + pri_set_message(s_pri_message); + /* connect my internal structure to the blank pointer passed to me */ *interface = &wanpipe_module_interface; /* indicate that the module should continue to be loaded */ - return SWITCH_STATUS_SUCCESS; + return status; } @@ -1076,7 +1094,7 @@ static void pri_thread_launch(struct sangoma_pri *spri) } -static int config_wanpipe(int reload) +static switch_status config_wanpipe(int reload) { switch_config cfg; char *var, *val; diff --git a/src/switch.c b/src/switch.c index 3f165e931a..9e8d99b939 100644 --- a/src/switch.c +++ b/src/switch.c @@ -43,8 +43,36 @@ int main(int argc, char *argv[]) { char *err = NULL; switch_event *event; + int bg = 0; + FILE *out = NULL; - if (switch_core_init() != SWITCH_STATUS_SUCCESS) { + if (argv[1] && !strcmp(argv[1], "-nc")) { + bg++; + } + + if (bg) { + int pid; +#ifdef WIN32 + char *path = ".\\freeswitch.log"; +#else + char *path = "/var/log/freeswitch.log"; + nice(-20); +#endif + + if ((out = fopen(path, "a")) == 0) { + fprintf(stderr, "Cannot open output file.\n"); + return 255; + } + + (void) signal(SIGHUP, (void *) handle_SIGHUP); + + if ((pid = fork())) { + fprintf(stderr, "%d Backgrounding.\n", (int)pid); + exit(0); + } + } + + if (switch_core_init(out) != SWITCH_STATUS_SUCCESS) { err = "Cannot Initilize\n"; } @@ -68,17 +96,16 @@ int main(int argc, char *argv[]) switch_console_printf(SWITCH_CHANNEL_CONSOLE, "freeswitch Version %s Started\n\n", SWITCH_VERSION_FULL); - if (argv[1] && !strcmp(argv[1], "-nc")) { - (void) signal(SIGHUP, (void *) handle_SIGHUP); + if (bg) { RUNNING = 1; while(RUNNING) { switch_yield(10000); } - } else { + } else { /* wait for console input */ switch_console_loop(); } - + if (switch_event_create(&event, SWITCH_EVENT_SHUTDOWN) == SWITCH_STATUS_SUCCESS) { switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Shutting Down"); switch_event_fire(&event); diff --git a/src/switch_core.c b/src/switch_core.c index 2f84a47917..008b8b53e6 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -157,6 +157,11 @@ SWITCH_DECLARE(switch_core_db *) switch_core_db_open_file(char *filename) return db; } +SWITCH_DECLARE(void) switch_core_set_console(FILE *handle) +{ + runtime.console = handle; +} + SWITCH_DECLARE(FILE *) switch_core_data_channel(switch_text_channel channel) { FILE *handle = stdout; @@ -167,7 +172,7 @@ SWITCH_DECLARE(FILE *) switch_core_data_channel(switch_text_channel channel) handle = runtime.console; break; default: - handle = stdout; + handle = runtime.console; break; } @@ -2190,13 +2195,13 @@ static void core_event_handler(switch_event *event) } } -SWITCH_DECLARE(switch_status) switch_core_init(void) +SWITCH_DECLARE(switch_status) switch_core_init(FILE *console) { #ifdef EMBED_PERL PerlInterpreter *my_perl; #endif - runtime.console = stdout; + runtime.console = console ? console : stdout; /* INIT APR and Create the pool context */ if (apr_initialize() != APR_SUCCESS) {