From 4f06f7b8fda32669ee6a062e546e09a27b8216bf Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 1 Mar 2006 05:22:43 +0000 Subject: [PATCH] let js teletone obj have dtmf callback git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@707 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- .../mod_spidermonkey/mod_spidermonkey.c | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c b/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c index 6a7049c3de..eb4e1bae4e 100644 --- a/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c +++ b/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c @@ -79,6 +79,9 @@ static JSClass global_class = { JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub }; +typedef enum { + TTF_DTMF = (1 << 0) +} teletone_flag_t; struct dtmf_callback_state { struct js_session *session_state; @@ -107,6 +110,9 @@ struct teletone_obj { switch_memory_pool *pool; switch_timer *timer; switch_timer timer_base; + char code_buffer[1024]; + char ret_val[1024]; + unsigned int flags; }; @@ -859,6 +865,17 @@ static JSBool teletone_add_tone(JSContext *cx, JSObject *obj, uintN argc, jsval return JS_FALSE; } +static JSBool teletone_on_dtmf(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + struct teletone_obj *tto = JS_GetPrivate(cx, obj); + if (argc > 0) { + char *func = JS_GetStringBytes(JS_ValueToString(cx, argv[0])); + switch_copy_string(tto->code_buffer, func, sizeof(tto->code_buffer)); + switch_set_flag(tto, TTF_DTMF); + } + return JS_TRUE; +} + static JSBool teletone_generate(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { struct teletone_obj *tto = JS_GetPrivate(cx, obj); @@ -871,6 +888,7 @@ static JSBool teletone_generate(JSContext *cx, JSObject *obj, uintN argc, jsval switch_frame *read_frame; int stream_id; switch_core_thread_session thread_session; + switch_channel *channel; tto->ts.debug = 1; @@ -882,6 +900,8 @@ static JSBool teletone_generate(JSContext *cx, JSObject *obj, uintN argc, jsval session = tto->session; write_frame.codec = &tto->codec; write_frame.data = fdata; + + channel = switch_core_session_get_channel(session); if (tto->timer) { for (stream_id = 0; stream_id < switch_core_session_get_stream_count(session); stream_id++) { @@ -890,6 +910,24 @@ static JSBool teletone_generate(JSContext *cx, JSObject *obj, uintN argc, jsval } for(;;) { + if (switch_test_flag(tto, TTF_DTMF)) { + char dtmf[128]; + char code[512]; + char *ret; + jsval tt_rval; + if (switch_channel_has_dtmf(channel)) { + switch_channel_dequeue_dtmf(channel, dtmf, sizeof(dtmf)); + snprintf(code, sizeof(code), "~%s(\"%s\")", tto->code_buffer, dtmf); + eval_some_js(code, cx, obj, &tt_rval); + ret = JS_GetStringBytes(JS_ValueToString(cx, tt_rval)); + if (strcmp(ret, "true") && strcmp(ret, "undefined")) { + switch_copy_string(tto->ret_val, ret, sizeof(tto->ret_val)); + *rval = STRING_TO_JSVAL (JS_NewStringCopyZ(cx, tto->ret_val)); + return JS_TRUE; + } + } + } + if (tto->timer) { int x; @@ -930,6 +968,7 @@ enum teletone_tinyid { static JSFunctionSpec teletone_methods[] = { {"generate", teletone_generate, 1}, + {"onDTMF", teletone_on_dtmf, 1}, {"addTone", teletone_add_tone, 10}, {0} };