FS-3692 --resolve

This commit is contained in:
Anthony Minessale
2011-11-15 11:28:05 -06:00
parent ebed43a4f6
commit 34338e5a56
3 changed files with 150 additions and 0 deletions

View File

@@ -27,6 +27,7 @@
* Michael Jerris <mike@jerris.com>
* Bret McDanel <bret AT 0xdecafbad dot com>
* Luke Dashjr <luke@openmethods.com> (OpenMethods, LLC)
* Chris Rienzo <chris@rienzo.net>
*
* switch_ivr_async.c -- IVR Library (async operations)
*
@@ -3143,7 +3144,94 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_bind_dtmf_meta_session(switch_core_se
return SWITCH_STATUS_SUCCESS;
}
typedef struct {
int done;
char *result;
} play_and_detect_speech_state_t;
static switch_status_t play_and_detect_input_callback(switch_core_session_t *session, void *input, switch_input_type_t input_type, void *data, unsigned int len)
{
play_and_detect_speech_state_t *state = (play_and_detect_speech_state_t *)data;
switch_event_t *event;
switch_channel_t *channel = switch_core_session_get_channel(session);
if (input_type == SWITCH_INPUT_TYPE_EVENT) {
event = (switch_event_t *)input;
if (event->event_id == SWITCH_EVENT_DETECTED_SPEECH && !state->done) {
const char *speech_type = switch_event_get_header(event, "Speech-Type");
if (!zstr(speech_type)) {
if (!strcasecmp(speech_type, "detected-speech")) {
const char *result;
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "(%s) DETECTED SPEECH\n", switch_channel_get_name(channel));
result = switch_event_get_body(event);
if (!zstr(result)) {
state->result = switch_core_session_strdup(session, result);
} else {
state->result = "";
}
state->done = 1;
return SWITCH_STATUS_BREAK;
} else if (!strcasecmp(speech_type, "begin-speaking")) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "(%s) START OF SPEECH\n", switch_channel_get_name(channel));
return SWITCH_STATUS_BREAK;
}
}
}
}
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_status_t status;
int recognizing = 0;
switch_input_args_t args = { 0 };
play_and_detect_speech_state_t state = { 0, "" };
switch_channel_t *channel = switch_core_session_get_channel(session);
if (result == NULL) {
goto done;
}
/* start speech detection */
if (switch_ivr_detect_speech(session, mod_name, grammar, grammar, NULL, NULL) != SWITCH_STATUS_SUCCESS) {
goto done;
}
recognizing = 1;
/* play the prompt, looking for detection result */
args.input_callback = play_and_detect_input_callback;
args.buf = &state;
args.buflen = sizeof(state);
status = switch_ivr_play_file(session, NULL, file, &args);
if (status != SWITCH_STATUS_BREAK && status != SWITCH_STATUS_SUCCESS) {
goto done;
}
/* wait for result if not done */
if (!state.done) {
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));
while (!state.done && switch_channel_ready(channel)) {
status = switch_ivr_sleep(session, 5000, SWITCH_FALSE, &args);
if (status != SWITCH_STATUS_BREAK && status != SWITCH_STATUS_SUCCESS) {
goto done;
}
}
}
recognizing = !state.done;
done:
if (recognizing) {
switch_ivr_pause_detect_speech(session);
}
*result = state.result;
if (!state.done) {
return SWITCH_STATUS_FALSE;
}
return SWITCH_STATUS_SUCCESS;
}
struct speech_thread_handle {
switch_core_session_t *session;