From df749a420fac1571e2c769a5b22efeba6a2e2b5f Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Mon, 7 Jan 2008 16:49:46 +0000 Subject: [PATCH] fix dtmf code git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@7125 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_ivr.h | 10 +++- .../mod_conference/mod_conference.c | 2 +- src/mod/applications/mod_rss/mod_rss.c | 2 +- .../mod_voicemail/mod_voicemail.c | 2 +- .../mod_spidermonkey/mod_spidermonkey.c | 13 ++++- src/switch_cpp.cpp | 2 +- src/switch_ivr.c | 49 ++++++++++++++++--- src/switch_ivr_menu.c | 2 +- src/switch_ivr_originate.c | 2 +- src/switch_ivr_play_say.c | 2 +- 10 files changed, 67 insertions(+), 19 deletions(-) diff --git a/src/include/switch_ivr.h b/src/include/switch_ivr.h index 748dc50c10..7eb3a4c40d 100644 --- a/src/include/switch_ivr.h +++ b/src/include/switch_ivr.h @@ -134,13 +134,19 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_collect_digits_callback(switch_core_s \param maxdigits max number of digits to read \param terminators digits to end the collection \param terminator actual digit that caused the collection to end (if any) - \param timeout timeout in ms + \param first_timeout timeout in ms + \param digit_timeout digit timeout in ms + \param abs_timeout abs timeout in ms \return SWITCH_STATUS_SUCCESS to keep the collection moving. */ SWITCH_DECLARE(switch_status_t) switch_ivr_collect_digits_count(switch_core_session_t *session, char *buf, switch_size_t buflen, - switch_size_t maxdigits, const char *terminators, char *terminator, uint32_t timeout); + switch_size_t maxdigits, + const char *terminators, char *terminator, + uint32_t first_timeout, + uint32_t digit_timeout, + uint32_t abs_timeout); /*! \brief Engage background Speech detection on a session diff --git a/src/mod/applications/mod_conference/mod_conference.c b/src/mod/applications/mod_conference/mod_conference.c index e9a7351caf..b0f9469968 100644 --- a/src/mod/applications/mod_conference/mod_conference.c +++ b/src/mod/applications/mod_conference/mod_conference.c @@ -4125,7 +4125,7 @@ SWITCH_STANDARD_APP(conference_function) status = switch_ivr_collect_digits_count(session, buf, sizeof(pin_buf) - strlen(pin_buf), - strlen(conference->pin) - strlen(pin_buf), "#", &term, 10000); + strlen(conference->pin) - strlen(pin_buf), "#", &term, 10000, 0, 0); } pin_valid = (status == SWITCH_STATUS_SUCCESS && strcmp(pin_buf, conference->pin) == 0); diff --git a/src/mod/applications/mod_rss/mod_rss.c b/src/mod/applications/mod_rss/mod_rss.c index 3d97f78e2c..93b46d401d 100644 --- a/src/mod/applications/mod_rss/mod_rss.c +++ b/src/mod/applications/mod_rss/mod_rss.c @@ -367,7 +367,7 @@ SWITCH_STANDARD_APP(rss_function) switch_size_t blen = sizeof(cmd) - strlen(cmd); cp = cmd + blen; - switch_ivr_collect_digits_count(session, cp, blen, blen, "#", &term, 5000); + switch_ivr_collect_digits_count(session, cp, blen, blen, "#", &term, 5000, 0, 0); } x = atoi(cmd) - 1; diff --git a/src/mod/applications/mod_voicemail/mod_voicemail.c b/src/mod/applications/mod_voicemail/mod_voicemail.c index ec3ade8e49..d7d6c170fc 100644 --- a/src/mod/applications/mod_voicemail/mod_voicemail.c +++ b/src/mod/applications/mod_voicemail/mod_voicemail.c @@ -826,7 +826,7 @@ static switch_status_t vm_macro_get(switch_core_session_t *session, maxlen = buflen -1; } if (bslen < maxlen) { - status = switch_ivr_collect_digits_count(session, buf + bslen, buflen, maxlen - bslen, term_chars, terminator_key, timeout); + status = switch_ivr_collect_digits_count(session, buf + bslen, buflen, maxlen - bslen, term_chars, terminator_key, timeout, 0, 0); } return status; diff --git a/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c b/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c index f102c336a0..aa39da22e7 100644 --- a/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c +++ b/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c @@ -1928,7 +1928,7 @@ static JSBool session_get_digits(JSContext * cx, JSObject * obj, uintN argc, jsv struct js_session *jss = JS_GetPrivate(cx, obj); char *terminators = NULL; char buf[513] = { 0 }; - int32 digits = 0, timeout = 5000; + int32 digits = 0, timeout = 5000, digit_timeout = 0, abs_timeout = 0; switch_channel_t *channel; METHOD_SANITY_CHECK(); @@ -1950,12 +1950,21 @@ static JSBool session_get_digits(JSContext * cx, JSObject * obj, uintN argc, jsv if (argc > 1) { terminators = JS_GetStringBytes(JS_ValueToString(cx, argv[1])); } + if (argc > 2) { JS_ValueToInt32(cx, argv[2], &timeout); } + if (argc > 3) { + JS_ValueToInt32(cx, argv[3], &digit_timeout); + } - switch_ivr_collect_digits_count(jss->session, buf, sizeof(buf), digits, terminators, &term, timeout); + if (argc > 4) { + JS_ValueToInt32(cx, argv[4], &abs_timeout); + } + + + switch_ivr_collect_digits_count(jss->session, buf, sizeof(buf), digits, terminators, &term, timeout, digit_timeout, abs_timeout); *rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, buf)); return JS_TRUE; } diff --git a/src/switch_cpp.cpp b/src/switch_cpp.cpp index a4c2d29fbb..13626237af 100644 --- a/src/switch_cpp.cpp +++ b/src/switch_cpp.cpp @@ -216,7 +216,7 @@ int CoreSession::getDigits(char *dtmf_buf, maxdigits, terminators, terminator, - (uint32_t) timeout); + (uint32_t) timeout, 0, 0); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "getDigits dtmf_buf: %s\n", dtmf_buf); end_allow_threads(); diff --git a/src/switch_ivr.c b/src/switch_ivr.c index 47b3c72fdd..8a1ac74d75 100644 --- a/src/switch_ivr.c +++ b/src/switch_ivr.c @@ -599,13 +599,18 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_collect_digits_callback(switch_core_s SWITCH_DECLARE(switch_status_t) switch_ivr_collect_digits_count(switch_core_session_t *session, char *buf, switch_size_t buflen, - switch_size_t maxdigits, const char *terminators, char *terminator, uint32_t timeout) + switch_size_t maxdigits, + const char *terminators, char *terminator, + uint32_t first_timeout, + uint32_t digit_timeout, + uint32_t abs_timeout) { switch_size_t i = 0, x = strlen(buf); switch_channel_t *channel; switch_status_t status = SWITCH_STATUS_FALSE; - switch_time_t started = 0; - uint32_t elapsed; + switch_time_t started = 0, digit_started = 0; + uint32_t abs_elapsed = 0, digit_elapsed = 0; + uint32_t eff_timeout = 0; channel = switch_core_session_get_channel(session); switch_assert(channel != NULL); @@ -622,16 +627,31 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_collect_digits_count(switch_core_sess } } - if (timeout) { - started = switch_time_now(); + if (abs_timeout) { + started = switch_timestamp_now(); } + if (digit_timeout && first_timeout) { + eff_timeout = first_timeout; + } else if (digit_timeout && !first_timeout) { + first_timeout = eff_timeout = digit_timeout; + } else if (first_timeout) { + digit_timeout = eff_timeout = first_timeout; + } + + + if (eff_timeout) { + digit_started = switch_timestamp_now(); + } + + + while (switch_channel_ready(channel)) { switch_frame_t *read_frame; - if (timeout) { - elapsed = (uint32_t) ((switch_time_now() - started) / 1000); - if (elapsed >= timeout) { + if (abs_timeout) { + abs_elapsed = (uint32_t) ((switch_timestamp_now() - started) / 1000); + if (abs_elapsed >= abs_timeout) { break; } } @@ -640,10 +660,23 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_collect_digits_count(switch_core_sess switch_ivr_parse_all_events(session); } + + if (eff_timeout) { + digit_elapsed = (uint32_t) ((switch_timestamp_now() - digit_started) / 1000); + if (digit_elapsed >= eff_timeout) { + break; + } + } + if (switch_channel_has_dtmf(channel)) { switch_dtmf_t dtmf = {0}; switch_size_t y; + if (eff_timeout) { + eff_timeout = digit_timeout; + digit_started = switch_timestamp_now(); + } + for (y = 0; y < maxdigits; y++) { if (switch_channel_dequeue_dtmf(channel, &dtmf) != SWITCH_STATUS_SUCCESS) { break; diff --git a/src/switch_ivr_menu.c b/src/switch_ivr_menu.c index 60e33203c9..de9f8130c6 100644 --- a/src/switch_ivr_menu.c +++ b/src/switch_ivr_menu.c @@ -274,7 +274,7 @@ static switch_status_t play_or_say(switch_core_session_t *session, switch_ivr_me menu->ptr += strlen(menu->buf); if (strlen(menu->buf) < need) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "waiting for %u digits\n", (uint32_t)need); - status = switch_ivr_collect_digits_count(session, menu->ptr, menu->inlen - strlen(menu->buf), need, "#", &terminator, menu->timeout); + status = switch_ivr_collect_digits_count(session, menu->ptr, menu->inlen - strlen(menu->buf), need, "#", &terminator, menu->timeout, 0, 0); } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "digits '%s'\n", menu->buf); } diff --git a/src/switch_ivr_originate.c b/src/switch_ivr_originate.c index 2c1d8f8f77..9b1bf72d7b 100644 --- a/src/switch_ivr_originate.c +++ b/src/switch_ivr_originate.c @@ -129,7 +129,7 @@ static void *SWITCH_THREAD_FUNC collect_thread_run(switch_thread_t * thread, voi args.buflen = sizeof(buf); switch_ivr_play_file(collect->session, NULL, collect->file, &args); } else { - switch_ivr_collect_digits_count(collect->session, buf, sizeof(buf), 1, SWITCH_BLANK_STRING, &term, 0); + switch_ivr_collect_digits_count(collect->session, buf, sizeof(buf), 1, SWITCH_BLANK_STRING, &term, 0, 0, 0); } for (p = buf; *p; p++) { diff --git a/src/switch_ivr_play_say.c b/src/switch_ivr_play_say.c index aecd1bcc32..bea3036f87 100644 --- a/src/switch_ivr_play_say.c +++ b/src/switch_ivr_play_say.c @@ -1179,7 +1179,7 @@ SWITCH_DECLARE(switch_status_t) switch_play_and_get_digits(switch_core_session_t switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Calling more digits try %d\n", max_tries); //Try to grab some more digits for the timeout period - status = switch_ivr_collect_digits_count(session, digit_buffer, digit_buffer_length, max_digits, valid_terminators, &terminator, timeout); + status = switch_ivr_collect_digits_count(session, digit_buffer, digit_buffer_length, max_digits, valid_terminators, &terminator, timeout, 0, 0); //Make sure we made it out alive if (status != SWITCH_STATUS_SUCCESS) {