diff --git a/src/include/switch_core.h b/src/include/switch_core.h index e5be44969b..c3fbefa9ee 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -906,6 +906,11 @@ SWITCH_DECLARE(FILE *) switch_core_get_console(void); \brief Launch a thread */ SWITCH_DECLARE(void) switch_core_launch_thread(void *(*func)(switch_thread *, void*), void *obj, switch_memory_pool *pool); + +/*! + \brief Initiate Globals +*/ +SWITCH_DECLARE(void) switch_core_set_globals(void); ///\} #ifdef USE_PERL diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 39d96b9545..5626f1916e 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -42,9 +42,9 @@ extern "C" { #include #ifdef WIN32 -#define SEP "\\" +#define SWITCH_PATH_SEPARATOR "\\" #else -#define SEP "/" +#define SWITCH_PATH_SEPARATOR "/" #endif #ifndef SWITCH_PREFIX_DIR @@ -52,23 +52,23 @@ extern "C" { #endif #ifndef SWITCH_MOD_DIR -#define SWITCH_MOD_DIR SWITCH_PREFIX_DIR SEP "mod" +#define SWITCH_MOD_DIR SWITCH_PREFIX_DIR SWITCH_PATH_SEPARATOR "mod" #endif #ifndef SWITCH_CONF_DIR -#define SWITCH_CONF_DIR SWITCH_PREFIX_DIR SEP "conf" +#define SWITCH_CONF_DIR SWITCH_PREFIX_DIR SWITCH_PATH_SEPARATOR "conf" #endif #ifndef SWITCH_LOG_DIR -#define SWITCH_LOG_DIR SWITCH_PREFIX_DIR SEP "log" +#define SWITCH_LOG_DIR SWITCH_PREFIX_DIR SWITCH_PATH_SEPARATOR "log" #endif #ifndef SWITCH_DB_DIR -#define SWITCH_DB_DIR SWITCH_PREFIX_DIR SEP "db" +#define SWITCH_DB_DIR SWITCH_PREFIX_DIR SWITCH_PATH_SEPARATOR "db" #endif #ifndef SWITCH_SCRIPT_DIR -#define SWITCH_SCRIPT_DIR SWITCH_PREFIX_DIR SEP "scripts" +#define SWITCH_SCRIPT_DIR SWITCH_PREFIX_DIR SWITCH_PATH_SEPARATOR "scripts" #endif struct switch_directories { diff --git a/src/mod/languages/mod_spidermonkey/Makefile b/src/mod/languages/mod_spidermonkey/Makefile index ac721a643f..01501f0ad9 100644 --- a/src/mod/languages/mod_spidermonkey/Makefile +++ b/src/mod/languages/mod_spidermonkey/Makefile @@ -61,7 +61,7 @@ endif CFLAGS += -I$(BASE)/libs/mozilla/js/src -Wall -Wno-format -g -DXP_UNIX -DSVR4 -DSYSV -D_BSD_SOURCE -DPOSIX_SOURCE -DHAVE_LOCALTIME_R -DX86_LINUX -DDEBUG -DDEBUG_root -DJS_THREADSAFE -I$(BASE)/libs/mozilla/js/src -I$(BASE)/libs/mozilla/js/src/$(OS_CONFIG)_DBG.OBJ -Wall -Wno-format -g -DXP_UNIX -DSVR4 -DSYSV -D_BSD_SOURCE -DPOSIX_SOURCE -DHAVE_LOCALTIME_R -DX86_LINUX -DDEBUG -DDEBUG_root -DJS_THREADSAFE -I$(BASE)/libs/mozilla/nsprpub/dist/include/nspr -I$(OS_CONFIG)_DBG.OBJ LDFLAGS +=-DXP_UNIX -DSVR4 -DSYSV -D_BSD_SOURCE -DPOSIX_SOURCE -DHAVE_LOCALTIME_R -DX86_LINUX -DDEBUG -DDEBUG_root -DJS_THREADSAFE -I$(BASE)/libs/mozilla/nsprpub/dist/include/nspr -Wall -Wno-format -g -DXP_UNIX -DSVR4 -DSYSV -D_BSD_SOURCE -DPOSIX_SOURCE -DHAVE_LOCALTIME_R -DX86_LINUX -DDEBUG -DDEBUG_root -DJS_THREADSAFE -I$(BASE)/libs/mozilla/nsprpub/dist/include/nspr -lteletone -OBJS=$(BASE)/libs/mozilla/js/src/$(OS_CONFIG)_DBG.OBJ/libjs.a $(BASE)/libs/mozilla/nsprpub/dist/lib/libnspr4.a +OBJS=$(BASE)/libs/mozilla/js/src/$(OS_CONFIG)_DBG.OBJ/libjs.a $(BASE)/libs/mozilla/nsprpub/dist/lib/libnspr4.a -lcurl LINKER=$(CC) diff --git a/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c b/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c index 5e9bb1d918..76104e2361 100644 --- a/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c +++ b/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c @@ -110,6 +110,7 @@ struct teletone_obj { switch_core_session *session; switch_codec codec; switch_buffer *audio_buffer; + switch_buffer *loop_buffer; switch_memory_pool *pool; switch_timer *timer; switch_timer timer_base; @@ -762,7 +763,7 @@ static JSBool teletone_construct(JSContext *cx, JSObject *obj, uintN argc, jsval switch_codec *read_codec; switch_memory_pool *pool; char *timer_name = NULL; - + if (argc > 0) { if (JS_ValueToObject(cx, argv[0], &session_obj)) { if (!(jss = JS_GetPrivate(cx, session_obj))) { @@ -882,6 +883,7 @@ static JSBool teletone_on_dtmf(JSContext *cx, JSObject *obj, uintN argc, jsval * static JSBool teletone_generate(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { struct teletone_obj *tto = JS_GetPrivate(cx, obj); + int32 loops = 0; if (argc > 0) { char *script; @@ -893,7 +895,23 @@ static JSBool teletone_generate(JSContext *cx, JSObject *obj, uintN argc, jsval switch_core_thread_session thread_session; switch_channel *channel; + if (argc > 1) { + if (!JS_ValueToInt32(cx, argv[1], &loops)) { + switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Cannot Convert to INT\n"); + return JS_FALSE; + } + loops--; + if (!tto->loop_buffer) { + switch_buffer_create(tto->pool, &tto->loop_buffer, SWITCH_RECCOMMENDED_BUFFER_SIZE); + } + } + if (tto->audio_buffer) { + switch_buffer_zero(tto->audio_buffer); + } + if (tto->loop_buffer) { + switch_buffer_zero(tto->loop_buffer); + } tto->ts.debug = 1; tto->ts.debug_stream = switch_core_get_console(); @@ -944,7 +962,21 @@ static JSBool teletone_generate(JSContext *cx, JSObject *obj, uintN argc, jsval } } if ((write_frame.datalen = switch_buffer_read(tto->audio_buffer, fdata, write_frame.codec->implementation->bytes_per_frame)) <= 0) { - break; + if (loops > 0) { + switch_buffer *tmp; + + /* Switcharoo*/ + tmp = tto->audio_buffer; + tto->audio_buffer = tto->loop_buffer; + tto->loop_buffer = tmp; + loops--; + /* try again */ + if ((write_frame.datalen = switch_buffer_read(tto->audio_buffer, fdata, write_frame.codec->implementation->bytes_per_frame)) <= 0) { + break; + } + } else { + break; + } } write_frame.samples = write_frame.datalen / 2; @@ -954,6 +986,9 @@ static JSBool teletone_generate(JSContext *cx, JSObject *obj, uintN argc, jsval break; } } + if (tto->loop_buffer && loops) { + switch_buffer_write(tto->loop_buffer, write_frame.data, write_frame.datalen); + } } if (tto->timer) { diff --git a/src/switch.c b/src/switch.c index 6b96b19e40..9e064e15b4 100644 --- a/src/switch.c +++ b/src/switch.c @@ -65,34 +65,30 @@ int main(int argc, char *argv[]) char *lfile = "freeswitch.log"; char *pfile = "freeswitch.pid"; char path[256] = ""; + char *ppath = NULL; char *err = NULL; switch_event *event; int bg = 0; FILE *f; #ifdef WIN32 - char sep = '\\'; SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); #else - char sep = '/'; int pid; nice(-20); #endif - if (switch_core_init(NULL) != SWITCH_STATUS_SUCCESS) { - fprintf(stderr, "Cannot Initilize\n"); - return 255; - } - #ifndef WIN32 if (argv[1] && !strcmp(argv[1], "-stop")) { pid_t pid = 0; - snprintf(path, sizeof(path), "%s%c%s", SWITCH_GLOBAL_dirs.conf_dir, sep, pfile); + switch_core_set_globals(); + snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile); if ((f = fopen(path, "r")) == 0) { fprintf(stderr, "Cannot open pid file %s.\n", path); return 255; } fscanf(f, "%d", &pid); if (pid > 0) { + fprintf(stderr, "Killing %d\n", (int) pid); kill(pid, SIGTERM); } @@ -101,6 +97,23 @@ int main(int argc, char *argv[]) } #endif + if (argv[1] && !strcmp(argv[1], "-nc")) { + bg++; + } + + if (bg) { + //snprintf(path, sizeof(path), "%s%c%s", SWITCH_GLOBAL_dirs.log_dir, sep, lfile); + ppath = lfile; + } + + + if (switch_core_init(ppath) != SWITCH_STATUS_SUCCESS) { + fprintf(stderr, "Cannot Initilize\n"); + return 255; + } + + + /* set signal handlers */ (void) signal(SIGINT, (void *) handle_SIGINT); #ifdef SIGPIPE @@ -111,9 +124,7 @@ int main(int argc, char *argv[]) #endif - if (argv[1] && !strcmp(argv[1], "-nc")) { - bg++; - } + if (bg) { (void) signal(SIGHUP, (void *) handle_SIGHUP); @@ -129,7 +140,7 @@ int main(int argc, char *argv[]) #endif } - snprintf(path, sizeof(path), "%s%c%s", SWITCH_GLOBAL_dirs.conf_dir, sep, pfile); + snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile); if ((f = fopen(path, "w")) == 0) { fprintf(stderr, "Cannot open pid file %s.\n", path); return 255; @@ -138,12 +149,6 @@ int main(int argc, char *argv[]) fprintf(f, "%d", getpid()); fclose(f); - if (bg) { - snprintf(path, sizeof(path), "%s%c%s", SWITCH_GLOBAL_dirs.conf_dir, sep, lfile); - if (switch_core_set_console(path) != SWITCH_STATUS_SUCCESS) { - err = "Cannot open log file\n"; - } - } if (!err) { @@ -165,7 +170,7 @@ int main(int argc, char *argv[]) } switch_console_printf(SWITCH_CHANNEL_CONSOLE, "freeswitch Version %s Started\n\n", SWITCH_VERSION_FULL); - snprintf(path, sizeof(path), "%s%c%s", SWITCH_GLOBAL_dirs.conf_dir, sep, pfile); + snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile); if (bg) { bg = 0; diff --git a/src/switch_core.c b/src/switch_core.c index f8dabd91cb..1246212e04 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -2180,15 +2180,13 @@ static void core_event_handler(switch_event *event) } } -SWITCH_DECLARE(switch_status) switch_core_init(char *console) +SWITCH_DECLARE(void) switch_core_set_globals(void) { #ifdef WIN32 #define BUFSIZE 50 char lpPathBuffer[BUFSIZE]; DWORD dwBufSize=BUFSIZE; #endif - memset(&runtime, 0, sizeof(runtime)); - SWITCH_GLOBAL_dirs.base_dir = SWITCH_PREFIX_DIR; SWITCH_GLOBAL_dirs.mod_dir = SWITCH_MOD_DIR; SWITCH_GLOBAL_dirs.conf_dir = SWITCH_CONF_DIR; @@ -2205,12 +2203,24 @@ SWITCH_DECLARE(switch_status) switch_core_init(char *console) SWITCH_GLOBAL_dirs.temp_dir = "/tmp/"; #endif #endif +} + +SWITCH_DECLARE(switch_status) switch_core_init(char *console) +{ + + memset(&runtime, 0, sizeof(runtime)); - + switch_core_set_globals(); + #ifdef EMBED_PERL PerlInterpreter *my_perl; #endif if(console) { + if (*console != '/') { + char path[265]; + snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, console); + console = path; + } switch_core_set_console(console); } else { runtime.console = stdout; diff --git a/src/switch_event.c b/src/switch_event.c index 689e2b0fe6..bf5098d920 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -162,6 +162,10 @@ static void *SWITCH_THREAD_FUNC switch_event_thread(switch_thread *thread, void switch_event_deliver(&out_event); } } + + if (THREAD_RUNNING < 0) { + THREAD_RUNNING--; + } switch_yield(1000); } THREAD_RUNNING = 0; @@ -230,10 +234,18 @@ SWITCH_DECLARE(switch_status) switch_event_reserve_subclass_detailed(char *owner SWITCH_DECLARE(switch_status) switch_event_shutdown(void) { - THREAD_RUNNING = -1; + int x = 0, last = 0; - while (THREAD_RUNNING) { - switch_yield(1000); + if (THREAD_RUNNING > 0) { + THREAD_RUNNING = -1; + + while (x < 100 && THREAD_RUNNING) { + switch_yield(1000); + if (THREAD_RUNNING == last) { + x++; + } + last = THREAD_RUNNING; + } } return SWITCH_STATUS_SUCCESS; }