2007-03-29 22:34:40 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
2009-02-04 21:20:54 +00:00
|
|
|
* Copyright (C) 2005/2006, Anthony Minessale II <anthm@freeswitch.org>
|
2007-03-29 22:34:40 +00:00
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2007-03-29 22:34:40 +00:00
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2007-03-29 22:34:40 +00:00
|
|
|
* Paul D. Tinsley <pdt at jackhammer.org>
|
|
|
|
* Neal Horman <neal at wanlink dot com>
|
|
|
|
* Matt Klein <mklein@nmedia.net>
|
|
|
|
* Michael Jerris <mike@jerris.com>
|
|
|
|
*
|
|
|
|
* switch_ivr_play_say.c -- IVR Library (functions to play or say audio)
|
|
|
|
*
|
|
|
|
*/
|
2008-01-27 17:42:51 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
#include <switch.h>
|
|
|
|
|
|
|
|
static char *SAY_METHOD_NAMES[] = {
|
|
|
|
"N/A",
|
|
|
|
"PRONOUNCED",
|
|
|
|
"ITERATED",
|
|
|
|
"COUNTED",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static char *SAY_TYPE_NAMES[] = {
|
|
|
|
"NUMBER",
|
|
|
|
"ITEMS",
|
|
|
|
"PERSONS",
|
|
|
|
"MESSAGES",
|
|
|
|
"CURRENCY",
|
|
|
|
"TIME_MEASUREMENT",
|
|
|
|
"CURRENT_DATE",
|
|
|
|
"CURRENT_TIME",
|
|
|
|
"CURRENT_DATE_TIME",
|
|
|
|
"TELEPHONE_NUMBER",
|
|
|
|
"TELEPHONE_EXTENSION",
|
|
|
|
"URL",
|
|
|
|
"IP_ADDRESS",
|
|
|
|
"EMAIL_ADDRESS",
|
|
|
|
"POSTAL_ADDRESS",
|
|
|
|
"ACCOUNT_NUMBER",
|
|
|
|
"NAME_SPELLED",
|
|
|
|
"NAME_PHONETIC",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2008-05-09 22:16:08 +00:00
|
|
|
SWITCH_DECLARE(switch_say_method_t) switch_ivr_get_say_method_by_name(const char *name)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
|
|
|
int x = 0;
|
|
|
|
for (x = 0; SAY_METHOD_NAMES[x]; x++) {
|
|
|
|
if (!strcasecmp(SAY_METHOD_NAMES[x], name)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (switch_say_method_t) x;
|
|
|
|
}
|
|
|
|
|
2008-05-09 22:16:08 +00:00
|
|
|
SWITCH_DECLARE(switch_say_type_t) switch_ivr_get_say_type_by_name(const char *name)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
|
|
|
int x = 0;
|
|
|
|
for (x = 0; SAY_TYPE_NAMES[x]; x++) {
|
|
|
|
if (!strcasecmp(SAY_TYPE_NAMES[x], name)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-08 16:11:03 +00:00
|
|
|
return (switch_say_type_t) x;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2007-11-01 11:28:26 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_ivr_phrase_macro(switch_core_session_t *session, const char *macro_name, const char *data, const char *lang,
|
2007-03-30 00:15:25 +00:00
|
|
|
switch_input_args_t *args)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
2008-01-23 20:59:25 +00:00
|
|
|
switch_event_t *hint_data;
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_xml_t cfg, xml = NULL, language, macros, macro, input, action;
|
2008-01-23 20:59:25 +00:00
|
|
|
char *lname = NULL, *mname = NULL;
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_status_t status = SWITCH_STATUS_GENERR;
|
2007-11-01 11:28:26 +00:00
|
|
|
const char *old_sound_prefix = NULL, *sound_path = NULL, *tts_engine = NULL, *tts_voice = NULL;
|
|
|
|
const char *module_name = NULL, *chan_lang = NULL;
|
2008-01-28 07:26:10 +00:00
|
|
|
switch_channel_t *channel = switch_core_session_get_channel(session);
|
2007-03-29 22:34:40 +00:00
|
|
|
uint8_t done = 0;
|
2007-11-07 23:37:22 +00:00
|
|
|
int matches = 0;
|
2008-09-09 18:24:13 +00:00
|
|
|
const char *pause_val;
|
|
|
|
int pause = 100;
|
2007-03-29 22:34:40 +00:00
|
|
|
|
|
|
|
if (!macro_name) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No phrase macro specified.\n");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lang) {
|
|
|
|
chan_lang = switch_channel_get_variable(channel, "default_language");
|
|
|
|
if (!chan_lang) {
|
|
|
|
chan_lang = "en";
|
|
|
|
}
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "No language specified - Using [%s]\n", chan_lang);
|
|
|
|
} else {
|
|
|
|
chan_lang = lang;
|
|
|
|
}
|
|
|
|
|
2007-10-17 14:42:41 +00:00
|
|
|
module_name = chan_lang;
|
|
|
|
|
2008-10-02 17:10:05 +00:00
|
|
|
switch_event_create(&hint_data, SWITCH_EVENT_REQUEST_PARAMS);
|
2008-01-23 20:59:25 +00:00
|
|
|
switch_assert(hint_data);
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2008-01-23 20:59:25 +00:00
|
|
|
switch_event_add_header_string(hint_data, SWITCH_STACK_BOTTOM, "macro_name", macro_name);
|
|
|
|
switch_event_add_header_string(hint_data, SWITCH_STACK_BOTTOM, "lang", chan_lang);
|
|
|
|
if (data) {
|
|
|
|
switch_event_add_header_string(hint_data, SWITCH_STACK_BOTTOM, "data", data);
|
2008-01-28 21:07:20 +00:00
|
|
|
} else {
|
2008-05-27 04:30:03 +00:00
|
|
|
data = "";
|
|
|
|
}
|
2008-01-23 20:59:25 +00:00
|
|
|
switch_channel_event_set_data(channel, hint_data);
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2009-02-12 00:27:07 +00:00
|
|
|
if (switch_xml_locate("phrases", NULL, NULL, NULL, &xml, &cfg, hint_data, SWITCH_TRUE) != SWITCH_STATUS_SUCCESS) {
|
2008-10-11 05:44:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of phrases failed.\n");
|
2007-03-29 22:34:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(macros = switch_xml_child(cfg, "macros"))) {
|
2008-10-11 05:44:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't find macros tag.\n");
|
2007-03-29 22:34:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(language = switch_xml_child(macros, "language"))) {
|
2008-10-11 05:44:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't find language tag.\n");
|
2007-03-29 22:34:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (language) {
|
|
|
|
if ((lname = (char *) switch_xml_attr(language, "name")) && !strcasecmp(lname, chan_lang)) {
|
2007-10-17 14:42:41 +00:00
|
|
|
const char *tmp;
|
|
|
|
|
|
|
|
if ((tmp = switch_xml_attr(language, "module"))) {
|
|
|
|
module_name = tmp;
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
language = language->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!language) {
|
2008-10-11 05:44:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't find language %s.\n", chan_lang);
|
2007-03-29 22:34:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2007-10-15 18:02:21 +00:00
|
|
|
if (!(sound_path = (char *) switch_xml_attr(language, "sound-path"))) {
|
|
|
|
sound_path = (char *) switch_xml_attr(language, "sound_path");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(tts_engine = (char *) switch_xml_attr(language, "tts-engine"))) {
|
2007-11-07 18:50:34 +00:00
|
|
|
tts_engine = (char *) switch_xml_attr(language, "tts_engine");
|
2007-10-15 18:02:21 +00:00
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-10-15 18:02:21 +00:00
|
|
|
if (!(tts_voice = (char *) switch_xml_attr(language, "tts-voice"))) {
|
2007-11-07 18:50:34 +00:00
|
|
|
tts_voice = (char *) switch_xml_attr(language, "tts_voice");
|
2007-10-15 18:02:21 +00:00
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2007-10-12 22:08:30 +00:00
|
|
|
if (sound_path) {
|
2008-04-14 17:24:52 +00:00
|
|
|
char *p;
|
|
|
|
old_sound_prefix = switch_str_nil(switch_channel_get_variable(channel, "sound_prefix"));
|
|
|
|
p = switch_core_session_strdup(session, old_sound_prefix);
|
|
|
|
old_sound_prefix = p;
|
2007-10-12 22:08:30 +00:00
|
|
|
switch_channel_set_variable(channel, "sound_prefix", sound_path);
|
|
|
|
}
|
2008-09-09 18:24:13 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if (!(macro = switch_xml_child(language, "macro"))) {
|
2008-10-11 05:44:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't find any macro tags.\n");
|
2007-03-29 22:34:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (macro) {
|
|
|
|
if ((mname = (char *) switch_xml_attr(macro, "name")) && !strcasecmp(mname, macro_name)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
macro = macro->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!macro) {
|
2008-10-11 05:44:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't find macro %s.\n", macro_name);
|
2007-03-29 22:34:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2008-09-09 18:24:13 +00:00
|
|
|
if ((pause_val = switch_xml_attr(macro, "pause"))) {
|
|
|
|
int tmp = atoi(pause_val);
|
|
|
|
if (tmp >= 0) {
|
|
|
|
pause = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if (!(input = switch_xml_child(macro, "input"))) {
|
2008-10-11 05:44:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't find any input tags.\n");
|
2007-03-29 22:34:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
if (switch_channel_pre_answer(channel) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
|
|
|
|
while (input && !done) {
|
|
|
|
char *pattern = (char *) switch_xml_attr(input, "pattern");
|
2008-05-16 18:40:39 +00:00
|
|
|
const char *do_break = switch_xml_attr_soft(input, "break_on_match");
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if (pattern) {
|
|
|
|
switch_regex_t *re = NULL;
|
|
|
|
int proceed = 0, ovector[30];
|
|
|
|
char *substituted = NULL;
|
|
|
|
uint32_t len = 0;
|
|
|
|
char *odata = NULL;
|
|
|
|
char *expanded = NULL;
|
|
|
|
switch_xml_t match = NULL;
|
|
|
|
|
2007-11-07 23:37:22 +00:00
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if ((proceed = switch_regex_perform(data, pattern, &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {
|
|
|
|
match = switch_xml_child(input, "match");
|
|
|
|
} else {
|
|
|
|
match = switch_xml_child(input, "nomatch");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match) {
|
2007-11-07 23:37:22 +00:00
|
|
|
matches++;
|
2007-03-30 00:13:31 +00:00
|
|
|
for (action = switch_xml_child(match, "action"); action && status == SWITCH_STATUS_SUCCESS; action = action->next) {
|
2007-03-29 22:34:40 +00:00
|
|
|
char *adata = (char *) switch_xml_attr_soft(action, "data");
|
|
|
|
char *func = (char *) switch_xml_attr_soft(action, "function");
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if (strchr(pattern, '(') && strchr(adata, '$')) {
|
|
|
|
len = (uint32_t) (strlen(data) + strlen(adata) + 10);
|
|
|
|
if (!(substituted = malloc(len))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n");
|
|
|
|
switch_regex_safe_free(re);
|
|
|
|
switch_safe_free(expanded);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
memset(substituted, 0, len);
|
|
|
|
switch_perform_substitution(re, proceed, adata, data, substituted, len, ovector);
|
|
|
|
odata = substituted;
|
|
|
|
} else {
|
|
|
|
odata = adata;
|
|
|
|
}
|
|
|
|
|
|
|
|
expanded = switch_channel_expand_variables(channel, odata);
|
|
|
|
|
|
|
|
if (expanded == odata) {
|
|
|
|
expanded = NULL;
|
|
|
|
} else {
|
|
|
|
odata = expanded;
|
|
|
|
}
|
2008-09-10 21:55:33 +00:00
|
|
|
|
2007-10-17 14:42:41 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Handle %s:[%s] (%s:%s)\n", func, odata, chan_lang, module_name);
|
2007-03-29 22:34:40 +00:00
|
|
|
|
|
|
|
if (!strcasecmp(func, "play-file")) {
|
|
|
|
status = switch_ivr_play_file(session, NULL, odata, args);
|
|
|
|
} else if (!strcasecmp(func, "break")) {
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
} else if (!strcasecmp(func, "execute")) {
|
2007-11-13 03:47:07 +00:00
|
|
|
switch_application_interface_t *app;
|
|
|
|
char *cmd, *cmd_args;
|
|
|
|
status = SWITCH_STATUS_FALSE;
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2007-11-13 03:47:07 +00:00
|
|
|
cmd = switch_core_session_strdup(session, odata);
|
2008-05-19 19:39:20 +00:00
|
|
|
|
|
|
|
if (!cmd) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Allocation error calling execute\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-11-13 03:47:07 +00:00
|
|
|
cmd_args = switch_separate_paren_args(cmd);
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
if (!cmd_args) {
|
2007-11-13 03:47:07 +00:00
|
|
|
cmd_args = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((app = switch_loadable_module_get_application_interface(cmd)) != NULL) {
|
|
|
|
status = switch_core_session_exec(session, app, cmd_args);
|
2008-11-12 19:28:05 +00:00
|
|
|
UNPROTECT_INTERFACE(app);
|
2007-11-13 03:47:07 +00:00
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Application %s\n", cmd);
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
} else if (!strcasecmp(func, "say")) {
|
|
|
|
switch_say_interface_t *si;
|
2007-10-17 14:42:41 +00:00
|
|
|
if ((si = switch_loadable_module_get_say_interface(module_name))) {
|
2007-03-29 22:34:40 +00:00
|
|
|
char *say_type = (char *) switch_xml_attr_soft(action, "type");
|
|
|
|
char *say_method = (char *) switch_xml_attr_soft(action, "method");
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
status =
|
|
|
|
si->say_function(session, odata, switch_ivr_get_say_type_by_name(say_type), switch_ivr_get_say_method_by_name(say_method),
|
|
|
|
args);
|
2007-03-29 22:34:40 +00:00
|
|
|
} else {
|
2007-10-17 14:42:41 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid SAY Interface [%s]!\n", module_name);
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
} else if (!strcasecmp(func, "speak-text")) {
|
2007-11-01 11:28:26 +00:00
|
|
|
const char *my_tts_engine = switch_xml_attr(action, "tts-engine");
|
|
|
|
const char *my_tts_voice = switch_xml_attr(action, "tts-voice");
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-10-15 18:02:21 +00:00
|
|
|
if (!my_tts_engine) {
|
|
|
|
my_tts_engine = tts_engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!my_tts_voice) {
|
|
|
|
my_tts_voice = tts_voice;
|
|
|
|
}
|
|
|
|
if (switch_strlen_zero(tts_engine) || switch_strlen_zero(tts_voice)) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "TTS is not configured\n");
|
|
|
|
} else {
|
|
|
|
status = switch_ivr_speak_text(session, my_tts_engine, my_tts_voice, odata, args);
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
2008-09-09 18:24:13 +00:00
|
|
|
|
2008-12-10 00:48:24 +00:00
|
|
|
switch_ivr_sleep(session, pause, SWITCH_FALSE, NULL);
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_regex_safe_free(re);
|
|
|
|
switch_safe_free(expanded);
|
|
|
|
switch_safe_free(substituted);
|
2008-05-16 18:40:39 +00:00
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2008-05-16 18:40:39 +00:00
|
|
|
if (match && do_break && switch_true(do_break)) {
|
|
|
|
break;
|
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status != SWITCH_STATUS_SUCCESS) {
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
input = input->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2008-01-23 20:59:25 +00:00
|
|
|
if (hint_data) {
|
|
|
|
switch_event_destroy(&hint_data);
|
|
|
|
}
|
|
|
|
|
2007-11-07 23:37:22 +00:00
|
|
|
if (!matches) {
|
2008-10-11 05:44:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Macro [%s] did not match any patterns\n", macro_name);
|
2007-11-07 23:37:22 +00:00
|
|
|
}
|
|
|
|
|
2007-10-12 22:08:30 +00:00
|
|
|
if (old_sound_prefix) {
|
|
|
|
switch_channel_set_variable(channel, "sound_prefix", old_sound_prefix);
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
if (xml) {
|
|
|
|
switch_xml_free(xml);
|
|
|
|
}
|
2007-11-07 23:37:22 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *session,
|
2007-12-29 23:15:57 +00:00
|
|
|
switch_file_handle_t *fh, const char *file, switch_input_args_t *args, uint32_t limit)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
2008-01-28 07:26:10 +00:00
|
|
|
switch_channel_t *channel = switch_core_session_get_channel(session);
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_dtmf_t dtmf = { 0 };
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_file_handle_t lfh = { 0 };
|
|
|
|
switch_frame_t *read_frame;
|
2009-02-10 19:09:06 +00:00
|
|
|
switch_codec_t codec;
|
2007-03-29 22:34:40 +00:00
|
|
|
char *codec_name;
|
|
|
|
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
2007-11-01 11:28:26 +00:00
|
|
|
const char *p;
|
2007-03-29 22:34:40 +00:00
|
|
|
const char *vval;
|
|
|
|
time_t start = 0;
|
|
|
|
uint32_t org_silence_hits = 0;
|
2009-01-10 03:52:28 +00:00
|
|
|
int asis = 0;
|
2009-02-10 19:09:06 +00:00
|
|
|
switch_codec_implementation_t read_impl = {0};
|
|
|
|
switch_core_session_get_read_impl(session, &read_impl);
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
if (!switch_channel_ready(channel)) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_channel_pre_answer(channel) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!switch_channel_media_ready(channel)) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2008-01-28 07:26:10 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if (!fh) {
|
|
|
|
fh = &lfh;
|
|
|
|
}
|
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
fh->channels = read_impl.number_of_channels;
|
|
|
|
fh->native_rate = read_impl.actual_samples_per_second;
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2009-01-10 03:52:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (!strstr(file, SWITCH_URL_SEPARATOR)) {
|
|
|
|
char *ext;
|
|
|
|
const char *prefix;
|
|
|
|
|
|
|
|
prefix = switch_channel_get_variable(channel, "sound_prefix");
|
|
|
|
if (!prefix) {
|
|
|
|
prefix = SWITCH_GLOBAL_dirs.base_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!switch_is_file_path(file)) {
|
|
|
|
file = switch_core_session_sprintf(session, "%s%s%s", prefix, SWITCH_PATH_SEPARATOR, file);
|
|
|
|
}
|
|
|
|
if ((ext = strrchr(file, '.'))) {
|
|
|
|
ext++;
|
|
|
|
} else {
|
2009-02-10 19:09:06 +00:00
|
|
|
ext = read_impl.iananame;
|
2009-01-10 03:52:28 +00:00
|
|
|
file = switch_core_session_sprintf(session, "%s.%s", file, ext);
|
|
|
|
asis = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-06 20:44:53 +00:00
|
|
|
vval = switch_channel_get_variable(channel, "enable_file_write_buffering");
|
2009-02-07 21:22:12 +00:00
|
|
|
if (!vval || switch_true(vval)) {
|
2009-02-06 20:44:53 +00:00
|
|
|
fh->pre_buffer_datalen = SWITCH_DEFAULT_FILE_BUFFER_LEN;
|
|
|
|
}
|
2009-01-10 03:52:28 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if (switch_core_file_open(fh,
|
|
|
|
file,
|
2007-11-26 23:41:00 +00:00
|
|
|
fh->channels,
|
2009-02-10 19:09:06 +00:00
|
|
|
read_impl.actual_samples_per_second,
|
2008-05-08 15:50:28 +00:00
|
|
|
SWITCH_FILE_FLAG_WRITE | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
|
2009-01-09 20:34:01 +00:00
|
|
|
switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
|
2007-03-29 22:34:40 +00:00
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
|
|
|
|
2009-01-10 16:04:34 +00:00
|
|
|
if (switch_test_flag(fh, SWITCH_FILE_NATIVE)) {
|
|
|
|
asis = 1;
|
|
|
|
}
|
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
if (switch_channel_pre_answer(channel) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
|
|
|
|
if ((p = switch_channel_get_variable(channel, "RECORD_TITLE"))) {
|
2007-11-01 11:28:26 +00:00
|
|
|
vval = switch_core_session_strdup(session, p);
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_core_file_set_string(fh, SWITCH_AUDIO_COL_STR_TITLE, vval);
|
|
|
|
switch_channel_set_variable(channel, "RECORD_TITLE", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((p = switch_channel_get_variable(channel, "RECORD_COPYRIGHT"))) {
|
2007-11-01 11:28:26 +00:00
|
|
|
vval = switch_core_session_strdup(session, p);
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_core_file_set_string(fh, SWITCH_AUDIO_COL_STR_COPYRIGHT, vval);
|
|
|
|
switch_channel_set_variable(channel, "RECORD_COPYRIGHT", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((p = switch_channel_get_variable(channel, "RECORD_SOFTWARE"))) {
|
2007-11-01 11:28:26 +00:00
|
|
|
vval = switch_core_session_strdup(session, p);
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_core_file_set_string(fh, SWITCH_AUDIO_COL_STR_SOFTWARE, vval);
|
|
|
|
switch_channel_set_variable(channel, "RECORD_SOFTWARE", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((p = switch_channel_get_variable(channel, "RECORD_ARTIST"))) {
|
2007-11-01 11:28:26 +00:00
|
|
|
vval = switch_core_session_strdup(session, p);
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_core_file_set_string(fh, SWITCH_AUDIO_COL_STR_ARTIST, vval);
|
|
|
|
switch_channel_set_variable(channel, "RECORD_ARTIST", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((p = switch_channel_get_variable(channel, "RECORD_COMMENT"))) {
|
2007-11-01 11:28:26 +00:00
|
|
|
vval = switch_core_session_strdup(session, p);
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_core_file_set_string(fh, SWITCH_AUDIO_COL_STR_COMMENT, vval);
|
|
|
|
switch_channel_set_variable(channel, "RECORD_COMMENT", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((p = switch_channel_get_variable(channel, "RECORD_DATE"))) {
|
2007-11-01 11:28:26 +00:00
|
|
|
vval = switch_core_session_strdup(session, p);
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_core_file_set_string(fh, SWITCH_AUDIO_COL_STR_DATE, vval);
|
|
|
|
switch_channel_set_variable(channel, "RECORD_DATE", NULL);
|
|
|
|
}
|
|
|
|
|
2009-01-10 03:52:28 +00:00
|
|
|
if (!asis) {
|
|
|
|
codec_name = "L16";
|
|
|
|
if (switch_core_codec_init(&codec,
|
|
|
|
codec_name,
|
|
|
|
NULL,
|
2009-02-10 19:09:06 +00:00
|
|
|
read_impl.actual_samples_per_second,
|
|
|
|
read_impl.microseconds_per_packet / 1000,
|
|
|
|
read_impl.number_of_channels,
|
2009-01-10 03:52:28 +00:00
|
|
|
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
|
|
|
|
switch_core_session_get_pool(session)) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Raw Codec Activated\n");
|
|
|
|
switch_core_session_set_read_codec(session, &codec);
|
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
|
|
|
|
"Raw Codec Activation Failed %s@%uhz %u channels %dms\n", codec_name, fh->samplerate,
|
2009-02-10 19:09:06 +00:00
|
|
|
fh->channels, read_impl.microseconds_per_packet / 1000);
|
2009-01-10 03:52:28 +00:00
|
|
|
switch_core_file_close(fh);
|
|
|
|
switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
|
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (limit) {
|
2009-01-25 21:23:07 +00:00
|
|
|
start = switch_epoch_time_now(NULL);
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
2009-01-10 03:52:28 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if (fh->thresh) {
|
2009-01-10 03:52:28 +00:00
|
|
|
if (asis) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Can't detect silence on a native recording.\n");
|
2007-03-29 22:34:40 +00:00
|
|
|
} else {
|
2009-01-10 03:52:28 +00:00
|
|
|
if (fh->silence_hits) {
|
2009-02-10 19:09:06 +00:00
|
|
|
fh->silence_hits = fh->samplerate * fh->silence_hits / read_impl.samples_per_packet;
|
2009-01-10 03:52:28 +00:00
|
|
|
} else {
|
2009-02-10 19:09:06 +00:00
|
|
|
fh->silence_hits = fh->samplerate * 3 / read_impl.samples_per_packet;
|
2009-01-10 03:52:28 +00:00
|
|
|
}
|
|
|
|
org_silence_hits = fh->silence_hits;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
for (;;) {
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_size_t len;
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-07-02 20:16:08 +00:00
|
|
|
if (!switch_channel_ready(channel)) {
|
|
|
|
status = SWITCH_STATUS_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-06-25 21:25:33 +00:00
|
|
|
if (switch_channel_test_flag(channel, CF_BREAK)) {
|
|
|
|
switch_channel_clear_flag(channel, CF_BREAK);
|
2007-07-02 20:16:08 +00:00
|
|
|
status = SWITCH_STATUS_BREAK;
|
2007-06-25 21:25:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-05-14 23:50:38 +00:00
|
|
|
if (switch_core_session_private_event_count(session)) {
|
|
|
|
switch_ivr_parse_all_events(session);
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2009-01-25 21:23:07 +00:00
|
|
|
if (start && (switch_epoch_time_now(NULL) - start) > limit) {
|
2007-03-29 22:34:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args && (args->input_callback || args->buf || args->buflen)) {
|
|
|
|
/*
|
|
|
|
dtmf handler function you can hook up to be executed when a digit is dialed during playback
|
|
|
|
if you return anything but SWITCH_STATUS_SUCCESS the playback will stop.
|
|
|
|
*/
|
|
|
|
if (switch_channel_has_dtmf(channel)) {
|
|
|
|
if (!args->input_callback && !args->buf) {
|
|
|
|
status = SWITCH_STATUS_BREAK;
|
|
|
|
break;
|
|
|
|
}
|
2007-12-22 00:32:20 +00:00
|
|
|
switch_channel_dequeue_dtmf(channel, &dtmf);
|
2007-03-29 22:34:40 +00:00
|
|
|
if (args->input_callback) {
|
2008-05-27 04:30:03 +00:00
|
|
|
status = args->input_callback(session, (void *) &dtmf, SWITCH_INPUT_TYPE_DTMF, args->buf, args->buflen);
|
2007-03-29 22:34:40 +00:00
|
|
|
} else {
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_copy_string((char *) args->buf, (void *) &dtmf, args->buflen);
|
2007-03-29 22:34:40 +00:00
|
|
|
status = SWITCH_STATUS_BREAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args->input_callback) {
|
2007-05-14 23:50:38 +00:00
|
|
|
switch_event_t *event = NULL;
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if (switch_core_session_dequeue_event(session, &event) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
status = args->input_callback(session, event, SWITCH_INPUT_TYPE_EVENT, args->buf, args->buflen);
|
|
|
|
switch_event_destroy(&event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status != SWITCH_STATUS_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-08 19:19:47 +00:00
|
|
|
status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
|
2007-03-29 22:34:40 +00:00
|
|
|
if (!SWITCH_READ_ACCEPTABLE(status)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-10-02 00:09:49 +00:00
|
|
|
if (args && (args->read_frame_callback)) {
|
|
|
|
if (args->read_frame_callback(session, read_frame, args->user_data) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-10 03:52:28 +00:00
|
|
|
if (!asis && fh->thresh) {
|
2007-03-29 22:34:40 +00:00
|
|
|
int16_t *fdata = (int16_t *) read_frame->data;
|
|
|
|
uint32_t samples = read_frame->datalen / sizeof(*fdata);
|
|
|
|
uint32_t score, count = 0, j = 0;
|
|
|
|
double energy = 0;
|
2007-11-24 21:48:25 +00:00
|
|
|
int divisor = 0;
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
for (count = 0; count < samples; count++) {
|
|
|
|
energy += abs(fdata[j]);
|
2009-02-10 19:09:06 +00:00
|
|
|
j += read_impl.number_of_channels;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
if (!(divisor = read_impl.actual_samples_per_second / 8000)) {
|
2007-11-24 21:48:25 +00:00
|
|
|
divisor = 1;
|
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-11-24 21:48:25 +00:00
|
|
|
score = (uint32_t) (energy / (samples / divisor));
|
2007-03-29 22:34:40 +00:00
|
|
|
if (score < fh->thresh) {
|
|
|
|
if (!--fh->silence_hits) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fh->silence_hits = org_silence_hits;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-12 21:30:55 +00:00
|
|
|
if (!switch_test_flag(fh, SWITCH_FILE_PAUSE) && !switch_test_flag(read_frame, SFF_CNG)) {
|
2007-11-26 23:41:00 +00:00
|
|
|
int16_t *data = read_frame->data;
|
2009-01-10 03:52:28 +00:00
|
|
|
len = (switch_size_t) asis ? read_frame->datalen : read_frame->datalen / 2;
|
|
|
|
|
2007-11-26 23:41:00 +00:00
|
|
|
if (switch_core_file_write(fh, data, &len) != SWITCH_STATUS_SUCCESS) {
|
2007-03-29 22:34:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_core_file_close(fh);
|
2009-01-09 20:34:01 +00:00
|
|
|
switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
|
2007-03-29 22:34:40 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
static int teletone_handler(teletone_generation_session_t *ts, teletone_tone_map_t *map)
|
2007-06-17 01:16:39 +00:00
|
|
|
{
|
|
|
|
switch_buffer_t *audio_buffer = ts->user_data;
|
|
|
|
int wrote;
|
|
|
|
|
|
|
|
if (!audio_buffer) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
wrote = teletone_mux_tones(ts, map);
|
|
|
|
switch_buffer_write(audio_buffer, ts->buffer, wrote * 2);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_ivr_gentones(switch_core_session_t *session, char *script, int32_t loops, switch_input_args_t *args)
|
|
|
|
{
|
|
|
|
teletone_generation_session_t ts;
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_dtmf_t dtmf = { 0 };
|
2007-06-17 01:16:39 +00:00
|
|
|
switch_buffer_t *audio_buffer;
|
|
|
|
switch_frame_t *read_frame = NULL;
|
2009-02-10 19:09:06 +00:00
|
|
|
switch_codec_t write_codec = { 0 };
|
2007-09-24 15:52:32 +00:00
|
|
|
switch_frame_t write_frame = { 0 };
|
2008-02-14 17:12:17 +00:00
|
|
|
switch_byte_t data[SWITCH_RECOMMENDED_BUFFER_SIZE];
|
2008-01-28 07:26:10 +00:00
|
|
|
switch_channel_t *channel = switch_core_session_get_channel(session);
|
2009-02-10 19:09:06 +00:00
|
|
|
switch_codec_implementation_t read_impl = {0};
|
|
|
|
switch_core_session_get_read_impl(session, &read_impl);
|
2007-06-17 01:16:39 +00:00
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
if (switch_channel_pre_answer(channel) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-06-17 01:16:39 +00:00
|
|
|
if (switch_core_codec_init(&write_codec,
|
|
|
|
"L16",
|
|
|
|
NULL,
|
2009-02-10 19:09:06 +00:00
|
|
|
read_impl.actual_samples_per_second,
|
|
|
|
read_impl.microseconds_per_packet / 1000,
|
2008-05-27 04:30:03 +00:00
|
|
|
1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
|
2007-06-17 01:16:39 +00:00
|
|
|
NULL, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2007-09-24 15:52:32 +00:00
|
|
|
memset(&ts, 0, sizeof(ts));
|
2007-06-17 01:16:39 +00:00
|
|
|
write_frame.codec = &write_codec;
|
|
|
|
write_frame.data = data;
|
|
|
|
|
|
|
|
switch_buffer_create_dynamic(&audio_buffer, 512, 1024, 0);
|
|
|
|
teletone_init_session(&ts, 0, teletone_handler, audio_buffer);
|
2009-02-10 19:09:06 +00:00
|
|
|
ts.rate = read_impl.actual_samples_per_second;
|
2007-09-24 15:52:32 +00:00
|
|
|
ts.channels = 1;
|
2007-06-17 01:16:39 +00:00
|
|
|
teletone_run(&ts, script);
|
|
|
|
|
|
|
|
if (loops) {
|
|
|
|
switch_buffer_set_loops(audio_buffer, loops);
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
for (;;) {
|
2008-05-24 00:02:18 +00:00
|
|
|
int done = 0;
|
2007-11-26 23:41:00 +00:00
|
|
|
switch_status_t status;
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-07-02 20:16:08 +00:00
|
|
|
if (!switch_channel_ready(channel)) {
|
2008-05-27 04:30:03 +00:00
|
|
|
status = SWITCH_STATUS_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2007-07-02 20:16:08 +00:00
|
|
|
|
2007-06-25 21:25:33 +00:00
|
|
|
if (switch_channel_test_flag(channel, CF_BREAK)) {
|
|
|
|
switch_channel_clear_flag(channel, CF_BREAK);
|
2007-07-03 02:10:35 +00:00
|
|
|
status = SWITCH_STATUS_BREAK;
|
2007-06-25 21:25:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-05-08 19:19:47 +00:00
|
|
|
status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
|
2007-11-26 23:41:00 +00:00
|
|
|
|
2007-06-17 01:16:39 +00:00
|
|
|
if (!SWITCH_READ_ACCEPTABLE(status)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-25 19:53:43 +00:00
|
|
|
if (switch_core_session_private_event_count(session)) {
|
|
|
|
switch_ivr_parse_all_events(session);
|
|
|
|
}
|
|
|
|
|
2008-05-24 00:02:18 +00:00
|
|
|
if (args && (args->input_callback || args->buf || args->buflen)) {
|
|
|
|
/*
|
|
|
|
dtmf handler function you can hook up to be executed when a digit is dialed during gentones
|
|
|
|
if you return anything but SWITCH_STATUS_SUCCESS the playback will stop.
|
|
|
|
*/
|
|
|
|
if (switch_channel_has_dtmf(channel)) {
|
|
|
|
if (!args->input_callback && !args->buf) {
|
|
|
|
status = SWITCH_STATUS_BREAK;
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch_channel_dequeue_dtmf(channel, &dtmf);
|
|
|
|
if (args->input_callback) {
|
2008-05-27 04:30:03 +00:00
|
|
|
status = args->input_callback(session, (void *) &dtmf, SWITCH_INPUT_TYPE_DTMF, args->buf, args->buflen);
|
2008-05-24 00:02:18 +00:00
|
|
|
} else {
|
2008-05-27 04:30:03 +00:00
|
|
|
*((char *) args->buf) = dtmf.digit;
|
2008-05-24 00:02:18 +00:00
|
|
|
status = SWITCH_STATUS_BREAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args->input_callback) {
|
|
|
|
switch_event_t *event;
|
|
|
|
|
|
|
|
if (switch_core_session_dequeue_event(session, &event) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
status = args->input_callback(session, event, SWITCH_INPUT_TYPE_EVENT, args->buf, args->buflen);
|
|
|
|
switch_event_destroy(&event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status != SWITCH_STATUS_SUCCESS) {
|
|
|
|
done = 1;
|
2007-10-02 00:09:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
if ((write_frame.datalen = (uint32_t) switch_buffer_read_loop(audio_buffer, write_frame.data, read_impl.decoded_bytes_per_packet)) <= 0) {
|
2007-06-17 01:16:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
write_frame.samples = write_frame.datalen / 2;
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2008-05-08 19:19:47 +00:00
|
|
|
if (switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
|
2007-06-17 01:16:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_core_codec_destroy(&write_codec);
|
|
|
|
switch_buffer_destroy(&audio_buffer);
|
|
|
|
teletone_destroy_session(&ts);
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
#define FILE_STARTSAMPLES 1024 * 32
|
|
|
|
#define FILE_BLOCKSIZE 1024 * 8
|
|
|
|
#define FILE_BUFSIZE 1024 * 64
|
|
|
|
|
2007-11-01 11:28:26 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *session, switch_file_handle_t *fh, const char *file, switch_input_args_t *args)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
2008-01-28 07:26:10 +00:00
|
|
|
switch_channel_t *channel = switch_core_session_get_channel(session);
|
2008-05-19 19:57:27 +00:00
|
|
|
int16_t *abuf = NULL;
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_dtmf_t dtmf = { 0 };
|
2007-03-29 22:34:40 +00:00
|
|
|
uint32_t interval = 0, samples = 0, framelen, sample_start = 0;
|
|
|
|
uint32_t ilen = 0;
|
|
|
|
switch_size_t olen = 0, llen = 0;
|
|
|
|
switch_frame_t write_frame = { 0 };
|
|
|
|
switch_timer_t timer = { 0 };
|
|
|
|
switch_codec_t codec;
|
|
|
|
switch_memory_pool_t *pool = switch_core_session_get_pool(session);
|
|
|
|
char *codec_name;
|
|
|
|
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
|
|
|
switch_file_handle_t lfh;
|
|
|
|
const char *p;
|
|
|
|
char *title = "", *copyright = "", *software = "", *artist = "", *comment = "", *date = "";
|
|
|
|
uint8_t asis = 0;
|
|
|
|
char *ext;
|
2007-11-01 11:28:26 +00:00
|
|
|
const char *prefix;
|
|
|
|
const char *timer_name;
|
|
|
|
const char *prebuf;
|
2008-03-19 15:34:43 +00:00
|
|
|
const char *alt = NULL;
|
2008-07-23 22:10:48 +00:00
|
|
|
int eof = 0;
|
|
|
|
switch_size_t bread = 0;
|
2008-08-12 19:12:38 +00:00
|
|
|
int l16 = 0;
|
2009-02-10 19:09:06 +00:00
|
|
|
switch_codec_implementation_t read_impl = {0};
|
|
|
|
switch_core_session_get_read_impl(session, &read_impl);
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
if (switch_channel_pre_answer(channel) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2008-01-07 21:47:32 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
prefix = switch_channel_get_variable(channel, "sound_prefix");
|
|
|
|
timer_name = switch_channel_get_variable(channel, "timer_name");
|
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
if (switch_strlen_zero(file) || !switch_channel_media_ready(channel)) {
|
2007-12-18 17:33:29 +00:00
|
|
|
status = SWITCH_STATUS_FALSE;
|
|
|
|
goto end;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
if (!strcasecmp(read_impl.iananame, "l16")) {
|
2008-10-20 03:58:04 +00:00
|
|
|
l16++;
|
|
|
|
}
|
|
|
|
|
2008-03-19 15:34:43 +00:00
|
|
|
if ((alt = strchr(file, ':'))) {
|
|
|
|
char *dup;
|
|
|
|
|
|
|
|
if (!strncasecmp(file, "phrase:", 7)) {
|
2008-03-19 15:56:24 +00:00
|
|
|
char *arg = NULL;
|
2008-04-07 20:22:38 +00:00
|
|
|
const char *lang = switch_channel_get_variable(channel, "language");
|
2008-03-19 15:34:43 +00:00
|
|
|
alt = file + 7;
|
|
|
|
dup = switch_core_session_strdup(session, alt);
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2008-03-19 15:34:43 +00:00
|
|
|
if (dup) {
|
2008-05-19 19:45:50 +00:00
|
|
|
if ((arg = strchr(dup, ':'))) {
|
|
|
|
*arg++ = '\0';
|
|
|
|
}
|
2008-03-19 15:34:43 +00:00
|
|
|
return switch_ivr_phrase_macro(session, dup, arg, lang, args);
|
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Args\n");
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
} else if (!strncasecmp(file, "say:", 4)) {
|
|
|
|
char *engine = NULL, *voice = NULL, *text = NULL;
|
|
|
|
alt = file + 4;
|
2008-05-27 04:30:03 +00:00
|
|
|
dup = switch_core_session_strdup(session, alt);
|
2008-03-19 15:34:43 +00:00
|
|
|
engine = dup;
|
|
|
|
|
2008-05-21 20:32:51 +00:00
|
|
|
if (!switch_strlen_zero(engine)) {
|
|
|
|
if ((voice = strchr(engine, ':'))) {
|
|
|
|
*voice++ = '\0';
|
|
|
|
if (!switch_strlen_zero(voice) && (text = strchr(voice, ':'))) {
|
|
|
|
*text++ = '\0';
|
|
|
|
}
|
2008-03-19 15:34:43 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-21 20:32:51 +00:00
|
|
|
|
|
|
|
if (!switch_strlen_zero(engine) && !switch_strlen_zero(voice) && !switch_strlen_zero(text)) {
|
2008-04-07 20:22:38 +00:00
|
|
|
return switch_ivr_speak_text(session, engine, voice, text, args);
|
2008-05-21 20:32:51 +00:00
|
|
|
} else if (!switch_strlen_zero(engine) && !(voice && text)) {
|
2008-04-07 20:22:38 +00:00
|
|
|
text = engine;
|
2008-05-27 04:30:03 +00:00
|
|
|
engine = (char *) switch_channel_get_variable(channel, "tts_engine");
|
|
|
|
voice = (char *) switch_channel_get_variable(channel, "tts_voice");
|
2008-04-07 20:22:38 +00:00
|
|
|
if (engine && text) {
|
|
|
|
return switch_ivr_speak_text(session, engine, voice, text, args);
|
|
|
|
}
|
2008-03-19 15:34:43 +00:00
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Args\n");
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2008-03-19 15:34:43 +00:00
|
|
|
}
|
|
|
|
|
2007-11-06 22:19:49 +00:00
|
|
|
if (!prefix) {
|
|
|
|
prefix = SWITCH_GLOBAL_dirs.base_dir;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if (!strstr(file, SWITCH_URL_SEPARATOR)) {
|
2007-10-31 20:37:30 +00:00
|
|
|
if (!switch_is_file_path(file)) {
|
|
|
|
file = switch_core_session_sprintf(session, "%s%s%s", prefix, SWITCH_PATH_SEPARATOR, file);
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
if ((ext = strrchr(file, '.'))) {
|
|
|
|
ext++;
|
|
|
|
} else {
|
2009-02-10 19:09:06 +00:00
|
|
|
ext = read_impl.iananame;
|
2007-10-31 20:37:30 +00:00
|
|
|
file = switch_core_session_sprintf(session, "%s.%s", file, ext);
|
2007-03-29 22:34:40 +00:00
|
|
|
asis = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fh) {
|
|
|
|
fh = &lfh;
|
|
|
|
memset(fh, 0, sizeof(lfh));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fh->samples > 0) {
|
|
|
|
sample_start = fh->samples;
|
|
|
|
fh->samples = 0;
|
|
|
|
}
|
|
|
|
|
2007-06-09 23:02:38 +00:00
|
|
|
if ((prebuf = switch_channel_get_variable(channel, "stream_prebuffer"))) {
|
|
|
|
int maybe = atoi(prebuf);
|
|
|
|
if (maybe > 0) {
|
|
|
|
fh->prebuf = maybe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if (switch_core_file_open(fh,
|
|
|
|
file,
|
2009-02-10 19:09:06 +00:00
|
|
|
read_impl.number_of_channels,
|
|
|
|
read_impl.actual_samples_per_second,
|
2008-05-08 15:50:28 +00:00
|
|
|
SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
|
2009-01-09 20:34:01 +00:00
|
|
|
switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
|
2007-12-18 17:33:29 +00:00
|
|
|
status = SWITCH_STATUS_NOTFOUND;
|
|
|
|
goto end;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
if (switch_test_flag(fh, SWITCH_FILE_NATIVE)) {
|
|
|
|
asis = 1;
|
|
|
|
}
|
|
|
|
|
2008-05-19 19:57:27 +00:00
|
|
|
switch_zmalloc(abuf, FILE_STARTSAMPLES * sizeof(*abuf));
|
2007-03-29 22:34:40 +00:00
|
|
|
write_frame.data = abuf;
|
2007-12-18 17:33:29 +00:00
|
|
|
write_frame.buflen = FILE_STARTSAMPLES;
|
2007-03-29 22:34:40 +00:00
|
|
|
|
|
|
|
if (sample_start > 0) {
|
|
|
|
uint32_t pos = 0;
|
2007-07-05 17:03:14 +00:00
|
|
|
switch_core_file_seek(fh, &pos, 0, SEEK_SET);
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_core_file_seek(fh, &pos, sample_start, SEEK_CUR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_core_file_get_string(fh, SWITCH_AUDIO_COL_STR_TITLE, &p) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
title = switch_core_session_strdup(session, p);
|
|
|
|
switch_channel_set_variable(channel, "RECORD_TITLE", p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_core_file_get_string(fh, SWITCH_AUDIO_COL_STR_COPYRIGHT, &p) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
copyright = switch_core_session_strdup(session, p);
|
|
|
|
switch_channel_set_variable(channel, "RECORD_COPYRIGHT", p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_core_file_get_string(fh, SWITCH_AUDIO_COL_STR_SOFTWARE, &p) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
software = switch_core_session_strdup(session, p);
|
|
|
|
switch_channel_set_variable(channel, "RECORD_SOFTWARE", p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_core_file_get_string(fh, SWITCH_AUDIO_COL_STR_ARTIST, &p) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
artist = switch_core_session_strdup(session, p);
|
|
|
|
switch_channel_set_variable(channel, "RECORD_ARTIST", p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_core_file_get_string(fh, SWITCH_AUDIO_COL_STR_COMMENT, &p) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
comment = switch_core_session_strdup(session, p);
|
|
|
|
switch_channel_set_variable(channel, "RECORD_COMMENT", p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_core_file_get_string(fh, SWITCH_AUDIO_COL_STR_DATE, &p) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
date = switch_core_session_strdup(session, p);
|
|
|
|
switch_channel_set_variable(channel, "RECORD_DATE", p);
|
|
|
|
}
|
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
interval = read_impl.microseconds_per_packet / 1000;
|
2007-03-29 22:34:40 +00:00
|
|
|
|
|
|
|
if (!fh->audio_buffer) {
|
|
|
|
switch_buffer_create_dynamic(&fh->audio_buffer, FILE_BLOCKSIZE, FILE_BUFSIZE, 0);
|
|
|
|
if (!fh->audio_buffer) {
|
2008-10-11 05:44:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Setup buffer failed\n");
|
2007-03-29 22:34:40 +00:00
|
|
|
|
|
|
|
switch_core_file_close(fh);
|
2009-01-09 20:34:01 +00:00
|
|
|
switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2007-12-18 17:33:29 +00:00
|
|
|
status = SWITCH_STATUS_GENERR;
|
|
|
|
goto end;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asis) {
|
2009-02-10 19:09:06 +00:00
|
|
|
write_frame.codec = switch_core_session_get_read_codec(session);
|
|
|
|
samples = read_impl.samples_per_packet;
|
|
|
|
framelen = read_impl.encoded_bytes_per_packet;
|
2007-03-29 22:34:40 +00:00
|
|
|
} else {
|
|
|
|
codec_name = "L16";
|
|
|
|
|
|
|
|
if (switch_core_codec_init(&codec,
|
|
|
|
codec_name,
|
|
|
|
NULL,
|
|
|
|
fh->samplerate,
|
2007-03-30 00:13:31 +00:00
|
|
|
interval, fh->channels, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, pool) == SWITCH_STATUS_SUCCESS) {
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG,
|
2007-03-30 00:13:31 +00:00
|
|
|
SWITCH_LOG_DEBUG, "Codec Activated %s@%uhz %u channels %dms\n", codec_name, fh->samplerate, fh->channels, interval);
|
2007-03-29 22:34:40 +00:00
|
|
|
|
|
|
|
write_frame.codec = &codec;
|
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
|
2007-03-30 00:13:31 +00:00
|
|
|
"Raw Codec Activation Failed %s@%uhz %u channels %dms\n", codec_name, fh->samplerate, fh->channels, interval);
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_core_file_close(fh);
|
2009-01-09 20:34:01 +00:00
|
|
|
switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
|
2007-12-18 17:33:29 +00:00
|
|
|
status = SWITCH_STATUS_GENERR;
|
|
|
|
goto end;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
2008-10-20 17:48:42 +00:00
|
|
|
samples = codec.implementation->samples_per_packet;
|
|
|
|
framelen = codec.implementation->decoded_bytes_per_packet;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (timer_name) {
|
|
|
|
uint32_t len;
|
|
|
|
|
|
|
|
len = samples * 2;
|
|
|
|
if (switch_core_timer_init(&timer, timer_name, interval, samples, pool) != SWITCH_STATUS_SUCCESS) {
|
2008-10-11 05:44:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Setup timer failed!\n");
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_core_codec_destroy(&codec);
|
|
|
|
switch_core_file_close(fh);
|
2009-01-09 20:34:01 +00:00
|
|
|
switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
|
2007-12-18 17:33:29 +00:00
|
|
|
status = SWITCH_STATUS_GENERR;
|
|
|
|
goto end;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
2008-10-11 05:44:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Setup timer success %u bytes per %d ms!\n", len, interval);
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
write_frame.rate = fh->samplerate;
|
|
|
|
|
|
|
|
if (timer_name) {
|
|
|
|
/* start a thread to absorb incoming audio */
|
2008-10-23 23:48:11 +00:00
|
|
|
switch_core_service_session(session);
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ilen = samples;
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
for (;;) {
|
2007-03-29 22:34:40 +00:00
|
|
|
int done = 0;
|
|
|
|
int do_speed = 1;
|
|
|
|
int last_speed = -1;
|
|
|
|
|
2007-07-03 02:10:35 +00:00
|
|
|
if (!switch_channel_ready(channel)) {
|
2008-05-27 04:30:03 +00:00
|
|
|
status = SWITCH_STATUS_FALSE;
|
2008-07-23 22:10:48 +00:00
|
|
|
break;
|
2008-05-27 04:30:03 +00:00
|
|
|
}
|
2007-07-03 02:10:35 +00:00
|
|
|
|
2007-06-25 21:25:33 +00:00
|
|
|
if (switch_channel_test_flag(channel, CF_BREAK)) {
|
|
|
|
switch_channel_clear_flag(channel, CF_BREAK);
|
2007-07-03 02:10:35 +00:00
|
|
|
status = SWITCH_STATUS_BREAK;
|
2008-07-23 22:10:48 +00:00
|
|
|
break;
|
2007-06-25 21:25:33 +00:00
|
|
|
}
|
|
|
|
|
2007-05-14 23:50:38 +00:00
|
|
|
if (switch_core_session_private_event_count(session)) {
|
|
|
|
switch_ivr_parse_all_events(session);
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (args && (args->input_callback || args->buf || args->buflen)) {
|
|
|
|
/*
|
|
|
|
dtmf handler function you can hook up to be executed when a digit is dialed during playback
|
|
|
|
if you return anything but SWITCH_STATUS_SUCCESS the playback will stop.
|
|
|
|
*/
|
|
|
|
if (switch_channel_has_dtmf(channel)) {
|
|
|
|
if (!args->input_callback && !args->buf) {
|
|
|
|
status = SWITCH_STATUS_BREAK;
|
|
|
|
done = 1;
|
2008-07-23 22:10:48 +00:00
|
|
|
break;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
2007-12-22 00:32:20 +00:00
|
|
|
switch_channel_dequeue_dtmf(channel, &dtmf);
|
2007-03-29 22:34:40 +00:00
|
|
|
if (args->input_callback) {
|
2008-05-27 04:30:03 +00:00
|
|
|
status = args->input_callback(session, (void *) &dtmf, SWITCH_INPUT_TYPE_DTMF, args->buf, args->buflen);
|
2007-03-29 22:34:40 +00:00
|
|
|
} else {
|
2008-05-27 04:30:03 +00:00
|
|
|
*((char *) args->buf) = dtmf.digit;
|
2007-03-29 22:34:40 +00:00
|
|
|
status = SWITCH_STATUS_BREAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args->input_callback) {
|
2007-05-14 23:50:38 +00:00
|
|
|
switch_event_t *event;
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if (switch_core_session_dequeue_event(session, &event) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
status = args->input_callback(session, event, SWITCH_INPUT_TYPE_EVENT, args->buf, args->buflen);
|
|
|
|
switch_event_destroy(&event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status != SWITCH_STATUS_SUCCESS) {
|
|
|
|
done = 1;
|
2008-07-23 22:10:48 +00:00
|
|
|
break;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_test_flag(fh, SWITCH_FILE_PAUSE)) {
|
2007-12-18 17:43:38 +00:00
|
|
|
if (framelen > FILE_STARTSAMPLES) {
|
|
|
|
framelen = FILE_STARTSAMPLES;
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
memset(abuf, 0, framelen);
|
|
|
|
olen = ilen;
|
|
|
|
do_speed = 0;
|
2008-07-23 22:10:48 +00:00
|
|
|
} else if (fh->sp_audio_buffer && (eof || (switch_buffer_inuse(fh->sp_audio_buffer) > (switch_size_t) (framelen)))) {
|
|
|
|
if (!(bread = switch_buffer_read(fh->sp_audio_buffer, abuf, framelen))) {
|
2008-07-23 23:24:18 +00:00
|
|
|
if (eof) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2008-07-23 22:10:48 +00:00
|
|
|
}
|
2008-07-23 23:24:18 +00:00
|
|
|
|
|
|
|
if (bread < framelen) {
|
|
|
|
memset(abuf + bread, 0, framelen - bread);
|
|
|
|
}
|
|
|
|
|
|
|
|
olen = asis ? framelen : ilen;
|
2008-03-07 22:40:20 +00:00
|
|
|
do_speed = 0;
|
2008-07-23 22:10:48 +00:00
|
|
|
} else if (fh->audio_buffer && (eof || (switch_buffer_inuse(fh->audio_buffer) > (switch_size_t) (framelen)))) {
|
|
|
|
if (!(bread = switch_buffer_read(fh->audio_buffer, abuf, framelen))) {
|
2008-07-23 23:24:18 +00:00
|
|
|
if (eof) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2008-07-23 22:10:48 +00:00
|
|
|
}
|
2008-07-23 23:24:18 +00:00
|
|
|
|
|
|
|
if (bread < framelen) {
|
|
|
|
memset(abuf + bread, 0, framelen - bread);
|
|
|
|
}
|
|
|
|
|
|
|
|
olen = asis ? framelen : ilen;
|
2007-03-29 22:34:40 +00:00
|
|
|
} else {
|
2008-07-23 22:10:48 +00:00
|
|
|
if (eof) {
|
|
|
|
break;
|
|
|
|
}
|
2007-12-18 17:33:29 +00:00
|
|
|
olen = FILE_STARTSAMPLES;
|
2007-03-29 22:34:40 +00:00
|
|
|
if (!asis) {
|
|
|
|
olen /= 2;
|
|
|
|
}
|
2008-05-28 15:14:18 +00:00
|
|
|
if (switch_core_file_read(fh, abuf, &olen) != SWITCH_STATUS_SUCCESS) {
|
2008-07-23 22:10:48 +00:00
|
|
|
eof++;
|
|
|
|
continue;
|
2008-05-28 15:14:18 +00:00
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_buffer_write(fh->audio_buffer, abuf, asis ? olen : olen * 2);
|
|
|
|
olen = switch_buffer_read(fh->audio_buffer, abuf, framelen);
|
|
|
|
if (!asis) {
|
|
|
|
olen /= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (done || olen <= 0) {
|
2008-07-23 22:10:48 +00:00
|
|
|
break;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!asis) {
|
|
|
|
if (fh->speed > 2) {
|
|
|
|
fh->speed = 2;
|
|
|
|
} else if (fh->speed < -2) {
|
|
|
|
fh->speed = -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!asis && fh->audio_buffer && last_speed > -1 && last_speed != fh->speed) {
|
2008-03-07 22:40:20 +00:00
|
|
|
switch_buffer_zero(fh->sp_audio_buffer);
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_test_flag(fh, SWITCH_FILE_SEEK)) {
|
|
|
|
/* file position has changed flush the buffer */
|
|
|
|
switch_buffer_zero(fh->audio_buffer);
|
|
|
|
switch_clear_flag(fh, SWITCH_FILE_SEEK);
|
|
|
|
}
|
|
|
|
|
2008-03-07 22:40:20 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if (!asis && fh->speed && do_speed) {
|
|
|
|
float factor = 0.25f * abs(fh->speed);
|
|
|
|
switch_size_t newlen, supplement, step;
|
|
|
|
short *bp = write_frame.data;
|
|
|
|
switch_size_t wrote = 0;
|
|
|
|
|
|
|
|
supplement = (int) (factor * olen);
|
2008-03-07 22:40:20 +00:00
|
|
|
if (!supplement) {
|
|
|
|
supplement = 1;
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
newlen = (fh->speed > 0) ? olen - supplement : olen + supplement;
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
step = (fh->speed > 0) ? (newlen / supplement) : (olen / supplement);
|
|
|
|
|
2008-03-07 22:40:20 +00:00
|
|
|
if (!fh->sp_audio_buffer) {
|
|
|
|
switch_buffer_create_dynamic(&fh->sp_audio_buffer, 1024, 1024, 0);
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
while ((wrote + step) < newlen) {
|
2008-03-07 22:40:20 +00:00
|
|
|
switch_buffer_write(fh->sp_audio_buffer, bp, step * 2);
|
2007-03-29 22:34:40 +00:00
|
|
|
wrote += step;
|
|
|
|
bp += step;
|
|
|
|
if (fh->speed > 0) {
|
|
|
|
bp++;
|
|
|
|
} else {
|
|
|
|
float f;
|
|
|
|
short s;
|
|
|
|
f = (float) (*bp + *(bp + 1) + *(bp - 1));
|
|
|
|
f /= 3;
|
|
|
|
s = (short) f;
|
2008-03-07 22:40:20 +00:00
|
|
|
switch_buffer_write(fh->sp_audio_buffer, &s, 2);
|
2007-03-29 22:34:40 +00:00
|
|
|
wrote++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wrote < newlen) {
|
|
|
|
switch_size_t r = newlen - wrote;
|
2008-03-07 22:40:20 +00:00
|
|
|
switch_buffer_write(fh->sp_audio_buffer, bp, r * 2);
|
2007-03-29 22:34:40 +00:00
|
|
|
wrote += r;
|
|
|
|
}
|
|
|
|
last_speed = fh->speed;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (olen < llen) {
|
|
|
|
uint8_t *dp = (uint8_t *) write_frame.data;
|
|
|
|
memset(dp + (int) olen, 0, (int) (llen - olen));
|
|
|
|
olen = llen;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
write_frame.samples = (uint32_t) olen;
|
2007-07-05 17:03:14 +00:00
|
|
|
|
|
|
|
if (asis) {
|
2008-05-27 04:30:03 +00:00
|
|
|
write_frame.datalen = (uint32_t) olen;
|
2007-07-05 17:03:14 +00:00
|
|
|
} else {
|
|
|
|
write_frame.datalen = write_frame.samples * 2;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
llen = olen;
|
|
|
|
|
|
|
|
if (timer_name) {
|
|
|
|
write_frame.timestamp = timer.samplecount;
|
|
|
|
}
|
|
|
|
#ifndef WIN32
|
2008-09-24 19:36:24 +00:00
|
|
|
#if SWITCH_BYTE_ORDER == __BIG_ENDIAN
|
2008-08-12 19:12:38 +00:00
|
|
|
if (!asis && l16) {
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_swap_linear(write_frame.data, (int) write_frame.datalen / 2);
|
|
|
|
}
|
2008-09-24 19:36:24 +00:00
|
|
|
#endif
|
2007-03-29 22:34:40 +00:00
|
|
|
#endif
|
2007-07-05 17:03:14 +00:00
|
|
|
if (fh->vol) {
|
|
|
|
switch_change_sln_volume(write_frame.data, write_frame.datalen / 2, fh->vol);
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2007-07-05 17:03:14 +00:00
|
|
|
fh->offset_pos += write_frame.samples / 2;
|
2008-05-08 19:19:47 +00:00
|
|
|
status = switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0);
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if (status == SWITCH_STATUS_MORE_DATA) {
|
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
|
|
|
continue;
|
|
|
|
} else if (status != SWITCH_STATUS_SUCCESS) {
|
|
|
|
done = 1;
|
2008-07-23 22:10:48 +00:00
|
|
|
break;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (done) {
|
2008-07-23 22:10:48 +00:00
|
|
|
break;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (timer_name) {
|
|
|
|
if (switch_core_timer_next(&timer) != SWITCH_STATUS_SUCCESS) {
|
2008-07-23 22:10:48 +00:00
|
|
|
break;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
} else { /* time off the channel (if you must) */
|
|
|
|
switch_frame_t *read_frame;
|
2007-12-18 17:50:39 +00:00
|
|
|
switch_status_t tstatus;
|
2007-03-29 22:34:40 +00:00
|
|
|
while (switch_channel_ready(channel) && switch_channel_test_flag(channel, CF_HOLD)) {
|
|
|
|
switch_yield(10000);
|
|
|
|
}
|
2008-03-11 16:55:58 +00:00
|
|
|
|
2008-05-08 19:19:47 +00:00
|
|
|
tstatus = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-12-18 17:50:39 +00:00
|
|
|
if (!SWITCH_READ_ACCEPTABLE(tstatus)) {
|
2008-07-23 22:10:48 +00:00
|
|
|
break;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
2007-10-02 00:09:49 +00:00
|
|
|
|
|
|
|
if (args && (args->read_frame_callback)) {
|
2008-04-04 21:18:16 +00:00
|
|
|
int ok = 1;
|
|
|
|
switch_set_flag(fh, SWITCH_FILE_CALLBACK);
|
2007-10-02 00:09:49 +00:00
|
|
|
if (args->read_frame_callback(session, read_frame, args->user_data) != SWITCH_STATUS_SUCCESS) {
|
2008-04-04 21:18:16 +00:00
|
|
|
ok = 0;
|
|
|
|
}
|
|
|
|
switch_clear_flag(fh, SWITCH_FILE_CALLBACK);
|
|
|
|
if (!ok) {
|
2008-07-23 22:10:48 +00:00
|
|
|
break;
|
2007-10-02 00:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "done playing file\n");
|
2007-07-05 17:03:14 +00:00
|
|
|
switch_core_file_seek(fh, &fh->last_pos, 0, SEEK_CUR);
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_core_file_close(fh);
|
|
|
|
switch_buffer_destroy(&fh->audio_buffer);
|
2008-03-07 22:40:20 +00:00
|
|
|
switch_buffer_destroy(&fh->sp_audio_buffer);
|
2007-03-29 22:34:40 +00:00
|
|
|
if (!asis) {
|
|
|
|
switch_core_codec_destroy(&codec);
|
|
|
|
}
|
|
|
|
if (timer_name) {
|
|
|
|
/* End the audio absorbing thread */
|
2008-10-23 23:48:11 +00:00
|
|
|
switch_core_thread_session_end(session);
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_core_timer_destroy(&timer);
|
|
|
|
}
|
|
|
|
|
2007-12-18 17:33:29 +00:00
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
end:
|
2008-05-19 19:57:27 +00:00
|
|
|
switch_safe_free(abuf);
|
2007-12-18 17:33:29 +00:00
|
|
|
|
2009-01-09 20:34:01 +00:00
|
|
|
switch_core_session_reset(session, SWITCH_FALSE, SWITCH_TRUE);
|
2007-03-29 22:34:40 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2008-07-10 19:59:57 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_ivr_wait_for_silence(switch_core_session_t *session, uint32_t thresh,
|
|
|
|
uint32_t silence_hits, uint32_t listen_hits, uint32_t timeout_ms, const char *file)
|
2008-07-10 15:57:41 +00:00
|
|
|
{
|
|
|
|
uint32_t score, count = 0, j = 0;
|
|
|
|
double energy = 0;
|
|
|
|
switch_channel_t *channel = switch_core_session_get_channel(session);
|
|
|
|
int divisor = 0;
|
|
|
|
uint32_t org_silence_hits = silence_hits;
|
|
|
|
uint32_t channels;
|
|
|
|
switch_frame_t *read_frame;
|
|
|
|
switch_status_t status = SWITCH_STATUS_FALSE;
|
|
|
|
int16_t *data;
|
2008-07-11 13:01:27 +00:00
|
|
|
uint32_t listening = 0;
|
2008-07-10 15:57:41 +00:00
|
|
|
int countdown = 0;
|
|
|
|
switch_codec_t raw_codec = {0};
|
|
|
|
int16_t *abuf = NULL;
|
|
|
|
switch_frame_t write_frame = {0};
|
|
|
|
switch_file_handle_t fh = {0};
|
2008-07-10 19:59:57 +00:00
|
|
|
int32_t sample_count = 0;
|
2009-02-10 19:09:06 +00:00
|
|
|
switch_codec_implementation_t read_impl = {0};
|
|
|
|
switch_core_session_get_read_impl(session, &read_impl);
|
2008-07-10 15:57:41 +00:00
|
|
|
|
|
|
|
|
2008-07-10 19:59:57 +00:00
|
|
|
if (timeout_ms) {
|
2009-02-10 19:09:06 +00:00
|
|
|
sample_count = (read_impl.actual_samples_per_second / 1000) * timeout_ms;
|
2008-07-10 19:59:57 +00:00
|
|
|
}
|
|
|
|
|
2008-07-10 15:57:41 +00:00
|
|
|
if (file) {
|
|
|
|
if (switch_core_file_open(&fh,
|
|
|
|
file,
|
2009-02-10 19:09:06 +00:00
|
|
|
read_impl.number_of_channels,
|
|
|
|
read_impl.actual_samples_per_second,
|
2008-07-10 15:57:41 +00:00
|
|
|
SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
|
2009-01-09 20:34:01 +00:00
|
|
|
switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
|
2008-07-10 15:57:41 +00:00
|
|
|
return SWITCH_STATUS_NOTFOUND;
|
|
|
|
}
|
|
|
|
switch_zmalloc(abuf, SWITCH_RECOMMENDED_BUFFER_SIZE);
|
|
|
|
write_frame.data = abuf;
|
|
|
|
write_frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (switch_core_codec_init(&raw_codec,
|
|
|
|
"L16",
|
|
|
|
NULL,
|
2009-02-10 19:09:06 +00:00
|
|
|
read_impl.actual_samples_per_second,
|
|
|
|
read_impl.microseconds_per_packet / 1000,
|
2008-07-10 15:57:41 +00:00
|
|
|
1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
|
|
|
|
NULL, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
|
|
|
|
status = SWITCH_STATUS_FALSE;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
write_frame.codec = &raw_codec;
|
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
divisor = read_impl.actual_samples_per_second / 8000;
|
|
|
|
channels = read_impl.number_of_channels;
|
2008-07-10 15:57:41 +00:00
|
|
|
|
|
|
|
switch_core_session_set_read_codec(session, &raw_codec);
|
|
|
|
|
|
|
|
while (switch_channel_ready(channel)) {
|
|
|
|
|
|
|
|
status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
|
|
|
|
|
|
|
|
if (!SWITCH_READ_ACCEPTABLE(status)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-07-10 19:59:57 +00:00
|
|
|
if (sample_count) {
|
2008-10-20 17:48:42 +00:00
|
|
|
sample_count -= raw_codec.implementation->samples_per_packet;
|
2008-07-10 19:59:57 +00:00
|
|
|
if (sample_count <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-10 15:57:41 +00:00
|
|
|
if (abuf) {
|
2008-10-20 17:48:42 +00:00
|
|
|
switch_size_t olen = raw_codec.implementation->samples_per_packet;
|
2008-07-10 15:57:41 +00:00
|
|
|
|
|
|
|
if (switch_core_file_read(&fh, abuf, &olen) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-07-16 04:48:01 +00:00
|
|
|
write_frame.samples = (uint32_t)olen;
|
|
|
|
write_frame.datalen = (uint32_t)(olen * sizeof(int16_t));
|
2008-07-10 15:57:41 +00:00
|
|
|
if ((status = switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0)) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (countdown) {
|
|
|
|
if (!--countdown) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data = (int16_t *) read_frame->data;
|
|
|
|
|
|
|
|
for (energy = 0, j = 0, count = 0; count < read_frame->samples; count++) {
|
|
|
|
energy += abs(data[j++]);
|
|
|
|
j += channels;
|
|
|
|
}
|
|
|
|
|
|
|
|
score = (uint32_t) (energy / (read_frame->samples / divisor));
|
|
|
|
|
|
|
|
if (score >= thresh) {
|
|
|
|
listening++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listening > listen_hits && score < thresh) {
|
|
|
|
if (!--silence_hits) {
|
|
|
|
countdown = 25;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
silence_hits = org_silence_hits;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-09 20:34:01 +00:00
|
|
|
switch_core_session_reset(session, SWITCH_FALSE, SWITCH_TRUE);
|
2008-07-10 15:57:41 +00:00
|
|
|
switch_core_codec_destroy(&raw_codec);
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
|
|
|
if (abuf) {
|
|
|
|
switch_core_file_close(&fh);
|
2008-09-04 22:43:49 +00:00
|
|
|
free(abuf);
|
2008-07-10 15:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2008-03-04 18:55:16 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_ivr_read(switch_core_session_t *session,
|
|
|
|
uint32_t min_digits,
|
|
|
|
uint32_t max_digits,
|
|
|
|
const char *prompt_audio_file,
|
|
|
|
const char *var_name,
|
2008-11-13 20:23:59 +00:00
|
|
|
char *digit_buffer,
|
|
|
|
switch_size_t digit_buffer_length,
|
|
|
|
uint32_t timeout,
|
|
|
|
const char *valid_terminators)
|
2008-03-04 18:55:16 +00:00
|
|
|
{
|
|
|
|
switch_channel_t *channel;
|
|
|
|
switch_input_args_t args = { 0 };
|
|
|
|
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
|
|
|
char terminator;
|
2008-03-08 04:18:23 +00:00
|
|
|
size_t len;
|
2008-03-04 18:55:16 +00:00
|
|
|
|
|
|
|
switch_assert(session);
|
|
|
|
|
2008-11-14 14:20:10 +00:00
|
|
|
if (max_digits < min_digits) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
|
|
|
|
"Max digits %u is less than Min %u, forcing Max to %u\n", max_digits, min_digits, min_digits);
|
|
|
|
max_digits = min_digits;
|
|
|
|
}
|
|
|
|
|
2008-03-04 18:55:16 +00:00
|
|
|
channel = switch_core_session_get_channel(session);
|
2008-08-05 21:50:48 +00:00
|
|
|
switch_channel_set_variable(channel, SWITCH_READ_RESULT_VARIABLE, NULL);
|
2008-03-04 18:55:16 +00:00
|
|
|
|
2008-09-08 22:34:28 +00:00
|
|
|
if ((min_digits && digit_buffer_length < min_digits) || digit_buffer_length < max_digits) {
|
2008-03-04 18:55:16 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Buffer too small!\n");
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
if (switch_channel_pre_answer(channel) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2008-03-04 18:55:16 +00:00
|
|
|
|
|
|
|
memset(digit_buffer, 0, digit_buffer_length);
|
|
|
|
args.buf = digit_buffer;
|
2008-05-27 04:30:03 +00:00
|
|
|
args.buflen = (uint32_t) digit_buffer_length;
|
2008-03-04 18:55:16 +00:00
|
|
|
|
2008-03-13 01:08:42 +00:00
|
|
|
if (!switch_strlen_zero(prompt_audio_file) && strcasecmp(prompt_audio_file, "silence")) {
|
2008-11-14 14:20:10 +00:00
|
|
|
if ((status = switch_ivr_play_file(session, NULL, prompt_audio_file, &args)) == SWITCH_STATUS_BREAK) {
|
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
2008-03-04 18:55:16 +00:00
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2008-03-04 18:55:16 +00:00
|
|
|
if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(digit_buffer);
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2008-09-08 22:34:28 +00:00
|
|
|
if ((min_digits && len < min_digits) || len < max_digits) {
|
2008-03-04 18:55:16 +00:00
|
|
|
args.buf = digit_buffer + len;
|
2008-05-27 04:30:03 +00:00
|
|
|
args.buflen = (uint32_t) (digit_buffer_length - len);
|
2008-03-04 18:55:16 +00:00
|
|
|
status = switch_ivr_collect_digits_count(session, digit_buffer, digit_buffer_length, max_digits, valid_terminators, &terminator, timeout, 0, 0);
|
|
|
|
}
|
|
|
|
|
2008-11-13 20:23:59 +00:00
|
|
|
len = strlen(digit_buffer);
|
|
|
|
if ((min_digits && len < min_digits)) {
|
|
|
|
status = SWITCH_STATUS_TOO_SMALL;
|
|
|
|
}
|
|
|
|
|
2008-11-14 14:20:10 +00:00
|
|
|
switch (status) {
|
|
|
|
case SWITCH_STATUS_SUCCESS:
|
2008-08-05 21:50:48 +00:00
|
|
|
switch_channel_set_variable(channel, SWITCH_READ_RESULT_VARIABLE, "success");
|
2008-11-14 14:20:10 +00:00
|
|
|
break;
|
|
|
|
case SWITCH_STATUS_TIMEOUT:
|
2008-08-05 21:50:48 +00:00
|
|
|
switch_channel_set_variable(channel, SWITCH_READ_RESULT_VARIABLE, "timeout");
|
2008-11-14 14:20:10 +00:00
|
|
|
break;
|
|
|
|
default:
|
2008-08-05 21:50:48 +00:00
|
|
|
switch_channel_set_variable(channel, SWITCH_READ_RESULT_VARIABLE, "failure");
|
2008-11-14 14:20:10 +00:00
|
|
|
break;
|
|
|
|
|
2008-08-05 21:50:48 +00:00
|
|
|
}
|
2008-11-14 14:20:10 +00:00
|
|
|
|
|
|
|
end:
|
2008-03-04 18:55:16 +00:00
|
|
|
|
|
|
|
if (var_name && !switch_strlen_zero(digit_buffer)) {
|
|
|
|
switch_channel_set_variable(channel, var_name, digit_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2008-03-04 18:55:16 +00:00
|
|
|
}
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_play_and_get_digits(switch_core_session_t *session,
|
|
|
|
uint32_t min_digits,
|
|
|
|
uint32_t max_digits,
|
|
|
|
uint32_t max_tries,
|
|
|
|
uint32_t timeout,
|
2009-01-14 20:29:16 +00:00
|
|
|
const char *valid_terminators,
|
|
|
|
const char *prompt_audio_file,
|
|
|
|
const char *bad_input_audio_file,
|
2009-01-15 03:42:45 +00:00
|
|
|
const char *var_name,
|
2008-11-14 02:59:00 +00:00
|
|
|
char *digit_buffer,
|
2008-11-13 20:23:59 +00:00
|
|
|
uint32_t digit_buffer_length,
|
2009-01-14 20:29:16 +00:00
|
|
|
const char *digits_regex)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
2008-11-13 20:23:59 +00:00
|
|
|
switch_channel_t *channel = switch_core_session_get_channel(session);
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2008-11-13 20:23:59 +00:00
|
|
|
while(switch_channel_ready(channel) && max_tries) {
|
|
|
|
switch_status_t status;
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
memset(digit_buffer, 0, digit_buffer_length);
|
2008-11-13 20:23:59 +00:00
|
|
|
switch_channel_flush_dtmf(channel);
|
2009-01-15 03:42:45 +00:00
|
|
|
status = switch_ivr_read(session, min_digits, max_digits, prompt_audio_file, var_name,
|
2008-11-13 20:23:59 +00:00
|
|
|
digit_buffer, digit_buffer_length, timeout, valid_terminators);
|
2008-11-14 02:59:00 +00:00
|
|
|
if (status == SWITCH_STATUS_TIMEOUT && strlen(digit_buffer) >= min_digits) {
|
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-11-13 20:23:59 +00:00
|
|
|
if (status == SWITCH_STATUS_SUCCESS) {
|
2008-11-14 14:20:10 +00:00
|
|
|
if (!switch_strlen_zero(digit_buffer)) {
|
2008-11-13 20:23:59 +00:00
|
|
|
if (switch_strlen_zero(digits_regex)) {
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
if (switch_regex_match(digit_buffer, digits_regex) == SWITCH_STATUS_SUCCESS) {
|
2008-11-13 20:23:59 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-13 20:23:59 +00:00
|
|
|
if (!switch_channel_ready(channel)) {
|
|
|
|
break;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2008-11-13 20:23:59 +00:00
|
|
|
switch_ivr_play_file(session, NULL, bad_input_audio_file, NULL);
|
|
|
|
max_tries--;
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2008-11-14 14:20:10 +00:00
|
|
|
memset(digit_buffer, 0, digit_buffer_length);
|
2007-03-29 22:34:40 +00:00
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_ivr_speak_text_handle(switch_core_session_t *session,
|
|
|
|
switch_speech_handle_t *sh,
|
2007-03-30 00:13:31 +00:00
|
|
|
switch_codec_t *codec, switch_timer_t *timer, char *text, switch_input_args_t *args)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
2008-01-28 07:26:10 +00:00
|
|
|
switch_channel_t *channel = switch_core_session_get_channel(session);
|
2007-03-29 22:34:40 +00:00
|
|
|
short abuf[960];
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_dtmf_t dtmf = { 0 };
|
2007-03-29 22:34:40 +00:00
|
|
|
uint32_t len = 0;
|
|
|
|
switch_size_t ilen = 0;
|
|
|
|
switch_frame_t write_frame = { 0 };
|
|
|
|
int x;
|
|
|
|
int done = 0;
|
|
|
|
int lead_in_out = 10;
|
|
|
|
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
|
|
|
switch_speech_flag_t flags = SWITCH_SPEECH_FLAG_NONE;
|
2007-08-02 00:54:39 +00:00
|
|
|
uint32_t rate = 0;
|
2007-10-17 20:14:19 +00:00
|
|
|
switch_size_t extra = 0;
|
2007-10-16 16:49:44 +00:00
|
|
|
char *p, *tmp = NULL;
|
2007-11-01 11:28:26 +00:00
|
|
|
const char *star, *pound;
|
2007-10-16 18:49:56 +00:00
|
|
|
switch_size_t starlen, poundlen;
|
2007-03-29 22:34:40 +00:00
|
|
|
|
|
|
|
if (!sh) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
if (switch_channel_pre_answer(channel) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
|
|
|
|
write_frame.data = abuf;
|
|
|
|
write_frame.buflen = sizeof(abuf);
|
|
|
|
|
2007-08-02 00:54:39 +00:00
|
|
|
len = sh->samples * 2;
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
flags = 0;
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-10-16 18:49:56 +00:00
|
|
|
if (!(star = switch_channel_get_variable(channel, "star_replace"))) {
|
|
|
|
star = "star";
|
|
|
|
}
|
|
|
|
if (!(pound = switch_channel_get_variable(channel, "pound_replace"))) {
|
|
|
|
pound = "pound";
|
|
|
|
}
|
|
|
|
starlen = strlen(star);
|
|
|
|
poundlen = strlen(pound);
|
|
|
|
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
for (p = text; p && *p; p++) {
|
2007-10-16 16:49:44 +00:00
|
|
|
if (*p == '*') {
|
2007-10-16 18:49:56 +00:00
|
|
|
extra += starlen;
|
2007-10-16 16:49:44 +00:00
|
|
|
} else if (*p == '#') {
|
2007-10-16 18:49:56 +00:00
|
|
|
extra += poundlen;
|
2007-10-16 16:49:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (extra) {
|
|
|
|
char *tp;
|
|
|
|
switch_size_t mylen = strlen(text) + extra + 1;
|
|
|
|
tmp = malloc(mylen);
|
2008-05-21 20:25:40 +00:00
|
|
|
if (!tmp) {
|
|
|
|
return SWITCH_STATUS_MEMERR;
|
|
|
|
}
|
2007-10-16 18:49:56 +00:00
|
|
|
memset(tmp, 0, mylen);
|
2007-10-16 16:49:44 +00:00
|
|
|
tp = tmp;
|
|
|
|
for (p = text; p && *p; p++) {
|
|
|
|
if (*p == '*') {
|
2007-10-16 18:49:56 +00:00
|
|
|
strncat(tp, star, starlen);
|
|
|
|
tp += starlen;
|
2008-05-27 04:30:03 +00:00
|
|
|
} else if (*p == '#') {
|
2007-10-16 18:49:56 +00:00
|
|
|
strncat(tp, pound, poundlen);
|
|
|
|
tp += poundlen;
|
2007-10-16 16:49:44 +00:00
|
|
|
} else {
|
|
|
|
*tp++ = *p;
|
|
|
|
}
|
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-10-16 16:49:44 +00:00
|
|
|
text = tmp;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_core_speech_feed_tts(sh, text, &flags);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Speaking text: %s\n", text);
|
2007-10-16 16:49:44 +00:00
|
|
|
switch_safe_free(tmp);
|
|
|
|
text = NULL;
|
2007-03-29 22:34:40 +00:00
|
|
|
|
|
|
|
write_frame.rate = sh->rate;
|
|
|
|
|
|
|
|
memset(write_frame.data, 0, len);
|
|
|
|
write_frame.datalen = len;
|
|
|
|
write_frame.samples = len / 2;
|
|
|
|
write_frame.codec = codec;
|
|
|
|
|
2007-12-11 19:23:57 +00:00
|
|
|
switch_assert(codec->implementation != NULL);
|
2007-07-02 20:16:08 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
for (x = 0; !done && x < lead_in_out; x++) {
|
2008-10-20 17:48:42 +00:00
|
|
|
switch_yield(codec->implementation->microseconds_per_packet);
|
2007-03-29 22:34:40 +00:00
|
|
|
if (timer) {
|
|
|
|
write_frame.timestamp = timer->samplecount;
|
|
|
|
}
|
2008-05-08 19:19:47 +00:00
|
|
|
if (switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
|
2007-03-29 22:34:40 +00:00
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ilen = len;
|
2008-05-27 04:30:03 +00:00
|
|
|
for (;;) {
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_event_t *event;
|
|
|
|
|
2007-07-03 02:10:35 +00:00
|
|
|
if (!switch_channel_ready(channel)) {
|
2008-05-27 04:30:03 +00:00
|
|
|
status = SWITCH_STATUS_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2007-07-03 02:10:35 +00:00
|
|
|
|
2007-06-25 21:25:33 +00:00
|
|
|
if (switch_channel_test_flag(channel, CF_BREAK)) {
|
|
|
|
switch_channel_clear_flag(channel, CF_BREAK);
|
2007-07-03 02:10:35 +00:00
|
|
|
status = SWITCH_STATUS_BREAK;
|
2007-06-25 21:25:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
if (switch_core_session_dequeue_private_event(session, &event) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_ivr_parse_event(session, event);
|
|
|
|
switch_event_destroy(&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args && (args->input_callback || args->buf || args->buflen)) {
|
2008-09-02 10:39:39 +00:00
|
|
|
/* dtmf handler function you can hook up to be executed when a digit is dialed during playback
|
|
|
|
* if you return anything but SWITCH_STATUS_SUCCESS the playback will stop.
|
2007-03-29 22:34:40 +00:00
|
|
|
*/
|
|
|
|
if (switch_channel_has_dtmf(channel)) {
|
|
|
|
if (!args->input_callback && !args->buf) {
|
|
|
|
status = SWITCH_STATUS_BREAK;
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (args->buf && !strcasecmp(args->buf, "_break_")) {
|
|
|
|
status = SWITCH_STATUS_BREAK;
|
|
|
|
} else {
|
2007-12-22 00:32:20 +00:00
|
|
|
switch_channel_dequeue_dtmf(channel, &dtmf);
|
2007-03-29 22:34:40 +00:00
|
|
|
if (args->input_callback) {
|
2007-12-22 00:32:20 +00:00
|
|
|
status = args->input_callback(session, (void *) &dtmf, SWITCH_INPUT_TYPE_DTMF, args->buf, args->buflen);
|
2007-03-29 22:34:40 +00:00
|
|
|
} else {
|
2007-12-22 00:32:20 +00:00
|
|
|
switch_copy_string((char *) args->buf, (void *) &dtmf, args->buflen);
|
2007-03-29 22:34:40 +00:00
|
|
|
status = SWITCH_STATUS_BREAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args->input_callback) {
|
|
|
|
if (switch_core_session_dequeue_event(session, &event) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
status = args->input_callback(session, event, SWITCH_INPUT_TYPE_EVENT, args->buf, args->buflen);
|
|
|
|
switch_event_destroy(&event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status != SWITCH_STATUS_SUCCESS) {
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_test_flag(sh, SWITCH_SPEECH_FLAG_PAUSE)) {
|
|
|
|
if (timer) {
|
|
|
|
if (switch_core_timer_next(timer) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch_frame_t *read_frame;
|
2008-05-08 19:19:47 +00:00
|
|
|
switch_status_t tstatus = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
|
2007-03-29 22:34:40 +00:00
|
|
|
|
|
|
|
while (switch_channel_ready(channel) && switch_channel_test_flag(channel, CF_HOLD)) {
|
|
|
|
switch_yield(10000);
|
|
|
|
}
|
|
|
|
|
2007-12-18 17:50:39 +00:00
|
|
|
if (!SWITCH_READ_ACCEPTABLE(tstatus)) {
|
2007-03-29 22:34:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-10-02 00:09:49 +00:00
|
|
|
|
|
|
|
if (args && (args->read_frame_callback)) {
|
|
|
|
if (args->read_frame_callback(session, read_frame, args->user_data) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = SWITCH_SPEECH_FLAG_BLOCKING;
|
|
|
|
status = switch_core_speech_read_tts(sh, abuf, &ilen, &rate, &flags);
|
|
|
|
|
|
|
|
if (status != SWITCH_STATUS_SUCCESS) {
|
|
|
|
for (x = 0; !done && x < lead_in_out; x++) {
|
2008-10-20 17:48:42 +00:00
|
|
|
switch_yield(codec->implementation->microseconds_per_packet);
|
2007-03-29 22:34:40 +00:00
|
|
|
if (timer) {
|
|
|
|
write_frame.timestamp = timer->samplecount;
|
|
|
|
}
|
2008-05-08 19:19:47 +00:00
|
|
|
if (switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
|
2007-03-29 22:34:40 +00:00
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status == SWITCH_STATUS_BREAK) {
|
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (done) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
write_frame.datalen = (uint32_t) ilen;
|
|
|
|
write_frame.samples = (uint32_t) (ilen / 2);
|
|
|
|
if (timer) {
|
|
|
|
write_frame.timestamp = timer->samplecount;
|
|
|
|
}
|
2008-05-08 19:19:47 +00:00
|
|
|
if (switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
|
2007-03-29 22:34:40 +00:00
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (done) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timer) {
|
|
|
|
if (switch_core_timer_next(timer) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else { /* time off the channel (if you must) */
|
|
|
|
switch_frame_t *read_frame;
|
2008-05-08 19:19:47 +00:00
|
|
|
switch_status_t tstatus = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
|
2007-03-29 22:34:40 +00:00
|
|
|
|
|
|
|
while (switch_channel_ready(channel) && switch_channel_test_flag(channel, CF_HOLD)) {
|
|
|
|
switch_yield(10000);
|
|
|
|
}
|
|
|
|
|
2007-12-18 17:50:39 +00:00
|
|
|
if (!SWITCH_READ_ACCEPTABLE(tstatus)) {
|
2007-03-29 22:34:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-10-02 00:09:49 +00:00
|
|
|
|
|
|
|
if (args && (args->read_frame_callback)) {
|
|
|
|
if (args->read_frame_callback(session, read_frame, args->user_data) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "done speaking text\n");
|
|
|
|
flags = 0;
|
|
|
|
switch_core_speech_flush_tts(sh);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2007-10-15 16:25:08 +00:00
|
|
|
struct cached_speech_handle {
|
|
|
|
char tts_name[80];
|
|
|
|
char voice_name[80];
|
|
|
|
switch_speech_handle_t sh;
|
|
|
|
switch_codec_t codec;
|
|
|
|
switch_timer_t timer;
|
|
|
|
};
|
2008-09-02 10:39:39 +00:00
|
|
|
|
2007-10-15 16:25:08 +00:00
|
|
|
typedef struct cached_speech_handle cached_speech_handle_t;
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
SWITCH_DECLARE(void) switch_ivr_clear_speech_cache(switch_core_session_t *session)
|
2007-10-15 16:25:08 +00:00
|
|
|
{
|
|
|
|
cached_speech_handle_t *cache_obj = NULL;
|
2008-01-28 07:26:10 +00:00
|
|
|
switch_channel_t *channel = switch_core_session_get_channel(session);
|
2007-10-15 16:25:08 +00:00
|
|
|
|
|
|
|
if ((cache_obj = switch_channel_get_private(channel, SWITCH_CACHE_SPEECH_HANDLES_OBJ_NAME))) {
|
|
|
|
switch_speech_flag_t flags = SWITCH_SPEECH_FLAG_NONE;
|
|
|
|
if (cache_obj->timer.interval) {
|
|
|
|
switch_core_timer_destroy(&cache_obj->timer);
|
|
|
|
}
|
|
|
|
switch_core_speech_close(&cache_obj->sh, &flags);
|
|
|
|
switch_core_codec_destroy(&cache_obj->codec);
|
|
|
|
switch_channel_set_private(channel, SWITCH_CACHE_SPEECH_HANDLES_OBJ_NAME, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_ivr_speak_text(switch_core_session_t *session,
|
2007-11-01 11:28:26 +00:00
|
|
|
const char *tts_name, const char *voice_name, char *text, switch_input_args_t *args)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
2008-01-28 07:26:10 +00:00
|
|
|
switch_channel_t *channel = switch_core_session_get_channel(session);
|
2007-08-28 17:06:20 +00:00
|
|
|
uint32_t rate = 0;
|
2007-03-29 22:34:40 +00:00
|
|
|
int interval = 0;
|
|
|
|
switch_frame_t write_frame = { 0 };
|
2007-10-15 16:25:08 +00:00
|
|
|
switch_timer_t ltimer, *timer;
|
|
|
|
switch_codec_t lcodec, *codec;
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_memory_pool_t *pool = switch_core_session_get_pool(session);
|
|
|
|
char *codec_name;
|
|
|
|
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
2007-10-15 16:25:08 +00:00
|
|
|
switch_speech_handle_t lsh, *sh;
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_speech_flag_t flags = SWITCH_SPEECH_FLAG_NONE;
|
|
|
|
switch_codec_t *read_codec;
|
2007-11-01 11:28:26 +00:00
|
|
|
const char *timer_name, *var;
|
2007-10-15 16:25:08 +00:00
|
|
|
cached_speech_handle_t *cache_obj = NULL;
|
|
|
|
int need_create = 1, need_alloc = 1;
|
2009-02-10 19:09:06 +00:00
|
|
|
switch_codec_implementation_t read_impl = {0};
|
|
|
|
switch_core_session_get_read_impl(session, &read_impl);
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
if (switch_channel_pre_answer(channel) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2008-01-07 21:47:32 +00:00
|
|
|
|
2007-10-15 16:25:08 +00:00
|
|
|
sh = ↰
|
|
|
|
codec = &lcodec;
|
|
|
|
timer = <imer;
|
|
|
|
|
|
|
|
if ((var = switch_channel_get_variable(channel, SWITCH_CACHE_SPEECH_HANDLES_VARIABLE)) && switch_true(var)) {
|
|
|
|
if ((cache_obj = switch_channel_get_private(channel, SWITCH_CACHE_SPEECH_HANDLES_OBJ_NAME))) {
|
|
|
|
need_create = 0;
|
|
|
|
if (!strcasecmp(cache_obj->tts_name, tts_name)) {
|
|
|
|
need_alloc = 0;
|
|
|
|
} else {
|
|
|
|
switch_ivr_clear_speech_cache(session);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cache_obj) {
|
|
|
|
cache_obj = switch_core_session_alloc(session, sizeof(*cache_obj));
|
|
|
|
}
|
|
|
|
if (need_alloc) {
|
|
|
|
switch_copy_string(cache_obj->tts_name, tts_name, sizeof(cache_obj->tts_name));
|
|
|
|
switch_copy_string(cache_obj->voice_name, voice_name, sizeof(cache_obj->voice_name));
|
|
|
|
switch_channel_set_private(channel, SWITCH_CACHE_SPEECH_HANDLES_OBJ_NAME, cache_obj);
|
|
|
|
}
|
|
|
|
sh = &cache_obj->sh;
|
|
|
|
codec = &cache_obj->codec;
|
|
|
|
timer = &cache_obj->timer;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
timer_name = switch_channel_get_variable(channel, "timer_name");
|
|
|
|
|
2009-01-09 20:34:01 +00:00
|
|
|
switch_core_session_reset(session, SWITCH_FALSE, SWITCH_TRUE);
|
2007-08-02 00:54:39 +00:00
|
|
|
read_codec = switch_core_session_get_read_codec(session);
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
rate = read_impl.actual_samples_per_second;
|
|
|
|
interval = read_impl.microseconds_per_packet / 1000;
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-10-15 16:25:08 +00:00
|
|
|
if (need_create) {
|
|
|
|
memset(sh, 0, sizeof(*sh));
|
2008-05-27 04:30:03 +00:00
|
|
|
if (switch_core_speech_open(sh, tts_name, voice_name, (uint32_t) rate, interval,
|
2007-10-15 16:25:08 +00:00
|
|
|
&flags, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid TTS module!\n");
|
2009-01-09 20:34:01 +00:00
|
|
|
switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
|
2007-10-15 16:25:08 +00:00
|
|
|
if (cache_obj) {
|
|
|
|
switch_channel_set_private(channel, SWITCH_CACHE_SPEECH_HANDLES_OBJ_NAME, NULL);
|
|
|
|
}
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
} else if (cache_obj && strcasecmp(cache_obj->voice_name, voice_name)) {
|
|
|
|
switch_copy_string(cache_obj->voice_name, voice_name, sizeof(cache_obj->voice_name));
|
|
|
|
switch_core_speech_text_param_tts(sh, "voice", voice_name);
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2009-02-10 19:09:06 +00:00
|
|
|
if (switch_channel_pre_answer(channel) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "OPEN TTS %s\n", tts_name);
|
|
|
|
|
|
|
|
codec_name = "L16";
|
|
|
|
|
2007-10-15 16:25:08 +00:00
|
|
|
if (need_create) {
|
|
|
|
if (switch_core_codec_init(codec,
|
|
|
|
codec_name,
|
2008-05-27 04:30:03 +00:00
|
|
|
NULL, (int) rate, interval, 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
|
|
|
|
pool) == SWITCH_STATUS_SUCCESS) {
|
2007-10-15 16:25:08 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Raw Codec Activated\n");
|
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Raw Codec Activation Failed %s@%uhz 1 channel %dms\n", codec_name, rate, interval);
|
2007-03-29 22:34:40 +00:00
|
|
|
flags = 0;
|
2007-10-15 16:25:08 +00:00
|
|
|
switch_core_speech_close(sh, &flags);
|
2009-01-09 20:34:01 +00:00
|
|
|
switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
|
2007-10-15 16:25:08 +00:00
|
|
|
if (cache_obj) {
|
|
|
|
switch_channel_set_private(channel, SWITCH_CACHE_SPEECH_HANDLES_OBJ_NAME, NULL);
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
2007-10-15 16:25:08 +00:00
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-10-15 16:25:08 +00:00
|
|
|
write_frame.codec = codec;
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2007-10-15 16:25:08 +00:00
|
|
|
if (timer_name) {
|
|
|
|
if (need_create) {
|
|
|
|
if (switch_core_timer_init(timer, timer_name, interval, (int) sh->samples, pool) != SWITCH_STATUS_SUCCESS) {
|
2008-10-11 05:44:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Setup timer failed!\n");
|
2007-10-15 16:25:08 +00:00
|
|
|
switch_core_codec_destroy(write_frame.codec);
|
|
|
|
flags = 0;
|
|
|
|
switch_core_speech_close(sh, &flags);
|
2009-01-09 20:34:01 +00:00
|
|
|
switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
|
2007-10-15 16:25:08 +00:00
|
|
|
if (cache_obj) {
|
|
|
|
switch_channel_set_private(channel, SWITCH_CACHE_SPEECH_HANDLES_OBJ_NAME, NULL);
|
|
|
|
}
|
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
2008-10-11 05:44:11 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Setup timer success %u bytes per %d ms!\n", sh->samples * 2, interval);
|
2007-10-15 16:25:08 +00:00
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
/* start a thread to absorb incoming audio */
|
2008-10-23 23:48:11 +00:00
|
|
|
switch_core_service_session(session);
|
2008-05-08 19:19:47 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-10-15 16:25:08 +00:00
|
|
|
status = switch_ivr_speak_text_handle(session, sh, write_frame.codec, timer_name ? timer : NULL, text, args);
|
2007-03-29 22:34:40 +00:00
|
|
|
flags = 0;
|
2007-10-15 16:25:08 +00:00
|
|
|
|
|
|
|
if (!cache_obj) {
|
|
|
|
switch_core_speech_close(sh, &flags);
|
|
|
|
switch_core_codec_destroy(codec);
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
|
|
|
|
if (timer_name) {
|
|
|
|
/* End the audio absorbing thread */
|
2008-10-23 23:48:11 +00:00
|
|
|
switch_core_thread_session_end(session);
|
2007-10-15 16:25:08 +00:00
|
|
|
if (!cache_obj) {
|
|
|
|
switch_core_timer_destroy(timer);
|
|
|
|
}
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2009-01-09 20:34:01 +00:00
|
|
|
switch_core_session_reset(session, SWITCH_FALSE, SWITCH_TRUE);
|
2007-03-29 22:34:40 +00:00
|
|
|
return status;
|
|
|
|
}
|
2008-01-27 05:02:52 +00:00
|
|
|
|
2008-04-09 18:15:15 +00:00
|
|
|
|
|
|
|
static switch_status_t hold_on_dtmf(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen)
|
|
|
|
{
|
|
|
|
char *stop_key = (char *) buf;
|
|
|
|
|
|
|
|
switch (itype) {
|
|
|
|
case SWITCH_INPUT_TYPE_DTMF:
|
2008-05-27 04:30:03 +00:00
|
|
|
{
|
|
|
|
switch_dtmf_t *dtmf = (switch_dtmf_t *) input;
|
2008-04-09 18:15:15 +00:00
|
|
|
if (dtmf->digit == *stop_key) {
|
|
|
|
return SWITCH_STATUS_BREAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_ivr_soft_hold(switch_core_session_t *session, const char *unhold_key, const char *moh_a, const char *moh_b)
|
|
|
|
{
|
|
|
|
switch_channel_t *channel, *other_channel;
|
|
|
|
switch_core_session_t *other_session;
|
|
|
|
const char *other_uuid, *moh = NULL;
|
|
|
|
int moh_br = 0;
|
|
|
|
switch_input_args_t args = { 0 };
|
|
|
|
args.input_callback = hold_on_dtmf;
|
|
|
|
args.buf = (void *) unhold_key;
|
2008-05-27 04:30:03 +00:00
|
|
|
args.buflen = (uint32_t) strlen(unhold_key);
|
|
|
|
|
2008-04-09 18:15:15 +00:00
|
|
|
switch_assert(session != NULL);
|
|
|
|
channel = switch_core_session_get_channel(session);
|
|
|
|
switch_assert(channel != NULL);
|
|
|
|
|
|
|
|
if ((other_uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE))) {
|
|
|
|
if ((other_session = switch_core_session_locate(other_uuid))) {
|
|
|
|
other_channel = switch_core_session_get_channel(other_session);
|
|
|
|
|
|
|
|
if (moh_b) {
|
2008-05-27 04:30:03 +00:00
|
|
|
moh = moh_b;
|
2008-04-09 18:15:15 +00:00
|
|
|
} else {
|
|
|
|
moh = switch_channel_get_variable(other_channel, "hold_music");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!switch_strlen_zero(moh) && strcasecmp(moh, "silence") && !switch_channel_test_flag(other_channel, CF_BROADCAST)) {
|
|
|
|
switch_ivr_broadcast(other_uuid, moh, SMF_ECHO_ALEG | SMF_LOOP);
|
|
|
|
moh_br++;
|
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2008-04-09 18:15:15 +00:00
|
|
|
if (moh_a) {
|
|
|
|
moh = moh_a;
|
|
|
|
} else {
|
|
|
|
moh = switch_channel_get_variable(channel, "hold_music");
|
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2008-04-09 18:15:15 +00:00
|
|
|
if (!switch_strlen_zero(moh) && strcasecmp(moh, "silence")) {
|
|
|
|
switch_ivr_play_file(session, NULL, moh, &args);
|
|
|
|
} else {
|
|
|
|
switch_ivr_collect_digits_callback(session, &args, 0);
|
2008-05-27 04:30:03 +00:00
|
|
|
}
|
2008-04-09 18:15:15 +00:00
|
|
|
|
|
|
|
if (moh_br) {
|
|
|
|
switch_channel_stop_broadcast(other_channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_core_session_rwunlock(other_session);
|
|
|
|
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2008-04-09 18:15:15 +00:00
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2008-04-09 18:15:15 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Channel %s is not in a bridge\n", switch_channel_get_name(channel));
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-01-27 05:02:52 +00:00
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
2008-02-03 22:14:57 +00:00
|
|
|
* indent-tabs-mode:t
|
2008-01-27 05:02:52 +00:00
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
2008-07-03 19:12:26 +00:00
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
|
2008-01-27 05:02:52 +00:00
|
|
|
*/
|