add support for configurable timeout and passing of args to play_and_detect_speech

This commit is contained in:
Anthony Minessale 2012-01-05 10:38:08 -06:00
parent e185ff0075
commit 410e523c24
3 changed files with 45 additions and 15 deletions

View File

@ -162,13 +162,18 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_collect_digits_count(switch_core_sess
\param mod_name the module name of the ASR library \param mod_name the module name of the ASR library
\param grammar the grammar text, URI, or local file name \param grammar the grammar text, URI, or local file name
\param result of speech recognition, allocated from the session pool \param result of speech recognition, allocated from the session pool
\param input_timeout time to wait for input
\param args arguements to pass for callbacks etc
\return SWITCH_STATUS_SUCCESS if all is well \return SWITCH_STATUS_SUCCESS if all is well
*/ */
SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_detect_speech(switch_core_session_t *session, SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_detect_speech(switch_core_session_t *session,
const char *file, const char *file,
const char *mod_name, const char *mod_name,
const char *grammar, const char *grammar,
char **result); char **result,
uint32_t input_timeout,
switch_input_args_t *args);
/*! /*!
\brief Engage background Speech detection on a session \brief Engage background Speech detection on a session

View File

@ -495,7 +495,7 @@ SWITCH_STANDARD_APP(play_and_detect_speech_function)
char *engine = argv[0]; char *engine = argv[0];
char *grammar = argv[1]; char *grammar = argv[1];
char *result = NULL; char *result = NULL;
switch_ivr_play_and_detect_speech(session, file, engine, grammar, &result); switch_ivr_play_and_detect_speech(session, file, engine, grammar, &result, 0, NULL);
switch_channel_set_variable(channel, "detect_speech_result", result); switch_channel_set_variable(channel, "detect_speech_result", result);
} else { } else {
/* bad input */ /* bad input */

View File

@ -3220,11 +3220,17 @@ static switch_status_t play_and_detect_input_callback(switch_core_session_t *ses
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_detect_speech(switch_core_session_t *session, const char *file, const char *mod_name, const char *grammar, char **result) SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_detect_speech(switch_core_session_t *session,
const char *file,
const char *mod_name,
const char *grammar,
char **result,
uint32_t input_timeout,
switch_input_args_t *args)
{ {
switch_status_t status; switch_status_t status = SWITCH_STATUS_SUCCESS;
int recognizing = 0; int recognizing = 0;
switch_input_args_t args = { 0 }; switch_input_args_t myargs = { 0 };
play_and_detect_speech_state_t state = { 0, "" }; play_and_detect_speech_state_t state = { 0, "" };
switch_channel_t *channel = switch_core_session_get_channel(session); switch_channel_t *channel = switch_core_session_get_channel(session);
@ -3232,6 +3238,12 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_detect_speech(switch_core_se
goto done; goto done;
} }
if (!input_timeout) input_timeout = 5000;
if (!args) {
args = &myargs;
}
/* start speech detection */ /* start speech detection */
if (switch_ivr_detect_speech(session, mod_name, grammar, grammar, NULL, NULL) != SWITCH_STATUS_SUCCESS) { if (switch_ivr_detect_speech(session, mod_name, grammar, grammar, NULL, NULL) != SWITCH_STATUS_SUCCESS) {
goto done; goto done;
@ -3239,10 +3251,16 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_detect_speech(switch_core_se
recognizing = 1; recognizing = 1;
/* play the prompt, looking for detection result */ /* play the prompt, looking for detection result */
args.input_callback = play_and_detect_input_callback; args->input_callback = play_and_detect_input_callback;
args.buf = &state; args->buf = &state;
args.buflen = sizeof(state); args->buflen = sizeof(state);
status = switch_ivr_play_file(session, NULL, file, &args); status = switch_ivr_play_file(session, NULL, file, args);
if (args->dmachine && (switch_ivr_dmachine_get_match(args->dmachine) || switch_ivr_dmachine_get_failed_digits(args->dmachine))) {
state.done = 1;
goto done;
}
if (status != SWITCH_STATUS_BREAK && status != SWITCH_STATUS_SUCCESS) { if (status != SWITCH_STATUS_BREAK && status != SWITCH_STATUS_SUCCESS) {
goto done; goto done;
} }
@ -3252,7 +3270,13 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_detect_speech(switch_core_se
switch_ivr_detect_speech_start_input_timers(session); switch_ivr_detect_speech_start_input_timers(session);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "(%s) WAITING FOR RESULT\n", switch_channel_get_name(channel)); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "(%s) WAITING FOR RESULT\n", switch_channel_get_name(channel));
while (!state.done && switch_channel_ready(channel)) { while (!state.done && switch_channel_ready(channel)) {
status = switch_ivr_sleep(session, 5000, SWITCH_FALSE, &args); status = switch_ivr_sleep(session, input_timeout, SWITCH_FALSE, args);
if (args->dmachine && (switch_ivr_dmachine_get_match(args->dmachine) || switch_ivr_dmachine_get_failed_digits(args->dmachine))) {
state.done = 1;
goto done;
}
if (status != SWITCH_STATUS_BREAK && status != SWITCH_STATUS_SUCCESS) { if (status != SWITCH_STATUS_BREAK && status != SWITCH_STATUS_SUCCESS) {
goto done; goto done;
} }
@ -3268,9 +3292,10 @@ done:
*result = state.result; *result = state.result;
if (!state.done) { if (!state.done) {
return SWITCH_STATUS_FALSE; status = SWITCH_STATUS_FALSE;
} }
return SWITCH_STATUS_SUCCESS;
return status;;
} }
struct speech_thread_handle { struct speech_thread_handle {