From 75bdc43d82340588c09786a1204be7e2176581d3 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Fri, 19 Jul 2013 14:05:03 -0500 Subject: [PATCH] FS-5573 --resolve --- src/include/switch_ivr.h | 12 ++++ .../applications/mod_dptools/mod_dptools.c | 2 + src/switch_ivr_async.c | 71 ++++++++++++------- 3 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/include/switch_ivr.h b/src/include/switch_ivr.h index 7a8d9e6603..74f9a8e738 100644 --- a/src/include/switch_ivr.h +++ b/src/include/switch_ivr.h @@ -184,6 +184,18 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_detect_speech(switch_core_se switch_input_args_t *args); +/*! + \brief Initialize background Speech detection on a session, so that parameters can be set, and grammars loaded. + After calling this function, it is possible to call switch_ivr_set_param_detect_speech() to set recognition parameters. + Calling switch_ivr_detect_speech_load_grammar() starts the speech recognition. + \param session the session to attach + \param mod_name the module name of the ASR library + \param dest the destination address + \param ah an ASR handle to use (NULL to create one) + \return SWITCH_STATUS_SUCCESS if all is well +*/ +SWITCH_DECLARE(switch_status_t) switch_ivr_detect_speech_init(switch_core_session_t *session, const char *mod_name, + const char *dest, switch_asr_handle_t *ah); /*! \brief Engage background Speech detection on a session \param session the session to attach diff --git a/src/mod/applications/mod_dptools/mod_dptools.c b/src/mod/applications/mod_dptools/mod_dptools.c index 8f1057a137..0b07a72438 100755 --- a/src/mod/applications/mod_dptools/mod_dptools.c +++ b/src/mod/applications/mod_dptools/mod_dptools.c @@ -445,6 +445,8 @@ SWITCH_STANDARD_APP(detect_speech_function) switch_ivr_detect_speech_disable_grammar(session, argv[1]); } else if (!strcasecmp(argv[0], "grammarsalloff")) { switch_ivr_detect_speech_disable_all_grammars(session); + } else if (!strcasecmp(argv[0], "init")) { + switch_ivr_detect_speech_init(session, argv[1], argv[2], NULL); } else if (!strcasecmp(argv[0], "pause")) { switch_ivr_pause_detect_speech(session); } else if (!strcasecmp(argv[0], "resume")) { diff --git a/src/switch_ivr_async.c b/src/switch_ivr_async.c index b36e7dfa72..9f4ea8cf5b 100644 --- a/src/switch_ivr_async.c +++ b/src/switch_ivr_async.c @@ -4134,9 +4134,8 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_detect_speech_disable_all_grammars(sw return SWITCH_STATUS_FALSE; } -SWITCH_DECLARE(switch_status_t) switch_ivr_detect_speech(switch_core_session_t *session, - const char *mod_name, - const char *grammar, const char *name, const char *dest, switch_asr_handle_t *ah) +SWITCH_DECLARE(switch_status_t) switch_ivr_detect_speech_init(switch_core_session_t *session, const char *mod_name, + const char *dest, switch_asr_handle_t *ah) { switch_channel_t *channel = switch_core_session_get_channel(session); switch_status_t status; @@ -4146,9 +4145,10 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_detect_speech(switch_core_session_t * const char *p; char key[512] = ""; - switch_core_session_get_read_impl(session, &read_impl); - - switch_snprintf(key, sizeof(key), "%s/%s/%s/%s", mod_name, grammar, name, dest); + if (sth) { + /* Already initialized */ + return SWITCH_STATUS_SUCCESS; + } if (!ah) { if (!(ah = switch_core_session_alloc(session, sizeof(*ah)))) { @@ -4156,32 +4156,13 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_detect_speech(switch_core_session_t * } } - if (sth) { - if (switch_core_asr_load_grammar(sth->ah, grammar, name) != SWITCH_STATUS_SUCCESS) { - switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Error loading Grammar\n"); - switch_ivr_stop_detect_speech(session); - return SWITCH_STATUS_FALSE; - } - - if ((p = switch_channel_get_variable(channel, "fire_asr_events")) && switch_true(p)) { - switch_set_flag(sth->ah, SWITCH_ASR_FLAG_FIRE_EVENTS); - } - - return SWITCH_STATUS_SUCCESS; - } + switch_core_session_get_read_impl(session, &read_impl); if ((status = switch_core_asr_open(ah, mod_name, "L16", read_impl.actual_samples_per_second, dest, &flags, - switch_core_session_get_pool(session))) == SWITCH_STATUS_SUCCESS) { - - if (switch_core_asr_load_grammar(ah, grammar, name) != SWITCH_STATUS_SUCCESS) { - switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Error loading Grammar\n"); - switch_core_asr_close(ah, &flags); - return SWITCH_STATUS_FALSE; - } - } else { + switch_core_session_get_pool(session))) != SWITCH_STATUS_SUCCESS) { return status; } @@ -4194,6 +4175,8 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_detect_speech(switch_core_session_t * switch_set_flag(ah, SWITCH_ASR_FLAG_FIRE_EVENTS); } + switch_snprintf(key, sizeof(key), "%s/%s/%s/%s", mod_name, NULL, NULL, dest); + if ((status = switch_core_media_bug_add(session, "detect_speech", key, speech_callback, sth, 0, SMBF_READ_STREAM | SMBF_NO_PAUSE, &sth->bug)) != SWITCH_STATUS_SUCCESS) { switch_core_asr_close(ah, &flags); @@ -4210,6 +4193,40 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_detect_speech(switch_core_session_t * return SWITCH_STATUS_SUCCESS; } +SWITCH_DECLARE(switch_status_t) switch_ivr_detect_speech(switch_core_session_t *session, + const char *mod_name, + const char *grammar, const char *name, const char *dest, switch_asr_handle_t *ah) +{ + switch_channel_t *channel = switch_core_session_get_channel(session); + switch_status_t status; + struct speech_thread_handle *sth = switch_channel_get_private(channel, SWITCH_SPEECH_KEY); + const char *p; + + if (!sth) { + /* No speech thread handle available yet, init speech detection first. */ + if ((status = switch_ivr_detect_speech_init(session, mod_name, dest, ah)) != SWITCH_STATUS_SUCCESS) { + return status; + } + + /* Fetch the new speech thread handle */ + if (!(sth = switch_channel_get_private(channel, SWITCH_SPEECH_KEY))) { + return SWITCH_STATUS_FALSE; + } + } + + if (switch_core_asr_load_grammar(sth->ah, grammar, name) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Error loading Grammar\n"); + switch_ivr_stop_detect_speech(session); + return SWITCH_STATUS_FALSE; + } + + if ((p = switch_channel_get_variable(channel, "fire_asr_events")) && switch_true(p)) { + switch_set_flag(sth->ah, SWITCH_ASR_FLAG_FIRE_EVENTS); + } + + return SWITCH_STATUS_SUCCESS; +} + struct hangup_helper { char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; switch_bool_t bleg;